Search code examples
scalaakkaakka-streamakka-testkit

Testing Akka Streams divertTo with streams testkit


I have a graph composed of several flows, each of which returns an Either of some error or the actual result. The code is using divertTo in order to send any Lefts to a different sink than the happy-path downstream sink.

The problem I am having is that when using the Akka Streams testkit, I can't find a way to probe the sink used in the divertTo call. I can probe the happy-path sink fine but I really need to find a way to test the not-so-happy paths to prove that my flow is working.

Has anybody done this type of thing before using the streams testkit?


Solution

  • The problem I am having is that...I can't find a way to probe the sink used in the divertTo call....Has anybody done this type of thing before using the streams testkit?

    Yes: https://github.com/akka/akka/blob/release-2.5/akka-stream-tests/src/test/scala/akka/stream/scaladsl/GraphPartitionSpec.scala

    From the above spec:

    "divertTo must send matching elements to the sink" in assertAllStagesStopped {
      val odd = TestSubscriber.probe[Int]()
      val even = TestSubscriber.probe[Int]()
      Source(1 to 2).divertTo(Sink.fromSubscriber(odd), _ % 2 != 0).to(Sink.fromSubscriber(even)).run()
      even.request(1)
      even.expectNoMessage(1.second)
      odd.request(1)
      odd.expectNext(1)
      even.expectNext(2)
      odd.expectComplete()
      even.expectComplete()
    }