1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! A collection of simple utilities for building
//! flake-free testable systems.
// #![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]

use std::cell::RefCell;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use serde::de::DeserializeOwned;
use serde::Serialize;

/// Spawn threads with linux realtime priorities, and
/// inherit the spawner's shared seeded random number
/// generator and clock.
pub mod spawn;

/// Files that can simulate power failures by losing
/// data written after the last sync.
pub mod file;

/// A trait for building networked systems
/// that can be plugged into simulated networks
/// and partition tested in accelerated time.
pub trait Reactor: Sized {
    type Peer: std::net::ToSocketAddrs;
    type Message: Serialize + DeserializeOwned;
    type Config;
    type Error;

    const PERIODIC_INTERVAL: Option<Duration> = None;

    fn start(config: Self::Config) -> Result<Self, Self::Error>;

    fn receive(
        &mut self,
        from: Self::Peer,
        msg: Self::Message,
    ) -> Vec<(Self::Peer, Self::Message)>;

    fn periodic(&mut self) -> Vec<(Self::Peer, Self::Message)> {
        unimplemented!()
    }
}

thread_local! {
    pub static CONTEXT: RefCell<Arc<Context>> = RefCell::new(Arc::new(Context::default()));
}

fn context() -> Arc<Context> {
    CONTEXT.with(|c| c.borrow().clone())
}

fn set_context(context: Arc<Context>) {
    CONTEXT.with(|c| *c.borrow_mut() = context);
}

mod context;

use self::context::Context;

pub use self::context::{
    now, seed, set_seed, set_time, sleep, thread_rng, Rand,
};