From 4ac6a4e2feaf5ec3e08cd0be73a8085639f24a49 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Wed, 11 Aug 2010 12:27:15 +0200 Subject: [PATCH] Ported ReceiveTimeoutSpec to UntypedActor --- .../UntypedActorReceiveTimeoutSpec.scala | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 akka-core/src/test/scala/actor/untyped-actor/UntypedActorReceiveTimeoutSpec.scala diff --git a/akka-core/src/test/scala/actor/untyped-actor/UntypedActorReceiveTimeoutSpec.scala b/akka-core/src/test/scala/actor/untyped-actor/UntypedActorReceiveTimeoutSpec.scala new file mode 100644 index 0000000000..6ed8184514 --- /dev/null +++ b/akka-core/src/test/scala/actor/untyped-actor/UntypedActorReceiveTimeoutSpec.scala @@ -0,0 +1,59 @@ +package se.scalablesolutions.akka.actor + +import org.scalatest.junit.JUnitSuite +import org.junit.Test + +import java.util.concurrent.TimeUnit +import org.multiverse.api.latches.StandardLatch +import UntypedActor._ + +object UntypedActorReceiveTimeoutSpec { + object Tick + class TestReceiveTimeoutActor extends UntypedActor { + val latch = new StandardLatch + + def onReceive(message: Any):Unit = message match { + case ReceiveTimeout => latch.open + case Tick => + } + } + + class FiveOhOhTestReceiveTimeoutActor extends TestReceiveTimeoutActor { + getContext.setReceiveTimeout(500L) + } +} + +class UntypedActorReceiveTimeoutSpec extends JUnitSuite { + import UntypedActorReceiveTimeoutSpec._ + + @Test def receiveShouldGetTimeout = { + val timeoutActor = actorOf(classOf[FiveOhOhTestReceiveTimeoutActor]).start + + assert(timeoutActor.actorRef.actor.asInstanceOf[TestReceiveTimeoutActor].latch.tryAwait(3, TimeUnit.SECONDS)) + } + + @Test def swappedReceiveShouldAlsoGetTimout = { + val timeoutActor = actorOf(classOf[FiveOhOhTestReceiveTimeoutActor]).start + + assert(timeoutActor.actorRef.actor.asInstanceOf[TestReceiveTimeoutActor].latch.tryAwait(3, TimeUnit.SECONDS)) + + val swappedLatch = new StandardLatch + timeoutActor sendOneWay HotSwap(Some{ + case ReceiveTimeout => swappedLatch.open + }) + + assert(swappedLatch.tryAwait(3, TimeUnit.SECONDS)) + } + + @Test def timeoutShouldBeCancelledAfterRegularReceive = { + val timeoutActor = actorOf(classOf[FiveOhOhTestReceiveTimeoutActor]).start + timeoutActor sendOneWay Tick + assert(timeoutActor.actorRef.actor.asInstanceOf[TestReceiveTimeoutActor].latch.tryAwait(1, TimeUnit.SECONDS) == false) + } + + @Test def timeoutShouldNotBeSentWhenNotSpecified = { + val timeoutLatch = new StandardLatch + val timeoutActor = actorOf(classOf[TestReceiveTimeoutActor]).start + assert(timeoutLatch.tryAwait(2, TimeUnit.SECONDS) == false) + } +}