From 32f4170b8be49c6ae134cadf3985faf0049d4b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 19 Oct 2016 22:00:32 +0200 Subject: [PATCH] Add basic timer test. --- tests/0-timer.d | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/0-timer.d diff --git a/tests/0-timer.d b/tests/0-timer.d new file mode 100644 index 0000000..b90931f --- /dev/null +++ b/tests/0-timer.d @@ -0,0 +1,55 @@ +/++ dub.sdl: + name "test" + dependency "eventcore" path=".." ++/ +module test; + +import eventcore.core; +import std.datetime : Clock, SysTime, UTC; +import std.stdio : writefln; +import core.time : Duration, msecs; + +SysTime s_startTime; +int s_cnt = 0; +bool s_done; + +void main() +{ + s_startTime = Clock.currTime(UTC()); + + auto tm = eventDriver.timers.create(); + eventDriver.timers.wait(tm, (tm) nothrow @safe { + scope (failure) assert(false); + + auto dur = Clock.currTime(UTC()) - s_startTime; + assert(dur > 200.msecs); + assert(dur < 220.msecs); + + s_startTime = Clock.currTime(UTC()); + eventDriver.timers.set(tm, 100.msecs, 100.msecs); + + void secondTier(TimerID timer) nothrow @safe { + scope (failure) assert(false); + auto dur = Clock.currTime(UTC()) - s_startTime; + s_cnt++; + assert(dur > 100.msecs * s_cnt); + assert(dur < 100.msecs * s_cnt + 20.msecs); + + if (s_cnt == 3) { + s_done = true; + eventDriver.core.exit(); + } else eventDriver.timers.wait(tm, &secondTier); + } + + eventDriver.timers.wait(tm, &secondTier); + }); + + eventDriver.timers.set(tm, 200.msecs, 0.msecs); + + ExitReason er; + do er = eventDriver.core.processEvents(Duration.max); + while (er == ExitReason.idle); + //assert(er == ExitReason.outOfWaiters); // FIXME: see above + assert(s_done); + s_done = false; +}