2017-09-26 13:18:20 +00:00
|
|
|
/++ dub.sdl:
|
|
|
|
name "test"
|
|
|
|
dependency "eventcore" path=".."
|
|
|
|
+/
|
|
|
|
module test;
|
|
|
|
|
|
|
|
import eventcore.core;
|
2018-12-23 13:15:54 +00:00
|
|
|
import core.time : Duration, MonoTime, msecs;
|
2017-09-26 13:18:20 +00:00
|
|
|
import std.stdio : writefln;
|
|
|
|
|
2018-12-23 13:15:54 +00:00
|
|
|
MonoTime s_startTime;
|
2017-09-26 13:18:20 +00:00
|
|
|
int s_cnt = 0;
|
|
|
|
bool s_done;
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2018-12-23 13:15:54 +00:00
|
|
|
s_startTime = MonoTime.currTime();
|
2017-09-26 13:18:20 +00:00
|
|
|
|
|
|
|
bool timer1fired = false;
|
|
|
|
|
|
|
|
// first timer: one-shot 200ms
|
|
|
|
auto tm = eventDriver.timers.create();
|
|
|
|
eventDriver.timers.wait(tm, (tm) nothrow @safe {
|
2017-12-17 10:23:35 +00:00
|
|
|
try {
|
2017-12-16 17:53:58 +00:00
|
|
|
writefln("First timer");
|
2018-12-23 13:15:54 +00:00
|
|
|
auto dur = MonoTime.currTime() - s_startTime;
|
2017-09-26 13:18:20 +00:00
|
|
|
|
2017-12-17 19:34:52 +00:00
|
|
|
assert(dur >= 1200.msecs, (dur - 1200.msecs).toString());
|
|
|
|
assert(dur < 1300.msecs, (dur - 1200.msecs).toString());
|
2017-09-26 13:18:20 +00:00
|
|
|
|
2017-12-17 10:23:35 +00:00
|
|
|
timer1fired = true;
|
|
|
|
} catch (Exception e) {
|
|
|
|
assert(false, e.msg);
|
|
|
|
}
|
2017-09-26 13:18:20 +00:00
|
|
|
});
|
2017-12-17 19:34:52 +00:00
|
|
|
eventDriver.timers.set(tm, 1200.msecs, 0.msecs);
|
2017-09-26 13:18:20 +00:00
|
|
|
|
2017-12-17 19:34:52 +00:00
|
|
|
// second timer repeating 300ms, 5 times
|
2017-09-26 13:18:20 +00:00
|
|
|
auto tm2 = eventDriver.timers.create();
|
2017-12-17 19:34:52 +00:00
|
|
|
eventDriver.timers.set(tm2, 300.msecs, 300.msecs);
|
2017-09-26 13:18:20 +00:00
|
|
|
void periodicCallback(TimerID timer) nothrow @safe {
|
|
|
|
try {
|
|
|
|
writefln("Second timer");
|
|
|
|
|
2018-12-23 13:15:54 +00:00
|
|
|
auto dur = MonoTime.currTime() - s_startTime;
|
2017-09-26 13:18:20 +00:00
|
|
|
s_cnt++;
|
2017-12-17 19:34:52 +00:00
|
|
|
assert(dur > 300.msecs * s_cnt, (dur - 300.msecs * s_cnt).toString());
|
|
|
|
assert(dur < 300.msecs * s_cnt + 100.msecs, (dur - 300.msecs * s_cnt).toString());
|
|
|
|
assert(s_cnt <= 5);
|
2017-09-26 13:18:20 +00:00
|
|
|
|
2017-12-17 19:34:52 +00:00
|
|
|
if (s_cnt == 5) {
|
2017-09-26 13:18:20 +00:00
|
|
|
s_done = true;
|
|
|
|
eventDriver.timers.stop(timer);
|
|
|
|
assert(timer1fired, "Timer 1 didn't fire within 300ms");
|
|
|
|
} else eventDriver.timers.wait(tm2, &periodicCallback);
|
|
|
|
} catch (Exception e) {
|
|
|
|
assert(false, e.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eventDriver.timers.wait(tm2, &periodicCallback);
|
|
|
|
|
|
|
|
|
|
|
|
ExitReason er;
|
|
|
|
do er = eventDriver.core.processEvents(Duration.max);
|
|
|
|
while (er == ExitReason.idle);
|
|
|
|
assert(er == ExitReason.outOfWaiters);
|
|
|
|
assert(s_done);
|
|
|
|
s_done = false;
|
|
|
|
}
|