|
| 1 | +/* |
| 2 | + * Copyright (C) 2014-2023 Lightbend Inc. <https://www.lightbend.com> |
| 3 | + */ |
| 4 | + |
| 5 | +package akka.stream.scaladsl |
| 6 | + |
| 7 | +import akka.stream.OverflowStrategy |
| 8 | +import akka.stream.testkit._ |
| 9 | +import akka.stream.testkit.scaladsl.TestSink |
| 10 | + |
| 11 | +import java.util.concurrent.ThreadLocalRandom |
| 12 | +import java.util.concurrent.atomic.AtomicInteger |
| 13 | +import scala.concurrent.Future |
| 14 | +import scala.concurrent.duration.DurationInt |
| 15 | + |
| 16 | +class FlowFlatMapConcatSpec extends StreamSpec(""" |
| 17 | + akka.stream.materializer.initial-input-buffer-size = 2 |
| 18 | + """) with ScriptedTest { |
| 19 | + val toSeq = Flow[Int].grouped(1000).toMat(Sink.head)(Keep.right) |
| 20 | + |
| 21 | + "A flatMapConcat" must { |
| 22 | + |
| 23 | + "work with value presented sources" in { |
| 24 | + Source( |
| 25 | + List(Source.empty[Int], Source.single(1), Source(List(2, 3, 4)), Source.lazyFuture(() => Future.successful(5)))) |
| 26 | + .flatMapConcat(identity, ThreadLocalRandom.current().nextInt(1, 129)) |
| 27 | + .runWith(toSeq) |
| 28 | + .futureValue should ===(1 to 5) |
| 29 | + } |
| 30 | + |
| 31 | + "work with value presented sources when demands slow" in { |
| 32 | + val prob = Source( |
| 33 | + List(Source.empty[Int], Source.single(1), Source(List(2, 3, 4)), Source.lazyFuture(() => Future.successful(5)))) |
| 34 | + .flatMapConcat(identity, ThreadLocalRandom.current().nextInt(1, 129)) |
| 35 | + .runWith(TestSink()) |
| 36 | + |
| 37 | + prob.request(1) |
| 38 | + prob.expectNext(1) |
| 39 | + prob.expectNoMessage(1.seconds) |
| 40 | + prob.request(2) |
| 41 | + prob.expectNext(2, 3) |
| 42 | + prob.expectNoMessage(1.seconds) |
| 43 | + prob.request(2) |
| 44 | + prob.expectNext(4, 5) |
| 45 | + prob.expectComplete() |
| 46 | + } |
| 47 | + |
| 48 | + "can do pre materialization when parallelism > 1" in { |
| 49 | + val materializationCounter = new AtomicInteger(0) |
| 50 | + val randomParallelism = ThreadLocalRandom.current().nextInt(4, 65) |
| 51 | + val prob = Source(1 to (randomParallelism * 3)) |
| 52 | + .flatMapConcat( |
| 53 | + value => { |
| 54 | + Source |
| 55 | + .lazySingle(() => { |
| 56 | + materializationCounter.incrementAndGet() |
| 57 | + value |
| 58 | + }) |
| 59 | + .buffer(1, overflowStrategy = OverflowStrategy.backpressure) |
| 60 | + }, |
| 61 | + randomParallelism) |
| 62 | + .runWith(TestSink()) |
| 63 | + |
| 64 | + expectNoMessage(1.seconds) |
| 65 | + materializationCounter.get() shouldBe 0 |
| 66 | + |
| 67 | + prob.request(1) |
| 68 | + prob.expectNext(1.seconds, 1) |
| 69 | + expectNoMessage(1.seconds) |
| 70 | + materializationCounter.get() shouldBe (randomParallelism + 1) |
| 71 | + materializationCounter.set(0) |
| 72 | + |
| 73 | + prob.request(2) |
| 74 | + prob.expectNextN(List(2, 3)) |
| 75 | + expectNoMessage(1.seconds) |
| 76 | + materializationCounter.get() shouldBe 2 |
| 77 | + materializationCounter.set(0) |
| 78 | + |
| 79 | + prob.request(randomParallelism - 3) |
| 80 | + prob.expectNextN(4 to randomParallelism) |
| 81 | + expectNoMessage(1.seconds) |
| 82 | + materializationCounter.get() shouldBe (randomParallelism - 3) |
| 83 | + materializationCounter.set(0) |
| 84 | + |
| 85 | + prob.request(randomParallelism) |
| 86 | + prob.expectNextN(randomParallelism + 1 to randomParallelism * 2) |
| 87 | + expectNoMessage(1.seconds) |
| 88 | + materializationCounter.get() shouldBe randomParallelism |
| 89 | + materializationCounter.set(0) |
| 90 | + |
| 91 | + prob.request(randomParallelism) |
| 92 | + prob.expectNextN(randomParallelism * 2 + 1 to randomParallelism * 3) |
| 93 | + expectNoMessage(1.seconds) |
| 94 | + materializationCounter.get() shouldBe 0 |
| 95 | + prob.expectComplete() |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments