Implemented `hasPendingEvents()` to check whether there are any pending events at all

Use `isPendingEvents()` is unit tests to ensure that we actually are able to see ALL events pushed dispatched before we, possibly, pre-maturely shutdown the engine with `shutdown()`

Removed unused function `runner(Event)`

Added new commented out test case

Formatted method documentation of some methods to conform to D's style
This commit is contained in:
Tristan B. Velloza Kildaire 2022-11-26 17:07:17 +02:00
parent 03dfe3ccfd
commit 490cfe85d6
1 changed files with 124 additions and 24 deletions

View File

@ -13,13 +13,13 @@ import eventy.exceptions;
import std.stdio; import std.stdio;
/* TODO: Move elsewhere, this thing thinks it's a delegate in the unit test, idk why */ // /* TODO: Move elsewhere, this thing thinks it's a delegate in the unit test, idk why */
private void runner(Event e) // private void runner(Event e)
{ // {
import std.stdio; // import std.stdio;
writeln("Running event", e.id); // writeln("Running event", e.id);
} // }
unittest unittest
{ {
@ -71,10 +71,69 @@ unittest
writeln("done with main thread code"); writeln("done with main thread code");
while(engine.hasPendingEvents()) {}
/* TODO: Before shutting down, actually test it out (i.e. all events ran) */ /* TODO: Before shutting down, actually test it out (i.e. all events ran) */
engine.shutdown(); engine.shutdown();
} }
// unittest
// {
// EngineSettings customSettings = {holdOffMode: HoldOffMode.YIELD};
// Engine engine = new Engine(customSettings);
// engine.start();
// /**
// * Let the event engine know what typeIDs are
// * allowed to be queued
// */
// engine.addQueue(1);
// engine.addQueue(2);
// /**
// * Create a new Signal Handler that will handles
// * event types `1` and `2` with the given `handler()`
// * function
// */
// class SignalHandler1 : Signal
// {
// this()
// {
// super([1, 2]);
// }
// public override void handler(Event e)
// {
// import std.stdio;
// writeln("Running event", e.id);
// }
// }
// /**
// * Tell the event engine that I want to register
// * the following handler for its queues `1` and `2`
// */
// Signal j = new SignalHandler1();
// engine.addSignalHandler(j);
// Event eTest = new Event(1);
// engine.push(eTest);
// eTest = new Event(2);
// engine.push(eTest);
// Thread.sleep(dur!("seconds")(2));
// engine.push(eTest);
// writeln("done with main thread code");
// /* TODO: Before shutting down, actually test it out (i.e. all events ran) */
// engine.shutdown();
// }
/** /**
* Engine * Engine
* *
@ -158,6 +217,13 @@ public final class Engine : Thread
* *
* @param e the Signal handler to add * @param e the Signal handler to add
*/ */
/**
* Attaches a new signal handler to the engine
*
* Params:
* e = the signal handler to add
*/
public void addSignalHandler(Signal e) public void addSignalHandler(Signal e)
{ {
/* Lock the signal-set */ /* Lock the signal-set */
@ -170,18 +236,19 @@ public final class Engine : Thread
handlerLock.unlock(); handlerLock.unlock();
} }
/** /**
* Event loop * The main event loop
*/ *
public void run() * This checks at a certain interval (see HoldOffMode) if
* there are any events in any of the queues, if so,
* the dispatcher for said event type is called
*/
private void run()
{ {
running = true; running = true;
while (running) while (running)
{ {
/* TODO: Implement me */
/** /**
* Lock the queue-set * Lock the queue-set
* *
@ -235,19 +302,19 @@ public final class Engine : Thread
} }
} }
/** /**
* Stops the event engine * Stops the engine
* *
* TODO: Examine edge cases where this might not work * TODO: Examine cases where this may not work
*/ * TODO: Should we perhaps kill all other things
* i.e. stopping running threads
*/
public void shutdown() public void shutdown()
{ {
/* TODO: Insert a lock here, that dispatch should adhere too as well */ /* TODO: Insert a lock here, that dispatch should adhere too as well */
/* Stop the loop */ /* Stop the loop */
running = false; running = false;
} }
/** /**
@ -380,10 +447,13 @@ public final class Engine : Thread
return matchedHandlers; return matchedHandlers;
} }
/** /**
* Checks if there is a Signal that handles the given * Checks if there is a signal handler that handles
* event ID * the given event id
*/ * Params:
* id = the event ID to check
* Returns:
*/
public bool isSignalExists(ulong id) public bool isSignalExists(ulong id)
{ {
return getSignalsForEvent(new Event(id)).length != 0; return getSignalsForEvent(new Event(id)).length != 0;
@ -474,4 +544,34 @@ public final class Engine : Thread
/* TODO: Implement me */ /* TODO: Implement me */
return null; return null;
} }
/**
* Checks if any of the queues in the event engine
* have any pending events in them waiting dispatch
*
* Returns: <code>true</code> if there are pending events,
* <code>false</code> otherwise
*/
public bool hasPendingEvents()
{
bool isPending = false;
/* Lock the queues */
queueLock.lock();
foreach (Queue queue; queues)
{
if (queue.hasEvents())
{
isPending = true;
break;
}
}
/* Unlock the queues */
queueLock.unlock();
return isPending;
}
} }