Company
Date Published
Author
Tom Karpiniec
Word count
1601
Language
English
Hacker News points
None

Summary

Ditto, a company that heavily utilizes asynchronous Rust for networking code, has developed an internal library called `ditto_time` to address the challenges of testing asynchronous code with timers. Traditional methods of testing async code can lead to non-deterministic and slow tests, which can cause unreliable CI/CD processes. The `ditto_time` library abstracts over `std::time` and Tokio's timer functions, allowing tests to execute almost instantly and deterministically by advancing mock time rather than real time. This approach ensures that async code, such as that involving timers, can be tested as-is, reflecting real-world scenarios without the drawbacks of conventional testing methods. The library employs thread-local storage to handle multiple concurrent tests and uses a `TimeControl` instance to manage mock timers, which aids in maintaining the integrity of test environments. By using this technique, Ditto can efficiently test its async business logic directly, benefiting both the company and its customers by avoiding unnecessary segmentation of code into synchronous and asynchronous components.