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

312 Views Asked by At

I have a use case where I pass a sink to some actor - so I can also pass a 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?

1

There are 1 best solutions below

1
On BEST ANSWER

Try using 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)