Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Reproduce, Random and Time #123

Closed
violin0622 opened this issue Jun 8, 2023 · 4 comments
Closed

Question: Reproduce, Random and Time #123

violin0622 opened this issue Jun 8, 2023 · 4 comments

Comments

@violin0622
Copy link

violin0622 commented Jun 8, 2023

I read the examples and tests code in turmoil but still have some puzzle:

  1. In the similar project MadSim , there is a "Test Seed" for every run to generate a deterministic time and random number, so users can use same seed to get exactly same result. Can turmoil do something like this? and how ?

BTW, I thought it was Sim::epoch() to do this, but I got different result by every run in code below:

use rand::SeedableRng;
use std::time::SystemTime;
use turmoil::{Builder, Result};

fn main() {
    println!("Hello, world!");
}

#[test]
fn test_main() -> Result {
    let mut sim = Builder::new()
        .epoch(SystemTime::UNIX_EPOCH)
        .rng(rand::rngs::StdRng::seed_from_u64(10))
        .build();

    sim.client("host", async {
        println!("Hello world!");

// now() is diffferent in every run . And there seems no API in turmoil to mock time.
        println!("now: {:?}", std::time::Instant::now());
        Ok(())
    });
    sim.run()
}
  1. Can turmoil simulate IO other than network? (for example, Disk IO )
@mcches
Copy link
Contributor

mcches commented Jun 9, 2023

For 1. you've got the rng and epoch setup correctly, but unfortunately we aren't tied tightly into tokio yet for Instant to work, note you are using std::time not tokio::time as well. The tokio Instant's elapsed() will work correctly, and we also expose the total elapsed() time in each host for building more accurate simulated clocks across host restarts.

For 2. we haven't built IO yet, but that is something we'd like to include: #15

@violin0622
Copy link
Author

For 1. you've got the rng and epoch setup correctly, but unfortunately we aren't tied tightly into tokio yet for Instant to work, note you are using std::time not tokio::time as well. The tokio Instant's elapsed() will work correctly, and we also expose the total elapsed() time in each host for building more accurate simulated clocks across host restarts.

Thx, I found turmoil::elapsed(), it works as alternatives of now() . But when I want to sleep(duration), without an mock API like turmoil:sleep() , how do I sleep a logical duration rather than realistic duration ?

@mcches
Copy link
Contributor

mcches commented Jun 14, 2023

Thx, I found turmoil::elapsed(), it works as alternatives of now() . But when I want to sleep(duration), without an mock API like turmoil:sleep() , how do I sleep a logical duration rather than realistic duration ?

tokio::time::sleep works on logical time as each host's runtime is paused.

See: https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html#method.start_paused
See: https://github.com/tokio-rs/turmoil/blob/main/src/rt.rs#L112

@violin0622
Copy link
Author

Good !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants