2014-09-04 14:31:22 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2014-10-27 14:35:41 +01:00
|
|
|
package akka.stream.scaladsl
|
2014-09-04 14:31:22 +02:00
|
|
|
|
2014-12-01 20:07:55 +02:00
|
|
|
import akka.stream.scaladsl.OperationAttributes._
|
2015-01-27 18:29:20 +01:00
|
|
|
import akka.stream.ActorFlowMaterializer
|
2014-12-01 20:07:55 +02:00
|
|
|
import akka.stream.OverflowStrategy
|
2014-09-04 14:31:22 +02:00
|
|
|
import akka.stream.testkit.AkkaSpec
|
2014-10-03 17:33:14 +02:00
|
|
|
import akka.stream.testkit.StreamTestKit.{ PublisherProbe, SubscriberProbe }
|
2014-11-12 10:43:39 +01:00
|
|
|
import akka.stream.stage._
|
2014-09-30 15:40:08 +02:00
|
|
|
|
|
|
|
|
object FlowGraphCompileSpec {
|
|
|
|
|
class Fruit
|
|
|
|
|
class Apple extends Fruit
|
|
|
|
|
}
|
2014-09-04 14:31:22 +02:00
|
|
|
|
|
|
|
|
class FlowGraphCompileSpec extends AkkaSpec {
|
2014-09-30 15:40:08 +02:00
|
|
|
import FlowGraphCompileSpec._
|
2014-09-04 14:31:22 +02:00
|
|
|
|
2015-01-27 18:29:20 +01:00
|
|
|
implicit val mat = ActorFlowMaterializer()
|
2014-09-04 14:31:22 +02:00
|
|
|
|
2014-11-12 10:43:39 +01:00
|
|
|
def op[In, Out]: () ⇒ PushStage[In, Out] = { () ⇒
|
|
|
|
|
new PushStage[In, Out] {
|
|
|
|
|
override def onPush(elem: In, ctx: Context[Out]): Directive =
|
|
|
|
|
ctx.push(elem.asInstanceOf[Out])
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-09 21:09:50 +01:00
|
|
|
val apples = () ⇒ Iterator.continually(new Apple)
|
|
|
|
|
|
2014-12-01 20:07:55 +02:00
|
|
|
val f1 = Flow[String].section(name("f1"))(_.transform(op[String, String]))
|
|
|
|
|
val f2 = Flow[String].section(name("f2"))(_.transform(op[String, String]))
|
|
|
|
|
val f3 = Flow[String].section(name("f3"))(_.transform(op[String, String]))
|
|
|
|
|
val f4 = Flow[String].section(name("f4"))(_.transform(op[String, String]))
|
|
|
|
|
val f5 = Flow[String].section(name("f5"))(_.transform(op[String, String]))
|
|
|
|
|
val f6 = Flow[String].section(name("f6"))(_.transform(op[String, String]))
|
2014-10-02 17:32:08 +02:00
|
|
|
|
2014-10-17 14:05:50 +02:00
|
|
|
val in1 = Source(List("a", "b", "c"))
|
|
|
|
|
val in2 = Source(List("d", "e", "f"))
|
|
|
|
|
val out1 = Sink.publisher[String]
|
2014-11-06 18:13:06 +01:00
|
|
|
val out2 = Sink.head[String]
|
2014-09-04 14:31:22 +02:00
|
|
|
|
|
|
|
|
"FlowGraph" should {
|
|
|
|
|
"build simple merge" in {
|
|
|
|
|
FlowGraph { b ⇒
|
|
|
|
|
val merge = Merge[String]
|
|
|
|
|
b.
|
|
|
|
|
addEdge(in1, f1, merge).
|
|
|
|
|
addEdge(in2, f2, merge).
|
|
|
|
|
addEdge(merge, f3, out1)
|
2014-09-10 11:25:38 +02:00
|
|
|
}.run()
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"build simple broadcast" in {
|
|
|
|
|
FlowGraph { b ⇒
|
|
|
|
|
val bcast = Broadcast[String]
|
|
|
|
|
b.
|
|
|
|
|
addEdge(in1, f1, bcast).
|
|
|
|
|
addEdge(bcast, f2, out1).
|
|
|
|
|
addEdge(bcast, f3, out2)
|
2014-09-10 11:25:38 +02:00
|
|
|
}.run()
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-03 09:00:08 +03:00
|
|
|
"build simple balance" in {
|
|
|
|
|
FlowGraph { b ⇒
|
|
|
|
|
val balance = Balance[String]
|
|
|
|
|
b.
|
|
|
|
|
addEdge(in1, f1, balance).
|
|
|
|
|
addEdge(balance, f2, out1).
|
|
|
|
|
addEdge(balance, f3, out2)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 14:31:22 +02:00
|
|
|
"build simple merge - broadcast" in {
|
|
|
|
|
FlowGraph { b ⇒
|
|
|
|
|
val merge = Merge[String]
|
|
|
|
|
val bcast = Broadcast[String]
|
|
|
|
|
b.
|
|
|
|
|
addEdge(in1, f1, merge).
|
|
|
|
|
addEdge(in2, f2, merge).
|
|
|
|
|
addEdge(merge, f3, bcast).
|
|
|
|
|
addEdge(bcast, f4, out1).
|
|
|
|
|
addEdge(bcast, f5, out2)
|
2014-09-10 11:25:38 +02:00
|
|
|
}.run()
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"build simple merge - broadcast with implicits" in {
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
val merge = Merge[String]
|
|
|
|
|
val bcast = Broadcast[String]
|
|
|
|
|
in1 ~> f1 ~> merge ~> f2 ~> bcast ~> f3 ~> out1
|
|
|
|
|
in2 ~> f4 ~> merge
|
|
|
|
|
bcast ~> f5 ~> out2
|
2014-09-10 11:25:38 +02:00
|
|
|
}.run()
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* in ---> f1 -+-> f2 -+-> f3 ---> out1
|
|
|
|
|
* ^ |
|
|
|
|
|
* | V
|
|
|
|
|
* f5 <-+- f4
|
|
|
|
|
* |
|
|
|
|
|
* V
|
|
|
|
|
* f6 ---> out2
|
|
|
|
|
*/
|
|
|
|
|
"detect cycle in " in {
|
|
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
FlowGraph { b ⇒
|
|
|
|
|
val merge = Merge[String]
|
|
|
|
|
val bcast1 = Broadcast[String]
|
|
|
|
|
val bcast2 = Broadcast[String]
|
2014-10-02 17:32:08 +02:00
|
|
|
val feedbackLoopBuffer = Flow[String].buffer(10, OverflowStrategy.dropBuffer)
|
2014-09-04 14:31:22 +02:00
|
|
|
b.
|
|
|
|
|
addEdge(in1, f1, merge).
|
|
|
|
|
addEdge(merge, f2, bcast1).
|
|
|
|
|
addEdge(bcast1, f3, out1).
|
2014-09-11 08:56:58 +02:00
|
|
|
addEdge(bcast1, feedbackLoopBuffer, bcast2).
|
2014-09-04 14:31:22 +02:00
|
|
|
addEdge(bcast2, f5, merge). // cycle
|
|
|
|
|
addEdge(bcast2, f6, out2)
|
|
|
|
|
}
|
|
|
|
|
}.getMessage.toLowerCase should include("cycle")
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"express complex topologies in a readable way" in {
|
2014-09-11 08:56:58 +02:00
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
b.allowCycles()
|
|
|
|
|
val merge = Merge[String]
|
|
|
|
|
val bcast1 = Broadcast[String]
|
|
|
|
|
val bcast2 = Broadcast[String]
|
2014-10-02 17:32:08 +02:00
|
|
|
val feedbackLoopBuffer = Flow[String].buffer(10, OverflowStrategy.dropBuffer)
|
2014-09-11 08:56:58 +02:00
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
in1 ~> f1 ~> merge ~> f2 ~> bcast1 ~> f3 ~> out1
|
|
|
|
|
bcast1 ~> feedbackLoopBuffer ~> bcast2 ~> f5 ~> merge
|
|
|
|
|
bcast2 ~> f6 ~> out2
|
|
|
|
|
}.run()
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-10 12:56:18 +02:00
|
|
|
"build broadcast - merge" in {
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
val bcast = Broadcast[String]
|
|
|
|
|
val bcast2 = Broadcast[String]
|
|
|
|
|
val merge = Merge[String]
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
in1 ~> f1 ~> bcast ~> f2 ~> merge ~> f3 ~> out1
|
2014-09-11 07:43:05 +02:00
|
|
|
bcast ~> f4 ~> merge
|
2014-09-10 12:56:18 +02:00
|
|
|
}.run()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"build wikipedia Topological_sorting" in {
|
|
|
|
|
// see https://en.wikipedia.org/wiki/Topological_sorting#mediaviewer/File:Directed_acyclic_graph.png
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
val b3 = Broadcast[String]
|
|
|
|
|
val b7 = Broadcast[String]
|
|
|
|
|
val b11 = Broadcast[String]
|
|
|
|
|
val m8 = Merge[String]
|
|
|
|
|
val m9 = Merge[String]
|
|
|
|
|
val m10 = Merge[String]
|
|
|
|
|
val m11 = Merge[String]
|
2014-10-17 14:05:50 +02:00
|
|
|
val in3 = Source(List("b"))
|
|
|
|
|
val in5 = Source(List("b"))
|
|
|
|
|
val in7 = Source(List("a"))
|
|
|
|
|
val out2 = Sink.publisher[String]
|
|
|
|
|
val out9 = Sink.publisher[String]
|
|
|
|
|
val out10 = Sink.publisher[String]
|
2014-12-01 20:07:55 +02:00
|
|
|
def f(s: String) = Flow[String].section(name(s))(_.transform(op[String, String]))
|
2014-09-10 12:56:18 +02:00
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
|
|
|
|
|
in7 ~> f("a") ~> b7 ~> f("b") ~> m11 ~> f("c") ~> b11 ~> f("d") ~> out2
|
|
|
|
|
b11 ~> f("e") ~> m9 ~> f("f") ~> out9
|
|
|
|
|
b7 ~> f("g") ~> m8 ~> f("h") ~> m9
|
|
|
|
|
b11 ~> f("i") ~> m10 ~> f("j") ~> out10
|
|
|
|
|
in5 ~> f("k") ~> m11
|
|
|
|
|
in3 ~> f("l") ~> b3 ~> f("m") ~> m8
|
|
|
|
|
b3 ~> f("n") ~> m10
|
|
|
|
|
}.run()
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-01 09:41:34 +02:00
|
|
|
"attachSource and attachSink" in {
|
2014-09-04 14:31:22 +02:00
|
|
|
val mg = FlowGraph { b ⇒
|
|
|
|
|
val merge = Merge[String]
|
2014-10-01 09:41:34 +02:00
|
|
|
val undefinedSource1 = UndefinedSource[String]
|
|
|
|
|
val undefinedSource2 = UndefinedSource[String]
|
|
|
|
|
val undefinedSink1 = UndefinedSink[String]
|
2014-09-04 14:31:22 +02:00
|
|
|
b.
|
2014-10-01 09:41:34 +02:00
|
|
|
addEdge(undefinedSource1, f1, merge).
|
2014-12-17 11:04:48 +01:00
|
|
|
addEdge(undefinedSource2, f2, merge).
|
2014-10-01 09:41:34 +02:00
|
|
|
addEdge(merge, f3, undefinedSink1)
|
2014-09-04 14:31:22 +02:00
|
|
|
|
2014-10-01 09:41:34 +02:00
|
|
|
b.attachSource(undefinedSource1, in1)
|
2014-12-17 11:04:48 +01:00
|
|
|
b.attachSource(undefinedSource2, in2)
|
2014-10-01 09:41:34 +02:00
|
|
|
b.attachSink(undefinedSink1, out1)
|
2014-09-04 14:31:22 +02:00
|
|
|
|
|
|
|
|
}.run()
|
2014-10-17 14:05:50 +02:00
|
|
|
mg.get(out1) should not be (null)
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"build partial flow graphs" in {
|
2014-10-01 09:41:34 +02:00
|
|
|
val undefinedSource1 = UndefinedSource[String]
|
|
|
|
|
val undefinedSource2 = UndefinedSource[String]
|
|
|
|
|
val undefinedSink1 = UndefinedSink[String]
|
2014-09-04 14:31:22 +02:00
|
|
|
val bcast = Broadcast[String]
|
|
|
|
|
|
|
|
|
|
val partial1 = PartialFlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
val merge = Merge[String]
|
2014-10-01 09:41:34 +02:00
|
|
|
undefinedSource1 ~> f1 ~> merge ~> f2 ~> bcast ~> f3 ~> undefinedSink1
|
|
|
|
|
undefinedSource2 ~> f4 ~> merge
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
2014-10-01 09:41:34 +02:00
|
|
|
partial1.undefinedSources should be(Set(undefinedSource1, undefinedSource2))
|
|
|
|
|
partial1.undefinedSinks should be(Set(undefinedSink1))
|
2014-09-04 14:31:22 +02:00
|
|
|
|
2014-12-17 11:04:48 +01:00
|
|
|
val undefinedSink2 = UndefinedSink[String]
|
|
|
|
|
|
2014-09-04 14:31:22 +02:00
|
|
|
val partial2 = PartialFlowGraph(partial1) { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
2014-10-01 09:41:34 +02:00
|
|
|
b.attachSource(undefinedSource1, in1)
|
|
|
|
|
b.attachSource(undefinedSource2, in2)
|
2014-12-17 11:04:48 +01:00
|
|
|
bcast ~> f5 ~> undefinedSink2
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
2014-10-01 09:41:34 +02:00
|
|
|
partial2.undefinedSources should be(Set.empty)
|
2014-12-17 11:04:48 +01:00
|
|
|
partial2.undefinedSinks should be(Set(undefinedSink1, undefinedSink2))
|
2014-10-01 09:41:34 +02:00
|
|
|
|
|
|
|
|
FlowGraph(partial2) { b ⇒
|
|
|
|
|
b.attachSink(undefinedSink1, out1)
|
2014-12-17 11:04:48 +01:00
|
|
|
b.attachSink(undefinedSink2, out2)
|
2014-10-01 09:41:34 +02:00
|
|
|
}.run()
|
2014-09-04 14:31:22 +02:00
|
|
|
|
2014-10-01 09:41:34 +02:00
|
|
|
FlowGraph(partial2) { b ⇒
|
2014-10-31 10:43:42 +02:00
|
|
|
b.attachSink(undefinedSink1, f1.to(out1))
|
2014-12-17 11:04:48 +01:00
|
|
|
b.attachSink(undefinedSink2, f2.to(out2))
|
2014-10-01 09:41:34 +02:00
|
|
|
}.run()
|
|
|
|
|
|
|
|
|
|
FlowGraph(partial1) { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
2014-10-31 10:43:42 +02:00
|
|
|
b.attachSink(undefinedSink1, f1.to(out1))
|
|
|
|
|
b.attachSource(undefinedSource1, Source(List("a", "b", "c")).via(f1))
|
|
|
|
|
b.attachSource(undefinedSource2, Source(List("d", "e", "f")).via(f2))
|
2014-10-01 09:41:34 +02:00
|
|
|
bcast ~> f5 ~> out2
|
2014-09-10 11:25:38 +02:00
|
|
|
}.run()
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-11 07:45:19 +02:00
|
|
|
"make it optional to specify flows" in {
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
val merge = Merge[String]
|
|
|
|
|
val bcast = Broadcast[String]
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
in1 ~> merge ~> bcast ~> out1
|
|
|
|
|
in2 ~> merge
|
|
|
|
|
bcast ~> out2
|
|
|
|
|
}.run()
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-09 15:22:49 +02:00
|
|
|
"chain input and output ports" in {
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
val zip = Zip[Int, String]
|
2014-10-17 14:05:50 +02:00
|
|
|
val out = Sink.publisher[(Int, String)]
|
2014-09-09 15:22:49 +02:00
|
|
|
import FlowGraphImplicits._
|
2014-10-02 17:32:08 +02:00
|
|
|
Source(List(1, 2, 3)) ~> zip.left ~> out
|
|
|
|
|
Source(List("a", "b", "c")) ~> zip.right
|
2014-09-09 15:22:49 +02:00
|
|
|
}.run()
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 14:03:22 +02:00
|
|
|
"build unzip - zip" in {
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
val zip = Zip[Int, String]
|
|
|
|
|
val unzip = Unzip[Int, String]
|
2014-10-17 14:05:50 +02:00
|
|
|
val out = Sink.publisher[(Int, String)]
|
2014-09-30 14:03:22 +02:00
|
|
|
import FlowGraphImplicits._
|
2014-10-02 17:32:08 +02:00
|
|
|
Source(List(1 -> "a", 2 -> "b", 3 -> "c")) ~> unzip.in
|
|
|
|
|
unzip.left ~> Flow[Int].map(_ * 2) ~> zip.left
|
2014-09-30 14:03:22 +02:00
|
|
|
unzip.right ~> zip.right
|
|
|
|
|
zip.out ~> out
|
|
|
|
|
}.run()
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-09 15:22:49 +02:00
|
|
|
"distinguish between input and output ports" in {
|
|
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
val zip = Zip[Int, String]
|
2014-09-30 14:03:22 +02:00
|
|
|
val unzip = Unzip[Int, String]
|
2014-10-17 14:05:50 +02:00
|
|
|
val wrongOut = Sink.publisher[(Int, Int)]
|
|
|
|
|
val whatever = Sink.publisher[Any]
|
2014-10-02 17:32:08 +02:00
|
|
|
"Flow(List(1, 2, 3)) ~> zip.left ~> wrongOut" shouldNot compile
|
|
|
|
|
"""Flow(List("a", "b", "c")) ~> zip.left""" shouldNot compile
|
|
|
|
|
"""Flow(List("a", "b", "c")) ~> zip.out""" shouldNot compile
|
2014-09-09 15:22:49 +02:00
|
|
|
"zip.left ~> zip.right" shouldNot compile
|
2014-10-02 17:32:08 +02:00
|
|
|
"Flow(List(1, 2, 3)) ~> zip.left ~> wrongOut" shouldNot compile
|
|
|
|
|
"""Flow(List(1 -> "a", 2 -> "b", 3 -> "c")) ~> unzip.in ~> whatever""" shouldNot compile
|
2014-09-09 15:22:49 +02:00
|
|
|
}
|
|
|
|
|
}.getMessage should include("empty")
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-12 12:12:32 +02:00
|
|
|
"check maximumInputCount" in {
|
|
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
val bcast = Broadcast[String]
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
in1 ~> bcast ~> out1
|
|
|
|
|
in2 ~> bcast // wrong
|
|
|
|
|
}
|
|
|
|
|
}.getMessage should include("at most 1 incoming")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"check maximumOutputCount" in {
|
|
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
val merge = Merge[String]
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
in1 ~> merge ~> out1
|
2014-10-30 17:18:14 +01:00
|
|
|
in2 ~> merge
|
2014-09-12 12:12:32 +02:00
|
|
|
merge ~> out2 // wrong
|
|
|
|
|
}
|
|
|
|
|
}.getMessage should include("at most 1 outgoing")
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 15:40:08 +02:00
|
|
|
"build with variance" in {
|
2014-10-17 14:05:50 +02:00
|
|
|
val out = Sink(SubscriberProbe[Fruit]())
|
2014-09-30 15:40:08 +02:00
|
|
|
FlowGraph { b ⇒
|
|
|
|
|
val merge = Merge[Fruit]
|
|
|
|
|
b.
|
2014-11-09 21:09:50 +01:00
|
|
|
addEdge(Source[Fruit](apples), Flow[Fruit], merge).
|
|
|
|
|
addEdge(Source[Apple](apples), Flow[Apple], merge).
|
2014-10-02 17:32:08 +02:00
|
|
|
addEdge(merge, Flow[Fruit].map(identity), out)
|
2014-09-30 15:40:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"build with implicits and variance" in {
|
|
|
|
|
PartialFlowGraph { implicit b ⇒
|
2014-10-17 14:05:50 +02:00
|
|
|
val inA = Source(PublisherProbe[Fruit]())
|
|
|
|
|
val inB = Source(PublisherProbe[Apple]())
|
|
|
|
|
val outA = Sink(SubscriberProbe[Fruit]())
|
|
|
|
|
val outB = Sink(SubscriberProbe[Fruit]())
|
2014-09-30 15:40:08 +02:00
|
|
|
val merge = Merge[Fruit]
|
2014-10-01 13:24:57 +02:00
|
|
|
val unzip = Unzip[Int, String]
|
2014-10-17 14:05:50 +02:00
|
|
|
val whatever = Sink.publisher[Any]
|
2014-09-30 15:40:08 +02:00
|
|
|
import FlowGraphImplicits._
|
2014-11-09 21:09:50 +01:00
|
|
|
Source[Fruit](apples) ~> merge
|
|
|
|
|
Source[Apple](apples) ~> merge
|
2014-09-30 15:40:08 +02:00
|
|
|
inA ~> merge
|
|
|
|
|
inB ~> merge
|
2014-10-02 17:32:08 +02:00
|
|
|
inA ~> Flow[Fruit].map(identity) ~> merge
|
|
|
|
|
inB ~> Flow[Apple].map(identity) ~> merge
|
2014-10-01 09:41:34 +02:00
|
|
|
UndefinedSource[Apple] ~> merge
|
|
|
|
|
UndefinedSource[Apple] ~> Flow[Fruit].map(identity) ~> merge
|
|
|
|
|
UndefinedSource[Apple] ~> Flow[Apple].map(identity) ~> merge
|
2014-10-02 17:32:08 +02:00
|
|
|
merge ~> Flow[Fruit].map(identity) ~> outA
|
|
|
|
|
|
2014-11-09 21:09:50 +01:00
|
|
|
Source[Apple](apples) ~> Broadcast[Apple] ~> merge
|
|
|
|
|
Source[Apple](apples) ~> Broadcast[Apple] ~> outB
|
|
|
|
|
Source[Apple](apples) ~> Broadcast[Apple] ~> UndefinedSink[Fruit]
|
2014-09-30 15:40:08 +02:00
|
|
|
inB ~> Broadcast[Apple] ~> merge
|
|
|
|
|
|
2014-10-02 17:32:08 +02:00
|
|
|
Source(List(1 -> "a", 2 -> "b", 3 -> "c")) ~> unzip.in
|
2014-10-01 13:24:57 +02:00
|
|
|
unzip.right ~> whatever
|
2014-10-01 09:41:34 +02:00
|
|
|
unzip.left ~> UndefinedSink[Any]
|
2014-10-01 13:24:57 +02:00
|
|
|
|
2014-10-01 09:41:34 +02:00
|
|
|
"UndefinedSource[Fruit] ~> Flow[Apple].map(identity) ~> merge" shouldNot compile
|
|
|
|
|
"UndefinedSource[Fruit] ~> Broadcast[Apple]" shouldNot compile
|
2014-09-30 15:40:08 +02:00
|
|
|
"merge ~> Broadcast[Apple]" shouldNot compile
|
2014-10-02 17:32:08 +02:00
|
|
|
"merge ~> Flow[Fruit].map(identity) ~> Broadcast[Apple]" shouldNot compile
|
2014-09-30 15:40:08 +02:00
|
|
|
"inB ~> merge ~> Broadcast[Apple]" shouldNot compile
|
|
|
|
|
"inA ~> Broadcast[Apple]" shouldNot compile
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-01 09:41:34 +02:00
|
|
|
"build with plain flow without junctions" in {
|
|
|
|
|
FlowGraph { b ⇒
|
|
|
|
|
b.addEdge(in1, f1, out1)
|
|
|
|
|
}.run()
|
|
|
|
|
FlowGraph { b ⇒
|
2014-10-31 10:43:42 +02:00
|
|
|
b.addEdge(in1, f1, f2.to(out1))
|
2014-10-01 09:41:34 +02:00
|
|
|
}.run()
|
|
|
|
|
FlowGraph { b ⇒
|
2014-10-31 10:43:42 +02:00
|
|
|
b.addEdge(in1.via(f1), f2, out1)
|
2014-10-01 09:41:34 +02:00
|
|
|
}.run()
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
in1 ~> f1 ~> out1
|
|
|
|
|
}.run()
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
in1 ~> out1
|
|
|
|
|
}.run()
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
2014-10-31 10:43:42 +02:00
|
|
|
in1 ~> f1.to(out1)
|
2014-10-01 09:41:34 +02:00
|
|
|
}.run()
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
2014-10-31 10:43:42 +02:00
|
|
|
in1.via(f1) ~> out1
|
2014-10-01 09:41:34 +02:00
|
|
|
}.run()
|
|
|
|
|
FlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
2014-10-31 10:43:42 +02:00
|
|
|
in1.via(f1) ~> f2.to(out1)
|
2014-10-01 09:41:34 +02:00
|
|
|
}.run()
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 10:43:42 +02:00
|
|
|
"build all combinations with implicits" when {
|
|
|
|
|
|
|
|
|
|
"Source is connected directly" in {
|
|
|
|
|
PartialFlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
Source.empty[Int] ~> Flow[Int]
|
|
|
|
|
Source.empty[Int] ~> Broadcast[Int]
|
|
|
|
|
Source.empty[Int] ~> Sink.ignore
|
|
|
|
|
Source.empty[Int] ~> UndefinedSink[Int]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"Source is connected through flow" in {
|
|
|
|
|
PartialFlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
Source.empty[Int] ~> Flow[Int] ~> Flow[Int]
|
|
|
|
|
Source.empty[Int] ~> Flow[Int] ~> Broadcast[Int]
|
|
|
|
|
Source.empty[Int] ~> Flow[Int] ~> Sink.ignore
|
|
|
|
|
Source.empty[Int] ~> Flow[Int] ~> UndefinedSink[Int]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"Junction is connected directly" in {
|
|
|
|
|
PartialFlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
Broadcast[Int] ~> Flow[Int]
|
|
|
|
|
Broadcast[Int] ~> Broadcast[Int]
|
|
|
|
|
Broadcast[Int] ~> Sink.ignore
|
|
|
|
|
Broadcast[Int] ~> UndefinedSink[Int]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"Junction is connected through flow" in {
|
|
|
|
|
PartialFlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
Broadcast[Int] ~> Flow[Int] ~> Flow[Int]
|
|
|
|
|
Broadcast[Int] ~> Flow[Int] ~> Broadcast[Int]
|
|
|
|
|
Broadcast[Int] ~> Flow[Int] ~> Sink.ignore
|
|
|
|
|
Broadcast[Int] ~> Flow[Int] ~> UndefinedSink[Int]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 11:49:07 +01:00
|
|
|
"Junction is connected through GraphBackedFlow" in {
|
2015-01-22 12:50:59 +01:00
|
|
|
val gflow = Flow[Int, String]() { implicit builder ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
|
|
|
|
|
val in = UndefinedSource[Int]
|
|
|
|
|
val out = UndefinedSink[String]
|
|
|
|
|
|
|
|
|
|
in ~> Flow[Int].map(_.toString) ~> out
|
|
|
|
|
|
|
|
|
|
(in, out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val sink = Sink.fold[Int, Int](0)(_ + _)
|
|
|
|
|
val graph = FlowGraph { implicit builder ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
|
|
|
|
|
val merge = Merge[Int]
|
|
|
|
|
|
|
|
|
|
Source(List(1, 2, 3)) ~> merge
|
|
|
|
|
Source.empty[Int] ~> merge
|
|
|
|
|
merge ~> gflow.map(_.toInt) ~> sink
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
graph.run()
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 10:43:42 +02:00
|
|
|
"UndefinedSource is connected directly" in {
|
|
|
|
|
PartialFlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
UndefinedSource[Int] ~> Flow[Int]
|
|
|
|
|
UndefinedSource[Int] ~> Broadcast[Int]
|
|
|
|
|
UndefinedSource[Int] ~> Sink.ignore
|
|
|
|
|
UndefinedSource[Int] ~> UndefinedSink[Int]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"UndefinedSource is connected through flow" in {
|
|
|
|
|
PartialFlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
UndefinedSource[Int] ~> Flow[Int] ~> Flow[Int]
|
|
|
|
|
UndefinedSource[Int] ~> Flow[Int] ~> Broadcast[Int]
|
|
|
|
|
UndefinedSource[Int] ~> Flow[Int] ~> Sink.ignore
|
|
|
|
|
UndefinedSource[Int] ~> Flow[Int] ~> UndefinedSink[Int]
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-22 12:50:59 +01:00
|
|
|
|
2014-10-31 10:43:42 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-01 09:41:34 +02:00
|
|
|
"build partial with only undefined sources and sinks" in {
|
|
|
|
|
PartialFlowGraph { b ⇒
|
|
|
|
|
b.addEdge(UndefinedSource[String], f1, UndefinedSink[String])
|
|
|
|
|
}
|
|
|
|
|
PartialFlowGraph { b ⇒
|
|
|
|
|
b.addEdge(UndefinedSource[String], f1, out1)
|
|
|
|
|
}
|
|
|
|
|
PartialFlowGraph { b ⇒
|
|
|
|
|
b.addEdge(in1, f1, UndefinedSink[String])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 15:13:26 +02:00
|
|
|
"support interconnect between two partial flow graphs" in {
|
|
|
|
|
val output1 = UndefinedSink[String]
|
|
|
|
|
val output2 = UndefinedSink[String]
|
|
|
|
|
val partial1 = PartialFlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
val bcast = Broadcast[String]
|
|
|
|
|
in1 ~> bcast ~> output1
|
|
|
|
|
bcast ~> output2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val input1 = UndefinedSource[String]
|
|
|
|
|
val input2 = UndefinedSource[String]
|
|
|
|
|
val partial2 = PartialFlowGraph { implicit b ⇒
|
|
|
|
|
import FlowGraphImplicits._
|
|
|
|
|
val merge = Merge[String]
|
|
|
|
|
input1 ~> merge ~> out1
|
|
|
|
|
input2 ~> merge
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FlowGraph { b ⇒
|
|
|
|
|
b.importPartialFlowGraph(partial1)
|
|
|
|
|
b.importPartialFlowGraph(partial2)
|
|
|
|
|
b.connect(output1, f1, input1)
|
|
|
|
|
b.connect(output2, f2, input2)
|
|
|
|
|
}.run()
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
}
|