0

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?

mrt181
  • 5,080
  • 8
  • 66
  • 86

1 Answers1

0

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)
Ava
  • 818
  • 10
  • 18
  • this example is using akka-testkit, not stream-testkit. akka-testkit can be used to test streams but stream-testkit will let you create TestSources and TestSinks which provide testing methods that are geared towards the stream usecase – Cpt. Senkfuss Jun 28 '22 at 09:03