From 5859dedcae8037cee625814562b16707ac45c690 Mon Sep 17 00:00:00 2001 From: fluency03 Date: Fri, 4 May 2018 12:25:54 +0200 Subject: [PATCH] add tests for AddBlockFromPool & GetBlockFromPool --- .../api/actors/BlockPoolActor.scala | 8 ++--- .../api/actors/BlockchainActor.scala | 10 +++--- .../blockchain/api/actors/TxPoolActor.scala | 12 +++---- .../api/actors/BlockPoolActorTest.scala | 8 ++--- .../api/actors/BlockchainActorTest.scala | 33 +++++++++++++++++-- .../api/actors/TxPoolActorTest.scala | 12 +++---- 6 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/BlockPoolActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/BlockPoolActor.scala index 559675a..3cd717b 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/BlockPoolActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/BlockPoolActor.scala @@ -54,10 +54,10 @@ class BlockPoolActor extends ActorSupport { ).values.toSeq private[this] def onCreateBlock(block: Block): Unit = { - if (blocksPool.contains(block.hash)) sender() ! FailureMsg(s"Block ${block.hash} already exists.") + if (blocksPool.contains(block.hash)) sender() ! FailureMsg(s"Block ${block.hash} already exists in the Pool.") else { blocksPool += (block.hash -> block) - sender() ! SuccessMsg(s"Block ${block.hash} created.") + sender() ! SuccessMsg(s"Block ${block.hash} created in the Pool.") } } @@ -66,7 +66,7 @@ class BlockPoolActor extends ActorSupport { private[this] def onDeleteBlock(hash: String): Unit = if (blocksPool.contains(hash)) { blocksPool -= hash - sender() ! SuccessMsg(s"Block $hash deleted.") - } else sender() ! FailureMsg(s"Block $hash does not exist.") + sender() ! SuccessMsg(s"Block $hash deleted from the Pool.") + } else sender() ! FailureMsg(s"Block $hash does not exist in the Pool.") } diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala index f21a05b..6f0aa75 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala @@ -1,6 +1,6 @@ package com.fluency03.blockchain.api.actors -import akka.actor.{ActorSelection, Props} +import akka.actor.{ActorRef, ActorSelection, Props} import akka.pattern.ask import com.fluency03.blockchain.api.actors.BlockchainActor._ import com.fluency03.blockchain.api._ @@ -102,15 +102,17 @@ class BlockchainActor extends ActorSupport { private def onAddBlockFromPool(hash: String): Unit = blockchainOpt match { case Some(blockchain) => val maybeBlock: Future[Option[Block]] = (blockPoolActor ? BlockPoolActor.GetBlock(hash)).mapTo[Option[Block]] + val theSender: ActorRef = sender() maybeBlock onComplete { case Success(blockOpt) => blockOpt match { case Some(block) => + log.error(theSender.toString()) blockchainOpt = Some(blockchain.addBlock(block)) hashIndexMapping += (block.hash -> blockchain.length) - sender() ! SuccessMsg(s"New Block ${block.hash} added on the chain.") - case None => sender() ! FailureMsg(s"Does not find Block $hash in the poll.") + theSender ! SuccessMsg(s"New Block ${block.hash} added on the chain.") + case None => theSender ! FailureMsg(s"Did not find Block $hash in the poll.") } - case Failure(t) => sender() ! FailureMsg(s"Cannot get Block $hash from the poll.") + case Failure(e) => theSender ! FailureMsg(s"Cannot get Block $hash from the poll: ${e.getMessage}") } case None => log.error("Blockchain does not exist! Clear the hash-to-index mapping!") diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/TxPoolActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/TxPoolActor.scala index 523f9fb..de949a4 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/TxPoolActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/TxPoolActor.scala @@ -53,10 +53,10 @@ class TxPoolActor extends ActorSupport { ).values.toSeq private def onCreateTransaction(tx: Transaction): Unit = - if (transPool.contains(tx.id)) sender() ! FailureMsg(s"Transaction ${tx.id} already exists.") + if (transPool.contains(tx.id)) sender() ! FailureMsg(s"Transaction ${tx.id} already exists in the Pool.") else { transPool += (tx.id -> tx) - sender() ! SuccessMsg(s"Transaction ${tx.id} created.") + sender() ! SuccessMsg(s"Transaction ${tx.id} created in the Pool.") } private def onGetTransaction(id: String): Unit = sender() ! transPool.get(id) @@ -64,8 +64,8 @@ class TxPoolActor extends ActorSupport { private def onDeleteTransaction(id: String): Unit = if (transPool contains id) { transPool -= id - sender() ! SuccessMsg(s"Transaction $id deleted.") - } else sender() ! FailureMsg(s"Transaction $id does not exist.") + sender() ! SuccessMsg(s"Transaction $id deleted from the Pool.") + } else sender() ! FailureMsg(s"Transaction $id does not exist in the Pool.") private def onUpdateTransaction(tx: Transaction): Unit = { val actualId = tx.id @@ -74,8 +74,8 @@ class TxPoolActor extends ActorSupport { val notExistBefore = !transPool.contains(actualId) transPool += (actualId -> tx) sender() ! { - if (notExistBefore) SuccessMsg(s"Transaction $actualId does not exist. New transaction created.") - else SuccessMsg(s"Transaction $actualId updated.") + if (notExistBefore) SuccessMsg(s"Transaction $actualId does not exist. New transaction created in the Pool.") + else SuccessMsg(s"Transaction $actualId updated in the Pool.") } } else sender() ! FailureMsg(s"Transaction does not have valid ID. Should be: $expectedId; actually is: $actualId") } diff --git a/src/test/scala/com/fluency03/blockchain/api/actors/BlockPoolActorTest.scala b/src/test/scala/com/fluency03/blockchain/api/actors/BlockPoolActorTest.scala index 98d2899..1b7e088 100644 --- a/src/test/scala/com/fluency03/blockchain/api/actors/BlockPoolActorTest.scala +++ b/src/test/scala/com/fluency03/blockchain/api/actors/BlockPoolActorTest.scala @@ -27,10 +27,10 @@ class BlockPoolActorTest extends TestKit(ActorSystem("BlocksActorTest")) with Im expectMsg(Seq.empty[Block]) blockPoolActor ! CreateBlock(Block.genesisBlock) - expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} created.")) + expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} created in the Pool.")) blockPoolActor ! CreateBlock(Block.genesisBlock) - expectMsg(FailureMsg(s"Block ${Block.genesisBlock.hash} already exists.")) + expectMsg(FailureMsg(s"Block ${Block.genesisBlock.hash} already exists in the Pool.")) blockPoolActor ! GetBlocks expectMsg(Seq(Block.genesisBlock)) @@ -45,10 +45,10 @@ class BlockPoolActorTest extends TestKit(ActorSystem("BlocksActorTest")) with Im expectMsg(Some(Block.genesisBlock)) blockPoolActor ! DeleteBlock(Block.genesisBlock.hash) - expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} deleted.")) + expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} deleted from the Pool.")) blockPoolActor ! DeleteBlock(Block.genesisBlock.hash) - expectMsg(FailureMsg(s"Block ${Block.genesisBlock.hash} does not exist.")) + expectMsg(FailureMsg(s"Block ${Block.genesisBlock.hash} does not exist in the Pool.")) blockPoolActor ! GetBlock(Block.genesisBlock.hash) expectMsg(None) diff --git a/src/test/scala/com/fluency03/blockchain/api/actors/BlockchainActorTest.scala b/src/test/scala/com/fluency03/blockchain/api/actors/BlockchainActorTest.scala index 0ca6c27..7f2fd66 100644 --- a/src/test/scala/com/fluency03/blockchain/api/actors/BlockchainActorTest.scala +++ b/src/test/scala/com/fluency03/blockchain/api/actors/BlockchainActorTest.scala @@ -1,9 +1,10 @@ package com.fluency03.blockchain.api.actors -import akka.actor.{ActorRef, ActorSystem, Props} +import akka.actor.{ActorRef, ActorSelection, ActorSystem, Props} import akka.testkit.{DefaultTimeout, ImplicitSender, TestKit} +import com.fluency03.blockchain.api.actors.BlockPoolActor.CreateBlock import com.fluency03.blockchain.api.actors.BlockchainActor._ -import com.fluency03.blockchain.api.{FailureMsg, SuccessMsg} +import com.fluency03.blockchain.api.{BLOCKCHAIN_ACTOR_NAME, BLOCK_POOL_ACTOR_NAME, FailureMsg, SuccessMsg} import com.fluency03.blockchain.core.{Block, Blockchain, Transaction} import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} @@ -16,7 +17,8 @@ class BlockchainActorTest extends TestKit(ActorSystem("BlockchainActorTest")) wi shutdown() } - val blockchainActor: ActorRef = system.actorOf(Props[BlockchainActor]) + val blockchainActor: ActorRef = system.actorOf(Props[BlockchainActor], BLOCKCHAIN_ACTOR_NAME) + val blockPoolActor: ActorRef = system.actorOf(Props[BlockPoolActor], BLOCK_POOL_ACTOR_NAME) "A BlockchainActor" should { "Respond with a Blockchain." in { @@ -67,6 +69,28 @@ class BlockchainActorTest extends TestKit(ActorSystem("BlockchainActorTest")) wi blockchainActor ! CheckBlockchainValidity expectMsg(SuccessMsg("true")) + blockPoolActor ! CreateBlock(genesis) + expectMsg(SuccessMsg(s"Block ${genesis.hash} created in the Pool.")) + + within(15 seconds) { + blockchainActor ! GetBlockFromPool(genesis.hash) + expectMsg(Some(genesis)) + + blockchainActor ! GetBlockFromPool("someid") + expectMsg(None) + } + + within(30 seconds) { + blockchainActor ! AddBlockFromPool(genesis.hash) + expectMsg(SuccessMsg(s"New Block ${genesis.hash} added on the chain.")) + + blockchainActor ! AddBlockFromPool("someid") + expectMsg(FailureMsg(s"Did not find Block someid in the poll.")) + } + + blockchainActor ! RemoveBlock + expectMsg(SuccessMsg(s"Last Block ${genesis.hash} removed from the chain.")) + within(15 seconds) { blockchainActor ! MineNextBlock("next", Seq.empty[String]) val actualBlock = expectMsgType[Some[Block]].get @@ -87,6 +111,9 @@ class BlockchainActorTest extends TestKit(ActorSystem("BlockchainActorTest")) wi blockchainActor ! AddBlock(genesis) expectMsg(FailureMsg("Blockchain does not exist.")) + blockchainActor ! AddBlockFromPool(genesis.hash) + expectMsg(FailureMsg("Blockchain does not exist.")) + blockchainActor ! RemoveBlock expectMsg(FailureMsg("Blockchain does not exist.")) diff --git a/src/test/scala/com/fluency03/blockchain/api/actors/TxPoolActorTest.scala b/src/test/scala/com/fluency03/blockchain/api/actors/TxPoolActorTest.scala index f530c4b..590a3af 100644 --- a/src/test/scala/com/fluency03/blockchain/api/actors/TxPoolActorTest.scala +++ b/src/test/scala/com/fluency03/blockchain/api/actors/TxPoolActorTest.scala @@ -30,10 +30,10 @@ class TxPoolActorTest extends TestKit(ActorSystem("TransactionsActorTest")) with val genesisTx: Transaction = createCoinbaseTx(0, genesisMiner, genesisTimestamp) txPoolActor ! CreateTransaction(genesisTx) - expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} created.")) + expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} created in the Pool.")) txPoolActor ! CreateTransaction(genesisTx) - expectMsg(FailureMsg(s"Transaction ${genesisTx.id} already exists.")) + expectMsg(FailureMsg(s"Transaction ${genesisTx.id} already exists in the Pool.")) txPoolActor ! GetTransactions expectMsg(Seq(genesisTx)) @@ -48,11 +48,11 @@ class TxPoolActorTest extends TestKit(ActorSystem("TransactionsActorTest")) with expectMsg(Some(genesisTx)) txPoolActor ! UpdateTransaction(genesisTx) - expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} updated.")) + expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} updated in the Pool.")) val tx1: Transaction = createCoinbaseTx(1, genesisMiner, genesisTimestamp + 10) txPoolActor ! UpdateTransaction(tx1) - expectMsg(SuccessMsg(s"Transaction ${tx1.id} does not exist. New transaction created.")) + expectMsg(SuccessMsg(s"Transaction ${tx1.id} does not exist. New transaction created in the Pool.")) val tx0: Transaction = Transaction(Seq.empty[TxIn], Seq.empty[TxOut], genesisTimestamp, "0000") val idOfTx0 = hashOfTransaction(tx0) @@ -60,10 +60,10 @@ class TxPoolActorTest extends TestKit(ActorSystem("TransactionsActorTest")) with expectMsg(FailureMsg(s"Transaction does not have valid ID. Should be: $idOfTx0; actually is: ${tx0.id}")) txPoolActor ! DeleteTransaction(genesisTx.id) - expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} deleted.")) + expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} deleted from the Pool.")) txPoolActor ! DeleteTransaction(genesisTx.id) - expectMsg(FailureMsg(s"Transaction ${genesisTx.id} does not exist.")) + expectMsg(FailureMsg(s"Transaction ${genesisTx.id} does not exist in the Pool.")) txPoolActor ! GetTransaction(genesisTx.id) expectMsg(None)