diff options
author | Pau Espin Pedrol <pespin@sysmocom.de> | 2018-12-12 17:39:08 +0100 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2018-12-13 13:58:24 +0000 |
commit | 25185886f015201c97956ca9f400aa2bdbbeec27 (patch) | |
tree | e0cfdeca280f2f4ba84ae36a30fedd989c251b2e | |
parent | Timeval: Move to osmo_clock_gettime (diff) | |
download | OsmoTRX-25185886f015201c97956ca9f400aa2bdbbeec27.tar.xz OsmoTRX-25185886f015201c97956ca9f400aa2bdbbeec27.zip |
TimevalTest: Make test deterministic with fake time
Change-Id: I74e577a0142fb6d1ef3630e02aff9910b191bff9
-rw-r--r-- | tests/CommonLibs/TimevalTest.cpp | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/tests/CommonLibs/TimevalTest.cpp b/tests/CommonLibs/TimevalTest.cpp index 61a92c9..b822982 100644 --- a/tests/CommonLibs/TimevalTest.cpp +++ b/tests/CommonLibs/TimevalTest.cpp @@ -31,34 +31,45 @@ #include <assert.h> #include <sys/time.h> +extern "C" { +#include <osmocom/core/timer.h> +} + using namespace std; int main(int argc, char *argv[]) { - long last_remaining = 10000; + + osmo_clock_override_enable(CLOCK_REALTIME, true); + + struct timespec *clk = osmo_clock_override_gettimespec(CLOCK_REALTIME); + clk->tv_sec = 0; + clk->tv_nsec = 1000; + + long last_remaining = 10000; /*10 sec */ Timeval then(last_remaining); assert(then.elapsed() == -last_remaining); cerr << then << " elapsed: " << then.elapsed() << endl; /* Check that last_remaining parameter affects setting time in the future */ - usleep(10000); + osmo_clock_override_add(CLOCK_REALTIME, 0, 10*1000*1000); double increased_time_secs = Timeval().seconds(); - assert(increased_time_secs <= then.seconds()); + assert(increased_time_secs < then.seconds()); struct timespec invariant_time = then.timespec(); int loops = 0; while (!then.passed()) { struct timespec tspecnow = then.timespec(); - cerr << "now: " << Timeval().seconds() << " then: " << then << " remaining: " << then.remaining() << endl; + cerr << "["<< loops << "] now: " << Timeval().seconds() << " then: " << then << " remaining: " << then.remaining() << endl; assert(last_remaining >= then.remaining()); assert(tspecnow.tv_sec == invariant_time.tv_sec && tspecnow.tv_nsec == invariant_time.tv_nsec); - usleep(500000); + osmo_clock_override_add(CLOCK_REALTIME, 0, 500000*1000); loops++; } cerr << "now: " << Timeval() << " then: " << then << " remaining: " << then.remaining() << endl; - assert(then.remaining() <= 0); - assert(loops >= 18); + assert(then.remaining() == -10); + assert(loops == 20); printf("Done\n"); } |