pekko/akka-docs/src/test/scala/docs/stream/SubstreamDocSpec.scala

67 lines
1.7 KiB
Scala
Raw Normal View History

/*
* Copyright (C) 2015-2020 Lightbend Inc. <https://www.lightbend.com>
2017-11-28 17:03:13 +09:00
*/
2017-11-28 17:03:13 +09:00
package docs.stream
import akka.stream.scaladsl.{ Sink, Source }
import akka.stream.{ SubstreamCancelStrategy }
2017-11-28 17:03:13 +09:00
import akka.testkit.AkkaSpec
class SubstreamDocSpec extends AkkaSpec {
"generate substreams by groupBy" in {
//#groupBy1
val source = Source(1 to 10).groupBy(3, _ % 3)
//#groupBy1
//#groupBy2
Source(1 to 10).groupBy(3, _ % 3).to(Sink.ignore).run()
//#groupBy2
//#groupBy3
2019-03-11 10:38:24 +01:00
Source(1 to 10).groupBy(3, _ % 3).mergeSubstreams.runWith(Sink.ignore)
2017-11-28 17:03:13 +09:00
//#groupBy3
//#groupBy4
2019-03-11 10:38:24 +01:00
Source(1 to 10).groupBy(3, _ % 3).mergeSubstreamsWithParallelism(2).runWith(Sink.ignore)
2017-11-28 17:03:13 +09:00
//concatSubstreams is equivalent to mergeSubstreamsWithParallelism(1)
2019-03-11 10:38:24 +01:00
Source(1 to 10).groupBy(3, _ % 3).concatSubstreams.runWith(Sink.ignore)
2017-11-28 17:03:13 +09:00
//#groupBy4
}
"generate substreams by splitWhen and splitAfter" in {
//#splitWhenAfter
Source(1 to 10).splitWhen(SubstreamCancelStrategy.drain)(_ == 3)
Source(1 to 10).splitAfter(SubstreamCancelStrategy.drain)(_ == 3)
//#splitWhenAfter
//#wordCount
val text =
"This is the first line.\n" +
2019-03-11 10:38:24 +01:00
"The second line.\n" +
"There is also the 3rd line\n"
2017-11-28 17:03:13 +09:00
val charCount = Source(text.toList)
.splitAfter { _ == '\n' }
.filter(_ != '\n')
.map(_ => 1)
2017-11-28 17:03:13 +09:00
.reduce(_ + _)
.to(Sink.foreach(println))
.run()
//#wordCount
}
"generate substreams by flatMapConcat and flatMapMerge" in {
//#flatMapConcat
2019-03-11 10:38:24 +01:00
Source(1 to 2).flatMapConcat(i => Source(List.fill(3)(i))).runWith(Sink.ignore)
2017-11-28 17:03:13 +09:00
//#flatMapConcat
//#flatMapMerge
2019-03-11 10:38:24 +01:00
Source(1 to 2).flatMapMerge(2, i => Source(List.fill(3)(i))).runWith(Sink.ignore)
2017-11-28 17:03:13 +09:00
//#flatMapMerge
}
}