2009-06-29 15:01:20 +02:00
|
|
|
package se.scalablesolutions.akka.kernel.actor
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.locks.ReentrantLock
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
|
2009-06-30 16:01:50 +02:00
|
|
|
import junit.framework.TestCase
|
|
|
|
|
import kernel.Kernel
|
2009-06-29 15:01:20 +02:00
|
|
|
import kernel.reactor._
|
|
|
|
|
|
|
|
|
|
import kernel.state.{CassandraStorageConfig, TransactionalState}
|
|
|
|
|
import org.junit.{Test, Before}
|
|
|
|
|
import org.junit.Assert._
|
|
|
|
|
|
|
|
|
|
class PersistentActor extends Actor {
|
2009-07-12 23:08:17 +02:00
|
|
|
timeout = 100000
|
|
|
|
|
makeTransactionRequired
|
2009-06-29 15:01:20 +02:00
|
|
|
private val mapState = TransactionalState.newPersistentMap(CassandraStorageConfig())
|
|
|
|
|
private val vectorState = TransactionalState.newPersistentVector(CassandraStorageConfig())
|
|
|
|
|
private val refState = TransactionalState.newPersistentRef(CassandraStorageConfig())
|
|
|
|
|
|
|
|
|
|
def receive: PartialFunction[Any, Unit] = {
|
|
|
|
|
case GetMapState(key) =>
|
|
|
|
|
reply(mapState.get(key).get)
|
2009-07-03 17:53:33 +02:00
|
|
|
case GetVectorSize =>
|
|
|
|
|
reply(vectorState.length.asInstanceOf[AnyRef])
|
2009-06-29 15:01:20 +02:00
|
|
|
case GetRefState =>
|
|
|
|
|
reply(refState.get.get)
|
|
|
|
|
case SetMapState(key, msg) =>
|
|
|
|
|
mapState.put(key, msg)
|
|
|
|
|
reply(msg)
|
|
|
|
|
case SetVectorState(msg) =>
|
|
|
|
|
vectorState.add(msg)
|
|
|
|
|
reply(msg)
|
|
|
|
|
case SetRefState(msg) =>
|
|
|
|
|
refState.swap(msg)
|
|
|
|
|
reply(msg)
|
|
|
|
|
case Success(key, msg) =>
|
|
|
|
|
mapState.put(key, msg)
|
|
|
|
|
vectorState.add(msg)
|
|
|
|
|
refState.swap(msg)
|
|
|
|
|
reply(msg)
|
|
|
|
|
case Failure(key, msg, failer) =>
|
|
|
|
|
mapState.put(key, msg)
|
|
|
|
|
vectorState.add(msg)
|
|
|
|
|
refState.swap(msg)
|
|
|
|
|
failer !! "Failure"
|
|
|
|
|
reply(msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-23 20:01:37 +02:00
|
|
|
@serializable class PersistentFailerActor extends Actor {
|
2009-07-12 23:08:17 +02:00
|
|
|
makeTransactionRequired
|
2009-06-29 15:01:20 +02:00
|
|
|
def receive: PartialFunction[Any, Unit] = {
|
|
|
|
|
case "Failure" =>
|
|
|
|
|
throw new RuntimeException("expected")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object PersistenceManager {
|
|
|
|
|
@volatile var isRunning = false
|
|
|
|
|
def init = if (!isRunning) {
|
2009-07-29 17:01:13 +02:00
|
|
|
Kernel.startCassandra
|
2009-06-29 15:01:20 +02:00
|
|
|
isRunning = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-30 16:01:50 +02:00
|
|
|
class PersistentActorSpec extends TestCase {
|
2009-06-29 15:01:20 +02:00
|
|
|
PersistenceManager.init
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
def testMapShouldNotRollbackStateForStatefulServerInCaseOfSuccess = {
|
|
|
|
|
val stateful = new PersistentActor
|
|
|
|
|
stateful.start
|
|
|
|
|
stateful !! SetMapState("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "init") // set init state
|
2009-07-03 17:15:36 +02:00
|
|
|
stateful !! Success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state") // transactionrequired
|
2009-06-29 15:01:20 +02:00
|
|
|
assertEquals("new state", (stateful !! GetMapState("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess")).get)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
def testMapShouldRollbackStateForStatefulServerInCaseOfFailure = {
|
|
|
|
|
val stateful = new PersistentActor
|
|
|
|
|
stateful.start
|
|
|
|
|
stateful !! SetMapState("testShouldRollbackStateForStatefulServerInCaseOfFailure", "init") // set init state
|
|
|
|
|
val failer = new PersistentFailerActor
|
|
|
|
|
failer.start
|
|
|
|
|
try {
|
2009-07-03 17:15:36 +02:00
|
|
|
stateful !! Failure("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer) // call failing transactionrequired method
|
2009-06-29 15:01:20 +02:00
|
|
|
fail("should have thrown an exception")
|
|
|
|
|
} catch {case e: RuntimeException => {}}
|
|
|
|
|
assertEquals("init", (stateful !! GetMapState("testShouldRollbackStateForStatefulServerInCaseOfFailure")).get) // check that state is == init state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
def testVectorShouldNotRollbackStateForStatefulServerInCaseOfSuccess = {
|
|
|
|
|
val stateful = new PersistentActor
|
|
|
|
|
stateful.start
|
|
|
|
|
stateful !! SetVectorState("init") // set init state
|
2009-07-03 17:15:36 +02:00
|
|
|
stateful !! Success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state") // transactionrequired
|
2009-07-04 06:38:47 +02:00
|
|
|
assertEquals(2, (stateful !! GetVectorSize).get)
|
2009-06-29 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
def testVectorShouldRollbackStateForStatefulServerInCaseOfFailure = {
|
|
|
|
|
val stateful = new PersistentActor
|
|
|
|
|
stateful.start
|
|
|
|
|
stateful !! SetVectorState("init") // set init state
|
|
|
|
|
val failer = new PersistentFailerActor
|
|
|
|
|
failer.start
|
|
|
|
|
try {
|
2009-07-03 17:15:36 +02:00
|
|
|
stateful !! Failure("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer) // call failing transactionrequired method
|
2009-06-29 15:01:20 +02:00
|
|
|
fail("should have thrown an exception")
|
|
|
|
|
} catch {case e: RuntimeException => {}}
|
2009-07-03 17:53:33 +02:00
|
|
|
assertEquals(1, (stateful !! GetVectorSize).get)
|
2009-06-29 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
def testRefShouldNotRollbackStateForStatefulServerInCaseOfSuccess = {
|
|
|
|
|
val stateful = new PersistentActor
|
|
|
|
|
stateful.start
|
|
|
|
|
stateful !! SetRefState("init") // set init state
|
2009-07-03 17:15:36 +02:00
|
|
|
stateful !! Success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state") // transactionrequired
|
2009-06-29 15:01:20 +02:00
|
|
|
assertEquals("new state", (stateful !! GetRefState).get)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
def testRefShouldRollbackStateForStatefulServerInCaseOfFailure = {
|
|
|
|
|
val stateful = new PersistentActor
|
|
|
|
|
stateful.start
|
|
|
|
|
stateful !! SetRefState("init") // set init state
|
|
|
|
|
val failer = new PersistentFailerActor
|
|
|
|
|
failer.start
|
|
|
|
|
try {
|
2009-07-03 17:15:36 +02:00
|
|
|
stateful !! Failure("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer) // call failing transactionrequired method
|
2009-06-29 15:01:20 +02:00
|
|
|
fail("should have thrown an exception")
|
|
|
|
|
} catch {case e: RuntimeException => {}}
|
|
|
|
|
assertEquals("init", (stateful !! GetRefState).get) // check that state is == init state
|
|
|
|
|
}
|
2009-07-29 17:01:13 +02:00
|
|
|
}
|