Skip to content

Commit 57ef919

Browse files
committed
[bfops/wasm-test]: WIP
1 parent 74b216f commit 57ef919

6 files changed

Lines changed: 48 additions & 12 deletions

File tree

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdks/rust/tests/connect_disconnect_client/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ crate-type = ["cdylib", "rlib"]
1313
default = ["native"]
1414

1515
# Builds the existing CLI test client.
16-
native = []
16+
native = ["dep:tokio"]
1717

1818
# Builds the client for wasm32-unknown-unknown using the Rust SDK `web` backend.
1919
web = [
2020
"spacetimedb-sdk/web",
2121
"dep:wasm-bindgen",
2222
"dep:wasm-bindgen-futures",
23+
"dep:console_error_panic_hook",
24+
"dep:gloo-timers",
2325
"dep:futures",
2426
]
2527

@@ -33,9 +35,12 @@ spacetimedb-sdk = { path = "../.." }
3335
test-counter = { path = "../test-counter" }
3436
anyhow.workspace = true
3537
futures = { workspace = true, optional = true }
38+
tokio = { workspace = true, optional = true }
39+
gloo-timers = { version = "0.3.0", features = ["futures"], optional = true }
3640

3741
wasm-bindgen = { version = "0.2.100", optional = true }
3842
wasm-bindgen-futures = { version = "0.4.45", optional = true }
43+
console_error_panic_hook = { version = "0.1.7", optional = true }
3944

4045
[lints]
4146
workspace = true

sdks/rust/tests/connect_disconnect_client/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use wasm_bindgen::prelude::wasm_bindgen;
88

99
#[cfg(all(target_arch = "wasm32", feature = "web"))]
1010
#[wasm_bindgen]
11-
pub async fn run(_test_name: String) {
12-
cli::dispatch();
11+
pub async fn run(_test_name: String, db_name: String) {
12+
console_error_panic_hook::set_once();
13+
// The shared wasm test harness always passes `(test_name, db_name)`, even for
14+
// fixed-flow clients like this one that ignore the selector.
15+
cli::set_web_db_name(db_name);
16+
cli::dispatch().await;
1317
}

sdks/rust/tests/connect_disconnect_client/src/main.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,36 @@ use test_counter::TestCounter;
88

99
const LOCALHOST: &str = "http://localhost:3000";
1010

11+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
12+
static WEB_DB_NAME: OnceLock<String> = OnceLock::new();
13+
14+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
15+
pub(crate) fn set_web_db_name(db_name: String) {
16+
WEB_DB_NAME.set(db_name).expect("WASM DB name was already initialized");
17+
}
18+
1119
fn db_name_or_panic() -> String {
12-
std::env::var("SPACETIME_SDK_TEST_DB_NAME").expect("Failed to read db name from env")
20+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
21+
{
22+
return WEB_DB_NAME
23+
.get()
24+
.cloned()
25+
.expect("Failed to read db name from wasm runner");
26+
}
27+
28+
#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
29+
{
30+
std::env::var("SPACETIME_SDK_TEST_DB_NAME").expect("Failed to read db name from env")
31+
}
1332
}
1433

1534
#[cfg(not(target_arch = "wasm32"))]
1635
fn main() {
17-
dispatch();
36+
// Keep a single async execution path so native and wasm exercise the same logic.
37+
tokio::runtime::Runtime::new().unwrap().block_on(dispatch());
1838
}
1939

20-
pub(crate) fn dispatch() {
40+
pub(crate) async fn dispatch() {
2141
let disconnect_test_counter = TestCounter::new();
2242
let disconnect_result = disconnect_test_counter.add_test("disconnect");
2343

@@ -62,20 +82,20 @@ pub(crate) fn dispatch() {
6282
None => disconnect_result(Ok(())),
6383
}
6484
});
65-
let connection = build_connection(connection);
85+
let connection = build_connection(connection).await;
6686

6787
#[cfg(not(target_arch = "wasm32"))]
6888
let join_handle = connection.run_threaded();
6989
#[cfg(target_arch = "wasm32")]
7090
connection.run_background_task();
7191

72-
connect_test_counter.wait_for_all();
92+
wait_for_all(&connect_test_counter).await;
7393

74-
connection.disconnect().unwrap();
94+
disconnect_connection(&connection).await;
7595
#[cfg(not(target_arch = "wasm32"))]
7696
join_handle.join().unwrap();
7797

78-
disconnect_test_counter.wait_for_all();
98+
wait_for_all(&disconnect_test_counter).await;
7999

80100
let reconnect_test_counter = TestCounter::new();
81101
let reconnected_result = reconnect_test_counter.add_test("on_reconnect");
@@ -88,7 +108,7 @@ pub(crate) fn dispatch() {
88108
})
89109
.with_database_name(db_name_or_panic())
90110
.with_uri(LOCALHOST);
91-
let new_connection = build_connection(new_connection);
111+
let new_connection = build_connection(new_connection).await;
92112

93113
new_connection
94114
.subscription_builder()

sdks/rust/tests/test-client/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ use wasm_bindgen::prelude::wasm_bindgen;
1313
pub async fn run(test_name: String, db_name: String) {
1414
console_error_panic_hook::set_once();
1515
cli::set_web_db_name(db_name);
16-
cli::dispatch_async(&test_name).await;
16+
cli::dispatch(&test_name).await;
1717
}

sdks/rust/tests/test-counter/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ license-file = "LICENSE"
99
[dependencies]
1010
spacetimedb-data-structures.workspace = true
1111
anyhow.workspace = true
12+
futures.workspace = true
13+
gloo-timers = { version = "0.3.0", features = ["futures"] }
1214

1315
[lints]
1416
workspace = true

0 commit comments

Comments
 (0)