From 0ff4f2cdc289f51848659c8a2768f672cfef7588 Mon Sep 17 00:00:00 2001 From: fluency03 Date: Wed, 25 Apr 2018 18:39:01 +0100 Subject: [PATCH] add NetworkActorTest --- .../blockchain/api/actors/NetworkActor.scala | 2 +- .../blockchain/api/routes/NetworkRoutes.scala | 2 +- .../api/actors/NetworkActorTest.scala | 41 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala index b56f631..3b0b838 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala @@ -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.") diff --git a/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala b/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala index 968dea3..9e0d9ff 100644 --- a/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala +++ b/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala @@ -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) } } ~ diff --git a/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala b/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala index fddb9ef..7b4116a 100644 --- a/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala +++ b/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala @@ -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 @@ -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 + } + }