2016-06-08 77 views
0

我试图测试一个简单的演员连接到另一个远程tcp。这里是我的演员TestProbe缺少akka消息

/** 
    * 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) 
    } 

} 

这里是我的测试用例

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) 
    } 
} 

终于,我可以告诉的连接是成功的,因为我的日志消息被击中:

2016-06- 08 09:58:21,548 - [DEBUG] - 来自 org.bitcoins.spvnode.networking.Client的 ClientTest-akka.actor.default-dispatcher-4 TCP连接到: testnet-seed.bi tcoin.schildbach.de:18333

2016年6月8日09:58:21550 - [调试] - 从类 org.bitcoins.spvnode.networking.Client在 ClientTest-akka.actor.default-dispatcher- 4地方: /192.168.1.107:43782

但是我的测试情况下,此错误

[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] ... 

好像我失去了一些东西简单的失败,但我想不出它到底是什么是。

+1

您正在向管理员发送回复,而不是发件人 – mattinbits

回答

1

为了回答我自己的问题,我只是对探听者进行探查,而不是试图听Client演员本身。

"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) 
    }