Skip to content

Commit 29896c0

Browse files
jyn514Joshua Nelson
authored and
Joshua Nelson
committed
Add test for uploading more than a single batch of files
1 parent 75abbb2 commit 29896c0

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

src/storage/mod.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use std::fs;
1414
use std::io::Read;
1515
use std::path::{Path, PathBuf};
1616

17+
const MAX_CONCURRENT_UPLOADS: usize = 1000;
18+
1719
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1820
pub(crate) struct Blob {
1921
pub(crate) path: String,
@@ -96,8 +98,6 @@ impl<'a> Storage<'a> {
9698
prefix: &str,
9799
root_dir: &Path,
98100
) -> Result<HashMap<PathBuf, String>, Error> {
99-
const MAX_CONCURRENT_UPLOADS: usize = 1000;
100-
101101
let trans = conn.transaction()?;
102102
let mut file_paths_and_mimes = HashMap::new();
103103

@@ -191,6 +191,40 @@ mod test {
191191
use crate::test::wrapper;
192192
use std::env;
193193

194+
pub(crate) fn assert_blob_eq(blob: &Blob, actual: &Blob) {
195+
assert_eq!(blob.path, actual.path);
196+
assert_eq!(blob.content, actual.content);
197+
assert_eq!(blob.mime, actual.mime);
198+
// NOTE: this does _not_ compare the upload time since min.io doesn't allow this to be configured
199+
}
200+
201+
pub(crate) fn test_roundtrip(blobs: &[Blob]) {
202+
let dir = tempdir::TempDir::new("docs.rs-upload-test").unwrap();
203+
for blob in blobs {
204+
let path = dir.path().join(&blob.path);
205+
if let Some(parent) = path.parent() {
206+
fs::create_dir_all(parent).unwrap();
207+
}
208+
fs::write(path, &blob.content).expect("failed to write to file");
209+
}
210+
wrapper(|env| {
211+
let db = env.db();
212+
let conn = db.conn();
213+
let mut backend = Storage::Database(DatabaseBackend::new(&conn));
214+
let stored_files = backend.store_all(&conn, "", dir.path()).unwrap();
215+
assert_eq!(stored_files.len(), blobs.len());
216+
for blob in blobs {
217+
let name = Path::new(&blob.path);
218+
assert!(stored_files.contains_key(name));
219+
220+
let actual = backend.get(&blob.path).unwrap();
221+
assert_blob_eq(blob, &actual);
222+
}
223+
224+
Ok(())
225+
});
226+
}
227+
194228
#[test]
195229
fn test_uploads() {
196230
use std::fs;
@@ -235,6 +269,19 @@ mod test {
235269
})
236270
}
237271

272+
#[test]
273+
fn test_batched_uploads() {
274+
let uploads: Vec<_> = (0..=MAX_CONCURRENT_UPLOADS + 1)
275+
.map(|i| Blob {
276+
mime: "text/rust".into(),
277+
content: "fn main() {}".into(),
278+
path: format!("{}.rs", i),
279+
date_updated: Timespec::new(42, 0),
280+
})
281+
.collect();
282+
test_roundtrip(&uploads);
283+
}
284+
238285
#[test]
239286
fn test_get_file_list() {
240287
let _ = env_logger::try_init();

src/storage/s3.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ pub(crate) fn s3_client() -> Option<S3Client> {
131131
#[cfg(test)]
132132
pub(crate) mod tests {
133133
use super::*;
134+
use crate::storage::test::*;
134135
use crate::test::*;
135136

136137
use crate::storage::s3::S3Backend;
@@ -176,12 +177,9 @@ pub(crate) mod tests {
176177
x => panic!("wrong error: {:?}", x),
177178
};
178179
}
179-
fn assert_blob(&self, blob: &Blob, path: &str) {
180+
pub(crate) fn assert_blob(&self, blob: &Blob, path: &str) {
180181
let actual = self.0.borrow().get(path).unwrap();
181-
assert_eq!(blob.path, actual.path);
182-
assert_eq!(blob.content, actual.content);
183-
assert_eq!(blob.mime, actual.mime);
184-
// NOTE: this does _not_ compare the upload time since min.io doesn't allow this to be configured
182+
assert_blob_eq(blob, &actual);
185183
}
186184
}
187185

0 commit comments

Comments
 (0)