0
votes

I'm trying to test a simple actor which is connecting to another remote on tcp. Here is my actor

/**
  * Created by chris on 6/6/16.
  */
class Client(listener : ActorRef, actorSystem: ActorSystem) extends Actor with BitcoinSLogger {

  /**
    * The manager is an actor that handles the underlying low level I/O resources (selectors, channels)
    * and instantiates workers for specific tasks, such as listening to incoming connections.
    */
  def manager = IO(Tcp)(actorSystem)


  def receive  = {

    case Tcp.Connect(remote,_,_,_,_) => manager ! Tcp.Connect(remote)
    case Tcp.CommandFailed(_: Tcp.Connect) =>
      logger.debug("Connection failed")
      listener ! "connect failed"
      context stop self

    case c @ Tcp.Connected(remote, local) =>
      logger.debug("Tcp connection to: " + remote)
      logger.debug("Local: " + local)
      listener ! c
      val connection = sender()
      connection ! Tcp.Register(self)
      context become {
        case data: ByteString =>
          connection ! Tcp.Write(data)
        case Tcp.CommandFailed(w: Tcp.Write) =>
          // O/S buffer was full
          listener ! "write failed"
        case Tcp.Received(data) =>
          listener ! data
        case "close" =>
          connection ! Tcp.Close
        case _: Tcp.ConnectionClosed =>
          listener ! "connection closed"
          context stop self
      }
  }
  def sendMessage(msg : NetworkRequest, peer : NetworkIpAddress) : Future[NetworkResponse] = ???

}


object Client {
  //private case class ClientImpl(remote: InetSocketAddress, listener: ActorRef, actorSystem : ActorSystem) extends Client

  def apply(listener : ActorRef, actorSystem : ActorSystem) : Props = {
    Props(classOf[Client], listener, actorSystem)
  }

}

here is my test case

class ClientTest extends TestKit(ActorSystem("ClientTest")) with FlatSpecLike with MustMatchers with BeforeAndAfterAll {

      "Client" must "connect to a node on the bitcoin network" in {

        val probe = TestProbe()
        val hostName = "testnet-seed.bitcoin.schildbach.de"
        val socket = new InetSocketAddress(hostName, TestNet3.port)
        val peerMessageHandler = PeerMessageHandler(system)
        val client = system.actorOf(Client(peerMessageHandler, system))
        probe.send(client, Tcp.Connect(socket))
        probe.expectMsgType[Tcp.Connected](10.seconds)
      }



  override def afterAll: Unit = {
    TestKit.shutdownActorSystem(system)
  }
}

finally, I can tell the connection is successful because my log messages are being hit:

2016-06-08 09:58:21,548 - [DEBUG] - from class org.bitcoins.spvnode.networking.Client in ClientTest-akka.actor.default-dispatcher-4 Tcp connection to: testnet-seed.bitcoin.schildbach.de:18333

2016-06-08 09:58:21,550 - [DEBUG] - from class org.bitcoins.spvnode.networking.Client in ClientTest-akka.actor.default-dispatcher-4 Local: /192.168.1.107:43782

however my test case is failing with this error

[info] ClientTest:
[info] Client
[info] - must connect to a node on the bitcoin network *** FAILED ***
[info]   java.lang.AssertionError: assertion failed: timeout (10 seconds) during expectMsgClass waiting for class akka.io.Tcp$Connected
[info]   at scala.Predef$.assert(Predef.scala:170)
[info]   at akka.testkit.TestKitBase$class.expectMsgClass_internal(TestKit.scala:435)
[info]   at akka.testkit.TestKitBase$class.expectMsgType(TestKit.scala:417)
[info]   at akka.testkit.TestKit.expectMsgType(TestKit.scala:737)
[info]   at org.bitcoins.spvnode.networking.ClientTest$$anonfun$1.apply$mcV$sp(ClientTest.scala:25)
[info]   at org.bitcoins.spvnode.networking.ClientTest$$anonfun$1.apply(ClientTest.scala:17)
[info]   at org.bitcoins.spvnode.networking.ClientTest$$anonfun$1.apply(ClientTest.scala:17)
[info]   at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
[info]   at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
[info]   at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info]   ...

Seems like I am missing something simple, but I can't figure out exactly what it is.

1
You're sending the reply to manager, not to sender - mattinbits

1 Answers

1
votes

To answer my own question, I just made probe the listener isntead of trying to listen on the Client actor itself.

  "Client" must "connect to a node on the bitcoin network" in {
    val probe = TestProbe()
    val hostName = "testnet-seed.bitcoin.schildbach.de"
    val socket = new InetSocketAddress(hostName, TestNet3.port)
    val peerMessageHandler = PeerMessageHandler(system)
    val client = TestActorRef(Client(probe.ref,system))
    //probe.send(client, Tcp.Connect(socket))
    client ! Tcp.Connect(socket)
    probe.expectMsgType[Tcp.Connected](10.seconds)
  }