pekko/akka-stream-tests/src/test/scala/akka/stream/FusingSpec.scala

75 lines
2.4 KiB
Scala
Raw Normal View History

2015-12-14 17:02:00 +01:00
/**
2018-01-04 17:26:29 +00:00
* Copyright (C) 2015-2018 Lightbend Inc. <https://www.lightbend.com>
2015-12-14 17:02:00 +01:00
*/
package akka.stream
import akka.stream.scaladsl._
import akka.stream.testkit.StreamSpec
import akka.stream.impl.fusing.GraphInterpreter
import akka.event.BusLogging
2015-12-14 17:02:00 +01:00
class FusingSpec extends StreamSpec {
2015-12-14 17:02:00 +01:00
2015-12-15 16:44:48 +01:00
final val Debug = false
2015-12-14 17:02:00 +01:00
implicit val materializer = ActorMaterializer()
2015-12-15 16:44:48 +01:00
def graph(async: Boolean) =
Source.unfold(1)(x Some(x x)).filter(_ % 2 == 1)
2015-12-15 16:44:48 +01:00
.alsoTo(Flow[Int].fold(0)(_ + _).to(Sink.head.named("otherSink")).addAttributes(if (async) Attributes.asyncBoundary else Attributes.none))
.via(Flow[Int].fold(1)(_ + _).named("mainSink"))
"SubFusingActorMaterializer" must {
"work with asynchronous boundaries in the subflows" in {
val async = Flow[Int].map(_ * 2).async
Source(0 to 9)
.map(_ * 10)
.flatMapMerge(5, i Source(i to (i + 9)).via(async))
.grouped(1000)
.runWith(Sink.head)
.futureValue
.sorted should ===(0 to 198 by 2)
}
2015-12-22 21:15:57 +01:00
"use multiple actors when there are asynchronous boundaries in the subflows (manual)" in {
def ref = {
val bus = GraphInterpreter.currentInterpreter.log.asInstanceOf[BusLogging]
bus.logSource
}
val async = Flow[Int].map(x { testActor ! ref; x }).async
Source(0 to 9)
.map(x { testActor ! ref; x })
.flatMapMerge(5, i Source.single(i).via(async))
.grouped(1000)
.runWith(Sink.head)
.futureValue
.sorted should ===(0 to 9)
val refs = receiveN(20)
withClue(s"refs=\n${refs.mkString("\n")}") {
2015-12-22 21:15:57 +01:00
refs.toSet.size should ===(11) // main flow + 10 subflows
}
}
"use multiple actors when there are asynchronous boundaries in the subflows (combinator)" in {
def ref = {
val bus = GraphInterpreter.currentInterpreter.log.asInstanceOf[BusLogging]
bus.logSource
}
val flow = Flow[Int].map(x { testActor ! ref; x })
Source(0 to 9)
.map(x { testActor ! ref; x })
.flatMapMerge(5, i Source.single(i).via(flow.async))
2015-12-22 21:15:57 +01:00
.grouped(1000)
.runWith(Sink.head)
.futureValue
.sorted should ===(0 to 9)
val refs = receiveN(20)
withClue(s"refs=\n${refs.mkString("\n")}") {
refs.toSet.size should ===(11) // main flow + 10 subflows
}
}
}
2015-12-14 17:02:00 +01:00
}