Mocking Time in Async Rust
I recently authored a post on the Ditto blog called Mocking Time in Async Rust, which is about a little helper library we built for use in tests. This was a bit of an experiment but it’s already bearing fruit. If you have any thoughts, want to recommend other work in this space, or have ideas for improvements, please drop me an email!
I promise there is a minimum of corporate fluff.
…
What we want is a way to test our time-driven code with fewer compromises.
- It should be possible to test async code “as-is” in unit tests.
- In a test context, passage of time should be abstracted so that tests execute near-instantly no matter how long the delays are.
- Tests should be fully deterministic: an earlier timer always gets to work to completion before a timer scheduled later.
- As a unit test advances mock time,
Instant::now()
should return the correct intermediate time during the triggering of each timer.- The processing work attached to a timer should allow new timers to be registered along the way.
Ditto has built an internal library to address all of these problems. This crate,
ditto_time
, abstracts overstd::time
and the tokio timer functions…