Skip to content

Commit ed11fea

Browse files
authored
chore(fs): allow running integration tests in parallel (#463)
* chore(fs): allow running integration tests in parallel * stamp: oops
1 parent 5758f3b commit ed11fea

File tree

1 file changed

+81
-30
lines changed

1 file changed

+81
-30
lines changed

crates/sb_fs/tests/integration_tests.rs

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use std::{collections::HashMap, time::Duration};
1+
use std::{collections::HashMap, path::Path, time::Duration};
22

33
use anyhow::Context;
44
use base::{server::ServerFlags, utils::test_utils::TestBedBuilder};
55
use ctor::ctor;
66
use deno_core::serde_json;
77
use event_worker::events::{LogLevel, WorkerEvents};
88
use hyper_v014::{body::to_bytes, Body, StatusCode};
9-
use rand::RngCore;
9+
use once_cell::sync::Lazy;
10+
use rand::{distributions::Alphanumeric, Rng, RngCore};
1011
use serde::Deserialize;
1112
use serial_test::serial;
1213
use tokio::sync::mpsc;
@@ -19,6 +20,39 @@ fn init() {
1920
let _ = dotenvy::from_filename("./tests/.env");
2021
}
2122

23+
fn is_supabase_storage_being_tested() -> bool {
24+
std::env::var("S3FS_TEST_SUPABASE_STORAGE").unwrap_or_default() == "true"
25+
}
26+
27+
fn get_root_path() -> &'static str {
28+
static VALUE: Lazy<String> = Lazy::new(|| {
29+
rand::thread_rng()
30+
.sample_iter(&Alphanumeric)
31+
.take(10)
32+
.map(char::from)
33+
.collect()
34+
});
35+
36+
VALUE.as_str()
37+
}
38+
39+
fn get_path<P>(path: P) -> String
40+
where
41+
P: AsRef<Path>,
42+
{
43+
let path = path.as_ref().to_str().unwrap();
44+
45+
if path.is_empty() {
46+
return get_root_path().to_string();
47+
}
48+
49+
format!(
50+
"{}/{}",
51+
get_root_path(),
52+
path.strip_prefix('/').unwrap_or(path)
53+
)
54+
}
55+
2256
fn get_tb_builder() -> TestBedBuilder {
2357
TestBedBuilder::new("./tests/fixture/main_with_s3fs").with_oneshot_policy(None)
2458
}
@@ -27,10 +61,14 @@ async fn remove(path: &str, recursive: bool) {
2761
let tb = get_tb_builder().build().await;
2862
let resp = tb
2963
.request(|b| {
30-
b.uri(format!("/remove/{}?recursive={}", path, recursive))
31-
.method("GET")
32-
.body(Body::empty())
33-
.context("can't make request")
64+
b.uri(format!(
65+
"/remove/{}?recursive={}",
66+
get_path(path),
67+
recursive
68+
))
69+
.method("GET")
70+
.body(Body::empty())
71+
.context("can't make request")
3472
})
3573
.await
3674
.unwrap();
@@ -57,7 +95,7 @@ async fn test_write_and_get_bytes(bytes: usize) {
5795

5896
let resp = tb
5997
.request(|b| {
60-
b.uri("/write/meow.bin")
98+
b.uri(format!("/write/{}", get_path("meow.bin")))
6199
.method("POST")
62100
.body(arr.clone().into())
63101
.context("can't make request")
@@ -73,7 +111,7 @@ async fn test_write_and_get_bytes(bytes: usize) {
73111
let tb = get_tb_builder().build().await;
74112
let mut resp = tb
75113
.request(|b| {
76-
b.uri("/get/meow.bin")
114+
b.uri(format!("/get/{}", get_path("meow.bin")))
77115
.method("GET")
78116
.body(Body::empty())
79117
.context("can't make request")
@@ -126,7 +164,7 @@ async fn test_write_and_get_over_50_mib() {
126164

127165
let resp = tb
128166
.request(|b| {
129-
b.uri("/write/meow.bin")
167+
b.uri(format!("/write/{}", get_path("meow.bin")))
130168
.method("POST")
131169
.body(arr.clone().into())
132170
.context("can't make request")
@@ -147,7 +185,7 @@ async fn test_write_and_get_over_50_mib() {
147185

148186
let resp = tb
149187
.request(|b| {
150-
b.uri("/get/meow.bin")
188+
b.uri(format!("/get/{}", get_path("meow.bin")))
151189
.method("GET")
152190
.body(Body::empty())
153191
.context("can't make request")
@@ -207,7 +245,7 @@ async fn test_mkdir_and_read_dir() {
207245
let tb = get_tb_builder().build().await;
208246
let resp = tb
209247
.request(|b| {
210-
b.uri("/mkdir/a")
248+
b.uri(format!("/mkdir/{}?recursive=true", get_path("a")))
211249
.method("GET")
212250
.body(Body::empty())
213251
.context("can't make request")
@@ -223,7 +261,7 @@ async fn test_mkdir_and_read_dir() {
223261
let tb = get_tb_builder().build().await;
224262
let mut resp = tb
225263
.request(|b| {
226-
b.uri("/read-dir")
264+
b.uri(format!("/read-dir/{}", get_root_path()))
227265
.method("GET")
228266
.body(Body::empty())
229267
.context("can't make request")
@@ -253,7 +291,7 @@ async fn test_mkdir_recursive_and_read_dir() {
253291
let tb = get_tb_builder().build().await;
254292
let resp = tb
255293
.request(|b| {
256-
b.uri("/mkdir/a/b/c/meow?recursive=true")
294+
b.uri(format!("/mkdir/{}?recursive=true", get_path("a/b/c/meow")))
257295
.method("GET")
258296
.body(Body::empty())
259297
.context("can't make request")
@@ -271,7 +309,7 @@ async fn test_mkdir_recursive_and_read_dir() {
271309
for [dir, expected] in [["", "a"], ["a", "b"], ["a/b", "c"], ["a/b/c", "meow"]] {
272310
let mut resp = tb
273311
.request(|b| {
274-
b.uri(format!("/read-dir/{}", dir))
312+
b.uri(format!("/read-dir/{}", get_path(dir)))
275313
.method("GET")
276314
.body(Body::empty())
277315
.context("can't make request")
@@ -302,7 +340,7 @@ async fn test_mkdir_with_no_recursive_opt_must_check_parent_path_exists() {
302340
let tb = get_tb_builder().build().await;
303341
let resp = tb
304342
.request(|b| {
305-
b.uri("/mkdir/a")
343+
b.uri(format!("/mkdir/{}?recursive=true", get_path("a")))
306344
.method("GET")
307345
.body(Body::empty())
308346
.context("can't make request")
@@ -322,7 +360,7 @@ async fn test_mkdir_with_no_recursive_opt_must_check_parent_path_exists() {
322360
.await;
323361
let resp = tb
324362
.request(|b| {
325-
b.uri("/mkdir/a/b/c")
363+
b.uri(format!("/mkdir/{}", get_path("a/b/c")))
326364
.method("GET")
327365
.body(Body::empty())
328366
.context("can't make request")
@@ -343,8 +381,9 @@ async fn test_mkdir_with_no_recursive_opt_must_check_parent_path_exists() {
343381
continue;
344382
}
345383

346-
found_no_such_file_or_directory_error =
347-
ev.msg.contains("No such file or directory: a/b");
384+
found_no_such_file_or_directory_error = ev
385+
.msg
386+
.contains(&format!("No such file or directory: {}", get_path("a/b")));
348387

349388
if found_no_such_file_or_directory_error {
350389
break;
@@ -365,7 +404,7 @@ async fn test_mkdir_recursive_and_remove_recursive() {
365404
let tb = get_tb_builder().build().await;
366405
let resp = tb
367406
.request(|b| {
368-
b.uri("/mkdir/a/b/c/meow?recursive=true")
407+
b.uri(format!("/mkdir/{}?recursive=true", get_path("a/b/c/meow")))
369408
.method("GET")
370409
.body(Body::empty())
371410
.context("can't make request")
@@ -389,7 +428,7 @@ async fn test_mkdir_recursive_and_remove_recursive() {
389428

390429
let resp = tb
391430
.request(|b| {
392-
b.uri("/write/a/b/c/meeeeow.bin")
431+
b.uri(format!("/write/{}", get_path("a/b/c/meeeeow.bin")))
393432
.method("POST")
394433
.body(arr.clone().into())
395434
.context("can't make request")
@@ -405,7 +444,7 @@ async fn test_mkdir_recursive_and_remove_recursive() {
405444
let tb = get_tb_builder().build().await;
406445
let mut resp = tb
407446
.request(|b| {
408-
b.uri("/read-dir/a/b/c")
447+
b.uri(format!("/read-dir/{}", get_path("a/b/c")))
409448
.method("GET")
410449
.body(Body::empty())
411450
.context("can't make request")
@@ -443,7 +482,7 @@ async fn test_mkdir_recursive_and_remove_recursive() {
443482
let tb = get_tb_builder().build().await;
444483
let mut resp = tb
445484
.request(|b| {
446-
b.uri("/read-dir/a")
485+
b.uri(format!("/read-dir/{}", get_path("a")))
447486
.method("GET")
448487
.body(Body::empty())
449488
.context("can't make request")
@@ -473,7 +512,7 @@ async fn test_mkdir_recursive_and_remove_recursive() {
473512
let tb = get_tb_builder().build().await;
474513
let mut resp = tb
475514
.request(|b| {
476-
b.uri("/read-dir")
515+
b.uri(format!("/read-dir/{}", get_root_path()))
477516
.method("GET")
478517
.body(Body::empty())
479518
.context("can't make request")
@@ -486,7 +525,15 @@ async fn test_mkdir_recursive_and_remove_recursive() {
486525
let buf = to_bytes(resp.body_mut()).await.unwrap();
487526
let value = DenoDirEntry::from_json_unchecked(&buf);
488527

489-
assert_eq!(value.len(), 1);
528+
assert_eq!(
529+
value.len(),
530+
if is_supabase_storage_being_tested() {
531+
// .emptyFolderPlaceholder in Supabase Storage
532+
2
533+
} else {
534+
1
535+
}
536+
);
490537
assert!(value.contains_key("a"));
491538
assert!(value.get("a").unwrap().is_directory);
492539

@@ -499,7 +546,7 @@ async fn test_mkdir_recursive_and_remove_recursive() {
499546
let tb = get_tb_builder().build().await;
500547
let mut resp = tb
501548
.request(|b| {
502-
b.uri("/read-dir")
549+
b.uri(format!("/read-dir/{}", get_root_path()))
503550
.method("GET")
504551
.body(Body::empty())
505552
.context("can't make request")
@@ -512,12 +559,16 @@ async fn test_mkdir_recursive_and_remove_recursive() {
512559
let buf = to_bytes(resp.body_mut()).await.unwrap();
513560
let value = DenoDirEntry::from_json_unchecked(&buf);
514561

515-
assert_eq!(value.len(), 0);
562+
assert_eq!(
563+
value.len(),
564+
if is_supabase_storage_being_tested() {
565+
// .emptyFolderPlaceholder in Supabase Storage
566+
1
567+
} else {
568+
0
569+
}
570+
);
516571

517572
tb.exit(Duration::from_secs(TESTBED_DEADLINE_SEC)).await;
518573
}
519574
}
520-
521-
fn is_supabase_storage_being_tested() -> bool {
522-
std::env::var("S3FS_TEST_SUPABASE_STORAGE").unwrap_or_default() == "true"
523-
}

0 commit comments

Comments
 (0)