File tree Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ description = "The labs of Raft consensus algorithm on MadSim."
8
8
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9
9
10
10
[dependencies ]
11
- madsim = { version = " 0.1" , features = [" rpc" , " macros" , " logger" ]}
11
+ madsim = { version = " 0.1.1 " , features = [" rpc" , " macros" , " logger" ]}
12
12
log = " 0.4"
13
13
rand = " 0.8"
14
14
futures = " 0.3"
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ If a test fails, you will see a seed in the output:
37
37
---- raft::tests::initial_election_2a stdout ----
38
38
thread 'raft::tests::initial_election_2a' panicked at 'expected one leader, got none', src/raft/tester.rs:91:9
39
39
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
40
- seed =1629626496
40
+ MADSIM_TEST_SEED =1629626496
41
41
```
42
42
43
43
Run the test again with the seed, and you will get exactly the same output:
@@ -58,6 +58,26 @@ Run the test multiple times to make sure you solution can stably pass the test:
58
58
MADSIM_TEST_NUM=100 cargo test --release
59
59
```
60
60
61
+ ### Ensure Determinism
62
+
63
+ Sometimes you may find that the test is not deterministic :(
64
+
65
+ Although the testing framework itself (MadSim) provides determinism, the entire system is not deterministic if your code introduces randomness.
66
+
67
+ Here are some tips to avoid randomness:
68
+
69
+ * Use ` madsim::rand::rng ` instead of ` rand::thread_rng ` to generate random numbers.
70
+ * Use ` futures::select_biased ` instead of ` futures::select ` macro.
71
+ * Do not ** iterate** through a ` HashMap ` .
72
+
73
+ To make sure your code is deterministic, run your test with the following environment variable:
74
+
75
+ ``` sh
76
+ MADSIM_TEST_CHECK_DETERMINISTIC=1 cargo test
77
+ ```
78
+
79
+ Your test will be run at least twice with the same seed. If any non-determinism is detected, it will panic as soon as possible.
80
+
61
81
Happy coding and Good luck!
62
82
63
83
## License
You can’t perform that action at this time.
0 commit comments