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 std.stdio;
/* TODO: Move elsewhere, this thing thinks it's a delegate in the unit test, idk why */
void runner(Event e)
{
import std.stdio;
writeln("Running event", e.id);
}
@ -25,7 +25,6 @@ unittest
Engine engine = new Engine();
engine.start();
/**
* Let the event engine know what typeIDs are
* allowed to be queued
@ -33,7 +32,6 @@ unittest
engine.addQueue(1);
engine.addQueue(2);
/**
* Create a new Signal Handler that will handles
* event types `1` and `2` with the given `handler()`
@ -49,6 +47,7 @@ unittest
public override void handler(Event e)
{
import std.stdio;
writeln("Running event", e.id);
}
}
@ -66,11 +65,13 @@ unittest
eTest = new Event(2);
engine.push(eTest);
Thread.sleep(dur!("seconds")(2));
engine.push(eTest);
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 DList!(DispatchWrapper) threadStore;
private Mutex threadStoreLock;
this()
{
super(&run);
queueLock = new Mutex();
handlerLock = new Mutex();
threadStoreLock = new Mutex();
}
/**
@ -168,7 +173,6 @@ public final class Engine : Thread
yield();
}
foreach (Queue queue; queues)
{
/* If the queue has evenets queued */
@ -209,7 +213,12 @@ public final class Engine : Thread
*/
public void shutdown()
{
/* TODO: Insert a lock here, that dispatch should adhere too as well */
/* Stop the loop */
running = false;
}
/**
@ -224,30 +233,91 @@ public final class Engine : Thread
foreach (Signal signal; signalSet)
{
/* 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 */
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
{
this()
{
super(&worker);
/* Lock the thread store from editing */
threadStoreLock.lock();
/* Add the thread */
threadStore ~= t;
/* Unlock the thread store for editing */
threadStoreLock.unlock();
}
public void worker()
/**
* 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)
{
super(&run);
this.signal = signal;
this.e = e;
}
private void run()
{
/* Run the signal handler */
signal.handler(e);
//handler(e);
}
};
return signalHandlerThread;
/* Remove myself from the thread store */
removeThread(this);
}
}
/**
@ -325,7 +395,6 @@ public final class Engine : Thread
throw new EventyException("Failure to add queue with ID already in use");
}
/* Unlock the queue collection */
queueLock.unlock();
}