Skip to content

Commit

Permalink
add NetworkActorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 25, 2018
1 parent f9c0a54 commit 0ff4f2c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class NetworkActor extends ActorSupport {
case _ => unhandled _
}

private def onGetPeers(): Unit = context.children.map(_.path.name).toSeq
private def onGetPeers(): Unit = sender() ! context.children.map(_.path.name).toSet

private def onCreatePeer(name: String): Unit =
if (context.child(name).isDefined) sender() ! FailureMsg(s"Peer $name has been created.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait NetworkRoutes extends RoutesSupport {
lazy val networkRoutes: Route =
path("peers") {
get {
val peers: Future[Seq[String]] = (networkActor ? GetPeers).mapTo[Seq[String]]
val peers: Future[Set[String]] = (networkActor ? GetPeers).mapTo[Set[String]]
complete(peers)
}
} ~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.fluency03.blockchain.api.actors

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestKit}
import com.fluency03.blockchain.api.actors.NetworkActor._
import com.fluency03.blockchain.api.{FailureMsg, SuccessMsg}
import com.fluency03.blockchain.core.Peer
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

class NetworkActorTest extends TestKit(ActorSystem("NetworkActorTest")) with ImplicitSender
Expand All @@ -13,7 +16,45 @@ class NetworkActorTest extends TestKit(ActorSystem("NetworkActorTest")) with Imp

val networkActor: ActorRef = system.actorOf(Props[NetworkActor])

"A NetworkActor" should {
"Respond with a Set of Peers." in {
NetworkActor.props shouldEqual Props[NetworkActor]

networkActor ! GetPeers
expectMsg(Set.empty[String])

val name = "peer"
networkActor ! CreatePeer(name)
expectMsg(SuccessMsg(s"Peer $name created."))

networkActor ! CreatePeer(name)
expectMsg(FailureMsg(s"Peer $name has been created."))

networkActor ! GetPeers
expectMsg(Set(name))

networkActor ! GetPeer(name)
val peerOpt = expectMsgType[Some[Peer]]
peerOpt.isDefined shouldEqual true
peerOpt.get.name shouldEqual name
peerOpt.get.publicKeys.size shouldEqual 1

networkActor ! DeletePeer(name)
expectMsg(SuccessMsg(s"Peer $name deleted."))

// wait 1 second for Peer Actor to completely stop
Thread.sleep(1000)

networkActor ! DeletePeer(name)
expectMsg(FailureMsg(s"Peer $name does not exist."))

networkActor ! GetPeer(name)
expectMsg(None)

networkActor ! "other"
expectNoMessage
}
}



Expand Down

0 comments on commit 0ff4f2c

Please sign in to comment.