2014-09-11 11:29:50 +03:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2014-10-27 14:35:41 +01:00
|
|
|
package akka.stream.scaladsl
|
2014-09-11 11:29:50 +03:00
|
|
|
|
2014-10-27 14:35:41 +01:00
|
|
|
import scala.concurrent.Await
|
|
|
|
|
import scala.concurrent.Future
|
2014-09-11 11:29:50 +03:00
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
import scala.util.control.NoStackTrace
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2015-01-27 18:29:20 +01:00
|
|
|
import akka.stream.ActorFlowMaterializer
|
2014-09-11 11:29:50 +03:00
|
|
|
import akka.stream.testkit.AkkaSpec
|
|
|
|
|
import akka.stream.testkit.StreamTestKit
|
|
|
|
|
import akka.testkit.TestLatch
|
2014-10-27 14:35:41 +01:00
|
|
|
import akka.testkit.TestProbe
|
2015-04-10 16:49:49 +02:00
|
|
|
import akka.stream.ActorOperationAttributes.supervisionStrategy
|
2015-02-04 09:26:32 +01:00
|
|
|
import akka.stream.Supervision.resumingDecider
|
|
|
|
|
import akka.stream.testkit.StreamTestKit.OnNext
|
|
|
|
|
import akka.stream.testkit.StreamTestKit.OnComplete
|
2015-03-03 10:57:25 +01:00
|
|
|
import akka.stream.impl.ReactiveStreamsCompliance
|
2014-09-11 11:29:50 +03:00
|
|
|
|
2014-09-29 17:54:09 +02:00
|
|
|
class FlowMapAsyncUnorderedSpec extends AkkaSpec {
|
2014-09-11 11:29:50 +03:00
|
|
|
|
2015-01-27 18:29:20 +01:00
|
|
|
implicit val materializer = ActorFlowMaterializer()
|
2014-09-11 11:29:50 +03:00
|
|
|
|
2014-09-29 17:54:09 +02:00
|
|
|
"A Flow with mapAsyncUnordered" must {
|
2014-09-11 11:29:50 +03:00
|
|
|
|
2014-09-29 17:54:09 +02:00
|
|
|
"produce future elements in the order they are ready" in {
|
2014-09-11 11:29:50 +03:00
|
|
|
val c = StreamTestKit.SubscriberProbe[Int]()
|
|
|
|
|
implicit val ec = system.dispatcher
|
2014-09-29 17:54:09 +02:00
|
|
|
val latch = (1 to 4).map(_ -> TestLatch(1)).toMap
|
2015-04-09 22:28:16 +02:00
|
|
|
val p = Source(1 to 4).mapAsyncUnordered(4, n ⇒ Future {
|
2014-09-29 17:54:09 +02:00
|
|
|
Await.ready(latch(n), 5.seconds)
|
2014-09-11 11:29:50 +03:00
|
|
|
n
|
2014-10-31 10:43:42 +02:00
|
|
|
}).to(Sink(c)).run()
|
2014-09-11 11:29:50 +03:00
|
|
|
val sub = c.expectSubscription()
|
2014-09-29 17:54:09 +02:00
|
|
|
sub.request(5)
|
|
|
|
|
latch(2).countDown()
|
|
|
|
|
c.expectNext(2)
|
|
|
|
|
latch(4).countDown()
|
|
|
|
|
c.expectNext(4)
|
|
|
|
|
latch(3).countDown()
|
|
|
|
|
c.expectNext(3)
|
|
|
|
|
latch(1).countDown()
|
|
|
|
|
c.expectNext(1)
|
2014-09-11 11:29:50 +03:00
|
|
|
c.expectComplete()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"not run more futures than requested elements" in {
|
|
|
|
|
val probe = TestProbe()
|
|
|
|
|
val c = StreamTestKit.SubscriberProbe[Int]()
|
|
|
|
|
implicit val ec = system.dispatcher
|
2015-04-09 22:28:16 +02:00
|
|
|
val p = Source(1 to 20).mapAsyncUnordered(4, n ⇒ Future {
|
2014-09-11 11:29:50 +03:00
|
|
|
probe.ref ! n
|
|
|
|
|
n
|
2014-10-31 10:43:42 +02:00
|
|
|
}).to(Sink(c)).run()
|
2014-09-11 11:29:50 +03:00
|
|
|
val sub = c.expectSubscription()
|
2015-04-09 22:28:16 +02:00
|
|
|
// first four run immediately
|
|
|
|
|
probe.expectMsgAllOf(1, 2, 3, 4)
|
|
|
|
|
c.expectNoMsg(200.millis)
|
|
|
|
|
probe.expectNoMsg(Duration.Zero)
|
2014-09-11 11:29:50 +03:00
|
|
|
sub.request(1)
|
2015-04-09 22:28:16 +02:00
|
|
|
var got = Set(c.expectNext())
|
|
|
|
|
probe.expectMsg(5)
|
2014-09-11 11:29:50 +03:00
|
|
|
probe.expectNoMsg(500.millis)
|
2015-04-09 22:28:16 +02:00
|
|
|
sub.request(25)
|
|
|
|
|
probe.expectMsgAllOf(6 to 20: _*)
|
|
|
|
|
c.probe.within(3.seconds) {
|
|
|
|
|
for (_ ← 2 to 20) got += c.expectNext()
|
|
|
|
|
}
|
2014-09-11 11:29:50 +03:00
|
|
|
|
2015-04-09 22:28:16 +02:00
|
|
|
got should be((1 to 20).toSet)
|
2014-09-29 17:54:09 +02:00
|
|
|
c.expectComplete()
|
2014-09-11 11:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"signal future failure" in {
|
|
|
|
|
val latch = TestLatch(1)
|
|
|
|
|
val c = StreamTestKit.SubscriberProbe[Int]()
|
|
|
|
|
implicit val ec = system.dispatcher
|
2015-04-09 22:28:16 +02:00
|
|
|
val p = Source(1 to 5).mapAsyncUnordered(4, n ⇒ Future {
|
2014-09-11 11:29:50 +03:00
|
|
|
if (n == 3) throw new RuntimeException("err1") with NoStackTrace
|
|
|
|
|
else {
|
|
|
|
|
Await.ready(latch, 10.seconds)
|
|
|
|
|
n
|
|
|
|
|
}
|
2014-10-31 10:43:42 +02:00
|
|
|
}).to(Sink(c)).run()
|
2014-09-11 11:29:50 +03:00
|
|
|
val sub = c.expectSubscription()
|
|
|
|
|
sub.request(10)
|
|
|
|
|
c.expectError.getMessage should be("err1")
|
|
|
|
|
latch.countDown()
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 17:54:09 +02:00
|
|
|
"signal error from mapAsyncUnordered" in {
|
2014-09-11 11:29:50 +03:00
|
|
|
val latch = TestLatch(1)
|
|
|
|
|
val c = StreamTestKit.SubscriberProbe[Int]()
|
|
|
|
|
implicit val ec = system.dispatcher
|
2015-04-09 22:28:16 +02:00
|
|
|
val p = Source(1 to 5).mapAsyncUnordered(4, n ⇒
|
2014-09-11 11:29:50 +03:00
|
|
|
if (n == 3) throw new RuntimeException("err2") with NoStackTrace
|
|
|
|
|
else {
|
|
|
|
|
Future {
|
|
|
|
|
Await.ready(latch, 10.seconds)
|
|
|
|
|
n
|
|
|
|
|
}
|
|
|
|
|
}).
|
2014-10-31 10:43:42 +02:00
|
|
|
to(Sink(c)).run()
|
2014-09-11 11:29:50 +03:00
|
|
|
val sub = c.expectSubscription()
|
|
|
|
|
sub.request(10)
|
|
|
|
|
c.expectError.getMessage should be("err2")
|
|
|
|
|
latch.countDown()
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 09:26:32 +01:00
|
|
|
"resume after future failure" in {
|
|
|
|
|
val c = StreamTestKit.SubscriberProbe[Int]()
|
|
|
|
|
implicit val ec = system.dispatcher
|
2015-04-09 15:16:59 +02:00
|
|
|
val p = Source(1 to 5)
|
|
|
|
|
.mapAsyncUnordered(4, n ⇒ Future {
|
|
|
|
|
if (n == 3) throw new RuntimeException("err3") with NoStackTrace
|
|
|
|
|
else n
|
|
|
|
|
})
|
|
|
|
|
.withAttributes(supervisionStrategy(resumingDecider))
|
|
|
|
|
.to(Sink(c)).run()
|
2015-02-04 09:26:32 +01:00
|
|
|
val sub = c.expectSubscription()
|
|
|
|
|
sub.request(10)
|
|
|
|
|
val expected = (OnComplete :: List(1, 2, 4, 5).map(OnNext.apply)).toSet
|
2015-04-14 12:39:24 +02:00
|
|
|
c.probe.receiveWhile(2.seconds, messages = 5) { case x ⇒ x }.toSet should be(expected)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"finish after future failure" in {
|
|
|
|
|
import system.dispatcher
|
|
|
|
|
Await.result(Source(1 to 3).mapAsyncUnordered(1, n ⇒ Future {
|
|
|
|
|
if (n == 3) throw new RuntimeException("err3b") with NoStackTrace
|
|
|
|
|
else n
|
|
|
|
|
}).withAttributes(supervisionStrategy(resumingDecider))
|
|
|
|
|
.grouped(10)
|
|
|
|
|
.runWith(Sink.head), 1.second) should be(Seq(1, 2))
|
2015-02-04 09:26:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"resume when mapAsyncUnordered throws" in {
|
|
|
|
|
val c = StreamTestKit.SubscriberProbe[Int]()
|
|
|
|
|
implicit val ec = system.dispatcher
|
2015-04-09 15:16:59 +02:00
|
|
|
val p = Source(1 to 5)
|
|
|
|
|
.mapAsyncUnordered(4, n ⇒
|
|
|
|
|
if (n == 3) throw new RuntimeException("err4") with NoStackTrace
|
|
|
|
|
else Future(n))
|
|
|
|
|
.withAttributes(supervisionStrategy(resumingDecider))
|
|
|
|
|
.to(Sink(c)).run()
|
2015-02-04 09:26:32 +01:00
|
|
|
val sub = c.expectSubscription()
|
|
|
|
|
sub.request(10)
|
|
|
|
|
val expected = (OnComplete :: List(1, 2, 4, 5).map(OnNext.apply)).toSet
|
2015-04-09 22:28:16 +02:00
|
|
|
c.probe.receiveWhile(3.seconds, messages = 5) { case x ⇒ x }.toSet should be(expected)
|
2015-02-04 09:26:32 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-03 10:57:25 +01:00
|
|
|
"signal NPE when future is completed with null" in {
|
|
|
|
|
val c = StreamTestKit.SubscriberProbe[String]()
|
2015-04-09 22:28:16 +02:00
|
|
|
val p = Source(List("a", "b")).mapAsyncUnordered(4, elem ⇒ Future.successful(null)).to(Sink(c)).run()
|
2015-03-03 10:57:25 +01:00
|
|
|
val sub = c.expectSubscription()
|
|
|
|
|
sub.request(10)
|
|
|
|
|
c.expectError.getMessage should be(ReactiveStreamsCompliance.ElementMustNotBeNullMsg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"resume when future is completed with null" in {
|
|
|
|
|
val c = StreamTestKit.SubscriberProbe[String]()
|
2015-04-09 15:16:59 +02:00
|
|
|
val p = Source(List("a", "b", "c"))
|
|
|
|
|
.mapAsyncUnordered(4, elem ⇒ if (elem == "b") Future.successful(null) else Future.successful(elem))
|
|
|
|
|
.withAttributes(supervisionStrategy(resumingDecider))
|
2015-03-03 10:57:25 +01:00
|
|
|
.to(Sink(c)).run()
|
|
|
|
|
val sub = c.expectSubscription()
|
|
|
|
|
sub.request(10)
|
|
|
|
|
for (elem ← List("a", "c")) c.expectNext(elem)
|
|
|
|
|
c.expectComplete()
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 14:35:20 +01:00
|
|
|
"should handle cancel properly" in {
|
|
|
|
|
val pub = StreamTestKit.PublisherProbe[Int]()
|
|
|
|
|
val sub = StreamTestKit.SubscriberProbe[Int]()
|
|
|
|
|
|
2015-04-09 22:28:16 +02:00
|
|
|
Source(pub).mapAsyncUnordered(4, Future.successful).runWith(Sink(sub))
|
2015-02-27 14:35:20 +01:00
|
|
|
|
|
|
|
|
val upstream = pub.expectSubscription()
|
|
|
|
|
upstream.expectRequest()
|
|
|
|
|
|
|
|
|
|
sub.expectSubscription().cancel()
|
|
|
|
|
|
|
|
|
|
upstream.expectCancellation()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-11 11:29:50 +03:00
|
|
|
}
|
|
|
|
|
}
|