Skip to content

Commit

Permalink
rt: fix rng_seed test for threaded runtime
Browse files Browse the repository at this point in the history
The improvement to the `rng_seed` tests added in #5075 missed a case in
the `rt_threaded` tests which was still checking for a specific value.
As described in that PR, this makes the tests fragile and changing tokio
internals may require updating the test.

This change fixes that half-implemented improvement so that the tests no
longer depend on the exact internal ordering, but rather compare two
runs of separate runtimes built with the same seed to check that the
results are the same.
  • Loading branch information
hds committed Oct 27, 2022
1 parent 58c4571 commit ef9aeef
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions tokio/tests/rt_threaded.rs
Expand Up @@ -571,35 +571,39 @@ mod unstable {
.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);
let rt1_values = rt1.block_on(async {
let rand_1 = tokio::macros::support::thread_rng_n(100);

let _ = tokio::spawn(async {
let rand_2 = 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);
tokio::macros::support::thread_rng_n(100)
})
.await;
.await
.unwrap();

(rand_1, rand_2)
});

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 rt2_values = rt2.block_on(async {
let rand_1 = tokio::macros::support::thread_rng_n(100);

let _ = tokio::spawn(async {
let rand_2 = 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);
tokio::macros::support::thread_rng_n(100)
})
.await;
.await
.unwrap();

(rand_1, rand_2)
});

assert_eq!(rt1_value, rt2_value);
assert_eq!(rt1_values, rt2_values);
}
}

0 comments on commit ef9aeef

Please sign in to comment.