MaybeSource rewritten as GraphStage #22789
This commit is contained in:
parent
8a095ed23d
commit
2b2923f1b6
10 changed files with 272 additions and 145 deletions
|
|
@ -64,79 +64,6 @@ class SourceSpec extends StreamSpec with DefaultTimeout {
|
|||
}
|
||||
}
|
||||
|
||||
"Failed Source" must {
|
||||
"emit error immediately" in {
|
||||
val ex = new RuntimeException with NoStackTrace
|
||||
val p = Source.failed(ex).runWith(Sink.asPublisher(false))
|
||||
val c = TestSubscriber.manualProbe[Int]()
|
||||
p.subscribe(c)
|
||||
c.expectSubscriptionAndError(ex)
|
||||
|
||||
// reject additional subscriber
|
||||
val c2 = TestSubscriber.manualProbe[Int]()
|
||||
p.subscribe(c2)
|
||||
c2.expectSubscriptionAndError()
|
||||
}
|
||||
}
|
||||
|
||||
"Maybe Source" must {
|
||||
"complete materialized future with None when stream cancels" in Utils.assertAllStagesStopped {
|
||||
val neverSource = Source.maybe[Int]
|
||||
val pubSink = Sink.asPublisher[Int](false)
|
||||
|
||||
val (f, neverPub) = neverSource.toMat(pubSink)(Keep.both).run()
|
||||
|
||||
val c = TestSubscriber.manualProbe[Int]()
|
||||
neverPub.subscribe(c)
|
||||
val subs = c.expectSubscription()
|
||||
|
||||
subs.request(1000)
|
||||
c.expectNoMsg(300.millis)
|
||||
|
||||
subs.cancel()
|
||||
Await.result(f.future, 3.seconds) shouldEqual None
|
||||
}
|
||||
|
||||
"allow external triggering of empty completion" in Utils.assertAllStagesStopped {
|
||||
val neverSource = Source.maybe[Int].filter(_ ⇒ false)
|
||||
val counterSink = Sink.fold[Int, Int](0) { (acc, _) ⇒ acc + 1 }
|
||||
|
||||
val (neverPromise, counterFuture) = neverSource.toMat(counterSink)(Keep.both).run()
|
||||
|
||||
// external cancellation
|
||||
neverPromise.trySuccess(None) shouldEqual true
|
||||
|
||||
Await.result(counterFuture, 3.seconds) shouldEqual 0
|
||||
}
|
||||
|
||||
"allow external triggering of non-empty completion" in Utils.assertAllStagesStopped {
|
||||
val neverSource = Source.maybe[Int]
|
||||
val counterSink = Sink.head[Int]
|
||||
|
||||
val (neverPromise, counterFuture) = neverSource.toMat(counterSink)(Keep.both).run()
|
||||
|
||||
// external cancellation
|
||||
neverPromise.trySuccess(Some(6)) shouldEqual true
|
||||
|
||||
Await.result(counterFuture, 3.seconds) shouldEqual 6
|
||||
}
|
||||
|
||||
"allow external triggering of onError" in Utils.assertAllStagesStopped {
|
||||
val neverSource = Source.maybe[Int]
|
||||
val counterSink = Sink.fold[Int, Int](0) { (acc, _) ⇒ acc + 1 }
|
||||
|
||||
val (neverPromise, counterFuture) = neverSource.toMat(counterSink)(Keep.both).run()
|
||||
|
||||
// external cancellation
|
||||
neverPromise.failure(new Exception("Boom") with NoStackTrace)
|
||||
|
||||
val ready = Await.ready(counterFuture, 3.seconds)
|
||||
val Failure(ex) = ready.value.get
|
||||
ex.getMessage should include("Boom")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
"Composite Source" must {
|
||||
"merge from many inputs" in {
|
||||
val probes = immutable.Seq.fill(5)(TestPublisher.manualProbe[Int]())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue