refactored test - lazy persistent vector doesn't seem to work anymore

This commit is contained in:
Debasish Ghosh 2010-11-05 02:54:12 +05:30
parent 35dab53de2
commit f43ced3779

View file

@ -22,74 +22,61 @@ import akka.actor.Actor._
*/
case class Balance(accountNo: String)
case class Debit(accountNo: String, amount: BigInt, failer: ActorRef)
case class MultiDebit(accountNo: String, amounts: List[BigInt], failer: ActorRef)
case class Credit(accountNo: String, amount: BigInt)
case class Debit(accountNo: String, amount: Int, failer: ActorRef)
case class MultiDebit(accountNo: String, amounts: List[Int], failer: ActorRef)
case class Credit(accountNo: String, amount: Int)
case object LogSize
class AccountActor extends Transactor {
import self._
private lazy val accountState = RedisStorage.newMap
private lazy val txnLog = RedisStorage.newVector
//timeout = 5000
private val txnLog = RedisStorage.newVector
self.timeout = 100000
def receive = {
// check balance
case Balance(accountNo) =>
txnLog.add("Balance:%s".format(accountNo).getBytes)
reply(BigInt(new String(accountState.get(accountNo.getBytes).get)))
self.reply(new String(accountState.get(accountNo.getBytes).get).toInt)
// debit amount: can fail
case Debit(accountNo, amount, failer) =>
txnLog.add("Debit:%s %s".format(accountNo, amount.toString).getBytes)
val m: BigInt =
accountState.get(accountNo.getBytes) match {
case Some(bytes) => BigInt(new String(bytes))
case None => 0
}
val Some(m) = accountState.get(accountNo.getBytes).map(x => (new String(x)).toInt) orElse Some(0)
accountState.put(accountNo.getBytes, (m - amount).toString.getBytes)
if (amount > m)
failer !! "Failure"
reply(m - amount)
self.reply(m - amount)
// many debits: can fail
// demonstrates true rollback even if multiple puts have been done
case MultiDebit(accountNo, amounts, failer) =>
txnLog.add("MultiDebit:%s %s".format(accountNo, amounts.map(_.intValue).foldLeft(0)(_ + _).toString).getBytes)
val m: BigInt =
accountState.get(accountNo.getBytes) match {
case Some(bytes) => BigInt(new String(bytes))
case None => 0
}
var bal: BigInt = 0
val Some(m) = accountState.get(accountNo.getBytes).map(x => (new String(x)).toInt) orElse Some(0)
var bal = 0
amounts.foreach {amount =>
bal = bal + amount
accountState.put(accountNo.getBytes, (m - bal).toString.getBytes)
}
if (bal > m) failer !! "Failure"
reply(m - bal)
self.reply(m - bal)
// credit amount
case Credit(accountNo, amount) =>
txnLog.add("Credit:%s %s".format(accountNo, amount.toString).getBytes)
val m: BigInt =
accountState.get(accountNo.getBytes) match {
case Some(bytes) => BigInt(new String(bytes))
case None => 0
}
val Some(m) = accountState.get(accountNo.getBytes).map(x => (new String(x)).toInt) orElse Some(0)
accountState.put(accountNo.getBytes, (m + amount).toString.getBytes)
reply(m + amount)
self.reply(m + amount)
case LogSize =>
reply(txnLog.length.asInstanceOf[AnyRef])
self.reply(txnLog.length.asInstanceOf[AnyRef])
}
}
@serializable class PersistentFailerActor extends Transactor {
// timeout = 5000
self.timeout = 5000
def receive = {
case "Failure" =>
throw new RuntimeException("Expected exception; to test fault-tolerance")
@ -99,19 +86,19 @@ class AccountActor extends Transactor {
class RedisPersistentActorSpec extends JUnitSuite {
@Test
def testSuccessfulDebit = {
val bactor = actorOf[AccountActor]
val bactor = actorOf(new AccountActor)
bactor.start
val failer = actorOf[PersistentFailerActor]
val failer = actorOf(new PersistentFailerActor)
failer.start
bactor !! Credit("a-123", 5000)
bactor !! Debit("a-123", 3000, failer)
assertEquals(BigInt(2000), (bactor !! Balance("a-123")).get)
assertEquals(2000, (bactor !! Balance("a-123")).get)
bactor !! Credit("a-123", 7000)
assertEquals(BigInt(9000), (bactor !! Balance("a-123")).get)
assertEquals(9000, (bactor !! Balance("a-123")).get)
bactor !! Debit("a-123", 8000, failer)
assertEquals(BigInt(1000), (bactor !! Balance("a-123")).get)
assertEquals(1000, (bactor !! Balance("a-123")).get)
val c = (bactor !! LogSize).as[Int].get
assertTrue(7 == c)
@ -122,7 +109,7 @@ class RedisPersistentActorSpec extends JUnitSuite {
val bactor = actorOf[AccountActor]
bactor.start
bactor !! Credit("a-123", 5000)
assertEquals(BigInt(5000), (bactor !! Balance("a-123")).get)
assertEquals(5000, (bactor !! Balance("a-123")).get)
val failer = actorOf[PersistentFailerActor]
failer.start
@ -131,7 +118,7 @@ class RedisPersistentActorSpec extends JUnitSuite {
fail("should throw exception")
} catch { case e: RuntimeException => {}}
assertEquals(BigInt(5000), (bactor !! Balance("a-123")).get)
assertEquals(5000, (bactor !! Balance("a-123")).get)
// should not count the failed one
val c = (bactor !! LogSize).as[Int].get
@ -144,7 +131,7 @@ class RedisPersistentActorSpec extends JUnitSuite {
bactor.start
bactor !! Credit("a-123", 5000)
assertEquals(BigInt(5000), (bactor !! (Balance("a-123"), 5000)).get)
assertEquals(5000, (bactor !! (Balance("a-123"), 5000)).get)
val failer = actorOf[PersistentFailerActor]
failer.start
@ -153,7 +140,7 @@ class RedisPersistentActorSpec extends JUnitSuite {
fail("should throw exception")
} catch { case e: RuntimeException => {}}
assertEquals(BigInt(5000), (bactor !! (Balance("a-123"), 5000)).get)
assertEquals(5000, (bactor !! (Balance("a-123"), 5000)).get)
// should not count the failed one
val c = (bactor !! LogSize).as[Int].get