Akka Stream Graphs - 如何从akka.stream.contrib测试PartitionWith

时间:2018-02-10 07:10:45

标签: scala scalatest akka-stream

我写了一个简单的测试来探索来自PartitionWith的{​​{1}}图形功能。以下是代码段:

akka.stream.contrib

当我运行上述测试时,它会抛出这个错误:

class Scratch
    extends TestKit(ActorSystem("PartitionWith"))
    with WordSpecLike
    with ScalaFutures
    with Eventually
    with Matchers {

  "PartitionWith" should {
    "split source" in {
      val source: Source[Either[Int, String], NotUsed] = Source(List(Left(1), Right("one")))
      val leftHeadSink = Sink.head[Int]
      val rightHeadSink = Sink.head[String]

      val flow = RunnableGraph.fromGraph(GraphDSL.create(source, leftHeadSink, rightHeadSink)((_, _, _)) {
        implicit builder: GraphDSL.Builder[(NotUsed, Future[Int], Future[String])] => (s, l, r) =>
          import GraphDSL.Implicits._

          val pw = builder.add(PartitionWith.apply[Either[Int, String], Int, String](identity))

          s ~> pw.in
          pw.out0 ~> l.in
          pw.out1 ~> r.in

          ClosedShape
      })

      val event = flow.run()
      event._2.futureValue shouldBe 1     // first check
      event._3.futureValue shouldBe "one" // second check
    }

它似乎在第二次检查中失败,因为The future returned an exception of type: java.util.NoSuchElementException, with message: head of empty stream. org.scalatest.exceptions.TestFailedException: The future returned an exception of type: java.util.NoSuchElementException, with message: head of empty stream. 为空。我想知道rightHeadSink中的Right("one")是否永远不会被处理?

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

当第一个Sink.head完成时,似乎整个流都已完成,因此第二个接收器无法检索其元素。

尝试使用2 Sink.seq而不是2 Sink.head来测试,以避免流的早期终止。