Home / Companies / Ditto / Blog / Post Details
Content Deep Dive

Mocking time in Async Rust

Blog post from Ditto

Post Details
Company
Date Published
Author
Tom Karpiniec
Word Count
1,601
Company Posts That Month
2
Language
English
Hacker News Points
-
Post removed?
No
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.

Trends Found in this Post

No tracked trend matches for this post yet.

Use This Data

Use this post, company, and trend context to find content marketing opportunities, perform competitive analysis, or address product feature gaps via the Plushcap MCP server or the Plushcap API.