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

Imprecise elapsed simulation time #207

Open
nickgian opened this issue Jan 2, 2025 · 0 comments
Open

Imprecise elapsed simulation time #207

nickgian opened this issue Jan 2, 2025 · 0 comments

Comments

@nickgian
Copy link
Contributor

nickgian commented Jan 2, 2025

sim_elapsed provides the simulation time elapsed as perceived by a host without a skew, by offsetting the host's time by the time the simulation started. However, when a host crashes time "freezes" for it and it remains skewed forever.
My title might be inaccurate - I am not sure if this is the desired behavior (to test impact of clock skew), but having a way for hosts to keep track of the simulation time elapsed (a global clock) simplifies testing global properties and debugging.

The example below demonstrates this:

   #[test]
    fn elapsed_time_across_crashes() -> Result {
        let tick_ms = 5;
        let mut sim = Builder::new()
            .tick_duration(Duration::from_millis(tick_ms))
            .build();


        sim.host("host", move || {
            async move {
                loop {
                    tokio::time::sleep(Duration::from_millis(1)).await;
                    println!("Time elapsed according to host: {}", sim_elapsed().unwrap().as_millis());
                }
            }
        });

        sim.host("host2", move || {
            async move {
                loop {
                    tokio::time::sleep(Duration::from_millis(1)).await;
                    println!("Time elapsed according to host2: {}", sim_elapsed().unwrap().as_millis());
                }
            }
        });

        println!("First run");
        sim.run()?;
        sim.crash("host");
        println!("Crashed host 1");
        sim.run()?;
        sim.bounce("host");
        println!("Bounced host 1");
        sim.run()?;

        Ok(())
    }

will output

First run
Time elapsed according to host: 1
Time elapsed according to host: 2
Time elapsed according to host: 3
Time elapsed according to host: 4
Time elapsed according to host2: 1
Time elapsed according to host2: 2
Time elapsed according to host2: 3
Time elapsed according to host2: 4
Crashed host 1
Time elapsed according to host2: 5
Time elapsed according to host2: 6
Time elapsed according to host2: 7
Time elapsed according to host2: 8
Time elapsed according to host2: 9
Bounced host 1
Time elapsed according to host: 6
Time elapsed according to host: 7
Time elapsed according to host: 8
Time elapsed according to host: 9
Time elapsed according to host2: 10
Time elapsed according to host2: 11
Time elapsed according to host2: 12
Time elapsed according to host2: 13
Time elapsed according to host2: 14
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

1 participant