Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jboner/akka
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Bonér committed Mar 31, 2011
2 parents 2f9ec86 + 1574f51 commit e7cf485
Show file tree
Hide file tree
Showing 15 changed files with 383 additions and 262 deletions.
@@ -1,18 +1,25 @@
/**
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
*/

package akka.actor

import java.util.concurrent.{TimeUnit, CyclicBarrier, TimeoutException}
import akka.config.Supervision._
import org.scalatest.junit.JUnitSuite
import org.junit.Test
import org.scalatest.WordSpec
import org.scalatest.matchers.MustMatchers
import org.scalatest.BeforeAndAfterEach

import akka.testing._
import akka.testing.Testing.sleepFor
import akka.util.duration._

import akka.dispatch.Dispatchers
import Actor._
import akka.config.Supervision._
import akka.dispatch.Dispatchers

import akka.Testing

object ActorFireForgetRequestReplySpec {
class ReplyActor extends Actor {

class ReplyActor extends Actor {
def receive = {
case "Send" =>
self.reply("Reply")
Expand All @@ -32,7 +39,6 @@ object ActorFireForgetRequestReplySpec {
}

class SenderActor(replyActor: ActorRef) extends Actor {

def receive = {
case "Init" =>
replyActor ! "Send"
Expand All @@ -50,44 +56,42 @@ object ActorFireForgetRequestReplySpec {

object state {
var s = "NIL"
val finished = new CyclicBarrier(2)
val finished = TestBarrier(2)
}
}

class ActorFireForgetRequestReplySpec extends JUnitSuite {
class ActorFireForgetRequestReplySpec extends WordSpec with MustMatchers with BeforeAndAfterEach {
import ActorFireForgetRequestReplySpec._

@Test
def shouldReplyToBangMessageUsingReply = {
override def beforeEach() = {
state.finished.reset
val replyActor = actorOf[ReplyActor].start
val senderActor = actorOf(new SenderActor(replyActor)).start
senderActor ! "Init"
try { state.finished.await(1L, TimeUnit.SECONDS) }
catch { case e: TimeoutException => fail("Never got the message") }
assert("Reply" === state.s)
}
}

"An Actor" must {

@Test
def shouldReplyToBangMessageUsingImplicitSender = {
state.finished.reset
val replyActor = actorOf[ReplyActor].start
val senderActor = actorOf(new SenderActor(replyActor)).start
senderActor ! "InitImplicit"
try { state.finished.await(1L, TimeUnit.SECONDS) }
catch { case e: TimeoutException => fail("Never got the message") }
assert("ReplyImplicit" === state.s)
}
"reply to bang message using reply" in {
val replyActor = actorOf[ReplyActor].start
val senderActor = actorOf(new SenderActor(replyActor)).start
senderActor ! "Init"
state.finished.await
state.s must be ("Reply")
}

@Test
def shouldShutdownCrashedTemporaryActor = {
state.finished.reset
val actor = actorOf[CrashingTemporaryActor].start
assert(actor.isRunning)
actor ! "Die"
try { state.finished.await(10L, TimeUnit.SECONDS) }
catch { case e: TimeoutException => fail("Never got the message") }
Thread.sleep(Testing.time(500))
assert(actor.isShutdown)
"reply to bang message using implicit sender" in {
val replyActor = actorOf[ReplyActor].start
val senderActor = actorOf(new SenderActor(replyActor)).start
senderActor ! "InitImplicit"
state.finished.await
state.s must be ("ReplyImplicit")
}

"should shutdown crashed temporary actor" in {
val actor = actorOf[CrashingTemporaryActor].start
actor.isRunning must be (true)
actor ! "Die"
state.finished.await
sleepFor(1 second)
actor.isShutdown must be (true)
}
}
}
50 changes: 26 additions & 24 deletions akka-actor/src/test/scala/akka/actor/actor/ActorRefSpec.scala
Expand Up @@ -4,19 +4,20 @@

package akka.actor

import org.scalatest.Spec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.BeforeAndAfterAll
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import org.scalatest.WordSpec
import org.scalatest.matchers.MustMatchers

import akka.testing._
import akka.util.duration._
import akka.testing.Testing.sleepFor

import akka.actor._
import akka.dispatch.Future
import java.util.concurrent.{CountDownLatch, TimeUnit}


object ActorRefSpec {

var latch = new CountDownLatch(4)
val latch = TestLatch(4)

class ReplyActor extends Actor {
var replyTo: Channel[Any] = null
Expand Down Expand Up @@ -49,7 +50,7 @@ object ActorRefSpec {
}

private def work {
Thread.sleep(1000)
sleepFor(1 second)
}
}

Expand All @@ -69,35 +70,36 @@ object ActorRefSpec {
}
}

@RunWith(classOf[JUnitRunner])
class ActorRefSpec extends
Spec with
ShouldMatchers with
BeforeAndAfterAll {

class ActorRefSpec extends WordSpec with MustMatchers {
import ActorRefSpec._

describe("ActorRef") {
it("should support to reply via channel") {
"An ActorRef" must {

"support reply via channel" in {
val serverRef = Actor.actorOf[ReplyActor].start
val clientRef = Actor.actorOf(new SenderActor(serverRef)).start

clientRef ! "complex"
clientRef ! "simple"
clientRef ! "simple"
clientRef ! "simple"
assert(latch.await(4L, TimeUnit.SECONDS))
latch = new CountDownLatch(4)

latch.await

latch.reset

clientRef ! "complex2"
clientRef ! "simple"
clientRef ! "simple"
clientRef ! "simple"
assert(latch.await(4L, TimeUnit.SECONDS))

latch.await

clientRef.stop
serverRef.stop
}

it("should stop when sent a poison pill") {
"stop when sent a poison pill" in {
val ref = Actor.actorOf(
new Actor {
def receive = {
Expand All @@ -115,11 +117,11 @@ class ActorRefSpec extends
fail("shouldn't get here")
}

assert(ffive.resultOrException.get == "five")
assert(fnull.resultOrException.get == "null")
ffive.resultOrException.get must be ("five")
fnull.resultOrException.get must be ("null")

assert(ref.isRunning == false)
assert(ref.isShutdown == true)
ref.isRunning must be (false)
ref.isShutdown must be (true)
}
}
}
119 changes: 58 additions & 61 deletions akka-actor/src/test/scala/akka/actor/actor/FSMActorSpec.scala
Expand Up @@ -4,34 +4,31 @@

package akka.actor

import org.scalatest.junit.JUnitSuite
import org.junit.Test
import FSM._

import org.multiverse.api.latches.StandardLatch
import org.scalatest.WordSpec
import org.scalatest.matchers.MustMatchers

import java.util.concurrent.TimeUnit
import akka.testing._

import FSM._
import akka.util.Duration
import akka.util.duration._

import akka.Testing

object FSMActorSpec {


val unlockedLatch = new StandardLatch
val lockedLatch = new StandardLatch
val unhandledLatch = new StandardLatch
val terminatedLatch = new StandardLatch
val transitionLatch = new StandardLatch
val initialStateLatch = new StandardLatch
val transitionCallBackLatch = new StandardLatch
val unlockedLatch = TestLatch()
val lockedLatch = TestLatch()
val unhandledLatch = TestLatch()
val terminatedLatch = TestLatch()
val transitionLatch = TestLatch()
val initialStateLatch = TestLatch()
val transitionCallBackLatch = TestLatch()

sealed trait LockState
case object Locked extends LockState
case object Open extends LockState

class Lock(code: String, timeout: (Long, TimeUnit)) extends Actor with FSM[LockState, CodeState] {
class Lock(code: String, timeout: Duration) extends Actor with FSM[LockState, CodeState] {

startWith(Locked, CodeState("", code))

Expand Down Expand Up @@ -94,53 +91,53 @@ object FSMActorSpec {
case class CodeState(soFar: String, code: String)
}

class FSMActorSpec extends JUnitSuite {
class FSMActorSpec extends WordSpec with MustMatchers {
import FSMActorSpec._

"An FSM Actor" must {

"unlock the lock" in {

// lock that locked after being open for 1 sec
val lock = Actor.actorOf(new Lock("33221", 1 second)).start

val transitionTester = Actor.actorOf(new Actor { def receive = {
case Transition(_, _, _) => transitionCallBackLatch.open
case CurrentState(_, Locked) => initialStateLatch.open
}}).start

lock ! SubscribeTransitionCallBack(transitionTester)
initialStateLatch.await

lock ! '3'
lock ! '3'
lock ! '2'
lock ! '2'
lock ! '1'

unlockedLatch.await
transitionLatch.await
transitionCallBackLatch.await
lockedLatch.await

lock ! "not_handled"
unhandledLatch.await

val answerLatch = TestLatch()
object Hello
object Bye
val tester = Actor.actorOf(new Actor {
protected def receive = {
case Hello => lock ! "hello"
case "world" => answerLatch.open
case Bye => lock ! "bye"
}
}).start
tester ! Hello
answerLatch.await

@Test
def unlockTheLock = {

// lock that locked after being open for 1 sec
val lock = Actor.actorOf(new Lock("33221", (Testing.time(1), TimeUnit.SECONDS))).start

val transitionTester = Actor.actorOf(new Actor { def receive = {
case Transition(_, _, _) => transitionCallBackLatch.open
case CurrentState(_, Locked) => initialStateLatch.open
}}).start

lock ! SubscribeTransitionCallBack(transitionTester)
assert(initialStateLatch.tryAwait(Testing.time(1), TimeUnit.SECONDS))

lock ! '3'
lock ! '3'
lock ! '2'
lock ! '2'
lock ! '1'

assert(unlockedLatch.tryAwait(Testing.time(1), TimeUnit.SECONDS))
assert(transitionLatch.tryAwait(Testing.time(1), TimeUnit.SECONDS))
assert(transitionCallBackLatch.tryAwait(Testing.time(1), TimeUnit.SECONDS))
assert(lockedLatch.tryAwait(Testing.time(2), TimeUnit.SECONDS))


lock ! "not_handled"
assert(unhandledLatch.tryAwait(Testing.time(2), TimeUnit.SECONDS))

val answerLatch = new StandardLatch
object Hello
object Bye
val tester = Actor.actorOf(new Actor {
protected def receive = {
case Hello => lock ! "hello"
case "world" => answerLatch.open
case Bye => lock ! "bye"
}
}).start
tester ! Hello
assert(answerLatch.tryAwait(Testing.time(2), TimeUnit.SECONDS))

tester ! Bye
assert(terminatedLatch.tryAwait(Testing.time(2), TimeUnit.SECONDS))
tester ! Bye
terminatedLatch.await
}
}
}
12 changes: 4 additions & 8 deletions akka-actor/src/test/scala/akka/actor/actor/FSMTimingSpec.scala
@@ -1,16 +1,13 @@
package akka.actor

import akka.testkit.TestKit
import akka.util.duration._

import org.scalatest.WordSpec
import org.scalatest.matchers.MustMatchers

class FSMTimingSpec
extends WordSpec
with MustMatchers
with TestKit {
import akka.testkit.TestKit
import akka.util.duration._


class FSMTimingSpec extends WordSpec with MustMatchers with TestKit {
import FSMTimingSpec._
import FSM._

Expand Down Expand Up @@ -140,4 +137,3 @@ object FSMTimingSpec {

}

// vim: set ts=2 sw=2 et:

0 comments on commit e7cf485

Please sign in to comment.