Permalink
Browse files

Enhanced DomainEventsTest to exercise mutli-threaded EventMonitoring.

  • Loading branch information...
1 parent 1ce0de6 commit 322e86d137b19679700b188cb5050e72d95c1776 @tfredrich committed Feb 7, 2012
@@ -149,6 +149,16 @@ public static void register(EventHandler handler)
instance().registerHandler(handler);
}
+ /**
+ * Remove an EventHandler from receiving notification when DomainEvent are raised.
+ *
+ * @param handler the handler to remove from receiving notifications.
+ */
+ public static void unregister(EventHandler handler)
+ {
+ instance().unregisterHandler(handler);
+ }
+
// SECTION: INSTANCE METHODS
@@ -203,13 +213,29 @@ private void setRetryOnError(boolean value)
*
* @param handler
*/
- private void registerHandler(EventHandler handler)
+ public void registerHandler(EventHandler handler)
{
for (EventMonitor eventMonitor : eventMonitors)
{
eventMonitor.register(handler);
}
}
+
+ /**
+ * Register an EventHandler for notification when DomainEvent are raised.
+ * <p/>
+ * Register all handlers *before* raising events as handlers get cached by which type(s) of events
+ * they handle upon raise.
+ *
+ * @param handler
+ */
+ public void unregisterHandler(EventHandler handler)
+ {
+ for (EventMonitor eventMonitor : eventMonitors)
+ {
+ eventMonitor.unregister(handler);
+ }
+ }
private int getEventWorkerCount()
{
@@ -73,6 +73,14 @@ public void register(EventHandler handler)
handlersByEvent.clear();
}
+ public void unregister(EventHandler handler)
+ {
+ if (handlers.remove(handler))
+ {
+ handlersByEvent.clear();
+ }
+ }
+
public synchronized void shutdown()
{
shouldShutDown = true;
@@ -116,7 +124,7 @@ public void run()
while ((event = eventQueue.poll()) != null)
{
-// System.out.println("Processing event: " + event.toString());
+ System.out.println("Processing event: " + event.toString());
for (EventHandler handler : getConsumersFor(event.getClass()))
{
try
@@ -129,7 +137,7 @@ public void run()
if (shouldReRaiseOnError)
{
-// System.out.println("Event handler failed. Re-publishing event: " + event.toString());
+ System.out.println("Event handler failed. Re-publishing event: " + event.toString());
eventQueue.raise(event);
}
}
@@ -138,6 +146,8 @@ public void run()
}
System.out.println("Event monitor exiting...");
+ handlers.clear();
+ handlersByEvent.clear();
}
@@ -6,6 +6,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -27,7 +28,7 @@
@BeforeClass
public static void startup()
{
- DomainEvents.setEventMonitorCount(3);
+ DomainEvents.setEventMonitorCount(5);
DomainEvents.startMonitoring();
}
@@ -44,6 +45,14 @@ public void setup()
DomainEvents.register(ignoredHandler);
DomainEvents.register(longHandler);
}
+
+ @After
+ public void teardown()
+ {
+ DomainEvents.unregister(handler);
+ DomainEvents.unregister(ignoredHandler);
+ DomainEvents.unregister(longHandler);
+ }
@Test
public void isSingleton()
@@ -131,10 +140,13 @@ public void shouldProcessInParallel()
DomainEvents.raise(new LongEvent());
DomainEvents.raise(new LongEvent());
DomainEvents.raise(new LongEvent());
- Thread.sleep(1500);
+ DomainEvents.raise(new LongEvent());
+ DomainEvents.raise(new LongEvent());
+ Thread.sleep(100);
assertEquals(0, handler.getCallCount());
assertEquals(0, ignoredHandler.getCallCount());
- assertEquals(3, longHandler.getCallCount());
+ System.out.println("longHandler instance=" + longHandler.toString());
+ assertEquals(5, longHandler.getCallCount());
}
@@ -174,7 +186,7 @@ public void kerBlooey()
{
}
- private class DomainEventsTestHandler
+ private static class DomainEventsTestHandler
implements EventHandler
{
private int callCount = 0;
@@ -205,7 +217,7 @@ public boolean handles(Class<? extends DomainEvent> eventClass)
}
}
- private class DomainEventsTestIgnoredEventsHandler
+ private static class DomainEventsTestIgnoredEventsHandler
implements EventHandler
{
private int callCount = 0;
@@ -234,7 +246,7 @@ public boolean handles(Class<? extends DomainEvent> eventClass)
}
}
- private class DomainEventsTestLongEventHandler
+ private static class DomainEventsTestLongEventHandler
implements EventHandler
{
private int callCount = 0;
@@ -246,7 +258,9 @@ public void handle(DomainEvent event)
++callCount;
try
{
- Thread.sleep(200);
+ // pretend the long event takes 1 second to process...
+ System.out.println("Event handler " + this.toString() + " going to sleep..." + callCount);
+ Thread.sleep(1000);
}
catch (InterruptedException e)
{

0 comments on commit 322e86d

Please sign in to comment.