diff --git a/akka-core/src/test/scala/SchedulerSpec.scala b/akka-core/src/test/scala/SchedulerSpec.scala index 7db5727834..08be87d728 100644 --- a/akka-core/src/test/scala/SchedulerSpec.scala +++ b/akka-core/src/test/scala/SchedulerSpec.scala @@ -3,12 +3,20 @@ package se.scalablesolutions.akka.actor import org.scalatest.junit.JUnitSuite import Actor._ import java.util.concurrent.{CountDownLatch, TimeUnit} -import org.junit.Test import se.scalablesolutions.akka.config.ScalaConfig._ import org.multiverse.api.latches.StandardLatch +import org.junit.Test class SchedulerSpec extends JUnitSuite { - @Test def schedulerShouldScheduleMoreThanOnce = { + + def withCleanEndState(action: => Unit) { + action + Scheduler.restart + ActorRegistry.shutdownAll + } + + + @Test def schedulerShouldScheduleMoreThanOnce = withCleanEndState { case object Tick val countDownLatch = new CountDownLatch(3) @@ -22,7 +30,7 @@ class SchedulerSpec extends JUnitSuite { assert(countDownLatch.await(1, TimeUnit.SECONDS)) } - @Test def schedulerShouldScheduleOnce = { + @Test def schedulerShouldScheduleOnce = withCleanEndState { case object Tick val countDownLatch = new CountDownLatch(2) val tickActor = actor { @@ -40,46 +48,40 @@ class SchedulerSpec extends JUnitSuite { /** * ticket #307 */ - @Test def actorRestartShouldPickUpScheduleAgain = { + @Test def actorRestartShouldPickUpScheduleAgain = withCleanEndState { - try { - object Ping - object Crash + object Ping + object Crash - val restartLatch = new StandardLatch - val pingLatch = new CountDownLatch(4) + val restartLatch = new StandardLatch + val pingLatch = new CountDownLatch(6) - val actor = actorOf(new Actor { - self.lifeCycle = Some(LifeCycle(Permanent)) + val actor = actorOf(new Actor { + self.lifeCycle = Some(LifeCycle(Permanent)) - def receive = { - case Ping => pingLatch.countDown - case Crash => throw new Exception("CRASH") - } + def receive = { + case Ping => pingLatch.countDown + case Crash => throw new Exception("CRASH") + } - override def postRestart(reason: Throwable) = restartLatch.open - }) + override def postRestart(reason: Throwable) = restartLatch.open + }) - Supervisor( - SupervisorConfig( - RestartStrategy(AllForOne, 3, 1000, - List(classOf[Exception])), - Supervise( - actor, - LifeCycle(Permanent)) - :: Nil)).start + Supervisor( + SupervisorConfig( + RestartStrategy(AllForOne, 3, 1000, + List(classOf[Exception])), + Supervise( + actor, + LifeCycle(Permanent)) + :: Nil)).start - Scheduler.schedule(actor, Ping, 500, 500, TimeUnit.MILLISECONDS) - // appx 2 pings before crash - Scheduler.scheduleOnce(actor, Crash, 1000, TimeUnit.MILLISECONDS) + Scheduler.schedule(actor, Ping, 500, 500, TimeUnit.MILLISECONDS) + // appx 2 pings before crash + Scheduler.scheduleOnce(actor, Crash, 1000, TimeUnit.MILLISECONDS) - assert(restartLatch.tryAwait(4, TimeUnit.SECONDS)) - // should be enough time for the ping countdown to recover and reach 4 pings - assert(pingLatch.await(4, TimeUnit.SECONDS)) - - } finally { - - Scheduler.shutdown - } + assert(restartLatch.tryAwait(2, TimeUnit.SECONDS)) + // should be enough time for the ping countdown to recover and reach 6 pings + assert(pingLatch.await(4, TimeUnit.SECONDS)) } }