Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-72366

Pass random seed to `srand` for unit tests

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None
    • None
    • Service Arch
    • ALL

    Description

      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"}
      

      Attachments

        Activity

          People

            billy.donahue@mongodb.com Billy Donahue
            pierlauro.sciarelli@mongodb.com Pierlauro Sciarelli
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: