From 9bf08757787faea3ca1c21108845bf338e9fbc92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Mon, 24 Oct 2016 00:44:55 +0200 Subject: [PATCH] Add event test and fix the notify_all==false case. --- source/eventcore/drivers/posix.d | 2 +- tests/0-event.d | 69 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/0-event.d diff --git a/source/eventcore/drivers/posix.d b/source/eventcore/drivers/posix.d index e13372e..5fa1f8d 100644 --- a/source/eventcore/drivers/posix.d +++ b/source/eventcore/drivers/posix.d @@ -1034,7 +1034,7 @@ final class PosixEventDriverEvents(Loop : PosixEventLoop) : EventDriverEvents { } } else { if (!m_loop.m_fds[event].event.waiters.empty) - m_loop.m_fds[event].event.waiters.consumeOne(); + m_loop.m_fds[event].event.waiters.consumeOne()(event); } } diff --git a/tests/0-event.d b/tests/0-event.d new file mode 100644 index 0000000..c8b412b --- /dev/null +++ b/tests/0-event.d @@ -0,0 +1,69 @@ +/++ dub.sdl: + name "test" + dependency "eventcore" path=".." ++/ +module test; + +import eventcore.core; +import std.stdio : writefln; +import core.stdc.signal; +import core.time : Duration, msecs; +import core.thread : Thread; + +bool s_done; +shared EventDriverEvents ss_evts; + +void test(bool notify_all) +{ + auto id = eventDriver.events.create(); + auto tm = eventDriver.timers.create(); + + eventDriver.events.wait(id, (id) { + assert(!s_done); + + eventDriver.events.wait(id, (id) { + assert(!s_done); + s_done = true; + eventDriver.timers.cancelWait(tm); + eventDriver.core.exit(); + }); + }); + + eventDriver.timers.set(tm, 500.msecs, 0.msecs); + eventDriver.timers.wait(tm, (tm) { + eventDriver.events.trigger(id, notify_all); + eventDriver.timers.set(tm, 500.msecs, 0.msecs); + eventDriver.timers.wait(tm, (tm) @trusted { + scope (failure) assert(false); + new Thread({ + ss_evts.trigger(id, notify_all); + }).start(); + + eventDriver.timers.set(tm, 500.msecs, 0.msecs); + eventDriver.timers.wait(tm, (tm) @trusted { + assert(false, "Test hung."); + }); + }); + }); + + 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; +} + +void main() +{ + ss_evts = cast(shared)eventDriver.events; + test(false); + test(true); +} + +void log(string s) +@safe nothrow { + import std.stdio : writeln; + scope (failure) assert(false); + writeln(s); +}