4 ms·
The easiest way to do this (in my experience) is libfaketime. I'm surprised not to see it mentioned. https://github.com/wolfcw/libfaketime https://github.com/
by ForkMeOnTinder 3y ago
The easiest way to do this (in my experience) is libfaketime. I'm surprised not to see it mentioned.
https://github.com/wolfcw/libfaketime https://github.com/wolfcw/libfaketime
- irrational 3y agoWhy the surprise? There is too much to know for anyone to know everything.
- theamk 3y agoI don't think it's that easy for the typical unit test though? Most of the time usage need to advance time in controlled fashion. So you'll need to create a shell wrapper that runs faketime with correct FAKETIME_TIMESTAMP_FILE value, hook it up to your build system, create C++ wrapper to write to this file... If you are controlling source code, adding a "time" argument might be easier to implement and easier to understand.
- deivid 3y agolibfaketime's awkward interface (control via timestamp files, needing LD_PRELOAD) is why I wrote an experimental library[0] which takes over the clock at the process level. My library modifies the code for the time-related vDSO functions, which is incredibly sketchy yet effective. There are also Python bindings at [1]. [0]: https://github.com/DavidVentura/tpom https://github.com/DavidVentura/tpom [1]: https://github.com/DavidVentura/py-tpom https://github.com/DavidVentura/py-tpom
- Brybry 3y agoWouldn't the FAKETIME environment variables be easy for typical unit testing?