-
Notifications
You must be signed in to change notification settings - Fork 1k
Add some tests of procedure concurrency #4955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gefjon
wants to merge
9
commits into
master
Choose a base branch
from
phoebe/test-procedure-concurrency
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8a05e99
Add Rust SDK test of procedure concurrency
gefjon 0723888
Test with both one and two connections, which see different behaviors
gefjon d51c5fb
Move new tests to a separate module/client pair
gefjon 5825261
Lints
gefjon 148ed27
Add test `procedure_concurrent_with_scheduled_reducer`
gefjon cf4ade6
Add a test of the current (mis)behavior of long-running scheduled pro…
gefjon abfbddb
Comments describing the test which tracks the scheduler misbehavior
gefjon 86ef8c1
Merge branch 'master' into phoebe/test-procedure-concurrency
gefjon a54d282
Same client actually are interleaved now, hooray!
gefjon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "sdk-test-procedure-concurrency-module" | ||
| version = "0.1.0" | ||
| edition.workspace = true | ||
| license-file = "LICENSE" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies] | ||
| log.workspace = true | ||
| anyhow.workspace = true | ||
| paste.workspace = true | ||
|
|
||
| [dependencies.spacetimedb] | ||
| workspace = true | ||
| features = ["unstable"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # `sdk-test-procedure-concurrency` *Rust* test | ||
|
|
||
| This module isolates procedure concurrency behavior that currently only has | ||
| Rust module coverage. | ||
|
|
||
| It is separate from [`sdk-test-procedure`](../sdk-test-procedure) so the shared | ||
| procedure test suite can continue targeting other module languages without also | ||
| requiring `ctx.sleep_until` support. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| use spacetimedb::{ | ||
| procedure, reducer, table, DbContext, ProcedureContext, ReducerContext, ScheduleAt, Table, TxContext, | ||
| }; | ||
| use std::time::Duration; | ||
|
|
||
| #[table(public, accessor = procedure_concurrency_row)] | ||
| struct ProcedureConcurrencyRow { | ||
| #[auto_inc] | ||
| insertion_order: u32, | ||
| insertion_context: String, | ||
| } | ||
|
|
||
| fn insert_procedure_concurrency_row(ctx: &TxContext, insertion_context: &str) { | ||
| ctx.db.procedure_concurrency_row().insert(ProcedureConcurrencyRow { | ||
| insertion_order: 0, | ||
| insertion_context: insertion_context.into(), | ||
| }); | ||
| } | ||
|
|
||
| #[reducer] | ||
| fn insert_reducer_row(ctx: &ReducerContext) { | ||
| ctx.db().procedure_concurrency_row().insert(ProcedureConcurrencyRow { | ||
| insertion_order: 0, | ||
| insertion_context: "reducer".into(), | ||
| }); | ||
| } | ||
|
|
||
| #[procedure] | ||
| fn procedure_sleep_between_inserts(ctx: &mut ProcedureContext) { | ||
| ctx.with_tx(|ctx| insert_procedure_concurrency_row(ctx, "procedure_before")); | ||
| ctx.sleep_until(ctx.timestamp + Duration::from_secs(10)); | ||
| ctx.with_tx(|ctx| insert_procedure_concurrency_row(ctx, "procedure_after")); | ||
| } | ||
|
|
||
| #[table(accessor = scheduled_reducer_row, scheduled(insert_scheduled_reducer))] | ||
| struct ScheduledReducerRow { | ||
| #[primary_key] | ||
| #[auto_inc] | ||
| scheduled_id: u64, | ||
| scheduled_at: ScheduleAt, | ||
| } | ||
|
|
||
| #[reducer] | ||
| fn insert_scheduled_reducer(ctx: &ReducerContext, _schedule: ScheduledReducerRow) { | ||
| ctx.db().procedure_concurrency_row().insert(ProcedureConcurrencyRow { | ||
| insertion_order: 0, | ||
| insertion_context: "scheduled_reducer".into(), | ||
| }); | ||
| } | ||
|
|
||
| #[procedure] | ||
| fn procedure_schedule_reducer_between_inserts(ctx: &mut ProcedureContext) { | ||
| ctx.with_tx(|ctx| { | ||
| insert_procedure_concurrency_row(ctx, "procedure_before"); | ||
| ctx.db.scheduled_reducer_row().insert(ScheduledReducerRow { | ||
| scheduled_id: 0, | ||
| scheduled_at: ctx.timestamp.into(), | ||
| }); | ||
| }); | ||
| ctx.sleep_until(ctx.timestamp + Duration::from_secs(10)); | ||
| ctx.with_tx(|ctx| insert_procedure_concurrency_row(ctx, "procedure_after")); | ||
| } | ||
|
|
||
| #[table(accessor = scheduled_procedure_row, scheduled(scheduled_procedure_sleep_between_inserts))] | ||
| struct ScheduledProcedureRow { | ||
| #[primary_key] | ||
| #[auto_inc] | ||
| scheduled_id: u64, | ||
| scheduled_at: ScheduleAt, | ||
| } | ||
|
|
||
| #[procedure] | ||
| fn scheduled_procedure_sleep_between_inserts(ctx: &mut ProcedureContext, _schedule: ScheduledProcedureRow) { | ||
| ctx.with_tx(|ctx| insert_procedure_concurrency_row(ctx, "scheduled_procedure_before")); | ||
| ctx.sleep_until(ctx.timestamp + Duration::from_secs(10)); | ||
| ctx.with_tx(|ctx| insert_procedure_concurrency_row(ctx, "scheduled_procedure_after")); | ||
| } | ||
|
|
||
| #[reducer] | ||
| fn schedule_procedure_then_reducer(ctx: &ReducerContext) { | ||
| ctx.db().scheduled_procedure_row().insert(ScheduledProcedureRow { | ||
| scheduled_id: 0, | ||
| scheduled_at: ctx.timestamp.into(), | ||
| }); | ||
| ctx.db().scheduled_reducer_row().insert(ScheduledReducerRow { | ||
| scheduled_id: 0, | ||
| scheduled_at: (ctx.timestamp + Duration::from_secs(2)).into(), | ||
| }); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| [package] | ||
| name = "procedure-concurrency-client" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license-file = "LICENSE" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "rlib"] | ||
|
|
||
| [features] | ||
| default = ["native"] | ||
|
|
||
| native = [ | ||
| "dep:env_logger", | ||
| "dep:tokio", | ||
| ] | ||
|
|
||
| browser = [ | ||
| "spacetimedb-sdk/browser", | ||
| "dep:wasm-bindgen", | ||
| "dep:wasm-bindgen-futures", | ||
| "dep:console_error_panic_hook", | ||
| "dep:futures", | ||
| ] | ||
|
|
||
| [[bin]] | ||
| name = "procedure-concurrency-client" | ||
| path = "src/main.rs" | ||
| required-features = ["native"] | ||
|
|
||
| [dependencies] | ||
| spacetimedb-sdk = { path = "../.." } | ||
| test-counter = { path = "../test-counter" } | ||
| anyhow.workspace = true | ||
| env_logger = { workspace = true, optional = true } | ||
| tokio = { workspace = true, optional = true } | ||
| futures = { workspace = true, optional = true } | ||
|
|
||
| wasm-bindgen = { version = "0.2.100", optional = true } | ||
| wasm-bindgen-futures = { version = "0.4.45", optional = true } | ||
| console_error_panic_hook = { version = "0.1.7", optional = true } | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| This test client is used with the module: | ||
|
|
||
| - [`sdk-test-procedure-concurrency`](/modules/sdk-test-procedure-concurrency) | ||
|
|
||
| The goal of the test is to exercise the current reducer/procedure concurrency | ||
| behavior of the Rust module ABI and the Rust SDK. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #![allow(clippy::disallowed_macros)] | ||
|
|
||
| mod module_bindings; | ||
| pub mod test_handlers; | ||
|
|
||
| #[cfg(all(target_arch = "wasm32", feature = "browser"))] | ||
| use wasm_bindgen::prelude::wasm_bindgen; | ||
|
|
||
| #[cfg(all(target_arch = "wasm32", feature = "browser"))] | ||
| #[wasm_bindgen] | ||
| pub async fn run(test_name: String, db_name: String) { | ||
| console_error_panic_hook::set_once(); | ||
| test_handlers::dispatch(&test_name, &db_name).await; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| use procedure_concurrency_client::test_handlers; | ||
|
|
||
| fn exit_on_panic() { | ||
| let default_hook = std::panic::take_hook(); | ||
| std::panic::set_hook(Box::new(move |panic_info| { | ||
| default_hook(panic_info); | ||
| std::process::exit(1); | ||
| })); | ||
| } | ||
|
|
||
| fn main() { | ||
| env_logger::init(); | ||
| exit_on_panic(); | ||
|
|
||
| let test = std::env::args() | ||
| .nth(1) | ||
| .expect("Pass a test name as a command-line argument to the test client"); | ||
| let db_name = std::env::var("SPACETIME_SDK_TEST_DB_NAME").expect("Failed to read db name from env"); | ||
|
|
||
| tokio::runtime::Runtime::new() | ||
| .unwrap() | ||
| .block_on(test_handlers::dispatch(&test, &db_name)); | ||
| } |
61 changes: 61 additions & 0 deletions
61
...rust/tests/procedure-concurrency-client/src/module_bindings/insert_reducer_row_reducer.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: my only concern here is that these sleeps make this test run for > 30s. Perhaps we could add a polling loop that sleeps for shorter intervals until we observe an insert event?