From c0c9487451848665c9ede5ae53f5c723f61d317d Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 5 Dec 2011 18:52:32 +0100 Subject: [PATCH] make testActor spew out uncollected messages after test end MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (so you don’t need expectNoMsg after tests) --- .../src/main/scala/akka/testkit/TestKit.scala | 5 +++ .../test/scala/akka/testkit/AkkaSpec.scala | 37 ++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/akka-testkit/src/main/scala/akka/testkit/TestKit.scala b/akka-testkit/src/main/scala/akka/testkit/TestKit.scala index c0476a74cc..b709243775 100644 --- a/akka-testkit/src/main/scala/akka/testkit/TestKit.scala +++ b/akka-testkit/src/main/scala/akka/testkit/TestKit.scala @@ -39,6 +39,11 @@ class TestActor(queue: BlockingDeque[TestActor.Message]) extends Actor { val observe = ignore map (ignoreFunc ⇒ if (ignoreFunc isDefinedAt x) !ignoreFunc(x) else true) getOrElse true if (observe) queue.offerLast(RealMessage(x, sender)) } + + override def postStop() = { + import scala.collection.JavaConverters._ + queue.asScala foreach { m ⇒ system.deadLetters ! DeadLetter(m.msg, m.sender, self) } + } } /** diff --git a/akka-testkit/src/test/scala/akka/testkit/AkkaSpec.scala b/akka-testkit/src/test/scala/akka/testkit/AkkaSpec.scala index be5320ee13..ed8b38ce10 100644 --- a/akka-testkit/src/test/scala/akka/testkit/AkkaSpec.scala +++ b/akka-testkit/src/test/scala/akka/testkit/AkkaSpec.scala @@ -14,6 +14,9 @@ import akka.dispatch.FutureTimeoutException import com.typesafe.config.Config import com.typesafe.config.ConfigFactory import akka.actor.PoisonPill +import java.util.concurrent.LinkedBlockingQueue +import akka.actor.CreateChild +import akka.actor.DeadLetter object TimingTest extends Tag("timing") @@ -94,11 +97,12 @@ class AkkaSpecSpec extends WordSpec with MustMatchers { "An AkkaSpec" must { "terminate all actors" in { + // verbose config just for demonstration purposes, please leave in in case of debugging import scala.collection.JavaConverters._ val conf = Map( "akka.actor.debug.lifecycle" -> true, "akka.actor.debug.event-stream" -> true, "akka.loglevel" -> "DEBUG", "akka.stdout-loglevel" -> "DEBUG") - val system = ActorSystem("test", ConfigFactory.parseMap(conf.asJava).withFallback(AkkaSpec.testConf)) + val system = ActorSystem("AkkaSpec1", ConfigFactory.parseMap(conf.asJava).withFallback(AkkaSpec.testConf)) val spec = new AkkaSpec(system) { val ref = Seq(testActor, system.actorOf(Props.empty, "name")) } @@ -108,11 +112,7 @@ class AkkaSpecSpec extends WordSpec with MustMatchers { } "must stop correctly when sending PoisonPill to rootGuardian" in { - import scala.collection.JavaConverters._ - val conf = Map( - "akka.actor.debug.lifecycle" -> true, "akka.actor.debug.event-stream" -> true, - "akka.loglevel" -> "DEBUG", "akka.stdout-loglevel" -> "DEBUG") - val system = ActorSystem("test", ConfigFactory.parseMap(conf.asJava).withFallback(AkkaSpec.testConf)) + val system = ActorSystem("AkkaSpec2", AkkaSpec.testConf) val spec = new AkkaSpec(system) {} val latch = new TestLatch(1)(system) system.registerOnTermination(latch.countDown()) @@ -122,6 +122,31 @@ class AkkaSpecSpec extends WordSpec with MustMatchers { latch.await(2 seconds) } + "must enqueue unread messages from testActor to deadLetters" in { + val system = ActorSystem("AkkaSpec2", AkkaSpec.testConf) + + var locker = Seq.empty[DeadLetter] + implicit val timeout = system.settings.ActorTimeout + implicit val davieJones = (system.actorFor("/") ? CreateChild(Props(new Actor { + def receive = { + case m: DeadLetter ⇒ locker :+= m + } + }), "davieJones")).as[ActorRef].get + + system.eventStream.subscribe(davieJones, classOf[DeadLetter]) + + val probe = new TestProbe(system) + probe.ref ! 42 + + val latch = new TestLatch(1)(system) + system.registerOnTermination(latch.countDown()) + system.stop() + latch.await(2 seconds) + + // this will typically also contain log messages which were sent after the logger shutdown + locker must contain(DeadLetter(42, davieJones, probe.ref)) + } + } }