@@ -3,6 +3,7 @@ use std::mem;
33use std:: pin:: Pin ;
44use std:: sync:: Arc ;
55
6+ use rand:: rngs:: SmallRng ;
67use tokio:: runtime:: Runtime ;
78use tokio:: task:: JoinHandle ;
89use tokio:: task:: LocalSet ;
@@ -33,6 +34,11 @@ pub enum Kind<'a> {
3334pub struct Config {
3435 /// Whether io is enabled on the runtime.
3536 pub enable_io : bool ,
37+
38+ /// Rng used to seed the tokio runtime.
39+ ///
40+ /// Only used if the `tokio_unstable` cfg flag is set.
41+ pub rng : Option < SmallRng > ,
3642}
3743
3844/// Per host simulated runtime.
@@ -62,11 +68,11 @@ pub struct Rt<'a> {
6268}
6369
6470impl < ' a > Rt < ' a > {
65- pub ( crate ) fn client < F > ( nodename : Arc < str > , client : F , config : Config ) -> Self
71+ pub ( crate ) fn client < F > ( nodename : Arc < str > , client : F , mut config : Config ) -> Self
6672 where
6773 F : Future < Output = Result > + ' static ,
6874 {
69- let ( tokio, local) = init ( & config) ;
75+ let ( tokio, local) = init ( & mut config) ;
7076
7177 let handle = with ( & tokio, & local, || tokio:: task:: spawn_local ( client) ) ;
7278
@@ -80,12 +86,12 @@ impl<'a> Rt<'a> {
8086 }
8187 }
8288
83- pub ( crate ) fn host < F , Fut > ( nodename : Arc < str > , software : F , config : Config ) -> Self
89+ pub ( crate ) fn host < F , Fut > ( nodename : Arc < str > , software : F , mut config : Config ) -> Self
8490 where
8591 F : Fn ( ) -> Fut + ' a ,
8692 Fut : Future < Output = Result > + ' static ,
8793 {
88- let ( tokio, local) = init ( & config) ;
94+ let ( tokio, local) = init ( & mut config) ;
8995
9096 let software: Software = Box :: new ( move || Box :: pin ( software ( ) ) ) ;
9197 let handle = with ( & tokio, & local, || tokio:: task:: spawn_local ( software ( ) ) ) ;
@@ -101,8 +107,8 @@ impl<'a> Rt<'a> {
101107 }
102108
103109 pub ( crate ) fn no_software ( ) -> Self {
104- let config = Config :: default ( ) ;
105- let ( tokio, local) = init ( & config) ;
110+ let mut config = Config :: default ( ) ;
111+ let ( tokio, local) = init ( & mut config) ;
106112
107113 Self {
108114 kind : Kind :: NoSoftware ,
@@ -215,14 +221,14 @@ impl<'a> Rt<'a> {
215221 ///
216222 /// Both the [`Runtime`] and [`LocalSet`] are replaced with new instances.
217223 fn cancel_tasks ( & mut self ) {
218- let ( tokio, local) = init ( & self . config ) ;
224+ let ( tokio, local) = init ( & mut self . config ) ;
219225
220226 _ = mem:: replace ( & mut self . tokio , tokio) ;
221227 drop ( mem:: replace ( & mut self . local , local) ) ;
222228 }
223229}
224230
225- fn init ( config : & Config ) -> ( Runtime , LocalSet ) {
231+ fn init ( config : & mut Config ) -> ( Runtime , LocalSet ) {
226232 let mut tokio_builder = tokio:: runtime:: Builder :: new_current_thread ( ) ;
227233
228234 #[ cfg( tokio_unstable) ]
@@ -232,6 +238,13 @@ fn init(config: &Config) -> (Runtime, LocalSet) {
232238 tokio_builder. enable_io ( ) ;
233239 }
234240
241+ #[ cfg( tokio_unstable) ]
242+ if let Some ( rng) = & mut config. rng {
243+ let bytes: [ u8 ; 32 ] = rand:: Rng :: gen ( rng) ;
244+ let seed = tokio:: runtime:: RngSeed :: from_bytes ( & bytes) ;
245+ tokio_builder. rng_seed ( seed) ;
246+ }
247+
235248 let tokio = tokio_builder
236249 . enable_time ( )
237250 . start_paused ( true )
0 commit comments