second iteration of STM done, simple tests work now
This commit is contained in:
parent
2639d14e1a
commit
cd1ef83e49
40 changed files with 4102 additions and 3215 deletions
|
|
@ -1,188 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009 Scalable Solutions.
|
||||
*/
|
||||
|
||||
package se.scalablesolutions.akka.kernel
|
||||
|
||||
import org.scalatest.Spec
|
||||
import org.scalatest.matchers.ShouldMatchers
|
||||
import org.scalatest.junit.JUnit3Suite
|
||||
|
||||
import se.scalablesolutions.akka.annotation.{oneway, transactional, stateful}
|
||||
|
||||
/**
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
object ActiveObjectSpec {
|
||||
var messageLog = ""
|
||||
}
|
||||
class ActiveObjectSpec extends Spec with ShouldMatchers {
|
||||
|
||||
describe("An ActiveObject") {
|
||||
|
||||
it("(with default supervisor) should dispatch method calls normally") {
|
||||
val foo = ActiveObject.newInstance[Foo](classOf[Foo], new FooImpl, 1000)
|
||||
|
||||
val result = foo.foo("foo ")
|
||||
ActiveObjectSpec.messageLog += result
|
||||
|
||||
foo.bar("bar ")
|
||||
ActiveObjectSpec.messageLog += "before_bar "
|
||||
|
||||
Thread.sleep(500)
|
||||
ActiveObjectSpec.messageLog should equal ("foo return_foo before_bar bar ")
|
||||
}
|
||||
|
||||
it("should not rollback state for a stateful server in case of success") {
|
||||
val stateful = ActiveObject.newInstance[Stateful](classOf[Stateful], new StatefulImpl, 1000)
|
||||
|
||||
stateful.success("new state")
|
||||
stateful.state should equal ("new state")
|
||||
}
|
||||
|
||||
it("should rollback state for a stateful server in case of failure") {
|
||||
val stateful = ActiveObject.newInstance[Stateful](classOf[Stateful], new StatefulImpl, 1000)
|
||||
val failer = ActiveObject.newInstance[Failer](classOf[Failer], new FailerImpl, 1000)
|
||||
|
||||
stateful.failure("new state", failer)
|
||||
stateful.state should equal ("nil")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait Foo {
|
||||
def foo(msg: String): String
|
||||
@transactional def fooInTx(msg: String): String
|
||||
@oneway def bar(msg: String)
|
||||
def longRunning
|
||||
def throwsException
|
||||
}
|
||||
|
||||
class FooImpl extends Foo {
|
||||
val bar: Bar = new BarImpl
|
||||
def foo(msg: String): String = {
|
||||
ActiveObjectSpec.messageLog += msg
|
||||
"return_foo "
|
||||
}
|
||||
def fooInTx(msg: String): String = {
|
||||
ActiveObjectSpec.messageLog += msg
|
||||
"return_foo "
|
||||
}
|
||||
def bar(msg: String) = bar.bar(msg)
|
||||
def longRunning = Thread.sleep(10000)
|
||||
def throwsException = error("expected")
|
||||
}
|
||||
|
||||
trait Bar {
|
||||
@oneway def bar(msg: String)
|
||||
}
|
||||
|
||||
class BarImpl extends Bar {
|
||||
def bar(msg: String) = {
|
||||
Thread.sleep(100)
|
||||
ActiveObjectSpec.messageLog += msg
|
||||
}
|
||||
}
|
||||
|
||||
trait Stateful {
|
||||
@transactional def success(msg: String)
|
||||
@transactional def failure(msg: String, failer: Failer)
|
||||
def state: String
|
||||
}
|
||||
|
||||
@stateful
|
||||
class StatefulImpl extends Stateful {
|
||||
var state: String = "nil"
|
||||
def success(msg: String) = state = msg
|
||||
def failure(msg: String, failer: Failer) = {
|
||||
state = msg
|
||||
failer.fail
|
||||
}
|
||||
}
|
||||
|
||||
trait Failer {
|
||||
def fail
|
||||
}
|
||||
|
||||
class FailerImpl extends Failer {
|
||||
def fail = throw new RuntimeException("expected")
|
||||
}
|
||||
|
||||
// @Test { val groups=Array("unit") }
|
||||
// def testCreateGenericServerBasedComponentUsingCustomSupervisorConfiguration = {
|
||||
// val proxy = new ActiveObjectProxy(new FooImpl, 1000)
|
||||
|
||||
// val supervisor =
|
||||
// ActiveObject.supervise(
|
||||
// RestartStrategy(AllForOne, 3, 100),
|
||||
// Component(
|
||||
// proxy,
|
||||
// LifeCycle(Permanent, 100))
|
||||
// :: Nil)
|
||||
|
||||
// val foo = ActiveObject.newInstance[Foo](classOf[Foo], proxy)
|
||||
|
||||
// val result = foo.foo("foo ")
|
||||
// messageLog += result
|
||||
|
||||
// foo.bar("bar ")
|
||||
// messageLog += "before_bar "
|
||||
|
||||
// Thread.sleep(500)
|
||||
// assert(messageLog === "foo return_foo before_bar bar ")
|
||||
|
||||
// supervisor ! Stop
|
||||
// }
|
||||
|
||||
// @Test { val groups=Array("unit") }
|
||||
// def testCreateTwoGenericServerBasedComponentUsingCustomSupervisorConfiguration = {
|
||||
// val fooProxy = new ActiveObjectProxy(new FooImpl, 1000)
|
||||
// val barProxy = new ActiveObjectProxy(new BarImpl, 1000)
|
||||
|
||||
// val supervisor =
|
||||
// ActiveObject.supervise(
|
||||
// RestartStrategy(AllForOne, 3, 100),
|
||||
// Component(
|
||||
// fooProxy,
|
||||
// LifeCycle(Permanent, 100)) ::
|
||||
// Component(
|
||||
// barProxy,
|
||||
// LifeCycle(Permanent, 100))
|
||||
// :: Nil)
|
||||
|
||||
// val foo = ActiveObject.newInstance[Foo](classOf[Foo], fooProxy)
|
||||
// val bar = ActiveObject.newInstance[Bar](classOf[Bar], barProxy)
|
||||
|
||||
// val result = foo.foo("foo ")
|
||||
// messageLog += result
|
||||
|
||||
// bar.bar("bar ")
|
||||
// messageLog += "before_bar "
|
||||
|
||||
// Thread.sleep(500)
|
||||
// assert(messageLog === "foo return_foo before_bar bar ")
|
||||
|
||||
// supervisor ! Stop
|
||||
// }
|
||||
|
||||
// @Test { val groups=Array("unit") }
|
||||
// def testCreateGenericServerBasedComponentUsingDefaultSupervisorAndForcedTimeout = {
|
||||
// val foo = ActiveObject.newInstance[Foo](classOf[Foo], new FooImpl, 1000)
|
||||
// intercept(classOf[ActiveObjectInvocationTimeoutException]) {
|
||||
// foo.longRunning
|
||||
// }
|
||||
// assert(true === true)
|
||||
// }
|
||||
|
||||
// @Test { val groups=Array("unit") }
|
||||
// def testCreateGenericServerBasedComponentUsingDefaultSupervisorAndForcedException = {
|
||||
// val foo = ActiveObject.newInstance[Foo](classOf[Foo], new FooImpl, 10000)
|
||||
// intercept(classOf[RuntimeException]) {
|
||||
// foo.throwsException
|
||||
// }
|
||||
// assert(true === true)
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
|
@ -10,9 +10,14 @@ import org.scalatest._
|
|||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
class AllSuite extends SuperSuite(
|
||||
|
||||
List(
|
||||
new ActiveObjectSpec,
|
||||
new RestManagerSpec
|
||||
new SupervisorSpec,
|
||||
new SupervisorStateSpec,
|
||||
new GenericServerSpec,
|
||||
new GenericServerContainerSpec
|
||||
// new ActiveObjectSpec,
|
||||
// new RestManagerSpec
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
|||
212
kernel/src/test/scala/GenericServerContainerSuite.scala
Executable file
212
kernel/src/test/scala/GenericServerContainerSuite.scala
Executable file
|
|
@ -0,0 +1,212 @@
|
|||
/**
|
||||
* Copyright (C) 2009 Scalable Solutions.
|
||||
*/
|
||||
|
||||
package se.scalablesolutions.akka.kernel
|
||||
|
||||
import scala.actors._
|
||||
import scala.actors.Actor._
|
||||
|
||||
import org.scalatest._
|
||||
|
||||
/**
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
class GenericServerContainerSpec extends Suite {
|
||||
|
||||
var inner: GenericServerContainerActor = null
|
||||
var server: GenericServerContainer = null
|
||||
def createProxy(f: () => GenericServer) = { val server = new GenericServerContainer("server", f); server.setTimeout(100); server }
|
||||
|
||||
def setup = {
|
||||
inner = new GenericServerContainerActor
|
||||
server = createProxy(() => inner)
|
||||
server.newServer
|
||||
server.start
|
||||
}
|
||||
|
||||
def testInit = {
|
||||
setup
|
||||
server.init("testInit")
|
||||
Thread.sleep(100)
|
||||
expect("initializing: testInit") {
|
||||
inner.log
|
||||
}
|
||||
}
|
||||
|
||||
def testTerminateWithReason = {
|
||||
setup
|
||||
server.terminate("testTerminateWithReason", 100)
|
||||
Thread.sleep(100)
|
||||
expect("terminating: testTerminateWithReason") {
|
||||
inner.log
|
||||
}
|
||||
}
|
||||
|
||||
def test_bang_1 = {
|
||||
setup
|
||||
server ! OneWay
|
||||
Thread.sleep(100)
|
||||
expect("got a oneway") {
|
||||
inner.log
|
||||
}
|
||||
}
|
||||
|
||||
def test_bang_2 = {
|
||||
setup
|
||||
server ! Ping
|
||||
Thread.sleep(100)
|
||||
expect("got a ping") {
|
||||
inner.log
|
||||
}
|
||||
}
|
||||
|
||||
def test_bangbangbang = {
|
||||
setup
|
||||
expect("pong") {
|
||||
(server !!! Ping).getOrElse("nil")
|
||||
}
|
||||
expect("got a ping") {
|
||||
inner.log
|
||||
}
|
||||
}
|
||||
|
||||
def test_bangquestion = {
|
||||
setup
|
||||
expect("pong") {
|
||||
val res: String = server !? Ping
|
||||
res
|
||||
}
|
||||
expect("got a ping") {
|
||||
inner.log
|
||||
}
|
||||
}
|
||||
|
||||
def test_bangbangbang_Timeout1 = {
|
||||
setup
|
||||
expect("pong") {
|
||||
(server !!! Ping).getOrElse("nil")
|
||||
}
|
||||
expect("got a ping") {
|
||||
inner.log
|
||||
}
|
||||
}
|
||||
|
||||
def test_bangbangbang_Timeout2 = {
|
||||
setup
|
||||
expect("error handler") {
|
||||
server !!! (OneWay, "error handler")
|
||||
}
|
||||
expect("got a oneway") {
|
||||
inner.log
|
||||
}
|
||||
}
|
||||
|
||||
def test_bangbangbang_GetFutureTimeout1 = {
|
||||
setup
|
||||
val future = server !! Ping
|
||||
future.receiveWithin(100) match {
|
||||
case None => fail("timed out") // timed out
|
||||
case Some(reply) =>
|
||||
expect("got a ping") {
|
||||
inner.log
|
||||
}
|
||||
assert("pong" === reply)
|
||||
}
|
||||
}
|
||||
|
||||
def test_bangbangbang_GetFutureTimeout2 = {
|
||||
setup
|
||||
val future = server !! OneWay
|
||||
future.receiveWithin(100) match {
|
||||
case None =>
|
||||
expect("got a oneway") {
|
||||
inner.log
|
||||
}
|
||||
case Some(reply) =>
|
||||
fail("expected a timeout, got Some(reply)")
|
||||
}
|
||||
}
|
||||
|
||||
def testHotSwap = {
|
||||
setup
|
||||
// using base
|
||||
expect("pong") {
|
||||
(server !!! Ping).getOrElse("nil")
|
||||
}
|
||||
|
||||
// hotswapping
|
||||
server.hotswap(Some({
|
||||
case Ping => reply("hotswapped pong")
|
||||
}))
|
||||
expect("hotswapped pong") {
|
||||
(server !!! Ping).getOrElse("nil")
|
||||
}
|
||||
}
|
||||
|
||||
def testDoubleHotSwap = {
|
||||
setup
|
||||
// using base
|
||||
expect("pong") {
|
||||
(server !!! Ping).getOrElse("nil")
|
||||
}
|
||||
|
||||
// hotswapping
|
||||
server.hotswap(Some({
|
||||
case Ping => reply("hotswapped pong")
|
||||
}))
|
||||
expect("hotswapped pong") {
|
||||
(server !!! Ping).getOrElse("nil")
|
||||
}
|
||||
|
||||
// hotswapping again
|
||||
server.hotswap(Some({
|
||||
case Ping => reply("hotswapped pong again")
|
||||
}))
|
||||
expect("hotswapped pong again") {
|
||||
(server !!! Ping).getOrElse("nil")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def testHotSwapReturnToBase = {
|
||||
setup
|
||||
// using base
|
||||
expect("pong") {
|
||||
(server !!! Ping).getOrElse("nil")
|
||||
}
|
||||
|
||||
// hotswapping
|
||||
server.hotswap(Some({
|
||||
case Ping => reply("hotswapped pong")
|
||||
}))
|
||||
expect("hotswapped pong") {
|
||||
(server !!! Ping).getOrElse("nil")
|
||||
}
|
||||
|
||||
// restoring original base
|
||||
server.hotswap(None)
|
||||
expect("pong") {
|
||||
(server !!! Ping).getOrElse("nil")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class GenericServerContainerActor extends GenericServer {
|
||||
var log = ""
|
||||
|
||||
override def body: PartialFunction[Any, Unit] = {
|
||||
case Ping =>
|
||||
log = "got a ping"
|
||||
reply("pong")
|
||||
|
||||
case OneWay =>
|
||||
log = "got a oneway"
|
||||
}
|
||||
|
||||
override def init(config: AnyRef) = log = "initializing: " + config
|
||||
override def shutdown(reason: AnyRef) = log = "terminating: " + reason
|
||||
}
|
||||
|
||||
|
||||
37
kernel/src/test/scala/GenericServerSpec.scala
Executable file
37
kernel/src/test/scala/GenericServerSpec.scala
Executable file
|
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* Copyright (C) 2009 Scalable Solutions.
|
||||
*/
|
||||
|
||||
package se.scalablesolutions.akka.kernel
|
||||
|
||||
import org.scalatest._
|
||||
|
||||
import scala.actors.Actor._
|
||||
|
||||
/**
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
class GenericServerSpec extends Suite {
|
||||
|
||||
def testSendRegularMessage = {
|
||||
val server = new MyGenericServerActor
|
||||
server.start
|
||||
server !? Ping match {
|
||||
case reply: String =>
|
||||
assert("got a ping" === server.log)
|
||||
assert("pong" === reply)
|
||||
case _ => fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MyGenericServerActor extends GenericServer {
|
||||
var log: String = ""
|
||||
|
||||
override def body: PartialFunction[Any, Unit] = {
|
||||
case Ping =>
|
||||
log = "got a ping"
|
||||
reply("pong")
|
||||
}
|
||||
}
|
||||
|
||||
12
kernel/src/test/scala/Messages.scala
Executable file
12
kernel/src/test/scala/Messages.scala
Executable file
|
|
@ -0,0 +1,12 @@
|
|||
/**
|
||||
* Copyright (C) 2009 Scalable Solutions.
|
||||
*/
|
||||
|
||||
package se.scalablesolutions.akka.kernel
|
||||
|
||||
sealed abstract class TestMessage
|
||||
case object Ping extends TestMessage
|
||||
case object Pong extends TestMessage
|
||||
case object OneWay extends TestMessage
|
||||
case object Die extends TestMessage
|
||||
case object NotifySupervisorExit extends TestMessage
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009 Scalable Solutions.
|
||||
*/
|
||||
|
||||
package se.scalablesolutions.akka.kernel
|
||||
|
||||
import org.scalatest.Spec
|
||||
import org.scalatest.matchers.ShouldMatchers
|
||||
|
||||
import javax.ws.rs.{Produces, Path, GET}
|
||||
|
||||
//import com.sun.net.httpserver.HttpServer;
|
||||
//import com.sun.ws.rest.api.client.Client;
|
||||
//import com.sun.ws.rest.api.client.ClientResponse;
|
||||
//import com.sun.ws.rest.api.client.ResourceProxy;
|
||||
|
||||
/**
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
class RestManagerSpec extends Spec with ShouldMatchers {
|
||||
|
||||
describe("A RestManager") {
|
||||
|
||||
it("should be able to start and stop") {
|
||||
val threadSelector = Kernel.startJersey
|
||||
/* val cc = new DefaultClientConfig
|
||||
val c = Client.create(cc)
|
||||
val resource = c.proxy("http://localhost:9998/")
|
||||
val hello = resource.get(classOf[HelloWorldResource])
|
||||
val msg = hello.getMessage
|
||||
println("=============: " + msg)
|
||||
*/ threadSelector.stopEndpoint
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Path("/helloworld")
|
||||
class HelloWorldResource {
|
||||
@GET
|
||||
@Produces(Array("text/plain"))
|
||||
def getMessage = "Hello World"
|
||||
}
|
||||
430
kernel/src/test/scala/SupervisorSpec.scala
Executable file
430
kernel/src/test/scala/SupervisorSpec.scala
Executable file
|
|
@ -0,0 +1,430 @@
|
|||
/**
|
||||
* Copyright (C) 2009 Scalable Solutions.
|
||||
*/
|
||||
|
||||
package se.scalablesolutions.akka.kernel
|
||||
|
||||
import scala.actors._
|
||||
import scala.actors.Actor._
|
||||
import scala.collection.Map
|
||||
import scala.collection.mutable.HashMap
|
||||
|
||||
import org.scalatest._
|
||||
|
||||
/**
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
class SupervisorSpec extends Suite {
|
||||
|
||||
var messageLog: String = ""
|
||||
val pingpong1 = new GenericServerContainer("pingpong1", () => new PingPong1Actor)
|
||||
val pingpong2 = new GenericServerContainer("pingpong2", () => new PingPong2Actor)
|
||||
val pingpong3 = new GenericServerContainer("pingpong3", () => new PingPong3Actor)
|
||||
|
||||
pingpong1.setTimeout(100)
|
||||
pingpong2.setTimeout(100)
|
||||
pingpong3.setTimeout(100)
|
||||
|
||||
def testStartServer = {
|
||||
messageLog = ""
|
||||
val sup = getSingleActorAllForOneSupervisor
|
||||
sup ! Start
|
||||
|
||||
expect("pong") {
|
||||
(pingpong1 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
}
|
||||
|
||||
def testGetServer = {
|
||||
messageLog = ""
|
||||
val sup = getSingleActorAllForOneSupervisor
|
||||
sup ! Start
|
||||
val server = sup.getServerOrElse("pingpong1", throw new RuntimeException("server not found"))
|
||||
assert(server.isInstanceOf[GenericServerContainer])
|
||||
assert(server === pingpong1)
|
||||
}
|
||||
|
||||
def testGetServerOrFail = {
|
||||
messageLog = ""
|
||||
val sup = getSingleActorAllForOneSupervisor
|
||||
sup ! Start
|
||||
intercept(classOf[RuntimeException]) {
|
||||
sup.getServerOrElse("wrong_name", throw new RuntimeException("server not found"))
|
||||
}
|
||||
}
|
||||
|
||||
def testKillSingleActorOneForOne = {
|
||||
messageLog = ""
|
||||
val sup = getSingleActorOneForOneSupervisor
|
||||
sup ! Start
|
||||
|
||||
intercept(classOf[RuntimeException]) {
|
||||
pingpong1 !!! (Die, throw new RuntimeException("TIME OUT"))
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("oneforone") {
|
||||
messageLog
|
||||
}
|
||||
}
|
||||
|
||||
def testCallKillCallSingleActorOneForOne = {
|
||||
messageLog = ""
|
||||
val sup = getSingleActorOneForOneSupervisor
|
||||
sup ! Start
|
||||
|
||||
expect("pong") {
|
||||
(pingpong1 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("ping") {
|
||||
messageLog
|
||||
}
|
||||
intercept(classOf[RuntimeException]) {
|
||||
pingpong1 !!! (Die, throw new RuntimeException("TIME OUT"))
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pingoneforone") {
|
||||
messageLog
|
||||
}
|
||||
expect("pong") {
|
||||
(pingpong1 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pingoneforoneping") {
|
||||
messageLog
|
||||
}
|
||||
}
|
||||
|
||||
def testKillSingleActorAllForOne = {
|
||||
messageLog = ""
|
||||
val sup = getSingleActorAllForOneSupervisor
|
||||
sup ! Start
|
||||
intercept(classOf[RuntimeException]) {
|
||||
pingpong1 !!! (Die, throw new RuntimeException("TIME OUT"))
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("allforone") {
|
||||
messageLog
|
||||
}
|
||||
}
|
||||
|
||||
def testCallKillCallSingleActorAllForOne = {
|
||||
messageLog = ""
|
||||
val sup = getSingleActorAllForOneSupervisor
|
||||
sup ! Start
|
||||
expect("pong") {
|
||||
(pingpong1 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("ping") {
|
||||
messageLog
|
||||
}
|
||||
intercept(classOf[RuntimeException]) {
|
||||
pingpong1 !!! (Die, throw new RuntimeException("TIME OUT"))
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pingallforone") {
|
||||
messageLog
|
||||
}
|
||||
expect("pong") {
|
||||
(pingpong1 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pingallforoneping") {
|
||||
messageLog
|
||||
}
|
||||
}
|
||||
|
||||
def testKillMultipleActorsOneForOne = {
|
||||
messageLog = ""
|
||||
val sup = getMultipleActorsOneForOneConf
|
||||
sup ! Start
|
||||
intercept(classOf[RuntimeException]) {
|
||||
pingpong3 !!! (Die, throw new RuntimeException("TIME OUT"))
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("oneforone") {
|
||||
messageLog
|
||||
}
|
||||
}
|
||||
|
||||
def tesCallKillCallMultipleActorsOneForOne = {
|
||||
messageLog = ""
|
||||
val sup = getMultipleActorsOneForOneConf
|
||||
sup ! Start
|
||||
expect("pong") {
|
||||
(pingpong1 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pong") {
|
||||
(pingpong2 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pong") {
|
||||
(pingpong3 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pingpingping") {
|
||||
messageLog
|
||||
}
|
||||
intercept(classOf[RuntimeException]) {
|
||||
pingpong2 !!! (Die, throw new RuntimeException("TIME OUT"))
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pingpingpingoneforone") {
|
||||
messageLog
|
||||
}
|
||||
expect("pong") {
|
||||
(pingpong1 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pong") {
|
||||
(pingpong2 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pong") {
|
||||
(pingpong3 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pingpingpingoneforonepingpingping") {
|
||||
messageLog
|
||||
}
|
||||
}
|
||||
|
||||
def testKillMultipleActorsAllForOne = {
|
||||
messageLog = ""
|
||||
val sup = getMultipleActorsAllForOneConf
|
||||
sup ! Start
|
||||
intercept(classOf[RuntimeException]) {
|
||||
pingpong2 !!! (Die, throw new RuntimeException("TIME OUT"))
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("allforoneallforoneallforone") {
|
||||
messageLog
|
||||
}
|
||||
}
|
||||
|
||||
def tesCallKillCallMultipleActorsAllForOne = {
|
||||
messageLog = ""
|
||||
val sup = getMultipleActorsAllForOneConf
|
||||
sup ! Start
|
||||
expect("pong") {
|
||||
(pingpong1 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pong") {
|
||||
(pingpong2 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pong") {
|
||||
(pingpong3 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pingpingping") {
|
||||
messageLog
|
||||
}
|
||||
intercept(classOf[RuntimeException]) {
|
||||
pingpong2 !!! (Die, throw new RuntimeException("TIME OUT"))
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pingpingpingallforoneallforoneallforone") {
|
||||
messageLog
|
||||
}
|
||||
expect("pong") {
|
||||
(pingpong1 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pong") {
|
||||
(pingpong2 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pong") {
|
||||
(pingpong3 !!! Ping).getOrElse("nil")
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("pingpingpingallforoneallforoneallforonepingpingping") {
|
||||
messageLog
|
||||
}
|
||||
}
|
||||
|
||||
def testTerminateFirstLevelActorAllForOne = {
|
||||
messageLog = ""
|
||||
val sup = getNestedSupervisorsAllForOneConf
|
||||
sup ! Start
|
||||
intercept(classOf[RuntimeException]) {
|
||||
pingpong1 !!! (Die, throw new RuntimeException("TIME OUT"))
|
||||
}
|
||||
Thread.sleep(100)
|
||||
expect("allforoneallforoneallforone") {
|
||||
messageLog
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// =============================================
|
||||
// Creat some supervisors with different configurations
|
||||
|
||||
def getSingleActorAllForOneSupervisor: Supervisor = {
|
||||
|
||||
// Create an abstract SupervisorContainer that works for all implementations
|
||||
// of the different Actors (Services).
|
||||
//
|
||||
// Then create a concrete container in which we mix in support for the specific
|
||||
// implementation of the Actors we want to use.
|
||||
|
||||
object factory extends TestSupervisorFactory {
|
||||
override def getSupervisorConfig: SupervisorConfig = {
|
||||
SupervisorConfig(
|
||||
RestartStrategy(AllForOne, 3, 100),
|
||||
Worker(
|
||||
pingpong1,
|
||||
LifeCycle(Permanent, 100))
|
||||
:: Nil)
|
||||
}
|
||||
}
|
||||
factory.newSupervisor
|
||||
}
|
||||
|
||||
def getSingleActorOneForOneSupervisor: Supervisor = {
|
||||
object factory extends TestSupervisorFactory {
|
||||
override def getSupervisorConfig: SupervisorConfig = {
|
||||
SupervisorConfig(
|
||||
RestartStrategy(OneForOne, 3, 100),
|
||||
Worker(
|
||||
pingpong1,
|
||||
LifeCycle(Permanent, 100))
|
||||
:: Nil)
|
||||
}
|
||||
}
|
||||
factory.newSupervisor
|
||||
}
|
||||
|
||||
def getMultipleActorsAllForOneConf: Supervisor = {
|
||||
object factory extends TestSupervisorFactory {
|
||||
override def getSupervisorConfig: SupervisorConfig = {
|
||||
SupervisorConfig(
|
||||
RestartStrategy(AllForOne, 3, 100),
|
||||
Worker(
|
||||
pingpong1,
|
||||
LifeCycle(Permanent, 100))
|
||||
::
|
||||
Worker(
|
||||
pingpong2,
|
||||
LifeCycle(Permanent, 100))
|
||||
::
|
||||
Worker(
|
||||
pingpong3,
|
||||
LifeCycle(Permanent, 100))
|
||||
:: Nil)
|
||||
}
|
||||
}
|
||||
factory.newSupervisor
|
||||
}
|
||||
|
||||
def getMultipleActorsOneForOneConf: Supervisor = {
|
||||
object factory extends TestSupervisorFactory {
|
||||
override def getSupervisorConfig: SupervisorConfig = {
|
||||
SupervisorConfig(
|
||||
RestartStrategy(OneForOne, 3, 100),
|
||||
Worker(
|
||||
pingpong1,
|
||||
LifeCycle(Permanent, 100))
|
||||
::
|
||||
Worker(
|
||||
pingpong2,
|
||||
LifeCycle(Permanent, 100))
|
||||
::
|
||||
Worker(
|
||||
pingpong3,
|
||||
LifeCycle(Permanent, 100))
|
||||
:: Nil)
|
||||
}
|
||||
}
|
||||
factory.newSupervisor
|
||||
}
|
||||
|
||||
def getNestedSupervisorsAllForOneConf: Supervisor = {
|
||||
object factory extends TestSupervisorFactory {
|
||||
override def getSupervisorConfig: SupervisorConfig = {
|
||||
SupervisorConfig(
|
||||
RestartStrategy(AllForOne, 3, 100),
|
||||
Worker(
|
||||
pingpong1,
|
||||
LifeCycle(Permanent, 100))
|
||||
::
|
||||
SupervisorConfig(
|
||||
RestartStrategy(AllForOne, 3, 100),
|
||||
Worker(
|
||||
pingpong2,
|
||||
LifeCycle(Permanent, 100))
|
||||
::
|
||||
Worker(
|
||||
pingpong3,
|
||||
LifeCycle(Permanent, 100))
|
||||
:: Nil)
|
||||
:: Nil)
|
||||
}
|
||||
}
|
||||
factory.newSupervisor
|
||||
}
|
||||
|
||||
class PingPong1Actor extends GenericServer {
|
||||
override def body: PartialFunction[Any, Unit] = {
|
||||
case Ping =>
|
||||
messageLog += "ping"
|
||||
reply("pong")
|
||||
case Die =>
|
||||
throw new RuntimeException("Recieved Die message")
|
||||
}
|
||||
}
|
||||
|
||||
class PingPong2Actor extends GenericServer {
|
||||
override def body: PartialFunction[Any, Unit] = {
|
||||
case Ping =>
|
||||
messageLog += "ping"
|
||||
reply("pong")
|
||||
case Die =>
|
||||
throw new RuntimeException("Recieved Die message")
|
||||
}
|
||||
}
|
||||
|
||||
class PingPong3Actor extends GenericServer {
|
||||
override def body: PartialFunction[Any, Unit] = {
|
||||
case Ping =>
|
||||
messageLog += "ping"
|
||||
reply("pong")
|
||||
case Die =>
|
||||
throw new RuntimeException("Recieved Die message")
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================
|
||||
|
||||
class TestAllForOneStrategy(maxNrOfRetries: Int, withinTimeRange: Int) extends AllForOneStrategy(maxNrOfRetries, withinTimeRange) {
|
||||
override def postRestart(serverContainer: GenericServerContainer) = {
|
||||
messageLog += "allforone"
|
||||
}
|
||||
}
|
||||
|
||||
class TestOneForOneStrategy(maxNrOfRetries: Int, withinTimeRange: Int) extends OneForOneStrategy(maxNrOfRetries, withinTimeRange) {
|
||||
override def postRestart(serverContainer: GenericServerContainer) = {
|
||||
messageLog += "oneforone"
|
||||
}
|
||||
}
|
||||
|
||||
abstract class TestSupervisorFactory extends SupervisorFactory {
|
||||
override def create(strategy: RestartStrategy): Supervisor = strategy match {
|
||||
case RestartStrategy(scheme, maxNrOfRetries, timeRange) =>
|
||||
scheme match {
|
||||
case AllForOne => new Supervisor(new TestAllForOneStrategy(maxNrOfRetries, timeRange))
|
||||
case OneForOne => new Supervisor(new TestOneForOneStrategy(maxNrOfRetries, timeRange))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
92
kernel/src/test/scala/SupervisorStateSpec.scala
Executable file
92
kernel/src/test/scala/SupervisorStateSpec.scala
Executable file
|
|
@ -0,0 +1,92 @@
|
|||
/**
|
||||
* Copyright (C) 2009 Scalable Solutions.
|
||||
*/
|
||||
|
||||
package se.scalablesolutions.akka.kernel
|
||||
|
||||
import org.scalatest._
|
||||
|
||||
import scala.actors.Actor._
|
||||
|
||||
/**
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
class SupervisorStateSpec extends Suite {
|
||||
|
||||
val dummyActor = new GenericServer { override def body: PartialFunction[Any, Unit] = { case _ => }}
|
||||
val newDummyActor = () => dummyActor
|
||||
var state: SupervisorState = _
|
||||
var proxy: GenericServerContainer = _
|
||||
var supervisor: Supervisor = _
|
||||
|
||||
def setup = {
|
||||
proxy = new GenericServerContainer("server1", newDummyActor)
|
||||
object factory extends SupervisorFactory {
|
||||
override def getSupervisorConfig: SupervisorConfig = {
|
||||
SupervisorConfig(
|
||||
RestartStrategy(AllForOne, 3, 100),
|
||||
Worker(
|
||||
proxy,
|
||||
LifeCycle(Permanent, 100))
|
||||
:: Nil)
|
||||
}
|
||||
}
|
||||
supervisor = factory.newSupervisor
|
||||
state = new SupervisorState(supervisor, new AllForOneStrategy(3, 100))
|
||||
}
|
||||
|
||||
def testAddServer = {
|
||||
setup
|
||||
state.addServerContainer(proxy)
|
||||
state.getServerContainer("server1") match {
|
||||
case None => fail("should have returned server")
|
||||
case Some(server) =>
|
||||
assert(server != null)
|
||||
assert(server.isInstanceOf[GenericServerContainer])
|
||||
assert(proxy === server)
|
||||
}
|
||||
}
|
||||
|
||||
def testGetServer = {
|
||||
setup
|
||||
state.addServerContainer(proxy)
|
||||
state.getServerContainer("server1") match {
|
||||
case None => fail("should have returned server")
|
||||
case Some(server) =>
|
||||
assert(server != null)
|
||||
assert(server.isInstanceOf[GenericServerContainer])
|
||||
assert(proxy === server)
|
||||
}
|
||||
}
|
||||
|
||||
def testRemoveServer = {
|
||||
setup
|
||||
state.addServerContainer(proxy)
|
||||
|
||||
state.removeServerContainer("server1")
|
||||
state.getServerContainer("server1") match {
|
||||
case Some(_) => fail("should have returned None")
|
||||
case None =>
|
||||
}
|
||||
state.getServerContainer("dummyActor") match {
|
||||
case Some(_) => fail("should have returned None")
|
||||
case None =>
|
||||
}
|
||||
}
|
||||
|
||||
def testGetNonExistingServerBySymbol = {
|
||||
setup
|
||||
state.getServerContainer("server2") match {
|
||||
case Some(_) => fail("should have returned None")
|
||||
case None =>
|
||||
}
|
||||
}
|
||||
|
||||
def testGetNonExistingServerByActor = {
|
||||
setup
|
||||
state.getServerContainer("dummyActor") match {
|
||||
case Some(_) => fail("should have returned None")
|
||||
case None =>
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue