Skip to content

Commit fa0eeaf

Browse files
committed
add test schedule_procedure
1 parent dc881f2 commit fa0eeaf

File tree

10 files changed

+548
-4
lines changed

10 files changed

+548
-4
lines changed

modules/sdk-test-procedure/src/lib.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use spacetimedb::{procedure, table, ProcedureContext, SpacetimeType, Table, TxContext};
1+
use spacetimedb::{
2+
duration, procedure, reducer, table, DbContext, ProcedureContext, ReducerContext, ScheduleAt, SpacetimeType, Table,
3+
Timestamp, TxContext,
4+
};
25

36
#[derive(SpacetimeType)]
47
struct ReturnStruct {
@@ -98,3 +101,52 @@ fn insert_with_tx_rollback(ctx: &mut ProcedureContext) {
98101
// Assert that there's not a row.
99102
assert_row_count(ctx, 0);
100103
}
104+
105+
/// A reducer that schedules [`scheduled_proc`] via `ScheduledProcTable`.
106+
#[reducer]
107+
fn schedule_proc(ctx: &ReducerContext) {
108+
// Schedule the procedure to run in 1s.
109+
ctx.db().scheduled_proc_table().insert(ScheduledProcTable {
110+
scheduled_id: 0,
111+
scheduled_at: duration!("1000ms").into(),
112+
// Store the timestamp at which this reducer was called.
113+
// In tests, we'll compare this with the timestamp the procedure was called.
114+
reducer_ts: ctx.timestamp,
115+
x: 42,
116+
y: 24,
117+
});
118+
}
119+
120+
#[table(name = scheduled_proc_table, scheduled(scheduled_proc))]
121+
struct ScheduledProcTable {
122+
#[primary_key]
123+
#[auto_inc]
124+
scheduled_id: u64,
125+
scheduled_at: ScheduleAt,
126+
reducer_ts: Timestamp,
127+
x: u8,
128+
y: u8,
129+
}
130+
131+
/// A procedure that should be called 1s after `schedule_proc`.
132+
#[procedure]
133+
fn scheduled_proc(ctx: &mut ProcedureContext, data: ScheduledProcTable) {
134+
let ScheduledProcTable { reducer_ts, x, y, .. } = data;
135+
let procedure_ts = ctx.timestamp;
136+
ctx.with_tx(|ctx| {
137+
ctx.db().proc_inserts_into().insert(ProcInsertsInto {
138+
reducer_ts,
139+
procedure_ts,
140+
x,
141+
y,
142+
})
143+
});
144+
}
145+
146+
#[table(name = proc_inserts_into, public)]
147+
struct ProcInsertsInto {
148+
reducer_ts: Timestamp,
149+
procedure_ts: Timestamp,
150+
x: u8,
151+
y: u8,
152+
}

sdks/rust/tests/procedure-client/src/main.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn main() {
4343
"procedure-http-err" => exec_procedure_http_err(),
4444
"insert-with-tx-commit" => exec_insert_with_tx_commit(),
4545
"insert-with-tx-rollback" => exec_insert_with_tx_rollback(),
46+
"schedule-procedure" => exec_schedule_procedure(),
4647
_ => panic!("Unknown test: {test}"),
4748
}
4849
}
@@ -97,7 +98,7 @@ fn connect_then(
9798
}
9899

99100
/// A query that subscribes to all rows from all tables.
100-
const SUBSCRIBE_ALL: &[&str] = &["SELECT * FROM my_table;"];
101+
const SUBSCRIBE_ALL: &[&str] = &["SELECT * FROM my_table;", "SELECT * FROM proc_inserts_into;"];
101102

102103
fn subscribe_all_then(ctx: &impl RemoteDbContext, callback: impl FnOnce(&SubscriptionEventContext) + Send + 'static) {
103104
subscribe_these_then(ctx, SUBSCRIBE_ALL, callback)
@@ -310,5 +311,30 @@ fn exec_procedure_http_err() {
310311
})
311312
}
312313
});
314+
315+
test_counter.wait_for_all();
316+
}
317+
318+
fn exec_schedule_procedure() {
319+
let test_counter = TestCounter::new();
320+
let sub_applied_nothing_result = test_counter.add_test("on_subscription_applied_nothing");
321+
322+
let mut callback_result = Some(test_counter.add_test("insert_with_tx_commit_callback"));
323+
324+
connect_then(&test_counter, {
325+
move |ctx| {
326+
ctx.db().proc_inserts_into().on_insert(move |_, row| {
327+
assert_eq!(row.x, 24);
328+
assert_eq!(row.y, 42);
329+
(callback_result.take().unwrap())(Ok(()));
330+
});
331+
332+
subscribe_all_then(ctx, move |ctx| {
333+
sub_applied_nothing_result(assert_all_tables_empty(ctx));
334+
ctx.reducers.schedule_proc().unwrap();
335+
});
336+
}
337+
});
338+
313339
test_counter.wait_for_all();
314340
}

sdks/rust/tests/procedure-client/src/module_bindings/mod.rs

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

sdks/rust/tests/procedure-client/src/module_bindings/proc_inserts_into_table.rs

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

sdks/rust/tests/procedure-client/src/module_bindings/proc_inserts_into_type.rs

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

0 commit comments

Comments
 (0)