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

100 lines
2.7 KiB
Scala
Raw Normal View History

/*
* Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
*/
2014-12-22 12:43:01 +01:00
package docs.stream
import akka.NotUsed
import akka.stream._
2014-12-22 12:43:01 +01:00
import akka.stream.scaladsl._
import akka.testkit.AkkaSpec
2014-12-22 12:43:01 +01:00
class StreamBuffersRateSpec extends AkkaSpec {
"Demonstrate pipelining" in {
2014-12-22 16:56:11 +01:00
def println(s: Any) = ()
2014-12-22 12:43:01 +01:00
//#pipelining
Source(1 to 3)
2019-03-11 10:38:24 +01:00
.map { i =>
println(s"A: $i"); i
}
.async
.map { i =>
println(s"B: $i"); i
}
.async
.map { i =>
println(s"C: $i"); i
}
.async
2014-12-22 12:43:01 +01:00
.runWith(Sink.ignore)
//#pipelining
}
"Demonstrate buffer sizes" in {
//#section-buffer
2019-03-11 10:38:24 +01:00
val section = Flow[Int].map(_ * 2).async.addAttributes(Attributes.inputBuffer(initial = 1, max = 1)) // the buffer size of this map is 1
val flow = section.via(Flow[Int].map(_ / 2)).async // the buffer size of this map is the default
val runnableGraph =
Source(1 to 10).via(flow).to(Sink.foreach(elem => println(elem)))
val withOverriddenDefaults = runnableGraph.withAttributes(Attributes.inputBuffer(initial = 64, max = 64))
2014-12-22 12:43:01 +01:00
//#section-buffer
}
"buffering abstraction leak" in {
//#buffering-abstraction-leak
import scala.concurrent.duration._
case class Tick()
RunnableGraph.fromGraph(GraphDSL.create() { implicit b =>
import GraphDSL.Implicits._
2014-12-22 12:43:01 +01:00
// this is the asynchronous stage in this graph
val zipper = b.add(ZipWith[Tick, Int, Int]((tick, count) => count).async)
2014-12-22 12:43:01 +01:00
Source.tick(initialDelay = 3.second, interval = 3.second, Tick()) ~> zipper.in0
2014-12-22 12:43:01 +01:00
2019-03-11 10:38:24 +01:00
Source
.tick(initialDelay = 1.second, interval = 1.second, "message!")
.conflateWithSeed(seed = (_) => 1)((count, _) => count + 1) ~> zipper.in1
2014-12-22 12:43:01 +01:00
zipper.out ~> Sink.foreach(println)
ClosedShape
})
2014-12-22 12:43:01 +01:00
//#buffering-abstraction-leak
}
"explicit buffers" in {
2014-12-22 13:25:06 +01:00
trait Job
def inboundJobsConnector(): Source[Job, NotUsed] = Source.empty
2014-12-22 13:25:06 +01:00
//#explicit-buffers-backpressure
// Getting a stream of jobs from an imaginary external system as a Source
val jobs: Source[Job, NotUsed] = inboundJobsConnector()
2014-12-22 13:25:06 +01:00
jobs.buffer(1000, OverflowStrategy.backpressure)
//#explicit-buffers-backpressure
//#explicit-buffers-droptail
jobs.buffer(1000, OverflowStrategy.dropTail)
//#explicit-buffers-droptail
2015-06-01 18:08:13 +03:00
//#explicit-buffers-dropnew
jobs.buffer(1000, OverflowStrategy.dropNew)
//#explicit-buffers-dropnew
2014-12-22 13:25:06 +01:00
//#explicit-buffers-drophead
jobs.buffer(1000, OverflowStrategy.dropHead)
//#explicit-buffers-drophead
//#explicit-buffers-dropbuffer
jobs.buffer(1000, OverflowStrategy.dropBuffer)
//#explicit-buffers-dropbuffer
//#explicit-buffers-fail
jobs.buffer(1000, OverflowStrategy.fail)
//#explicit-buffers-fail
2014-12-22 13:25:06 +01:00
}
2014-12-22 12:43:01 +01:00
}