Skip to content

Commit

Permalink
C# SDK tests (#706)
Browse files Browse the repository at this point in the history
* Add C# SDK tests

* Add memoization

* Increase timeout

* Mark module_bindings as LF

* Regenerate from Rust again

* Sort tables & reducers for determinism

* cargo fmt

* Lint & fmt fixups

* Lint fixups

* Allow dirs ending in .wasm
  • Loading branch information
RReverser committed Jan 16, 2024
1 parent fd6a5ce commit 79b2d04
Show file tree
Hide file tree
Showing 9 changed files with 1,808 additions and 229 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**/module_bindings/** linguist-generated=true
**/module_bindings/** linguist-generated=true eol=lf
9 changes: 7 additions & 2 deletions crates/bindings-csharp/Codegen/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,13 @@ class {r.Name}: IReducer {{
tableNames.Combine(addReducers),
(context, tuple) =>
{
var tableNames = tuple.Left;
var addReducers = tuple.Right;
// Sort tables and reducers by name to match Rust behaviour.
// Not really important outside of testing, but for testing
// it matters because we commit module-bindings
// so they need to match 1:1 between different langs.
var tableNames = tuple.Left.Sort();
var addReducers = tuple.Right.Sort((a, b) => a.Name.CompareTo(b.Name));
// Don't generate the FFI boilerplate if there are no tables or reducers.
if (tableNames.IsEmpty && addReducers.IsEmpty)
return;
context.AddSource(
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/src/subcommands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::E
query_params.push(("trace_log", "true"));
}

let path_to_wasm = crate::tasks::build(path_to_project, skip_clippy, build_debug)?;
let path_to_wasm = if !path_to_project.is_dir() && path_to_project.extension().map_or(false, |ext| ext == "wasm") {
path_to_project.clone()
} else {
crate::tasks::build(path_to_project, skip_clippy, build_debug)?
};
let program_bytes = fs::read(path_to_wasm)?;
println!(
"Uploading to {} => {}",
Expand Down
2 changes: 1 addition & 1 deletion crates/sdk/tests/test-counter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl TestCounter {
let lock = self.inner.lock().expect("TestCounterInner Mutex is poisoned");
let (lock, timeout_result) = self
.wait_until_done
.wait_timeout_while(lock, Duration::from_secs(5), |inner| {
.wait_timeout_while(lock, Duration::from_secs(30), |inner| {
inner.outcomes.len() != inner.registered.len()
})
.expect("TestCounterInner Mutex is poisoned");
Expand Down
280 changes: 139 additions & 141 deletions crates/sdk/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,141 +1,139 @@
use spacetimedb_testing::sdk::Test;

const MODULE: &str = "sdk-test";
const CLIENT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/test-client");

fn make_test(subcommand: &str) -> Test {
Test::builder()
.with_name(subcommand)
.with_module(MODULE)
.with_client(CLIENT)
.with_language("rust")
.with_bindings_dir("src/module_bindings")
.with_compile_command("cargo build")
.with_run_command(format!("cargo run -- {}", subcommand))
.build()
}

#[test]
fn insert_primitive() {
make_test("insert_primitive").run();
}

#[test]
fn delete_primitive() {
make_test("delete_primitive").run();
}

#[test]
fn update_primitive() {
make_test("update_primitive").run();
}

#[test]
fn insert_identity() {
make_test("insert_identity").run();
}

#[test]
fn delete_identity() {
make_test("delete_identity").run();
}

#[test]
fn update_identity() {
make_test("delete_identity").run();
}

#[test]
fn insert_address() {
make_test("insert_address").run();
}

#[test]
fn delete_address() {
make_test("delete_address").run();
}

#[test]
fn update_address() {
make_test("delete_address").run();
}

#[test]
fn on_reducer() {
make_test("on_reducer").run();
}

#[test]
fn fail_reducer() {
make_test("fail_reducer").run();
}

#[test]
fn insert_vec() {
make_test("insert_vec").run();
}

#[test]
fn insert_simple_enum() {
make_test("insert_simple_enum").run();
}

#[test]
fn insert_enum_with_payload() {
make_test("insert_enum_with_payload").run();
}

#[test]
fn insert_long_table() {
make_test("insert_long_table").run();
}

#[test]
fn resubscribe() {
make_test("resubscribe").run();
}

#[test]
#[should_panic]
fn should_fail() {
make_test("should_fail").run();
}

#[test]
fn reauth() {
make_test("reauth_part_1").run();
make_test("reauth_part_2").run();
}

#[test]
fn reconnect_same_address() {
make_test("reconnect_same_address").run();
}

#[test]
fn connect_disconnect_callbacks() {
Test::builder()
.with_name("connect_disconnect_callback")
.with_module("sdk-test-connect-disconnect")
.with_client(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/connect_disconnect_client"))
.with_language("rust")
.with_bindings_dir("src/module_bindings")
.with_compile_command("cargo build")
.with_run_command("cargo run")
.build()
.run();
}

#[test]
fn connect_disconnect_callbacks_csharp() {
Test::builder()
.with_name("connect_disconnect_callback_csharp")
.with_module("sdk-test-connect-disconnect-cs")
.with_client(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/connect_disconnect_client"))
.with_language("rust")
.with_bindings_dir("src/module_bindings")
.with_compile_command("cargo build")
.with_run_command("cargo run")
.build()
.run();
}
macro_rules! declare_tests_with_suffix {
($lang:ident, $suffix:literal) => {
mod $lang {
use spacetimedb_testing::sdk::Test;

const MODULE: &str = concat!("sdk-test", $suffix);
const CLIENT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/test-client");

fn make_test(subcommand: &str) -> Test {
Test::builder()
.with_name(subcommand)
.with_module(MODULE)
.with_client(CLIENT)
.with_language("rust")
.with_bindings_dir("src/module_bindings")
.with_compile_command("cargo build")
.with_run_command(format!("cargo run -- {}", subcommand))
.build()
}

#[test]
fn insert_primitive() {
make_test("insert_primitive").run();
}

#[test]
fn delete_primitive() {
make_test("delete_primitive").run();
}

#[test]
fn update_primitive() {
make_test("update_primitive").run();
}

#[test]
fn insert_identity() {
make_test("insert_identity").run();
}

#[test]
fn delete_identity() {
make_test("delete_identity").run();
}

#[test]
fn update_identity() {
make_test("delete_identity").run();
}

#[test]
fn insert_address() {
make_test("insert_address").run();
}

#[test]
fn delete_address() {
make_test("delete_address").run();
}

#[test]
fn update_address() {
make_test("delete_address").run();
}

#[test]
fn on_reducer() {
make_test("on_reducer").run();
}

#[test]
fn fail_reducer() {
make_test("fail_reducer").run();
}

#[test]
fn insert_vec() {
make_test("insert_vec").run();
}

#[test]
fn insert_simple_enum() {
make_test("insert_simple_enum").run();
}

#[test]
fn insert_enum_with_payload() {
make_test("insert_enum_with_payload").run();
}

#[test]
fn insert_long_table() {
make_test("insert_long_table").run();
}

#[test]
fn resubscribe() {
make_test("resubscribe").run();
}

#[test]
#[should_panic]
fn should_fail() {
make_test("should_fail").run();
}

#[test]
fn reauth() {
make_test("reauth_part_1").run();
make_test("reauth_part_2").run();
}

#[test]
fn reconnect_same_address() {
make_test("reconnect_same_address").run();
}

#[test]
fn connect_disconnect_callbacks() {
Test::builder()
.with_name(concat!("connect_disconnect_callback_", stringify!($lang)))
.with_module(concat!("sdk-test-connect-disconnect", $suffix))
.with_client(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/connect_disconnect_client"
))
.with_language("rust")
.with_bindings_dir("src/module_bindings")
.with_compile_command("cargo build")
.with_run_command("cargo run")
.build()
.run();
}
}
};
}

declare_tests_with_suffix!(rust, "");
declare_tests_with_suffix!(csharp, "-cs");

2 comments on commit 79b2d04

@github-actions
Copy link

@github-actions github-actions bot commented on 79b2d04 Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking failed. Please check the workflow run for details.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callgrind benchmark in progress...

Please sign in to comment.