3 ms·
>reproduce the bug / reduce randomness In every app I do I abstract time and scheduling/threading away, to be able to run any domain logic in a deterministic a
by jffhn 4y ago
>reproduce the bug / reduce randomness
In every app I do I abstract time and scheduling/threading away, to be able to run any domain logic in a deterministic and as fast as possible way (i.e. the clock jumps to next event/treatment time instead of having to wait for it).
It's such a bliss for testing and debugging (can run hours in seconds, turn on all logs safely, rule out threading issues by reproducing with a single thread, etc.), it's a shame it's not a built-in feature in most (any?) languages or their core libraries clock/scheduling APIs.
- plugin-baby 4y agoI’d love to hear more about this. Which languages do you normally work in? Any recommended reading about this topic/approach?
- jffhn 4y ago>Which languages do you normally work in? I work mostly in Java, and started to do it in C/Ada, but anything Turing-complete should be fine. >Any recommended reading about this topic/approach? I learnt it implementing HLA norm for distributed simulations (https://en.wikipedia.org/wiki/High_Level_Architecture https://en.wikipedia.org/wiki/High_Level_Architecture), but most of HLA is about distributed work synchronization, if you stay local you just need to retain the idea of advancing the time discretely from software, instead of having it flow uncontrollably with hardware ticks. Here is a previous comment I made on the same topic: https://news.ycombinator.com/item?id=22390113 https://news.ycombinator.com/item?id=22390113 You can find links to code in my (other) comment in the linked InfoQ article. In particular you can try out clocks/schedulers provided by jolikit's SchedulingHelper class, with various kinds of SchedulingType: HARD (time = f(system_time)), SOFT_HARD_BASED (virtual time trying to keep up/down with a HARD master clock), and SOFT_AFAP (virtual time jumping instantly between works).
- dcuthbertson 4y agoI recently discovered the joy of abstracting away time. I write a lot of software in Go for work. This library, https://github.com/benbjohnson/clock https://github.com/benbjohnson/clock, provides a Clock interface that can be used for creating either clocks with the same interface as the standard "time" module, or to create a mock clock that increments to the next tick on any and all tickers defined in the app. It really makes unit tests go faster, though it can be tricky to get the code right.