Unit test: Shutdown engine after testing

This commit is contained in:
Tristan B. Velloza Kildaire 2022-05-19 16:48:09 +02:00
parent 692660772a
commit c6082f3941
1 changed files with 114 additions and 45 deletions

View File

@ -10,13 +10,13 @@ import core.thread : Thread, dur, Duration;
import eventy.exceptions; 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 */
void runner(Event e) void runner(Event e)
{ {
import std.stdio; import std.stdio;
writeln("Running event", e.id); writeln("Running event", e.id);
} }
@ -25,7 +25,6 @@ unittest
Engine engine = new Engine(); Engine engine = new Engine();
engine.start(); engine.start();
/** /**
* Let the event engine know what typeIDs are * Let the event engine know what typeIDs are
* allowed to be queued * allowed to be queued
@ -33,7 +32,6 @@ unittest
engine.addQueue(1); engine.addQueue(1);
engine.addQueue(2); engine.addQueue(2);
/** /**
* Create a new Signal Handler that will handles * Create a new Signal Handler that will handles
* event types `1` and `2` with the given `handler()` * event types `1` and `2` with the given `handler()`
@ -41,16 +39,17 @@ unittest
*/ */
class SignalHandler1 : Signal class SignalHandler1 : Signal
{ {
this() this()
{ {
super([1,2]); super([1, 2]);
} }
public override void handler(Event e) public override void handler(Event e)
{ {
import std.stdio; import std.stdio;
writeln("Running event", e.id);
} writeln("Running event", e.id);
}
} }
/** /**
@ -65,12 +64,14 @@ unittest
eTest = new Event(2); eTest = new Event(2);
engine.push(eTest); engine.push(eTest);
Thread.sleep(dur!("seconds")(2)); Thread.sleep(dur!("seconds")(2));
engine.push(eTest); engine.push(eTest);
writeln("naai"); writeln("naai");
/* TODO: Before shutting down, actually test it out (i.e. all events ran) */
engine.shutdown();
} }
/** /**
@ -97,11 +98,15 @@ public final class Engine : Thread
private bool running; private bool running;
private DList!(DispatchWrapper) threadStore;
private Mutex threadStoreLock;
this() this()
{ {
super(&run); super(&run);
queueLock = new Mutex(); queueLock = new Mutex();
handlerLock = new Mutex(); handlerLock = new Mutex();
threadStoreLock = new Mutex();
} }
/** /**
@ -131,7 +136,7 @@ public final class Engine : Thread
*/ */
public void addSignalHandler(Signal e) public void addSignalHandler(Signal e)
{ {
/* Lock the signal-set */ /* Lock the signal-set */
handlerLock.lock(); handlerLock.lock();
/* Add the new handler */ /* Add the new handler */
@ -148,7 +153,7 @@ public final class Engine : Thread
{ {
running = true; running = true;
while(running) while (running)
{ {
/* TODO: Implement me */ /* TODO: Implement me */
@ -163,28 +168,27 @@ public final class Engine : Thread
* Don't waste time spinning on mutex, * Don't waste time spinning on mutex,
* if it is not lockable then yield * if it is not lockable then yield
*/ */
while(!queueLock.tryLock_nothrow()) while (!queueLock.tryLock_nothrow())
{ {
yield(); yield();
} }
foreach (Queue queue; queues)
foreach(Queue queue; queues) {
{
/* If the queue has evenets queued */ /* If the queue has evenets queued */
if(queue.hasEvents()) if (queue.hasEvents())
{ {
/* TODO: Add different dequeuing techniques */ /* TODO: Add different dequeuing techniques */
/* Pop the first Event */ /* Pop the first Event */
Event headEvent = queue.popEvent(); Event headEvent = queue.popEvent();
/* Get all signal-handlers for this event type */ /* Get all signal-handlers for this event type */
Signal[] handlersMatched = getSignalsForEvent(headEvent); Signal[] handlersMatched = getSignalsForEvent(headEvent);
/* Dispatch the signal handlers */ /* Dispatch the signal handlers */
dispatch(handlersMatched, headEvent); dispatch(handlersMatched, headEvent);
} }
} }
@ -209,7 +213,12 @@ public final class Engine : Thread
*/ */
public void shutdown() public void shutdown()
{ {
/* TODO: Insert a lock here, that dispatch should adhere too as well */
/* Stop the loop */
running = false; running = false;
} }
/** /**
@ -221,33 +230,94 @@ public final class Engine : Thread
*/ */
private void dispatch(Signal[] signalSet, Event e) private void dispatch(Signal[] signalSet, Event e)
{ {
foreach(Signal signal; signalSet) foreach (Signal signal; signalSet)
{ {
/* Create a new Thread */ /* Create a new Thread */
Thread handlerThread = getThread(signal, e); // Thread handlerThread = getThread(signal, e);
DispatchWrapper handlerThread = new DispatchWrapper(signal, e);
/**
* TODO
*
* When we call `shutdown()` there may very well be a case of
* where the threadStoreLock unlocks after the clean up
* loop, but storeThread hangs here during that time,
* then proceeds to start the thread, we should therefore,
* either block on running changed (solution 1, not as granular)
*
* Solution 2: Block on dispatch being called <- use this method rather
* But still needs a running check, it must not go ahead if running is now
* false
*/
/* Store the thread */
storeThread(handlerThread);
/* Start the thread */ /* Start the thread */
handlerThread.start(); handlerThread.start();
} }
} }
private Thread getThread(Signal signal, Event e) /**
* Store the thread
*
* TODO: This can only be implemented if we use
* wrapper threads that exit, and we can signal
* removal from thread store then
*/
private void storeThread(DispatchWrapper t)
{ {
Thread signalHandlerThread = new class Thread /* Lock the thread store from editing */
threadStoreLock.lock();
/* Add the thread */
threadStore ~= t;
/* Unlock the thread store for editing */
threadStoreLock.unlock();
}
/**
* Removes a thread from the thread store
*/
private void removeThread(DispatchWrapper t)
{
/* Lock the thread store from editing */
threadStoreLock.lock();
/* Remove the thread */
threadStore.linearRemoveElement(t);
/* Unlock the thread store for editing */
threadStoreLock.unlock();
}
/**
* DispatchWrapper
*
* Effectively a thread but with the Signal,
* Event included with clean-up routines
*/
private class DispatchWrapper : Thread
{
private Signal signal;
private Event e;
this(Signal signal, Event e)
{ {
this() super(&run);
{ this.signal = signal;
super(&worker); this.e = e;
} }
public void worker() private void run()
{ {
signal.handler(e); /* Run the signal handler */
//handler(e); signal.handler(e);
}
};
return signalHandlerThread; /* Remove myself from the thread store */
removeThread(this);
}
} }
/** /**
@ -267,9 +337,9 @@ public final class Engine : Thread
handlerLock.lock(); handlerLock.lock();
/* Find all handlers matching */ /* Find all handlers matching */
foreach(Signal signal; handlers) foreach (Signal signal; handlers)
{ {
if(signal.handles(e.id)) if (signal.handles(e.id))
{ {
matchedHandlers ~= signal; matchedHandlers ~= signal;
} }
@ -291,7 +361,7 @@ public final class Engine : Thread
{ {
Queue matchedQueue = findQueue(e.id); Queue matchedQueue = findQueue(e.id);
if(matchedQueue) if (matchedQueue)
{ {
/* Append to the queue */ /* Append to the queue */
matchedQueue.add(e); matchedQueue.add(e);
@ -315,7 +385,7 @@ public final class Engine : Thread
queueLock.lock(); queueLock.lock();
/* If no such queue exists then add it (recursive mutex used) */ /* If no such queue exists then add it (recursive mutex used) */
if(!findQueue(id)) if (!findQueue(id))
{ {
/* Add the queue */ /* Add the queue */
queues ~= newQueue; queues ~= newQueue;
@ -324,7 +394,6 @@ public final class Engine : Thread
{ {
throw new EventyException("Failure to add queue with ID already in use"); throw new EventyException("Failure to add queue with ID already in use");
} }
/* Unlock the queue collection */ /* Unlock the queue collection */
queueLock.unlock(); queueLock.unlock();
@ -346,9 +415,9 @@ public final class Engine : Thread
/* Find the matching queue */ /* Find the matching queue */
Queue matchedQueue; Queue matchedQueue;
foreach(Queue queue; queues) foreach (Queue queue; queues)
{ {
if(queue.id == id) if (queue.id == id)
{ {
matchedQueue = queue; matchedQueue = queue;
break; break;
@ -367,4 +436,4 @@ public final class Engine : Thread
/* TODO: Implement me */ /* TODO: Implement me */
return null; return null;
} }
} }