Changing implementation of AgentSpec not to do Thread.sleep

This commit is contained in:
Viktor Klang 2012-07-03 23:12:35 +02:00
parent d4f759646c
commit 602c83f451

View file

@ -1,12 +1,11 @@
package akka.agent
import akka.dispatch.Await
import akka.util.Duration
import akka.dispatch.{ Await, Future }
import akka.util.{ Duration, Timeout }
import akka.util.duration._
import akka.util.Timeout
import akka.testkit._
import scala.concurrent.stm._
import java.util.concurrent.CountDownLatch
import java.util.concurrent.{ CountDownLatch, TimeUnit }
class CountDownFunction[A](num: Int = 1) extends Function1[A, A] {
val latch = new CountDownLatch(num)
@ -36,14 +35,16 @@ class AgentSpec extends AkkaSpec {
"maintain order between send and sendOff" in {
val countDown = new CountDownFunction[String]
val l1, l2 = new CountDownLatch(1)
val agent = Agent("a")
agent send (_ + "b")
val longRunning = (s: String) { Thread.sleep(2000); s + "c" }
val longRunning = (s: String) { l1.countDown; l2.await(5, TimeUnit.SECONDS); s + "c" }
agent sendOff longRunning
agent send (_ + "d")
agent send countDown
l1.await(5, TimeUnit.SECONDS)
l2.countDown
countDown.await(5 seconds)
agent() must be("abcd")
@ -51,16 +52,17 @@ class AgentSpec extends AkkaSpec {
}
"maintain order between alter and alterOff" in {
val l1, l2 = new CountDownLatch(1)
val agent = Agent("a")
val r1 = agent.alter(_ + "b")(5000)
val r2 = agent.alterOff((s: String) { Thread.sleep(2000); s + "c" })(5000)
val r2 = agent.alterOff((s: String) { l1.countDown; l2.await(5, TimeUnit.SECONDS); s + "c" })(5000)
val r3 = agent.alter(_ + "d")(5000)
val result = Future.sequence(Seq(r1, r2, r3)).map(_.mkString(":"))
l1.await(5, TimeUnit.SECONDS)
l2.countDown
Await.result(r1, 5 seconds) must be === "ab"
Await.result(r2, 5 seconds) must be === "abc"
Await.result(r3, 5 seconds) must be === "abcd"
Await.result(result, 5 seconds) must be === "ab:abc:abcd"
agent() must be("abcd")