2010-06-10 13:53:33 +12:00
|
|
|
package se.scalablesolutions.akka.stm
|
2010-04-05 11:53:43 +02:00
|
|
|
|
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._
|
|
|
|
|
|
2010-04-05 11:53:43 +02:00
|
|
|
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])
|
2010-04-07 14:23:58 +02:00
|
|
|
class StmSpec extends
|
|
|
|
|
Spec with
|
|
|
|
|
ShouldMatchers with
|
2010-04-05 11:53:43 +02:00
|
|
|
BeforeAndAfterAll {
|
2010-04-07 14:23:58 +02:00
|
|
|
|
2010-06-18 20:49:40 +12:00
|
|
|
describe("Local STM") {
|
2010-04-05 11:53:43 +02:00
|
|
|
it("should be able to do multiple consecutive atomic {..} statements") {
|
2010-06-18 20:49:40 +12:00
|
|
|
import local._
|
2010-04-05 11:53:43 +02:00
|
|
|
|
2010-06-10 13:38:13 +12:00
|
|
|
lazy val ref = Ref[Int]()
|
2010-04-05 11:53:43 +02:00
|
|
|
|
|
|
|
|
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") {
|
2010-06-18 20:49:40 +12:00
|
|
|
import local._
|
2010-04-05 11:53:43 +02:00
|
|
|
|
2010-06-10 13:38:13 +12:00
|
|
|
lazy val ref = Ref[Int]()
|
2010-04-05 11:53:43 +02:00
|
|
|
|
|
|
|
|
def increment = atomic {
|
|
|
|
|
ref.swap(ref.get.getOrElse(0) + 1)
|
|
|
|
|
}
|
|
|
|
|
def total: Int = atomic {
|
|
|
|
|
ref.get.getOrElse(0)
|
|
|
|
|
}
|
2010-04-07 14:23:58 +02:00
|
|
|
|
2010-04-05 11:53:43 +02:00
|
|
|
atomic {
|
|
|
|
|
increment
|
2010-04-07 14:23:58 +02:00
|
|
|
increment
|
2010-04-05 11:53:43 +02:00
|
|
|
}
|
|
|
|
|
atomic {
|
|
|
|
|
increment
|
|
|
|
|
total should equal(3)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it("should roll back failing nested atomic {..} statements") {
|
2010-06-18 20:49:40 +12:00
|
|
|
import local._
|
2010-04-05 11:53:43 +02:00
|
|
|
|
2010-06-10 13:38:13 +12:00
|
|
|
lazy val ref = Ref[Int]()
|
2010-04-05 11:53:43 +02:00
|
|
|
|
|
|
|
|
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) {
|
2010-04-05 11:53:43 +02:00
|
|
|
increment
|
|
|
|
|
increment
|
|
|
|
|
throw new Exception
|
2010-04-07 14:23:58 +02:00
|
|
|
}
|
2010-04-05 11:53:43 +02:00
|
|
|
} catch {
|
|
|
|
|
case e => {}
|
|
|
|
|
}
|
|
|
|
|
total should equal(0)
|
|
|
|
|
}
|
2010-05-24 13:55:56 +02:00
|
|
|
}
|
2010-05-24 12:31:04 +02:00
|
|
|
|
2010-06-18 20:49:40 +12:00
|
|
|
describe("Global STM") {
|
2010-05-24 13:12:06 +02:00
|
|
|
it("should be able to initialize with atomic {..} block inside actor constructor") {
|
2010-05-24 13:55:56 +02:00
|
|
|
import GlobalTransactionVectorTestActor._
|
2010-05-24 12:31:04 +02:00
|
|
|
try {
|
2010-05-24 13:55:56 +02:00
|
|
|
val actor = actorOf[GlobalTransactionVectorTestActor].start
|
2010-05-24 13:12:06 +02:00
|
|
|
actor !! Add(5)
|
2010-06-21 12:25:24 +02:00
|
|
|
val size1 = (actor !! Size).as[Int].getOrElse(fail("Could not get Vector::size"))
|
2010-05-24 13:12:06 +02:00
|
|
|
size1 should equal(2)
|
|
|
|
|
actor !! Add(2)
|
2010-06-21 12:25:24 +02:00
|
|
|
val size2 = (actor !! Size).as[Int].getOrElse(fail("Could not get Vector::size"))
|
2010-05-24 13:55:56 +02:00
|
|
|
size2 should equal(3)
|
2010-05-24 12:31:04 +02:00
|
|
|
} catch {
|
2010-06-01 18:41:39 +02:00
|
|
|
case e =>
|
2010-05-24 13:55:56 +02:00
|
|
|
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") {
|
2010-06-15 13:15:00 +02:00
|
|
|
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._
|
2010-06-23 10:23:52 +02:00
|
|
|
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-28 23:50:08 +02:00
|
|
|
*/
|
2010-05-24 12:31:04 +02:00
|
|
|
}
|
|
|
|
|
|
2010-05-24 13:55:56 +02:00
|
|
|
object GlobalTransactionVectorTestActor {
|
2010-05-24 13:12:06 +02:00
|
|
|
case class Add(value: Int)
|
|
|
|
|
case object Size
|
|
|
|
|
case object Success
|
|
|
|
|
}
|
2010-05-24 13:55:56 +02:00
|
|
|
class GlobalTransactionVectorTestActor extends Actor {
|
|
|
|
|
import GlobalTransactionVectorTestActor._
|
2010-06-18 20:49:40 +12:00
|
|
|
import se.scalablesolutions.akka.stm.global._
|
2010-05-24 12:31:04 +02:00
|
|
|
|
2010-06-18 20:49:40 +12:00
|
|
|
private val vector: TransactionalVector[Int] = atomic { TransactionalVector(1) }
|
2010-06-01 18:41:39 +02:00
|
|
|
|
2010-05-24 13:12:06 +02:00
|
|
|
def receive = {
|
2010-06-01 18:41:39 +02:00
|
|
|
case Add(value) =>
|
2010-06-18 20:49:40 +12:00
|
|
|
atomic { vector + value}
|
2010-05-24 13:12:06 +02:00
|
|
|
self.reply(Success)
|
2010-05-24 12:31:04 +02:00
|
|
|
|
2010-06-01 18:41:39 +02:00
|
|
|
case Size =>
|
2010-06-18 20:49:40 +12:00
|
|
|
val size = atomic { vector.size }
|
2010-05-24 13:12:06 +02:00
|
|
|
self.reply(size)
|
2010-04-05 11:53:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
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"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-10 13:38:13 +12:00
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
}
|