museun/mock_instant

removal of thread local clock breaks tests run in parallel

Closed this issue · 4 comments

Multiple tests can run at the same time on different threads, the thread local clock allowed each test to have its own state/clock but after updating to the latest version, when running an individual test, the test pass, however, when running multiple tests together, the tests fail because one test advancing the clock is messing with another test that expects the clock to not have been advanced yet for example and resetting the clock at the start of each test as suggested in the readme won't help with tests running at the same time.

This is also discussed in #14

The thread-local clock was also fragile. If one crate enabled the sync feature then all other crates that use this crate would break if they assumed they had the thread-local mock. This was a major problem, so I decided to remove it.

I'm considering adding it back, but instead of being behind a feature flag it'll be namespaced.

mock_instant::thread_local::MockClock and mock_instant::sync::MockClock (name pending)

This'll require a version bump because it'd break the api.

I've published a new version, 0.5.0 which adds a thread-local MockClock (and related types): https://docs.rs/mock_instant/latest/mock_instant/

I noticed you edited the title which mentioned #[tokio::test]

I would suggest when using tokio to use their Instant type. They provide a way to start their timer subsystem paused

https://docs.rs/tokio/1.38.0/tokio/time/struct.Instant.html

and
https://docs.rs/tokio/1.38.0/tokio/time/fn.pause.html
https://docs.rs/tokio/1.38.0/tokio/time/fn.resume.html
https://docs.rs/tokio/1.38.0/tokio/time/fn.advance.html

and
https://docs.rs/tokio/1.38.0/tokio/attr.test.html#configure-the-runtime-to-start-with-time-paused

I know its not as featureful as my crate, but it'll allow Instant::now() to be reset per test

thank you!