diff --git a/akka-actor-tests/src/test/scala/akka/actor/actor/ActorRefSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/actor/ActorRefSpec.scala index 83c3dc06f8..c5f93f831f 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/actor/ActorRefSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/actor/ActorRefSpec.scala @@ -326,7 +326,7 @@ class ActorRefSpec extends WordSpec with MustMatchers { } "restart when Kill:ed" in { - filterEvents(EventFilter[ActorKilledException]) { + filterException[ActorKilledException] { val latch = new CountDownLatch(2) val boss = Actor.actorOf(new Actor { diff --git a/akka-actor-tests/src/test/scala/akka/actor/actor/FSMActorSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/actor/FSMActorSpec.scala index aea9fe5743..d93e652937 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/actor/FSMActorSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/actor/FSMActorSpec.scala @@ -59,8 +59,8 @@ object FSMActorSpec { whenUnhandled { case Ev(msg) ⇒ { - unhandledLatch.open EventHandler.info(this, "unhandled event " + msg + " in state " + stateName + " with data " + stateData) + unhandledLatch.open stay } } @@ -112,7 +112,11 @@ class FSMActorSpec extends WordSpec with MustMatchers with TestKit with BeforeAn } override def beforeAll { - EventHandler notify Mute(EventFilter[EventHandler.EventHandlerException]("Next state 2 does not exist")) + EventHandler notify Mute(EventFilter[EventHandler.EventHandlerException]("Next state 2 does not exist"), + EventFilter.custom { + case _: EventHandler.Debug ⇒ true + case _ ⇒ false + }) val f = FSM.getClass.getDeclaredField("debugEvent") f.setAccessible(true) f.setBoolean(FSM, true) @@ -153,8 +157,13 @@ class FSMActorSpec extends WordSpec with MustMatchers with TestKit with BeforeAn transitionCallBackLatch.await lockedLatch.await - lock ! "not_handled" - unhandledLatch.await + filterEvents(EventFilter.custom { + case EventHandler.Info(_: Lock, _) ⇒ true + case _ ⇒ false + }) { + lock ! "not_handled" + unhandledLatch.await + } val answerLatch = TestLatch() object Hello diff --git a/akka-actor-tests/src/test/scala/akka/actor/actor/FSMTransitionSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/actor/FSMTransitionSpec.scala index c552345192..2c825f9063 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/actor/FSMTransitionSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/actor/FSMTransitionSpec.scala @@ -10,6 +10,7 @@ import akka.testkit._ import akka.testkit._ import akka.util.duration._ import akka.config.Supervision._ +import akka.event.EventHandler import FSM._ @@ -79,9 +80,14 @@ class FSMTransitionSpec extends WordSpec with MustMatchers with TestKit { val sup = Actor.actorOf[Supervisor].start() sup link fsm within(300 millis) { - fsm ! SubscribeTransitionCallBack(forward) - fsm ! "reply" - expectMsg("reply") + filterEvents(EventFilter.custom { + case EventHandler.Warning(_: MyFSM, _) ⇒ true + case _ ⇒ false + }) { + fsm ! SubscribeTransitionCallBack(forward) + fsm ! "reply" + expectMsg("reply") + } forward.start() fsm ! SubscribeTransitionCallBack(forward) expectMsg(CurrentState(fsm, 0)) diff --git a/akka-actor-tests/src/test/scala/akka/actor/actor/LoggingReceiveSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/actor/LoggingReceiveSpec.scala index 81d6d2e41d..b21584dfc0 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/actor/LoggingReceiveSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/actor/LoggingReceiveSpec.scala @@ -23,7 +23,10 @@ class LoggingReceiveSpec override def beforeAll { EventHandler.notify(TestEvent.Mute(EventFilter[UnhandledMessageException], - EventFilter[ActorKilledException])) + EventFilter[ActorKilledException], EventFilter.custom { + case d: EventHandler.Debug ⇒ true + case _ ⇒ false + })) EventHandler.addListener(testActor) EventHandler.level = EventHandler.DebugLevel } diff --git a/akka-actor-tests/src/test/scala/akka/actor/supervisor/SupervisorTreeSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/supervisor/SupervisorTreeSpec.scala index 8de9c69e35..565a11e375 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/supervisor/SupervisorTreeSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/supervisor/SupervisorTreeSpec.scala @@ -34,7 +34,7 @@ class SupervisorTreeSpec extends WordSpec with MustMatchers { "In a 3 levels deep supervisor tree (linked in the constructor) we" must { "be able to kill the middle actor and see itself and its child restarted" in { - filterEvents(EventFilter[Exception]) { + filterException[Exception] { log = "INIT" val lastActor = actorOf(new Chainer, "lastActor").start diff --git a/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala b/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala index 0b1d7d0a7f..09c2a3e776 100644 --- a/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala @@ -98,7 +98,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "has actions applied" must { "pass checks" in { - filterEvents(EventFilter[ArithmeticException]) { + filterException[ArithmeticException] { check({ (future: Future[Int], actions: List[FutureAction]) ⇒ val result = (future /: actions)(_ /: _) val expected = (future.await.value.get /: actions)(_ /: _) @@ -128,7 +128,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "throws an exception" must { behave like futureWithException[RuntimeException] { test ⇒ - filterEvents(EventFilter[RuntimeException]) { + filterException[RuntimeException] { val actor = actorOf[TestActor] actor.start() val future = actor ? "Failure" @@ -154,7 +154,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "will throw an exception" must { behave like futureWithException[ArithmeticException] { test ⇒ - filterEvents(EventFilter[ArithmeticException]) { + filterException[ArithmeticException] { val actor1 = actorOf[TestActor].start() val actor2 = actorOf(new Actor { def receive = { case s: String ⇒ self reply (s.length / 0) } }).start() val future = actor1 ? "Hello" flatMap { _ match { case s: String ⇒ actor2 ? s } } @@ -170,7 +170,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd "being tested" must { "compose with for-comprehensions" in { - filterEvents(EventFilter[ClassCastException]) { + filterException[ClassCastException] { val actor = actorOf(new Actor { def receive = { case s: String ⇒ self reply s.length @@ -199,7 +199,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "support pattern matching within a for-comprehension" in { - filterEvents(EventFilter[MatchError]) { + filterException[MatchError] { case class Req[T](req: T) case class Res[T](res: T) val actor = actorOf(new Actor { @@ -228,7 +228,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "recover from exceptions" in { - filterEvents(EventFilter[RuntimeException]) { + filterException[RuntimeException] { val future1 = Future(5) val future2 = future1 map (_ / 0) val future3 = future2 map (_.toString) @@ -296,7 +296,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "fold with an exception" in { - filterEvents(EventFilter[IllegalArgumentException]) { + filterException[IllegalArgumentException] { val actors = (1 to 10).toList map { _ ⇒ actorOf(new Actor { def receive = { @@ -345,7 +345,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "shouldReduceResultsWithException" in { - filterEvents(EventFilter[IllegalArgumentException]) { + filterException[IllegalArgumentException] { val actors = (1 to 10).toList map { _ ⇒ actorOf(new Actor { def receive = { @@ -363,7 +363,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "shouldReduceThrowIAEOnEmptyInput" in { - filterEvents(EventFilter[IllegalArgumentException]) { + filterException[IllegalArgumentException] { intercept[UnsupportedOperationException] { Futures.reduce(List[Future[Int]]())(_ + _).get } } } @@ -397,7 +397,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd "shouldHandleThrowables" in { class ThrowableTest(m: String) extends Throwable(m) - filterEvents(EventFilter[ThrowableTest]) { + filterException[ThrowableTest] { val f1 = Future { throw new ThrowableTest("test") } f1.await intercept[ThrowableTest] { f1.resultOrException } @@ -431,7 +431,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd assert(f2.get === 10) val f3 = Future({ Thread.sleep(10); 5 }, 10) - filterEvents(EventFilter[FutureTimeoutException]) { + filterException[FutureTimeoutException] { intercept[FutureTimeoutException] { f3.get } @@ -454,7 +454,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "futureComposingWithContinuationsFailureDivideZero" in { - filterEvents(EventFilter[ArithmeticException]) { + filterException[ArithmeticException] { import Future.flow val x = Future("Hello") @@ -467,7 +467,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "futureComposingWithContinuationsFailureCastInt" in { - filterEvents(EventFilter[ClassCastException]) { + filterException[ClassCastException] { import Future.flow val actor = actorOf[TestActor].start @@ -482,7 +482,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "futureComposingWithContinuationsFailureCastNothing" in { - filterEvents(EventFilter[ClassCastException]) { + filterException[ClassCastException] { import Future.flow val actor = actorOf[TestActor].start @@ -630,7 +630,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "futureCompletingWithContinuationsFailure" in { - filterEvents(EventFilter[ArithmeticException]) { + filterException[ArithmeticException] { import Future.flow val x, y, z = Promise[Int]() @@ -745,7 +745,7 @@ class FutureSpec extends WordSpec with MustMatchers with Checkers with BeforeAnd } "ticket812FutureDispatchCleanup" in { - filterEvents(EventFilter[FutureTimeoutException]) { + filterException[FutureTimeoutException] { implicit val dispatcher = new Dispatcher("ticket812FutureDispatchCleanup") assert(dispatcher.pendingFutures === 0) val future = Future({ Thread.sleep(100); "Done" }, 10) diff --git a/akka-actor-tests/src/test/scala/akka/routing/ActorPoolSpec.scala b/akka-actor-tests/src/test/scala/akka/routing/ActorPoolSpec.scala index 9b6cfac6aa..d040457572 100644 --- a/akka-actor-tests/src/test/scala/akka/routing/ActorPoolSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/routing/ActorPoolSpec.scala @@ -357,7 +357,7 @@ class ActorPoolSpec extends WordSpec with MustMatchers { } "provide default supervision of pooled actors" in { - filterEvents(EventFilter[RuntimeException]) { + filterException[RuntimeException] { import akka.config.Supervision._ val pingCount = new AtomicInteger(0) val deathCount = new AtomicInteger(0) diff --git a/akka-testkit/src/main/scala/akka/testkit/TestEventListener.scala b/akka-testkit/src/main/scala/akka/testkit/TestEventListener.scala index 9239b1103a..49481d723f 100644 --- a/akka-testkit/src/main/scala/akka/testkit/TestEventListener.scala +++ b/akka-testkit/src/main/scala/akka/testkit/TestEventListener.scala @@ -36,7 +36,7 @@ object EventFilter { def apply[A <: Throwable: Manifest](source: AnyRef, message: String): EventFilter = ErrorSourceMessageFilter(manifest[A].erasure, source, message) - def apply(test: (Event) ⇒ Boolean): EventFilter = + def custom(test: (Event) ⇒ Boolean): EventFilter = CustomEventFilter(test) } @@ -92,7 +92,7 @@ class TestEventListener extends EventHandler.DefaultListener { case event: Event if filter(event) ⇒ }: Receive) orElse super.receive - def filter(event: Event): Boolean = try { filters exists (_(event)) } catch { case e: Exception ⇒ false } + def filter(event: Event): Boolean = filters exists (f ⇒ try { f(event) } catch { case e: Exception ⇒ false }) def addFilter(filter: EventFilter): Unit = filters ::= filter diff --git a/akka-testkit/src/main/scala/akka/testkit/package.scala b/akka-testkit/src/main/scala/akka/testkit/package.scala index b729565ffb..c24c26539c 100644 --- a/akka-testkit/src/main/scala/akka/testkit/package.scala +++ b/akka-testkit/src/main/scala/akka/testkit/package.scala @@ -13,4 +13,6 @@ package object testkit { } def filterEvents[T](eventFilters: EventFilter*)(block: ⇒ T): T = filterEvents(eventFilters.toSeq)(block) + + def filterException[T <: Throwable: Manifest](block: ⇒ Unit): Unit = filterEvents(Seq(EventFilter[T]))(block) }