diff --git a/akka-patterns/src/test/scala/ActorPatternsTest.scala b/akka-patterns/src/test/scala/ActorPatternsTest.scala index 2235b1a1a7..d300578c05 100644 --- a/akka-patterns/src/test/scala/ActorPatternsTest.scala +++ b/akka-patterns/src/test/scala/ActorPatternsTest.scala @@ -14,141 +14,105 @@ import org.junit.{Before, After, Test} import scala.collection.mutable.HashSet @RunWith(classOf[JUnitRunner]) -class ActorPatternsTest extends junit.framework.TestCase with Suite with MustMatchers with ActorTestUtil with Logging { +class ActorPatternsTest extends junit.framework.TestCase with Suite with MustMatchers with Logging { import Patterns._ - @Test def testDispatcher = verify(new TestActor { - def test = { - val (testMsg1,testMsg2,testMsg3,testMsg4) = ("test1","test2","test3","test4") + @Test def testDispatcher = { + val (testMsg1,testMsg2,testMsg3,testMsg4) = ("test1","test2","test3","test4") - var targetOk = 0 - val t1: Actor = actor { - case `testMsg1` => targetOk += 2 - case `testMsg2` => targetOk += 4 - } + var targetOk = 0 + val t1 = actor { + case `testMsg1` => targetOk += 2 + case `testMsg2` => targetOk += 4 + } - val t2: Actor = actor { - case `testMsg3` => targetOk += 8 - } + val t2 = actor { + case `testMsg3` => targetOk += 8 + } - val d = dispatcherActor { - case `testMsg1`|`testMsg2` => t1 - case `testMsg3` => t2 - } + val d = dispatcherActor { + case `testMsg1`|`testMsg2` => t1 + case `testMsg3` => t2 + }.start - handle(d,t1,t2){ - d ! testMsg1 - d ! testMsg2 - d ! testMsg3 - Thread.sleep(1000) - targetOk must be(14) - } - } - }) + d ! testMsg1 + d ! testMsg2 + d ! testMsg3 + Thread.sleep(1000) + targetOk must be(14) + t1.stop + t2.stop + d.stop + } - @Test def testLogger = verify(new TestActor { - def test = { - val msgs = new HashSet[Any] - val t1: Actor = actor { - case _ => - } - val l = loggerActor(t1,(x) => msgs += x) - handle(t1,l) { - val t1 : Any = "foo" - val t2 : Any = "bar" - l ! t1 - l ! t2 - Thread.sleep(1000) - msgs must ( have size (2) and contain (t1) and contain (t2) ) - } + @Test def testLogger = { + val msgs = new HashSet[Any] + val t1 = actor { + case _ => } - }) + val l = loggerActor(t1,(x) => msgs += x).start + val foo : Any = "foo" + val bar : Any = "bar" + l ! foo + l ! bar + Thread.sleep(1000) + msgs must ( have size (2) and contain (foo) and contain (bar) ) + t1.stop + l.stop + } - @Test def testSmallestMailboxFirstDispatcher = verify(new TestActor { - def test = { - val t1ProcessedCount = new AtomicInteger(0) - val t1: Actor = actor { - case x => { - Thread.sleep(50) // slow actor - t1ProcessedCount.incrementAndGet - } - } - - val t2ProcessedCount = new AtomicInteger(0) - val t2: Actor = actor { - case x => { - t2ProcessedCount.incrementAndGet - } - } - - val d = loadBalancerActor(new SmallestMailboxFirstIterator(t1 :: t2 :: Nil)) - - handle(d, t1, t2) { - for (i <- 1 to 500) - d ! i - Thread.sleep(6000) - t1ProcessedCount.get must be < (t2ProcessedCount.get) // because t1 is much slower and thus has a bigger mailbox all the time - } + @Test def testSmallestMailboxFirstDispatcher = { + val t1ProcessedCount = new AtomicInteger(0) + val t1 = actor { + case x => + Thread.sleep(50) // slow actor + t1ProcessedCount.incrementAndGet } - }) + + val t2ProcessedCount = new AtomicInteger(0) + val t2 = actor { + case x => t2ProcessedCount.incrementAndGet + } + val d = loadBalancerActor(new SmallestMailboxFirstIterator(t1 :: t2 :: Nil)) + for (i <- 1 to 500) d ! i + Thread.sleep(6000) + t1ProcessedCount.get must be < (t2ProcessedCount.get) // because t1 is much slower and thus has a bigger mailbox all the time + t1.stop + t2.stop + d.stop + } - @Test def testListener = verify(new TestActor { + @Test def testListener = { import java.util.concurrent.{ CountDownLatch, TimeUnit } - def test = { - val latch = new CountDownLatch(2) - val num = new AtomicInteger(0) - val i = new Actor with Listeners { - def receive = listenerManagement orElse { - case "foo" => gossip("bar") - } + val latch = new CountDownLatch(2) + val num = new AtomicInteger(0) + val i = newActor(() => new Actor with Listeners { + def receive = listenerManagement orElse { + case "foo" => gossip("bar") } - i.start + }) + i.start - def newListener = actor { - case "bar" => - num.incrementAndGet - latch.countDown - } + def newListener = actor { + case "bar" => + num.incrementAndGet + latch.countDown + } - val a1 = newListener - val a2 = newListener - val a3 = newListener - - handle(i,a1,a2,a3) { - i ! Listen(a1) - i ! Listen(a2) - i ! Listen(a3) - i ! Deafen(a3) + val a1 = newListener + val a2 = newListener + val a3 = newListener + + i ! Listen(a1) + i ! Listen(a2) + i ! Listen(a3) + i ! Deafen(a3) - i ! "foo" - - val done = latch.await(5,TimeUnit.SECONDS) - done must be (true) - num.get must be (2) - } - } - }); + i ! "foo" -} - -trait ActorTestUtil { - - def handle[T](actors : Actor*)(test : => T) : T = { - for(a <- actors) a.start - try { - test - } - finally { - for(a <- actors) a.stop - } + val done = latch.await(5,TimeUnit.SECONDS) + done must be (true) + num.get must be (2) + i.stop } - - def verify(actor : TestActor) : Unit = handle(actor) { - actor.test - } -} - -abstract class TestActor extends Actor with ActorTestUtil { - def test : Unit - def receive = { case _ => } } \ No newline at end of file diff --git a/akka-persistence/akka-persistence-cassandra/src/test/scala/CassandraPersistentActorSpec.scala b/akka-persistence/akka-persistence-cassandra/src/test/scala/CassandraPersistentActorSpec.scala index 46d1b48a2d..30d22c5d5e 100644 --- a/akka-persistence/akka-persistence-cassandra/src/test/scala/CassandraPersistentActorSpec.scala +++ b/akka-persistence/akka-persistence-cassandra/src/test/scala/CassandraPersistentActorSpec.scala @@ -1,6 +1,7 @@ package se.scalablesolutions.akka.persistence.cassandra -import se.scalablesolutions.akka.actor.{Actor, Transactor} +import se.scalablesolutions.akka.actor.{Actor, ActorID, Transactor} +import Actor._ import org.junit.Test import org.junit.Assert._ @@ -16,13 +17,13 @@ case class SetMapState(key: String, value: String) case class SetVectorState(key: String) case class SetRefState(key: String) case class Success(key: String, value: String) -case class Failure(key: String, value: String, failer: Actor) +case class Failure(key: String, value: String, failer: ActorID) case class SetMapStateOneWay(key: String, value: String) case class SetVectorStateOneWay(key: String) case class SetRefStateOneWay(key: String) case class SuccessOneWay(key: String, value: String) -case class FailureOneWay(key: String, value: String, failer: Actor) +case class FailureOneWay(key: String, value: String, failer: ActorID) class CassandraPersistentActor extends Transactor { timeout = 100000 @@ -75,7 +76,7 @@ class CassandraPersistentActorSpec extends JUnitSuite { @Test def testMapShouldNotRollbackStateForStatefulServerInCaseOfSuccess = { - val stateful = new CassandraPersistentActor + val stateful = newActor[CassandraPersistentActor] stateful.start stateful !! SetMapState("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "init") // set init state stateful !! Success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state") // transactionrequired @@ -85,10 +86,10 @@ class CassandraPersistentActorSpec extends JUnitSuite { @Test def testMapShouldRollbackStateForStatefulServerInCaseOfFailure = { - val stateful = new CassandraPersistentActor + val stateful = newActor[CassandraPersistentActor] stateful.start stateful !! SetMapState("testShouldRollbackStateForStatefulServerInCaseOfFailure", "init") // set init state - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start try { stateful !! Failure("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer) // call failing transactionrequired method @@ -100,7 +101,7 @@ class CassandraPersistentActorSpec extends JUnitSuite { @Test def testVectorShouldNotRollbackStateForStatefulServerInCaseOfSuccess = { - val stateful = new CassandraPersistentActor + val stateful = newActor[CassandraPersistentActor] stateful.start stateful !! SetVectorState("init") // set init state stateful !! Success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state") // transactionrequired @@ -109,10 +110,10 @@ class CassandraPersistentActorSpec extends JUnitSuite { @Test def testVectorShouldRollbackStateForStatefulServerInCaseOfFailure = { - val stateful = new CassandraPersistentActor + val stateful = newActor[CassandraPersistentActor] stateful.start stateful !! SetVectorState("init") // set init state - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start try { stateful !! Failure("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer) // call failing transactionrequired method @@ -123,7 +124,7 @@ class CassandraPersistentActorSpec extends JUnitSuite { @Test def testRefShouldNotRollbackStateForStatefulServerInCaseOfSuccess = { - val stateful = new CassandraPersistentActor + val stateful = newActor[CassandraPersistentActor] stateful.start stateful !! SetRefState("init") // set init state stateful !! Success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state") // transactionrequired @@ -133,10 +134,10 @@ class CassandraPersistentActorSpec extends JUnitSuite { @Test def testRefShouldRollbackStateForStatefulServerInCaseOfFailure = { - val stateful = new CassandraPersistentActor + val stateful = newActor[CassandraPersistentActor] stateful.start stateful !! SetRefState("init") // set init state - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start try { stateful !! Failure("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer) // call failing transactionrequired method diff --git a/akka-persistence/akka-persistence-mongo/src/test/scala/MongoPersistentActorSpec.scala b/akka-persistence/akka-persistence-mongo/src/test/scala/MongoPersistentActorSpec.scala index 93aa1862d1..cb8ed36442 100644 --- a/akka-persistence/akka-persistence-mongo/src/test/scala/MongoPersistentActorSpec.scala +++ b/akka-persistence/akka-persistence-mongo/src/test/scala/MongoPersistentActorSpec.scala @@ -8,7 +8,8 @@ import org.junit.Assert._ import _root_.dispatch.json.{JsNumber, JsValue} import _root_.dispatch.json.Js._ -import se.scalablesolutions.akka.actor.{Transactor, Actor} +import se.scalablesolutions.akka.actor.{Transactor, Actor, ActorID} +import Actor._ /** * A persistent actor based on MongoDB storage. @@ -24,8 +25,8 @@ import se.scalablesolutions.akka.actor.{Transactor, Actor} */ case class Balance(accountNo: String) -case class Debit(accountNo: String, amount: BigInt, failer: Actor) -case class MultiDebit(accountNo: String, amounts: List[BigInt], failer: Actor) +case class Debit(accountNo: String, amount: BigInt, failer: ActorID) +case class MultiDebit(accountNo: String, amounts: List[BigInt], failer: ActorID) case class Credit(accountNo: String, amount: BigInt) case object LogSize @@ -101,9 +102,9 @@ class BankAccountActor extends Transactor { class MongoPersistentActorSpec extends TestCase { @Test def testSuccessfulDebit = { - val bactor = new BankAccountActor + val bactor = newActor[BankAccountActor] bactor.start - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start bactor !! Credit("a-123", 5000) bactor !! Debit("a-123", 3000, failer) @@ -126,14 +127,14 @@ class MongoPersistentActorSpec extends TestCase { @Test def testUnsuccessfulDebit = { - val bactor = new BankAccountActor + val bactor = newActor[BankAccountActor] bactor.start bactor !! Credit("a-123", 5000) val JsNumber(b) = (bactor !! Balance("a-123")).get.asInstanceOf[JsValue] assertEquals(BigInt(5000), BigInt(b.intValue)) - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start try { bactor !! Debit("a-123", 7000, failer) @@ -149,14 +150,14 @@ class MongoPersistentActorSpec extends TestCase { @Test def testUnsuccessfulMultiDebit = { - val bactor = new BankAccountActor + val bactor = newActor[BankAccountActor] bactor.start bactor !! Credit("a-123", 5000) val JsNumber(b) = (bactor !! Balance("a-123")).get.asInstanceOf[JsValue] assertEquals(BigInt(5000), BigInt(b.intValue)) - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start try { bactor !! MultiDebit("a-123", List(500, 2000, 1000, 3000), failer) diff --git a/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentActorSpec.scala b/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentActorSpec.scala index f21b1b72a0..c0516e7883 100644 --- a/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentActorSpec.scala +++ b/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentActorSpec.scala @@ -5,7 +5,8 @@ import junit.framework.TestCase import org.junit.{Test, Before} import org.junit.Assert._ -import se.scalablesolutions.akka.actor.{Actor, Transactor} +import se.scalablesolutions.akka.actor.{Actor, ActorID, Transactor} +import Actor._ /** * A persistent actor based on Redis storage. @@ -21,8 +22,8 @@ import se.scalablesolutions.akka.actor.{Actor, Transactor} */ case class Balance(accountNo: String) -case class Debit(accountNo: String, amount: BigInt, failer: Actor) -case class MultiDebit(accountNo: String, amounts: List[BigInt], failer: Actor) +case class Debit(accountNo: String, amount: BigInt, failer: ActorID) +case class MultiDebit(accountNo: String, amounts: List[BigInt], failer: ActorID) case class Credit(accountNo: String, amount: BigInt) case object LogSize @@ -97,9 +98,9 @@ class AccountActor extends Transactor { class RedisPersistentActorSpec extends TestCase { @Test def testSuccessfulDebit = { - val bactor = new AccountActor + val bactor = newActor[AccountActor] bactor.start - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start bactor !! Credit("a-123", 5000) bactor !! Debit("a-123", 3000, failer) @@ -117,12 +118,12 @@ class RedisPersistentActorSpec extends TestCase { @Test def testUnsuccessfulDebit = { - val bactor = new AccountActor + val bactor = newActor[AccountActor] bactor.start bactor !! Credit("a-123", 5000) assertEquals(BigInt(5000), (bactor !! Balance("a-123")).get) - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start try { bactor !! Debit("a-123", 7000, failer) @@ -138,13 +139,13 @@ class RedisPersistentActorSpec extends TestCase { @Test def testUnsuccessfulMultiDebit = { - val bactor = new AccountActor + val bactor = newActor[AccountActor] bactor.start bactor !! Credit("a-123", 5000) assertEquals(BigInt(5000), (bactor !! (Balance("a-123"), 5000)).get) - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start try { bactor !! MultiDebit("a-123", List(500, 2000, 1000, 3000), failer) diff --git a/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentQSpec.scala b/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentQSpec.scala index 2e5b5a507e..23b62f2963 100644 --- a/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentQSpec.scala +++ b/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentQSpec.scala @@ -5,7 +5,8 @@ import junit.framework.TestCase import org.junit.{Test, Before} import org.junit.Assert._ -import se.scalablesolutions.akka.actor.{Actor, Transactor} +import se.scalablesolutions.akka.actor.{Actor, ActorID, Transactor} +import Actor._ /** * A persistent actor based on Redis queue storage. @@ -16,7 +17,7 @@ import se.scalablesolutions.akka.actor.{Actor, Transactor} case class NQ(accountNo: String) case object DQ -case class MNDQ(accountNos: List[String], noOfDQs: Int, failer: Actor) +case class MNDQ(accountNos: List[String], noOfDQs: Int, failer: ActorID) case object SZ class QueueActor extends Transactor { @@ -53,7 +54,7 @@ class QueueActor extends Transactor { class RedisPersistentQSpec extends TestCase { @Test def testSuccessfulNQ = { - val qa = new QueueActor + val qa = newActor[QueueActor] qa.start qa !! NQ("a-123") qa !! NQ("a-124") @@ -64,7 +65,7 @@ class RedisPersistentQSpec extends TestCase { @Test def testSuccessfulDQ = { - val qa = new QueueActor + val qa = newActor[QueueActor] qa.start qa !! NQ("a-123") qa !! NQ("a-124") @@ -80,9 +81,9 @@ class RedisPersistentQSpec extends TestCase { @Test def testSuccessfulMNDQ = { - val qa = new QueueActor + val qa = newActor[QueueActor] qa.start - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start qa !! NQ("a-123") @@ -100,9 +101,9 @@ class RedisPersistentQSpec extends TestCase { @Test def testMixedMNDQ = { - val qa = new QueueActor + val qa = newActor[QueueActor] qa.start - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start // 3 enqueues diff --git a/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentSortedSetSpec.scala b/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentSortedSetSpec.scala index a871b616ef..a56e916937 100644 --- a/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentSortedSetSpec.scala +++ b/akka-persistence/akka-persistence-redis/src/test/scala/RedisPersistentSortedSetSpec.scala @@ -7,7 +7,8 @@ import org.scalatest.BeforeAndAfterAll import org.scalatest.junit.JUnitRunner import org.junit.runner.RunWith -import se.scalablesolutions.akka.actor.{Actor, Transactor} +import se.scalablesolutions.akka.actor.{Actor, ActorID, Transactor} +import Actor._ /** * A persistent actor based on Redis sortedset storage. @@ -42,7 +43,7 @@ case class SCORE(h: Hacker) case class RANGE(start: Int, end: Int) // add and remove subject to the condition that there will be at least 3 hackers -case class MULTI(add: List[Hacker], rem: List[Hacker], failer: Actor) +case class MULTI(add: List[Hacker], rem: List[Hacker], failer: ActorID) class SortedSetActor extends Transactor { timeout = 100000 @@ -110,7 +111,7 @@ class RedisPersistentSortedSetSpec extends val h6 = Hacker("Alan Turing", "1912") describe("Add and report cardinality of the set") { - val qa = new SortedSetActor + val qa = newActor[SortedSetActor] qa.start it("should enter 6 hackers") { @@ -166,10 +167,10 @@ class RedisPersistentSortedSetSpec extends describe("Transaction semantics") { it("should rollback on exception") { - val qa = new SortedSetActor + val qa = newActor[SortedSetActor] qa.start - val failer = new PersistentFailerActor + val failer = newActor[PersistentFailerActor] failer.start (qa !! SIZE).get.asInstanceOf[Int] should equal(0) @@ -194,7 +195,7 @@ class RedisPersistentSortedSetSpec extends describe("zrange") { it ("should report proper range") { - val qa = new SortedSetActor + val qa = newActor[SortedSetActor] qa.start qa !! ADD(h1) qa !! ADD(h2) @@ -213,7 +214,7 @@ class RedisPersistentSortedSetSpec extends } it ("should report proper rge") { - val qa = new SortedSetActor + val qa = newActor[SortedSetActor] qa.start qa !! ADD(h1) qa !! ADD(h2) diff --git a/akka-security/src/test/scala/SecuritySpec.scala b/akka-security/src/test/scala/SecuritySpec.scala index 6e88335d22..6b62a4ec67 100644 --- a/akka-security/src/test/scala/SecuritySpec.scala +++ b/akka-security/src/test/scala/SecuritySpec.scala @@ -5,6 +5,7 @@ package se.scalablesolutions.akka.security import se.scalablesolutions.akka.config.ScalaConfig._ +import se.scalablesolutions.akka.actor.Actor._ import org.scalatest.Suite import org.scalatest.junit.JUnitSuite @@ -20,7 +21,7 @@ import com.sun.jersey.core.util.Base64 class BasicAuthenticatorSpec extends junit.framework.TestCase with Suite with MockitoSugar with MustMatchers { - val authenticator = new BasicAuthenticator + val authenticator = newActor[BasicAuthenticator] authenticator.start @Test def testChallenge = {