2014-09-10 16:56:41 +03:00
|
|
|
/**
|
2016-02-23 12:58:39 +01:00
|
|
|
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
|
2014-09-10 16:56:41 +03:00
|
|
|
*/
|
2014-10-27 14:35:41 +01:00
|
|
|
package akka.stream.scaladsl
|
2014-09-10 16:56:41 +03:00
|
|
|
|
|
|
|
|
import akka.actor.ActorSystem
|
2015-06-23 18:28:53 +02:00
|
|
|
import akka.stream.ActorMaterializer
|
|
|
|
|
import akka.stream.ActorMaterializerSettings
|
2016-02-25 14:27:45 +01:00
|
|
|
import akka.stream.testkit.TestSubscriber
|
2014-09-10 16:56:41 +03:00
|
|
|
import org.reactivestreams.Subscriber
|
|
|
|
|
import org.scalatest.Matchers
|
2016-02-25 14:27:45 +01:00
|
|
|
import akka.testkit.AkkaSpec
|
2014-09-10 16:56:41 +03:00
|
|
|
|
|
|
|
|
class FlowAppendSpec extends AkkaSpec with River {
|
|
|
|
|
|
2015-06-23 18:28:53 +02:00
|
|
|
val settings = ActorMaterializerSettings(system)
|
|
|
|
|
implicit val materializer = ActorMaterializer(settings)
|
2014-09-10 16:56:41 +03:00
|
|
|
|
2014-10-02 17:32:08 +02:00
|
|
|
"Flow" should {
|
|
|
|
|
"append Flow" in riverOf[String] { subscriber ⇒
|
2014-10-31 10:43:42 +02:00
|
|
|
val flow = Flow[Int].via(otherFlow)
|
2015-12-17 11:48:30 +02:00
|
|
|
Source(elements).via(flow).to(Sink.fromSubscriber(subscriber)).run()
|
2014-09-10 16:56:41 +03:00
|
|
|
}
|
|
|
|
|
|
2014-10-02 17:32:08 +02:00
|
|
|
"append Sink" in riverOf[String] { subscriber ⇒
|
2015-12-17 11:48:30 +02:00
|
|
|
val sink = Flow[Int].to(otherFlow.to(Sink.fromSubscriber(subscriber)))
|
2014-10-31 10:43:42 +02:00
|
|
|
Source(elements).to(sink).run()
|
2014-09-10 16:56:41 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-02 17:32:08 +02:00
|
|
|
"Source" should {
|
|
|
|
|
"append Flow" in riverOf[String] { subscriber ⇒
|
|
|
|
|
Source(elements)
|
2014-10-31 10:43:42 +02:00
|
|
|
.via(otherFlow)
|
2015-12-17 11:48:30 +02:00
|
|
|
.to(Sink.fromSubscriber(subscriber)).run()
|
2014-09-10 16:56:41 +03:00
|
|
|
}
|
|
|
|
|
|
2014-10-02 17:32:08 +02:00
|
|
|
"append Sink" in riverOf[String] { subscriber ⇒
|
|
|
|
|
Source(elements)
|
2015-12-17 11:48:30 +02:00
|
|
|
.to(otherFlow.to(Sink.fromSubscriber(subscriber)))
|
2014-09-10 16:56:41 +03:00
|
|
|
.run()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait River { self: Matchers ⇒
|
|
|
|
|
|
2015-08-01 00:13:14 +02:00
|
|
|
val elements = 1 to 10
|
2014-10-02 17:32:08 +02:00
|
|
|
val otherFlow = Flow[Int].map(_.toString)
|
2014-09-10 16:56:41 +03:00
|
|
|
|
|
|
|
|
def riverOf[T](flowConstructor: Subscriber[T] ⇒ Unit)(implicit system: ActorSystem) = {
|
2015-04-24 11:45:03 +03:00
|
|
|
val subscriber = TestSubscriber.manualProbe[T]()
|
2014-09-10 16:56:41 +03:00
|
|
|
|
|
|
|
|
flowConstructor(subscriber)
|
|
|
|
|
|
|
|
|
|
val subscription = subscriber.expectSubscription()
|
|
|
|
|
subscription.request(elements.size)
|
2015-04-24 11:45:03 +03:00
|
|
|
elements.foreach { el ⇒
|
|
|
|
|
subscriber.expectNext() shouldBe el.toString
|
|
|
|
|
}
|
2014-09-10 16:56:41 +03:00
|
|
|
subscription.request(1)
|
|
|
|
|
subscriber.expectComplete()
|
|
|
|
|
}
|
|
|
|
|
}
|