From 9b486357ed1aa008afa09e95965eb6c94a8753a1 Mon Sep 17 00:00:00 2001 From: Hayden Stainsby Date: Wed, 5 Oct 2022 14:31:08 +0200 Subject: [PATCH] rt: improve `rng_seed` test robustness (#5075) The original tests for the `Builder::rng_seed` added in #4910 were a bit fragile. There have already been a couple of instances where internal refactoring caused the tests to fail and need to be modified. While it is expected that internal refactoring may cause the random values to change, this shouldn't cause the tests to break. The tests should be more robust and not be affected by internal refactoring or changes in the Rust compiler version. The tests are changed to perform the same operation in 2 runtimes created with the same seed, the expectation is that the values that result from each runtime are the same. --- tokio/tests/macros_select.rs | 40 +++++++++++++----------- tokio/tests/rt_basic.rs | 59 ++++++++++++++++++++++-------------- tokio/tests/rt_threaded.rs | 27 ++++++++++++++--- 3 files changed, 81 insertions(+), 45 deletions(-) diff --git a/tokio/tests/macros_select.rs b/tokio/tests/macros_select.rs index e642a5d9f..ede8390b7 100644 --- a/tokio/tests/macros_select.rs +++ b/tokio/tests/macros_select.rs @@ -607,40 +607,44 @@ mod unstable { #[test] fn deterministic_select_current_thread() { let seed = b"bytes used to generate seed"; - let rt = tokio::runtime::Builder::new_current_thread() + let rt1 = tokio::runtime::Builder::new_current_thread() .rng_seed(RngSeed::from_bytes(seed)) .build() .unwrap(); + let rt1_values = rt1.block_on(async { (select_0_to_9().await, select_0_to_9().await) }); - rt.block_on(async { - let num = select_0_to_9().await; - assert_eq!(num, 5); + let rt2 = tokio::runtime::Builder::new_current_thread() + .rng_seed(RngSeed::from_bytes(seed)) + .build() + .unwrap(); + let rt2_values = rt2.block_on(async { (select_0_to_9().await, select_0_to_9().await) }); - let num = select_0_to_9().await; - assert_eq!(num, 1); - }); + assert_eq!(rt1_values, rt2_values); } #[test] #[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))] fn deterministic_select_multi_thread() { let seed = b"bytes used to generate seed"; - let rt = tokio::runtime::Builder::new_multi_thread() + let rt1 = tokio::runtime::Builder::new_multi_thread() .worker_threads(1) .rng_seed(RngSeed::from_bytes(seed)) .build() .unwrap(); - - rt.block_on(async { - let _ = tokio::spawn(async { - let num = select_0_to_9().await; - assert_eq!(num, 6); - - let num = select_0_to_9().await; - assert_eq!(num, 9); - }) - .await; + let rt1_values = rt1.block_on(async { + let _ = tokio::spawn(async { (select_0_to_9().await, select_0_to_9().await) }).await; }); + + let rt2 = tokio::runtime::Builder::new_multi_thread() + .worker_threads(1) + .rng_seed(RngSeed::from_bytes(seed)) + .build() + .unwrap(); + let rt2_values = rt2.block_on(async { + let _ = tokio::spawn(async { (select_0_to_9().await, select_0_to_9().await) }).await; + }); + + assert_eq!(rt1_values, rt2_values); } async fn select_0_to_9() -> u32 { diff --git a/tokio/tests/rt_basic.rs b/tokio/tests/rt_basic.rs index 2f4418659..6caf0a44b 100644 --- a/tokio/tests/rt_basic.rs +++ b/tokio/tests/rt_basic.rs @@ -385,43 +385,58 @@ mod unstable { #[test] fn rng_seed() { let seed = b"bytes used to generate seed"; - let rt = tokio::runtime::Builder::new_current_thread() + let rt1 = tokio::runtime::Builder::new_current_thread() .rng_seed(RngSeed::from_bytes(seed)) .build() .unwrap(); + let rt1_values = rt1.block_on(async { + let rand_1 = tokio::macros::support::thread_rng_n(100); + let rand_2 = tokio::macros::support::thread_rng_n(100); - rt.block_on(async { - let random = tokio::macros::support::thread_rng_n(100); - assert_eq!(random, 59); - - let random = tokio::macros::support::thread_rng_n(100); - assert_eq!(random, 10); + (rand_1, rand_2) }); + + let rt2 = tokio::runtime::Builder::new_current_thread() + .rng_seed(RngSeed::from_bytes(seed)) + .build() + .unwrap(); + let rt2_values = rt2.block_on(async { + let rand_1 = tokio::macros::support::thread_rng_n(100); + let rand_2 = tokio::macros::support::thread_rng_n(100); + + (rand_1, rand_2) + }); + + assert_eq!(rt1_values, rt2_values); } #[test] fn rng_seed_multi_enter() { let seed = b"bytes used to generate seed"; - let rt = tokio::runtime::Builder::new_current_thread() + + fn two_rand_values() -> (u32, u32) { + let rand_1 = tokio::macros::support::thread_rng_n(100); + let rand_2 = tokio::macros::support::thread_rng_n(100); + + (rand_1, rand_2) + } + + let rt1 = tokio::runtime::Builder::new_current_thread() .rng_seed(RngSeed::from_bytes(seed)) .build() .unwrap(); + let rt1_values_1 = rt1.block_on(async { two_rand_values() }); + let rt1_values_2 = rt1.block_on(async { two_rand_values() }); - rt.block_on(async { - let random = tokio::macros::support::thread_rng_n(100); - assert_eq!(random, 59); + let rt2 = tokio::runtime::Builder::new_current_thread() + .rng_seed(RngSeed::from_bytes(seed)) + .build() + .unwrap(); + let rt2_values_1 = rt2.block_on(async { two_rand_values() }); + let rt2_values_2 = rt2.block_on(async { two_rand_values() }); - let random = tokio::macros::support::thread_rng_n(100); - assert_eq!(random, 10); - }); - - rt.block_on(async { - let random = tokio::macros::support::thread_rng_n(100); - assert_eq!(random, 86); - - let random = tokio::macros::support::thread_rng_n(100); - assert_eq!(random, 1); - }); + assert_eq!(rt1_values_1, rt2_values_1); + assert_eq!(rt1_values_2, rt2_values_2); } } diff --git a/tokio/tests/rt_threaded.rs b/tokio/tests/rt_threaded.rs index 9a8644af6..0cfbb5cd5 100644 --- a/tokio/tests/rt_threaded.rs +++ b/tokio/tests/rt_threaded.rs @@ -566,23 +566,40 @@ mod unstable { #[test] fn rng_seed() { let seed = b"bytes used to generate seed"; - let rt = tokio::runtime::Builder::new_multi_thread() + let rt1 = tokio::runtime::Builder::new_multi_thread() .worker_threads(1) .rng_seed(RngSeed::from_bytes(seed)) .build() .unwrap(); - - rt.block_on(async { + let rt1_value = rt1.block_on(async { let random = tokio::macros::support::thread_rng_n(100); assert_eq!(random, 86); let _ = tokio::spawn(async { // Because we only have a single worker thread, the // RNG will be deterministic here as well. - let random = tokio::macros::support::thread_rng_n(100); - assert_eq!(random, 64); + tokio::macros::support::thread_rng_n(100); }) .await; }); + + let rt2 = tokio::runtime::Builder::new_multi_thread() + .worker_threads(1) + .rng_seed(RngSeed::from_bytes(seed)) + .build() + .unwrap(); + let rt2_value = rt2.block_on(async { + let random = tokio::macros::support::thread_rng_n(100); + assert_eq!(random, 86); + + let _ = tokio::spawn(async { + // Because we only have a single worker thread, the + // RNG will be deterministic here as well. + tokio::macros::support::thread_rng_n(100); + }) + .await; + }); + + assert_eq!(rt1_value, rt2_value); } }