From d766dfce3be782eee5a47c39fffadf5d4e8d6823 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Thu, 5 Aug 2010 17:28:07 +0200 Subject: [PATCH] Added unit test to test for race-condition in ActorRegistry --- .../test/scala/misc/ActorRegistrySpec.scala | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/akka-core/src/test/scala/misc/ActorRegistrySpec.scala b/akka-core/src/test/scala/misc/ActorRegistrySpec.scala index d6fc463a65..61626e9db3 100644 --- a/akka-core/src/test/scala/misc/ActorRegistrySpec.scala +++ b/akka-core/src/test/scala/misc/ActorRegistrySpec.scala @@ -3,6 +3,7 @@ package se.scalablesolutions.akka.actor import org.scalatest.junit.JUnitSuite import org.junit.Test import Actor._ +import java.util.concurrent.{CyclicBarrier, TimeUnit, CountDownLatch} object ActorRegistrySpec { var record = "" @@ -214,4 +215,41 @@ class ActorRegistrySpec extends JUnitSuite { ActorRegistry.unregister(actor2) assert(ActorRegistry.actors.size === 0) } + + @Test def shouldBeAbleToRegisterActorsConcurrently { + ActorRegistry.shutdownAll + + val latch = new CountDownLatch(3) + val barrier = new CyclicBarrier(3) + + def mkTestActor(i:Int) = actorOf( new Actor { + self.id = i.toString + def receive = { case _ => } + }) + + def mkTestActors = for(i <- 1 to 10;j <- 1 to 1000) yield mkTestActor(i) + + def mkThread(actors: Iterable[ActorRef]) = new Thread { + start + override def run { + barrier.await + actors foreach { _.start } + latch.countDown + } + } + + val testActors1 = mkTestActors + val testActors2 = mkTestActors + val testActors3 = mkTestActors + + mkThread(testActors1) + mkThread(testActors2) + mkThread(testActors3) + + assert(latch.await(30,TimeUnit.SECONDS) === true) + + for(i <- 1 to 10) { + assert(ActorRegistry.actorsFor(i.toString).length === 3000) + } + } }