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.

  1. It should be possible to test async code “as-is” in unit tests.
  2. In a test context, passage of time should be abstracted so that tests execute near-instantly no matter how long the delays are.
  3. Tests should be fully deterministic: an earlier timer always gets to work to completion before a timer scheduled later.
  4. As a unit test advances mock time, Instant::now() should return the correct intermediate time during the triggering of each timer.
  5. 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 over std::time and the tokio timer functions…