pub struct Runtime { /* private fields */ }
madsim
only.Expand description
The madsim runtime.
The runtime provides basic components for deterministic simulation, including a random number generator, timer, task scheduler, and simulated network and file system.
Implementations
sourceimpl Runtime
impl Runtime
sourcepub fn with_seed_and_config(seed: u64, config: Config) -> Self
pub fn with_seed_and_config(seed: u64, config: Config) -> Self
Create a new runtime instance with given seed and config.
sourcepub fn add_simulator<S: Simulator>(&self)
pub fn add_simulator<S: Simulator>(&self)
Register a simulator.
sourcepub fn handle(&self) -> &Handle
pub fn handle(&self) -> &Handle
Return a handle to the runtime.
The returned handle can be used by the supervisor (future in block_on) to control the whole system. For example, kill a node or disconnect the network.
sourcepub fn create_node(&self) -> NodeBuilder<'_>
pub fn create_node(&self) -> NodeBuilder<'_>
Create a node.
The returned handle can be used to spawn tasks that run on this node.
sourcepub fn block_on<F: Future>(&self, future: F) -> F::Output
pub fn block_on<F: Future>(&self, future: F) -> F::Output
Run a future to completion on the runtime. This is the runtime’s entry point.
This runs the given future on the current thread until it is complete.
Example
use madsim::runtime::Runtime;
let rt = Runtime::new();
let ret = rt.block_on(async { 1 });
assert_eq!(ret, 1);
Unlike usual async runtime, when there is no runnable task, it will panic instead of blocking.
use madsim::runtime::Runtime;
use futures::future::pending;
Runtime::new().block_on(pending::<()>());
sourcepub fn set_time_limit(&mut self, limit: Duration)
pub fn set_time_limit(&mut self, limit: Duration)
Set a time limit of the execution.
The runtime will panic when time limit exceeded.
Example
use madsim::{runtime::Runtime, time::{sleep, Duration}};
let mut rt = Runtime::new();
rt.set_time_limit(Duration::from_secs(1));
rt.block_on(async {
sleep(Duration::from_secs(2)).await;
});
sourcepub fn enable_determinism_check(&self, log: Option<Log>)
pub fn enable_determinism_check(&self, log: Option<Log>)
Enable determinism check during the simulation.
Example
use madsim::{runtime::Runtime, time::{sleep, Duration}};
use rand::Rng;
let f = || async {
for _ in 0..10 {
madsim::rand::thread_rng().gen::<u64>();
// introduce non-determinism
let rand_num = rand::thread_rng().gen_range(0..10);
sleep(Duration::from_nanos(rand_num)).await;
}
};
let mut rt = Runtime::new();
rt.enable_determinism_check(None); // enable log
rt.block_on(f());
let log = rt.take_rand_log(); // take log for next turn
let mut rt = Runtime::new();
rt.enable_determinism_check(log); // enable check
rt.block_on(f()); // run the same logic again,
// should panic here.
sourcepub fn take_rand_log(self) -> Option<Log>
pub fn take_rand_log(self) -> Option<Log>
Take random log so that you can check determinism in the next turn.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Runtime
impl !Send for Runtime
impl !Sync for Runtime
impl Unpin for Runtime
impl UnwindSafe for Runtime
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
sourcefn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
sourcefn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
sourcefn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
sourcefn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more