diff --git a/quasar-actors/src/main/java/co/paralleluniverse/actors/behaviors/EventHandler.java b/quasar-actors/src/main/java/co/paralleluniverse/actors/behaviors/EventHandler.java index 8fdeb1ea65..23d1b446cf 100644 --- a/quasar-actors/src/main/java/co/paralleluniverse/actors/behaviors/EventHandler.java +++ b/quasar-actors/src/main/java/co/paralleluniverse/actors/behaviors/EventHandler.java @@ -1,6 +1,6 @@ /* * Quasar: lightweight threads and actors for the JVM. - * Copyright (c) 2013-2014, Parallel Universe Software Co. All rights reserved. + * Copyright (c) 2013-2016, Parallel Universe Software Co. All rights reserved. * * This program and the accompanying materials are dual-licensed under * either the terms of the Eclipse Public License v1.0 as published by @@ -13,6 +13,8 @@ */ package co.paralleluniverse.actors.behaviors; +import co.paralleluniverse.fibers.SuspendExecution; + /** * A handler that can be registered with an {@link EventSource} actor to receive all events {@link EventSource#notify(java.lang.Object) sent} * to the actor. @@ -20,5 +22,5 @@ * @author pron */ public interface EventHandler { - void handleEvent(Event event); + void handleEvent(Event event) throws SuspendExecution, InterruptedException; } diff --git a/quasar-actors/src/main/java/co/paralleluniverse/actors/behaviors/EventSourceActor.java b/quasar-actors/src/main/java/co/paralleluniverse/actors/behaviors/EventSourceActor.java index 50c1fe27f4..e09ea5881b 100644 --- a/quasar-actors/src/main/java/co/paralleluniverse/actors/behaviors/EventSourceActor.java +++ b/quasar-actors/src/main/java/co/paralleluniverse/actors/behaviors/EventSourceActor.java @@ -231,7 +231,7 @@ protected void onTerminate(Throwable cause) throws SuspendExecution, Interrupted handlers.clear(); } - private void notifyHandlers(Event event) { + private void notifyHandlers(Event event) throws InterruptedException, SuspendExecution { log().debug("{} Got event {}", this, event); for (ListIterator> it = handlers.listIterator(); it.hasNext();) { EventHandler handler = it.next(); diff --git a/quasar-actors/src/test/java/co/paralleluniverse/actors/behaviors/EventSourceTest.java b/quasar-actors/src/test/java/co/paralleluniverse/actors/behaviors/EventSourceTest.java index 1ad41d2c29..39cb244c42 100644 --- a/quasar-actors/src/test/java/co/paralleluniverse/actors/behaviors/EventSourceTest.java +++ b/quasar-actors/src/test/java/co/paralleluniverse/actors/behaviors/EventSourceTest.java @@ -1,6 +1,6 @@ /* * Quasar: lightweight threads and actors for the JVM. - * Copyright (c) 2013-2015, Parallel Universe Software Co. All rights reserved. + * Copyright (c) 2013-2016, Parallel Universe Software Co. All rights reserved. * * This program and the accompanying materials are dual-licensed under * either the terms of the Eclipse Public License v1.0 as published by @@ -38,14 +38,14 @@ * * @author pron */ -public class EventSourceTest { +public final class EventSourceTest { @Rule - public TestName name = new TestName(); + public final TestName name = new TestName(); @Rule - public TestRule watchman = TestUtil.WATCHMAN; + public final TestRule watchman = TestUtil.WATCHMAN; @After - public void tearDown() { + public final void tearDown() { ActorRegistry.clear(); } @@ -59,7 +59,7 @@ private EventSource spawnEventSource(Initializer initializer) { } private , Message, V> T spawnActor(T actor) { - Fiber fiber = new Fiber(actor); + final Fiber fiber = new Fiber<>(actor); fiber.setUncaughtExceptionHandler(new Strand.UncaughtExceptionHandler() { @Override public void uncaughtException(Strand s, Throwable e) { @@ -72,9 +72,9 @@ public void uncaughtException(Strand s, Throwable e) { } @Test - public void testInitializationAndTermination() throws Exception { + public final void testInitializationAndTermination() throws Exception { final Initializer init = mock(Initializer.class); - EventSource es = spawnEventSource(init); + final EventSource es = spawnEventSource(init); Thread.sleep(100); verify(init).init(); @@ -86,7 +86,7 @@ public void testInitializationAndTermination() throws Exception { } @Test - public void testNotify() throws Exception { + public final void testNotify() throws Exception { final EventHandler handler1 = mock(EventHandler.class); final EventHandler handler2 = mock(EventHandler.class); @@ -113,9 +113,34 @@ public void testNotify() throws Exception { verify(handler2).handleEvent("goodbye"); } + private static final class BlockingStringEventHandler implements EventHandler { + @Override + public final void handleEvent(String s) throws SuspendExecution, InterruptedException { + Fiber.sleep(10); + } + } + + @Test + public final void testBlock() throws Exception { + final EventHandler handler1 = new BlockingStringEventHandler(); + final EventHandler handler2 = new BlockingStringEventHandler(); + + final EventSource es = spawnEventSource(null); + + es.addHandler(handler1); + es.addHandler(handler2); + + es.notify("hello"); + + Thread.sleep(100); + + es.shutdown(); + LocalActor.join(es, 100, TimeUnit.MILLISECONDS); + } + @Ignore @Test - public void testExceptionThrownInHandler() throws Exception { + public final void testExceptionThrownInHandler() throws Exception { final Initializer init = mock(Initializer.class); final EventHandler handler1 = mock(EventHandler.class); final EventHandler handler2 = mock(EventHandler.class); @@ -139,10 +164,10 @@ public void testExceptionThrownInHandler() throws Exception { } @Test - public void testRegistration() throws Exception { - EventSource es = new EventSourceActor() { + public final void testRegistration() throws Exception { + final EventSource es = new EventSourceActor() { @Override - protected void init() throws SuspendExecution, InterruptedException { + protected final void init() throws SuspendExecution, InterruptedException { // Strand.sleep(1000); register("test1"); }