Darkenor
Darkenor

Reputation: 4449

How to pause a Boost unit test?

I'm writing a unit test for a timer I created and I'm running into a problem I encounter often with other projects. I would like to create a unit test that starts my timer, waits a few moments, and then checks the value of my timer after the timer has been run. I could then test all the functionality I have in my timer.

Is there a way to do this? I don't see anything in the documentation. If this is not a good practice, please advise what I should do instead.

Upvotes: 1

Views: 932

Answers (2)

Soverman
Soverman

Reputation: 1135

If you're unit-testing anything involving time you probably want to virtualize your notion of time so you can change it manually. Depending on what you're using for your timer this may be easier or harder to do (or built in), but once you've done it you can then write your unit tests using something like "setVirtualTime(time)".

Some advantages of this approach:

  1. The runtime of your unit tests don't depend on how long the timers in question are (once you have timers measured in hours / days this becomes important)
  2. Your unit tests are deterministic, and do not randomly fail depending on the vagaries of thread scheduling.

Upvotes: 5

Fred Larson
Fred Larson

Reputation: 62063

Maybe call sleep() in your unit test?

Upvotes: 1

Related Questions