2010-10-26 12:49:25 +02:00
|
|
|
package akka.actor
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-06-20 15:11:32 -06:00
|
|
|
import org.scalatest.BeforeAndAfterEach
|
2010-08-24 23:21:28 +02:00
|
|
|
import org.multiverse.api.latches.StandardLatch
|
2011-10-11 16:05:48 +02:00
|
|
|
import akka.testkit.AkkaSpec
|
2011-11-11 18:41:43 +01:00
|
|
|
import akka.testkit.EventFilter
|
2011-11-23 15:15:44 +01:00
|
|
|
import akka.util.duration._
|
|
|
|
|
import java.util.concurrent.{ CountDownLatch, ConcurrentLinkedQueue, TimeUnit }
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-10-21 17:01:22 +02:00
|
|
|
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
2011-10-11 16:05:48 +02:00
|
|
|
class SchedulerSpec extends AkkaSpec with BeforeAndAfterEach {
|
2011-11-10 11:53:36 +01:00
|
|
|
private val cancellables = new ConcurrentLinkedQueue[Cancellable]()
|
2011-07-12 12:17:32 +02:00
|
|
|
|
2011-11-10 11:53:36 +01:00
|
|
|
def collectCancellable(c: Cancellable): Cancellable = {
|
|
|
|
|
cancellables.add(c)
|
|
|
|
|
c
|
2011-07-12 12:17:32 +02:00
|
|
|
}
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
override def afterEach {
|
2011-11-10 11:53:36 +01:00
|
|
|
while (cancellables.peek() ne null) { Option(cancellables.poll()).foreach(_.cancel()) }
|
2010-08-24 23:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
"A Scheduler" must {
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
"schedule more than once" in {
|
|
|
|
|
case object Tick
|
|
|
|
|
val countDownLatch = new CountDownLatch(3)
|
2011-10-18 17:56:23 +02:00
|
|
|
val tickActor = actorOf(new Actor {
|
2011-10-11 16:05:48 +02:00
|
|
|
def receive = { case Tick ⇒ countDownLatch.countDown() }
|
|
|
|
|
})
|
2011-11-26 15:24:13 +01:00
|
|
|
// run every 50 milliseconds
|
2011-11-23 15:15:44 +01:00
|
|
|
collectCancellable(system.scheduler.schedule(tickActor, Tick, 0 milliseconds, 50 milliseconds))
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
// after max 1 second it should be executed at least the 3 times already
|
|
|
|
|
assert(countDownLatch.await(1, TimeUnit.SECONDS))
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
val countDownLatch2 = new CountDownLatch(3)
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-11-23 15:15:44 +01:00
|
|
|
collectCancellable(system.scheduler.schedule(() ⇒ countDownLatch2.countDown(), 0 milliseconds, 50 milliseconds))
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
// after max 1 second it should be executed at least the 3 times already
|
|
|
|
|
assert(countDownLatch2.await(2, TimeUnit.SECONDS))
|
|
|
|
|
}
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-11-25 17:56:14 +01:00
|
|
|
"should stop continuous scheduling if the receiving actor has been terminated" in {
|
|
|
|
|
// run immediately and then every 100 milliseconds
|
2011-11-26 15:24:13 +01:00
|
|
|
collectCancellable(system.scheduler.schedule(testActor, "msg", 0 milliseconds, 100 milliseconds))
|
2011-11-25 17:56:14 +01:00
|
|
|
|
|
|
|
|
// stop the actor and, hence, the continuous messaging from happening
|
2011-11-26 15:24:13 +01:00
|
|
|
testActor ! PoisonPill
|
2011-11-25 17:56:14 +01:00
|
|
|
|
2011-11-26 15:24:13 +01:00
|
|
|
expectNoMsg(500 milliseconds)
|
2011-11-25 17:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
"schedule once" in {
|
|
|
|
|
case object Tick
|
|
|
|
|
val countDownLatch = new CountDownLatch(3)
|
2011-10-18 17:56:23 +02:00
|
|
|
val tickActor = actorOf(new Actor {
|
2011-10-11 16:05:48 +02:00
|
|
|
def receive = { case Tick ⇒ countDownLatch.countDown() }
|
|
|
|
|
})
|
2011-11-09 15:25:14 +01:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
// run every 50 millisec
|
2011-11-23 15:15:44 +01:00
|
|
|
collectCancellable(system.scheduler.scheduleOnce(tickActor, Tick, 50 milliseconds))
|
|
|
|
|
collectCancellable(system.scheduler.scheduleOnce(() ⇒ countDownLatch.countDown(), 50 milliseconds))
|
2011-10-11 16:05:48 +02:00
|
|
|
|
|
|
|
|
// after 1 second the wait should fail
|
|
|
|
|
assert(countDownLatch.await(2, TimeUnit.SECONDS) == false)
|
|
|
|
|
// should still be 1 left
|
|
|
|
|
assert(countDownLatch.getCount == 1)
|
2010-08-24 23:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
/**
|
|
|
|
|
* ticket #372
|
|
|
|
|
*/
|
|
|
|
|
"be cancellable" in {
|
|
|
|
|
object Ping
|
|
|
|
|
val ticks = new CountDownLatch(1)
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
val actor = actorOf(new Actor {
|
2011-10-11 16:05:48 +02:00
|
|
|
def receive = { case Ping ⇒ ticks.countDown() }
|
|
|
|
|
})
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
(1 to 10).foreach { i ⇒
|
2011-11-23 15:15:44 +01:00
|
|
|
val timeout = collectCancellable(system.scheduler.scheduleOnce(actor, Ping, 1 second))
|
2011-11-09 15:25:14 +01:00
|
|
|
timeout.cancel()
|
2010-08-24 23:21:28 +02:00
|
|
|
}
|
2011-11-09 15:25:14 +01:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
assert(ticks.await(3, TimeUnit.SECONDS) == false) //No counting down should've been made
|
|
|
|
|
}
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
/**
|
|
|
|
|
* ticket #307
|
|
|
|
|
*/
|
|
|
|
|
"pick up schedule after actor restart" in {
|
|
|
|
|
object Ping
|
|
|
|
|
object Crash
|
|
|
|
|
|
|
|
|
|
val restartLatch = new StandardLatch
|
|
|
|
|
val pingLatch = new CountDownLatch(6)
|
|
|
|
|
|
2011-10-18 15:39:26 +02:00
|
|
|
val supervisor = actorOf(Props[Supervisor].withFaultHandler(AllForOneStrategy(List(classOf[Exception]), 3, 1000)))
|
|
|
|
|
val props = Props(new Actor {
|
2011-10-11 16:05:48 +02:00
|
|
|
def receive = {
|
|
|
|
|
case Ping ⇒ pingLatch.countDown()
|
|
|
|
|
case Crash ⇒ throw new Exception("CRASH")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postRestart(reason: Throwable) = restartLatch.open
|
2011-10-18 15:39:26 +02:00
|
|
|
})
|
|
|
|
|
val actor = (supervisor ? props).as[ActorRef].get
|
2011-10-11 16:05:48 +02:00
|
|
|
|
2011-11-23 15:15:44 +01:00
|
|
|
collectCancellable(system.scheduler.schedule(actor, Ping, 500 milliseconds, 500 milliseconds))
|
2011-10-11 16:05:48 +02:00
|
|
|
// appx 2 pings before crash
|
2011-11-11 18:41:43 +01:00
|
|
|
EventFilter[Exception]("CRASH", occurrences = 1) intercept {
|
2011-11-23 15:15:44 +01:00
|
|
|
collectCancellable(system.scheduler.scheduleOnce(actor, Crash, 1000 milliseconds))
|
2011-11-11 18:41:43 +01:00
|
|
|
}
|
2011-10-11 16:05:48 +02:00
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
}
|
2011-11-22 15:26:21 +01:00
|
|
|
|
|
|
|
|
"never fire prematurely" in {
|
2011-11-24 09:54:08 +01:00
|
|
|
val ticks = new CountDownLatch(300)
|
2011-11-22 15:26:21 +01:00
|
|
|
|
|
|
|
|
case class Msg(ts: Long)
|
|
|
|
|
|
|
|
|
|
val actor = actorOf(new Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case Msg(ts) ⇒
|
2011-11-24 14:30:03 +01:00
|
|
|
val now = System.nanoTime
|
|
|
|
|
// Make sure that no message has been dispatched before the scheduled time (10ms = 10000000ns) has occurred
|
|
|
|
|
if (now - ts < 10000000) throw new RuntimeException("Interval is too small: " + (now - ts))
|
2011-11-22 15:26:21 +01:00
|
|
|
ticks.countDown()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2011-11-24 09:54:08 +01:00
|
|
|
(1 to 300).foreach { i ⇒
|
2011-11-24 14:30:03 +01:00
|
|
|
collectCancellable(system.scheduler.scheduleOnce(actor, Msg(System.nanoTime), 10 milliseconds))
|
2011-11-22 15:26:21 +01:00
|
|
|
Thread.sleep(5)
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-24 09:54:08 +01:00
|
|
|
assert(ticks.await(3, TimeUnit.SECONDS) == true)
|
2011-11-22 15:26:21 +01:00
|
|
|
}
|
2011-11-23 15:15:44 +01:00
|
|
|
|
|
|
|
|
"schedule with different initial delay and frequency" in {
|
|
|
|
|
val ticks = new CountDownLatch(3)
|
|
|
|
|
|
|
|
|
|
case object Msg
|
|
|
|
|
|
|
|
|
|
val actor = actorOf(new Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case Msg ⇒ ticks.countDown()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
val startTime = System.nanoTime()
|
|
|
|
|
val cancellable = system.scheduler.schedule(actor, Msg, 1 second, 100 milliseconds)
|
|
|
|
|
ticks.await(3, TimeUnit.SECONDS)
|
|
|
|
|
val elapsedTimeMs = (System.nanoTime() - startTime) / 1000000
|
|
|
|
|
|
|
|
|
|
assert(elapsedTimeMs > 1200)
|
|
|
|
|
assert(elapsedTimeMs < 1500) // the precision is not ms exact
|
|
|
|
|
cancellable.cancel()
|
|
|
|
|
}
|
2010-08-24 23:21:28 +02:00
|
|
|
}
|
|
|
|
|
}
|