简体   繁体   English

如何测试是否调用了 akka-streams 接收器?

[英]How can I test that an akka-streams sink was called?

I have a use case where I pass a sink to some actor - so I can also pass a TestSink我有一个用例,我将接收器传递给某个演员 - 所以我也可以传递一个 TestSink

When that actor receives a message I pass a message to that sink using当那个演员收到一条消息时,我将一条消息传递给那个接收器,使用

case class SomeActor[T, U](sink: Sink[U, NotUsed] {
  def behavior: Behavior[T] = Behavors.receive[T] { (ctx, msg) =>
    msg match {
      case MessageT =>
        ref = sink.runWith(ActorSource.actorRef[U](PartialFunction.empty, PartialFunction.empty, 0, OverflowStrategy.fail)
        ref ! MessageU
        Behaviors.same
    }
  }
}

How can I test that the sink has received MessageU ?如何测试接收器是否已收到MessageU

Try using akka-stream-testkit ( https://doc.akka.io/docs/akka/current/stream/stream-testkit.html ).尝试使用 akka-stream-testkit ( https://doc.akka.io/docs/akka/current/stream/stream-testkit.html )。 Test code for your example (omitting types to keep it clean):您的示例的测试代码(省略类型以保持清洁):

val probe = TestProbe()
val sink = Sink.actorRef(probe.ref, onCompleteMessage = "completed", onFailureMessage = _ => "failed"))
val someActor = SomeActor(sink)

// use someActor.behavior

probe.expectMsg(1.second, MessageU)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM