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.
This commit is contained in:
Hayden Stainsby 2022-10-05 14:31:08 +02:00 committed by GitHub
parent fdc28bc883
commit 9b486357ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 45 deletions

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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);
}
}