2014-09-02 18:13:36 +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-02 18:13:36 +02:00
|
|
|
|
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
import scala.util.{ Failure, Success }
|
2014-10-27 14:35:41 +01:00
|
|
|
import scala.util.control.NoStackTrace
|
|
|
|
|
|
2015-01-27 18:29:20 +01:00
|
|
|
import akka.stream.ActorFlowMaterializer
|
|
|
|
|
import akka.stream.ActorFlowMaterializerSettings
|
2014-10-27 14:35:41 +01:00
|
|
|
import akka.stream.testkit.{ AkkaSpec, StreamTestKit }
|
|
|
|
|
import akka.stream.testkit.AkkaSpec
|
|
|
|
|
import akka.stream.testkit.ScriptedTest
|
|
|
|
|
import akka.testkit.TestProbe
|
2014-09-02 18:13:36 +02:00
|
|
|
|
|
|
|
|
class FlowOnCompleteSpec extends AkkaSpec with ScriptedTest {
|
|
|
|
|
|
2015-01-27 18:29:20 +01:00
|
|
|
val settings = ActorFlowMaterializerSettings(system)
|
2014-09-02 18:13:36 +02:00
|
|
|
.withInputBuffer(initialSize = 2, maxSize = 16)
|
|
|
|
|
|
2015-01-27 18:29:20 +01:00
|
|
|
implicit val materializer = ActorFlowMaterializer(settings)
|
2014-09-02 18:13:36 +02:00
|
|
|
|
|
|
|
|
"A Flow with onComplete" must {
|
|
|
|
|
|
|
|
|
|
"invoke callback on normal completion" in {
|
|
|
|
|
val onCompleteProbe = TestProbe()
|
|
|
|
|
val p = StreamTestKit.PublisherProbe[Int]()
|
2014-10-31 10:43:42 +02:00
|
|
|
Source(p).to(Sink.onComplete[Int](onCompleteProbe.ref ! _)).run()
|
2014-09-02 18:13:36 +02:00
|
|
|
val proc = p.expectSubscription
|
|
|
|
|
proc.expectRequest()
|
|
|
|
|
proc.sendNext(42)
|
|
|
|
|
onCompleteProbe.expectNoMsg(100.millis)
|
|
|
|
|
proc.sendComplete()
|
|
|
|
|
onCompleteProbe.expectMsg(Success(()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"yield the first error" in {
|
|
|
|
|
val onCompleteProbe = TestProbe()
|
|
|
|
|
val p = StreamTestKit.PublisherProbe[Int]()
|
2014-10-31 10:43:42 +02:00
|
|
|
Source(p).to(Sink.onComplete[Int](onCompleteProbe.ref ! _)).run()
|
2014-09-02 18:13:36 +02:00
|
|
|
val proc = p.expectSubscription
|
|
|
|
|
proc.expectRequest()
|
|
|
|
|
val ex = new RuntimeException("ex") with NoStackTrace
|
|
|
|
|
proc.sendError(ex)
|
|
|
|
|
onCompleteProbe.expectMsg(Failure(ex))
|
|
|
|
|
onCompleteProbe.expectNoMsg(100.millis)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"invoke callback for an empty stream" in {
|
|
|
|
|
val onCompleteProbe = TestProbe()
|
|
|
|
|
val p = StreamTestKit.PublisherProbe[Int]()
|
2014-10-31 10:43:42 +02:00
|
|
|
Source(p).to(Sink.onComplete[Int](onCompleteProbe.ref ! _)).run()
|
2014-09-02 18:13:36 +02:00
|
|
|
val proc = p.expectSubscription
|
|
|
|
|
proc.expectRequest()
|
|
|
|
|
proc.sendComplete()
|
|
|
|
|
onCompleteProbe.expectMsg(Success(()))
|
|
|
|
|
onCompleteProbe.expectNoMsg(100.millis)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"invoke callback after transform and foreach steps " in {
|
|
|
|
|
val onCompleteProbe = TestProbe()
|
|
|
|
|
val p = StreamTestKit.PublisherProbe[Int]()
|
|
|
|
|
import system.dispatcher // for the Future.onComplete
|
2014-10-17 14:05:50 +02:00
|
|
|
val foreachSink = Sink.foreach[Int] {
|
2014-09-02 18:13:36 +02:00
|
|
|
x ⇒ onCompleteProbe.ref ! ("foreach-" + x)
|
|
|
|
|
}
|
2014-10-02 13:34:27 +02:00
|
|
|
val future = Source(p).map { x ⇒
|
2014-09-02 18:13:36 +02:00
|
|
|
onCompleteProbe.ref ! ("map-" + x)
|
|
|
|
|
x
|
2014-10-17 14:05:50 +02:00
|
|
|
}.runWith(foreachSink)
|
2014-10-02 13:34:27 +02:00
|
|
|
future onComplete { onCompleteProbe.ref ! _ }
|
2014-09-02 18:13:36 +02:00
|
|
|
val proc = p.expectSubscription
|
|
|
|
|
proc.expectRequest()
|
|
|
|
|
proc.sendNext(42)
|
|
|
|
|
proc.sendComplete()
|
|
|
|
|
onCompleteProbe.expectMsg("map-42")
|
|
|
|
|
onCompleteProbe.expectMsg("foreach-42")
|
|
|
|
|
onCompleteProbe.expectMsg(Success(()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-16 15:07:41 +01:00
|
|
|
}
|