@@ -37,6 +37,23 @@ async fn read_logs(module: &ModuleHandle) -> Vec<String> {
37
37
. collect :: < Vec < _ > > ( )
38
38
}
39
39
40
+ async fn call_reducer ( module : & ModuleHandle , reducer : & str , args : & str , request_id : u32 ) {
41
+ let reducer_id = module. client . module . info ( ) . reducers_map . lookup_id ( reducer) . unwrap ( ) ;
42
+
43
+ let json = format ! (
44
+ r#"{{
45
+ "CallReducer": {{
46
+ "reducer_id": {reducer_id},
47
+ "args": {args},
48
+ "request_id": {request_id},
49
+ "flags": 0
50
+ }}
51
+ }}"#
52
+ )
53
+ . to_string ( ) ;
54
+ module. send ( json) . await . unwrap ( ) ;
55
+ }
56
+
40
57
// The tests MUST be run in sequence because they read the OS environment
41
58
// and can cause a race when run in parallel.
42
59
@@ -46,24 +63,10 @@ fn test_calling_a_reducer_in_module(module_name: &'static str) {
46
63
CompiledModule :: compile ( module_name, CompilationMode :: Debug ) . with_module_async (
47
64
DEFAULT_CONFIG ,
48
65
|module| async move {
49
- let json =
50
- r#"{"CallReducer": {"reducer": "add", "args": "[\"Tyrion\", 24]", "request_id": 0, "flags": 0 }}"#
51
- . to_string ( ) ;
52
- module. send ( json) . await . unwrap ( ) ;
53
-
54
- let json =
55
- r#"{"CallReducer": {"reducer": "add", "args": "[\"Cersei\", 31]", "request_id": 1, "flags": 0 }}"#
56
- . to_string ( ) ;
57
- module. send ( json) . await . unwrap ( ) ;
58
-
59
- let json =
60
- r#"{"CallReducer": {"reducer": "say_hello", "args": "[]", "request_id": 2, "flags": 0 }}"# . to_string ( ) ;
61
- module. send ( json) . await . unwrap ( ) ;
62
-
63
- let json = r#"{"CallReducer": {"reducer": "list_over_age", "args": "[30]", "request_id": 3, "flags": 0 }}"#
64
- . to_string ( ) ;
65
- module. send ( json) . await . unwrap ( ) ;
66
-
66
+ call_reducer ( & module, "add" , "[\" Tyrion\" , 24]" , 0 ) . await ;
67
+ call_reducer ( & module, "add" , "[\" Cersei\" , 31]" , 1 ) . await ;
68
+ call_reducer ( & module, "say_hello" , "[]" , 2 ) . await ;
69
+ call_reducer ( & module, "list_over_age" , "[30]" , 3 ) . await ;
67
70
assert_eq ! (
68
71
read_logs( & module) . await ,
69
72
[
@@ -174,16 +177,11 @@ fn test_call_query_macro() {
174
177
// Hand-written JSON. This will fail if the JSON encoding of `ClientMessage` changes.
175
178
test_call_query_macro_with_caller ( |module| async move {
176
179
// Note that JSON doesn't allow multiline strings, so the encoded args string must be on one line!
177
- let json = r#"
178
- { "CallReducer": {
179
- "reducer": "test",
180
- "args":
181
- "[ { \"x\": 0, \"y\": 2, \"z\": \"Macro\" }, { \"foo\": \"Foo\" }, { \"Foo\": {} }, { \"Baz\": \"buzz\" } ]",
182
- "request_id": 0,
183
- "flags": 0
184
- } }"#
185
- . to_string ( ) ;
186
- module. send ( json) . await . unwrap ( ) ;
180
+ call_reducer (
181
+ & module,
182
+ "test" , "[ { \" x\" : 0, \" y\" : 2, \" z\" : \" Macro\" }, { \" foo\" : \" Foo\" }, { \" Foo\" : {} }, { \" Baz\" : \" buzz\" } ]" ,
183
+ 0 ,
184
+ ) . await ;
187
185
} ) ;
188
186
189
187
let args_pv = product ! [
@@ -256,13 +254,9 @@ fn test_index_scans() {
256
254
}
257
255
258
256
async fn bench_call < ' a > ( module : & ModuleHandle , call : & str , count : & u32 ) -> Duration {
259
- let json =
260
- format ! ( r#"{{"CallReducer": {{"reducer": "{call}", "args": "[{count}]", "request_id": 0, "flags": 0 }}}}"# ) ;
261
-
257
+ let args = format ! ( "[{count}]" ) ;
262
258
let now = Instant :: now ( ) ;
263
-
264
- module. send ( json) . await . unwrap ( ) ;
265
-
259
+ call_reducer ( module, call, & args, 0 ) . await ;
266
260
now. elapsed ( )
267
261
}
268
262
0 commit comments