Skip to content

Commit

Permalink
add test for MineAndAddNextBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 5, 2018
1 parent 93c2b2a commit 8afc7bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import akka.http.scaladsl.server.directives.MethodDirectives.{delete, get, post}
import akka.http.scaladsl.server.directives.RouteDirectives.complete
import akka.http.scaladsl.unmarshalling.PredefinedFromStringUnmarshallers.CsvSeq
import akka.pattern.ask
import com.github.fluency03.blockchain.api.{FailureMsg, Input, Message}
import com.github.fluency03.blockchain.api.{Input, Message}
import com.github.fluency03.blockchain.api.actors.BlockchainActor._
import com.github.fluency03.blockchain.core.{Block, Blockchain, Transaction}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.github.fluency03.blockchain.api.actors

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestKit}
import com.github.fluency03.blockchain.api.actors.BlockPoolActor._
import akka.testkit.{DefaultTimeout, ImplicitSender, TestKit}
import com.github.fluency03.blockchain.api._
import com.github.fluency03.blockchain.core.Block
import com.github.fluency03.blockchain.api.actors.BlockPoolActor._
import com.github.fluency03.blockchain.api.actors.BlockchainActor.CreateBlockchain
import com.github.fluency03.blockchain.core.{Block, Transaction}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

class BlockPoolActorTest extends TestKit(ActorSystem("BlocksActorTest")) with ImplicitSender
import scala.concurrent.duration._

class BlockPoolActorTest extends TestKit(ActorSystem("BlocksActorTest")) with DefaultTimeout with ImplicitSender
with WordSpecLike with Matchers with BeforeAndAfterAll {

override def afterAll: Unit = {
Expand Down Expand Up @@ -55,6 +58,28 @@ class BlockPoolActorTest extends TestKit(ActorSystem("BlocksActorTest")) with Im
blockPoolActor ! GetBlock(Block.genesisBlock.hash)
expectMsg(Some(Block.genesisBlock))


within(15 seconds) {
blockchainActor ! CreateBlockchain
expectMsgType[SuccessMsg]
}

var actualBlock: Block = Block.genesisBlock
within(15 seconds) {
blockPoolActor ! MineAndAddNextBlock("next", Seq.empty[String])
actualBlock = expectMsgType[Some[Block]].get
actualBlock.data shouldEqual "next"
actualBlock.transactions shouldEqual Seq.empty[Transaction]
actualBlock.index shouldEqual 1
actualBlock.hasValidHash shouldEqual true
}

blockPoolActor ! GetBlocks
val blocks = expectMsgType[Seq[Block]]

blocks.length shouldEqual 2
blocks should contain allOf (Block.genesisBlock, actualBlock)

blockPoolActor ! DeleteBlock(Block.genesisBlock.hash)
expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} deleted from the Pool."))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.github.fluency03.blockchain.api.actors

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.testkit.{DefaultTimeout, ImplicitSender, TestKit}
import com.github.fluency03.blockchain.api._
import com.github.fluency03.blockchain.api.actors.BlockPoolActor.AddBlock
import com.github.fluency03.blockchain.api.actors.BlockchainActor._
import com.github.fluency03.blockchain.api._
import com.github.fluency03.blockchain.api.actors.TxPoolActor.AddTransaction
import com.github.fluency03.blockchain.core.Transaction.createCoinbaseTx
import com.github.fluency03.blockchain.core.{Block, Blockchain, Transaction}
Expand Down

0 comments on commit 8afc7bb

Please sign in to comment.