Skip to content

Commit

Permalink
rt: improve rng_seed test robustness (#5075)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hds committed Oct 5, 2022
1 parent fdc28bc commit 9b48635
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 42 deletions.
38 changes: 21 additions & 17 deletions tokio/tests/macros_select.rs
Expand Up @@ -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();
let rt1_values = rt1.block_on(async {
let _ = tokio::spawn(async { (select_0_to_9().await, select_0_to_9().await) }).await;
});

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 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 {
Expand Down
57 changes: 36 additions & 21 deletions tokio/tests/rt_basic.rs
Expand Up @@ -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);
(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);

let random = tokio::macros::support::thread_rng_n(100);
assert_eq!(random, 10);
(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 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 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, 1);
});
assert_eq!(rt1_values_1, rt2_values_1);
assert_eq!(rt1_values_2, rt2_values_2);
}
}

Expand Down
25 changes: 21 additions & 4 deletions tokio/tests/rt_threaded.rs
Expand Up @@ -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();
let rt1_value = rt1.block_on(async {
let random = tokio::macros::support::thread_rng_n(100);
assert_eq!(random, 86);

rt.block_on(async {
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;
});

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.
let random = tokio::macros::support::thread_rng_n(100);
assert_eq!(random, 64);
tokio::macros::support::thread_rng_n(100);
})
.await;
});

assert_eq!(rt1_value, rt2_value);
}
}

0 comments on commit 9b48635

Please sign in to comment.