Search code examples
scalaakkaakka-streamakka-testkit

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

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?


Solution

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