pekko/akka-core/src/test/scala/StmSpec.scala

182 lines
4.5 KiB
Scala
Raw Normal View History

2010-06-10 13:53:33 +12:00
package se.scalablesolutions.akka.stm
2010-06-10 13:53:33 +12:00
import se.scalablesolutions.akka.actor.{Actor, Transactor}
2010-05-01 12:59:24 +02:00
import Actor._
import org.scalatest.Spec
import org.scalatest.Assertions
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.BeforeAndAfterAll
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
@RunWith(classOf[JUnitRunner])
class StmSpec extends
Spec with
ShouldMatchers with
BeforeAndAfterAll {
describe("Local STM") {
it("should be able to do multiple consecutive atomic {..} statements") {
import local._
lazy val ref = Ref[Int]()
def increment = atomic {
ref.swap(ref.get.getOrElse(0) + 1)
}
def total: Int = atomic {
ref.get.getOrElse(0)
}
increment
increment
increment
total should equal(3)
}
it("should be able to do nested atomic {..} statements") {
import local._
lazy val ref = Ref[Int]()
def increment = atomic {
ref.swap(ref.get.getOrElse(0) + 1)
}
def total: Int = atomic {
ref.get.getOrElse(0)
}
atomic {
increment
increment
}
atomic {
increment
total should equal(3)
}
}
it("should roll back failing nested atomic {..} statements") {
import local._
lazy val ref = Ref[Int]()
def increment = atomic {
ref.swap(ref.get.getOrElse(0) + 1)
}
def total: Int = atomic {
ref.get.getOrElse(0)
}
try {
2010-06-10 15:49:26 +12:00
atomic(DefaultLocalTransactionFactory) {
increment
increment
throw new Exception
}
} catch {
case e => {}
}
total should equal(0)
}
}
2010-05-24 12:31:04 +02:00
describe("Global STM") {
it("should be able to initialize with atomic {..} block inside actor constructor") {
import GlobalTransactionVectorTestActor._
2010-05-24 12:31:04 +02:00
try {
val actor = actorOf[GlobalTransactionVectorTestActor].start
actor !! Add(5)
val size1 = (actor !! Size).as[Int].getOrElse(fail("Could not get Vector::size"))
size1 should equal(2)
actor !! Add(2)
val size2 = (actor !! Size).as[Int].getOrElse(fail("Could not get Vector::size"))
size2 should equal(3)
2010-05-24 12:31:04 +02:00
} catch {
2010-06-01 18:41:39 +02:00
case e =>
e.printStackTrace
fail(e.toString)
2010-05-24 12:31:04 +02:00
}
}
}
2010-06-24 08:48:48 +02:00
/*
2010-05-30 16:01:18 +02:00
describe("Transactor") {
it("should be able receive message sent with !! and pass it along to nested transactor with !! and receive reply; multiple times in a row") {
2010-05-30 16:01:18 +02:00
import GlobalTransactionVectorTestActor._
val actor = actorOf[NestedTransactorLevelOneActor].start
actor !! Add(2)
val size1 = (actor !! Size).as[Int].getOrElse(fail("Could not get size"))
size1 should equal(2)
actor !! Add(7)
actor ! "HiLevelOne"
val size2 = (actor !! Size).as[Int].getOrElse(fail("Could not get size"))
size2 should equal(7)
actor !! Add(0)
actor ! "HiLevelTwo"
val size3 = (actor !! Size).as[Int].getOrElse(fail("Could not get size"))
size3 should equal(0)
actor !! Add(3)
val size4 = (actor !! Size).as[Int].getOrElse(fail("Could not get size"))
size4 should equal(3)
2010-05-30 16:01:18 +02:00
}
}
*/
2010-05-24 12:31:04 +02:00
}
object GlobalTransactionVectorTestActor {
case class Add(value: Int)
case object Size
case object Success
}
class GlobalTransactionVectorTestActor extends Actor {
import GlobalTransactionVectorTestActor._
import se.scalablesolutions.akka.stm.global._
2010-05-24 12:31:04 +02:00
private val vector: TransactionalVector[Int] = atomic { TransactionalVector(1) }
2010-06-01 18:41:39 +02:00
def receive = {
2010-06-01 18:41:39 +02:00
case Add(value) =>
atomic { vector + value}
self.reply(Success)
2010-05-24 12:31:04 +02:00
2010-06-01 18:41:39 +02:00
case Size =>
val size = atomic { vector.size }
self.reply(size)
}
}
2010-05-30 16:01:18 +02:00
class NestedTransactorLevelOneActor extends Actor {
import GlobalTransactionVectorTestActor._
private val nested = actorOf[NestedTransactorLevelTwoActor].start
2010-06-24 08:48:48 +02:00
self.timeout = 10000
2010-06-01 18:41:39 +02:00
2010-05-30 16:01:18 +02:00
def receive = {
2010-06-01 18:41:39 +02:00
case add @ Add(_) =>
2010-05-30 16:01:18 +02:00
self.reply((nested !! add).get)
2010-06-01 18:41:39 +02:00
case Size =>
2010-05-30 16:01:18 +02:00
self.reply((nested !! Size).get)
case "HiLevelOne" => println("HiLevelOne")
case "HiLevelTwo" => nested ! "HiLevelTwo"
}
}
class NestedTransactorLevelTwoActor extends Transactor {
2010-05-30 16:01:18 +02:00
import GlobalTransactionVectorTestActor._
private val ref = Ref(0)
2010-06-24 08:48:48 +02:00
self.timeout = 10000
2010-06-01 18:41:39 +02:00
2010-05-30 16:01:18 +02:00
def receive = {
2010-06-01 18:41:39 +02:00
case Add(value) =>
2010-05-30 16:01:18 +02:00
ref.swap(value)
self.reply(Success)
2010-06-01 18:41:39 +02:00
case Size =>
2010-05-30 16:01:18 +02:00
self.reply(ref.getOrElse(-1))
2010-06-01 18:41:39 +02:00
2010-05-30 16:01:18 +02:00
case "HiLevelTwo" => println("HiLevelTwo")
}
}