mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
rt: remove rng_seed+mt test and update docs (#5142)
While setting the random number generator seed with the multi-threaded scheduler does result in deterministic behavior related to the random number generator, threads still introduce non-determinism, making it hard (impossible?) to test this. There also is little value in doing so. This patch also updates the docs to remove mention of work stealing.
This commit is contained in:
@@ -836,22 +836,19 @@ impl Builder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specifies the random number generation seed to use within all threads associated
|
/// Specifies the random number generation seed to use within all
|
||||||
/// with the runtime being built.
|
/// threads associated with the runtime being built.
|
||||||
///
|
///
|
||||||
/// This option is intended to make certain parts of the runtime deterministic.
|
/// This option is intended to make certain parts of the runtime
|
||||||
/// Specifically, it affects the [`tokio::select!`] macro and the work stealing
|
/// deterministic (e.g. the [`tokio::select!`] macro). In the case of
|
||||||
/// algorithm. In the case of [`tokio::select!`] it will ensure that the order that
|
/// [`tokio::select!`] it will ensure that the order that branches are
|
||||||
/// branches are polled is deterministic.
|
/// polled is deterministic.
|
||||||
///
|
///
|
||||||
/// In the case of work stealing, it's a little more complicated. Each worker will
|
/// In addition to the code specifying `rng_seed` and interacting with
|
||||||
/// be given a deterministic seed so that the starting peer for each work stealing
|
/// the runtime, the internals of Tokio and the Rust compiler may affect
|
||||||
/// search will be deterministic.
|
/// the sequences of random numbers. In order to ensure repeatable
|
||||||
///
|
/// results, the version of Tokio, the versions of all other
|
||||||
/// In addition to the code specifying `rng_seed` and interacting with the runtime,
|
/// dependencies that interact with Tokio, and the Rust compiler version
|
||||||
/// the internals of Tokio and the Rust compiler may affect the sequences of random
|
|
||||||
/// numbers. In order to ensure repeatable results, the version of Tokio, the versions
|
|
||||||
/// of all other dependencies that interact with Tokio, and the Rust compiler version
|
|
||||||
/// should also all remain constant.
|
/// should also all remain constant.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
|||||||
@@ -542,7 +542,6 @@ fn rt() -> runtime::Runtime {
|
|||||||
#[cfg(tokio_unstable)]
|
#[cfg(tokio_unstable)]
|
||||||
mod unstable {
|
mod unstable {
|
||||||
use super::*;
|
use super::*;
|
||||||
use tokio::runtime::RngSeed;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_disable_lifo_slot() {
|
fn test_disable_lifo_slot() {
|
||||||
@@ -562,48 +561,4 @@ mod unstable {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn rng_seed() {
|
|
||||||
let seed = b"bytes used to generate seed";
|
|
||||||
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 rand_1 = tokio::macros::support::thread_rng_n(100);
|
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
|
||||||
.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_values = rt2.block_on(async {
|
|
||||||
let rand_1 = tokio::macros::support::thread_rng_n(100);
|
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
(rand_1, rand_2)
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(rt1_values, rt2_values);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user