3

I'm writing a C++ program that keeps track of dates whenever it's ran, keeping a record. I want to test and debug this record keeping functionality, but the records themselves can span years; letting time pass naturally to expose bugs would take... a while.

Is there an established way of simulating the passage of time, so I an debug this program more easily? I'm using C's ctime library to acquire dates.

Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
  • 3
    I would do a dependency injection for a way to get the current time. That way you can inject your own impl that will "fast foward" (e.g. provide a canned answer at some point in the future) – AndyG Mar 05 '18 at 19:07

1 Answers1

4

If you want your system to be testable, you'll want to be able to replace your entire time engine.

There are a number of ways to replace a time engine.

The easiest would be to create a new time engine singleton that replicates the time engine API you are using (in your case, ctime).

Then sweep out every use of ctime based APIs and redirect them to your new time engine.

This new time engine can be configured to use ctime, or something else.

To be more reliable, you'll even want to change the binary layout and type of any data structures used that interact with your old API, audit every case where you convert things to/from void pointers or reinterpret cast them, etc.


Another approach is dependency injection, where instead of using a singleton you pass the time-engine as an argument to your program. Every part of your program that needs access to time now either stores a pointer to the time engine, or takes it as a function argument.


Now that you have control over time you can arrange for it to pass faster, jump around, etc. Depending on your system you may want to be more or less realistic in how time passes, and it could expose different real and not real bugs.

Even after you do all of this you will not be certain that your program doesn't have time bugs. You may interact with OS time in ways you don't expect. But you can at least solve some of these problems.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • I only use time() in one location, so wrapping it in a function that can transform it was definitely the easiest path, thanks! – Anne Quinn Mar 05 '18 at 19:57
  • 1
    @AnneQuinn In theory you also need to mess around with any reading of time from file stamps and any other time-related operations, in case you need to extend the lies. – Yakk - Adam Nevraumont Mar 05 '18 at 19:58