-
Type: Bug
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Service Arch
-
ALL
In order to generate random-like numbers, srand is usually initialized to some distinctive runtime value. For example, in mongod main and mongos_main the seed is always based on the current timestamp XOR some random address.
In unit tests the generator is not seeded, so it's very difficult to generate real random sequences. There are a lot of unit tests relying on `std::rand()` (aka `rand()`) expecting to get nondeterministic behavior but that's not the case as shown by this unit test executed 3 times:
TEST_F(TmpSuite, StdRandNotRandomAtAll) { for (int i = 0; i < 5; i++) { logd("XOXO Generated random {}", rand()); } }
$ ./db_s_shard_server_test --filter StdRandNotRandomAtAll | grep XOXO {"t":{"$date":"2022-12-22T12:12:25.263Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1804289383"} {"t":{"$date":"2022-12-22T12:12:25.263Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 846930886"} {"t":{"$date":"2022-12-22T12:12:25.263Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1681692777"} {"t":{"$date":"2022-12-22T12:12:25.263Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1714636915"} {"t":{"$date":"2022-12-22T12:12:25.263Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1957747793"} $ ./db_s_shard_server_test --filter StdRandNotRandomAtAll | grep XOXO {"t":{"$date":"2022-12-22T12:12:29.389Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1804289383"} {"t":{"$date":"2022-12-22T12:12:29.389Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 846930886"} {"t":{"$date":"2022-12-22T12:12:29.389Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1681692777"} {"t":{"$date":"2022-12-22T12:12:29.389Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1714636915"} {"t":{"$date":"2022-12-22T12:12:29.389Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1957747793"} $ ./db_s_shard_server_test --filter StdRandNotRandomAtAll | grep XOXO {"t":{"$date":"2022-12-22T12:12:37.409Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1804289383"} {"t":{"$date":"2022-12-22T12:12:37.409Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 846930886"} {"t":{"$date":"2022-12-22T12:12:37.409Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1681692777"} {"t":{"$date":"2022-12-22T12:12:37.409Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1714636915"} {"t":{"$date":"2022-12-22T12:12:37.409Z"},"s":"I", "c":"-", "id":0, "ctx":"main","msg":"XOXO Generated random 1957747793"}
- duplicates
-
SERVER-43838 unit test: pseudorandom test seed option
- Backlog
- related to
-
SERVER-72373 Ban `rand`
- Closed