TestProbe缺少Akka消息

编程入门 行业动态 更新时间:2024-10-23 13:33:29
本文介绍了TestProbe缺少Akka消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试测试连接到tcp上另一个遥控器的简单角色。这是我的演员

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

这是我的测试用例

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]-来自类 中的org.bitcoins.spvnodeworking.Client ClientTest-akka.actor.default-dispatcher-4 Tcp连接至: testnet-seed.bitcoin.schildbach.de:18333

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

2016-06-08 09:58:21,550-[调试]-来自 ClientTest-akka.actor中的类 org.bitcoins.spvnodeworking.Client。 default-dispatcher-4本地: /192.168.1.107:43782

2016-06-08 09:58:21,550 - [DEBUG] - from class org.bitcoins.spvnodeworking.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.spvnodeworking.ClientTest$$anonfun$1.apply$mcV$sp(ClientTest.scala:25) [info] at org.bitcoins.spvnodeworking.ClientTest$$anonfun$1.apply(ClientTest.scala:17) [info] at org.bitcoins.spvnodeworking.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.

推荐答案

要回答我自己的问题,我只是探究了试听 Client actor的监听器的想法。本身。

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

更多推荐

TestProbe缺少Akka消息

本文发布于:2023-11-25 03:01:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628049.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:消息   TestProbe   Akka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!