2015-07-17 17:03:48 +03:00
|
|
|
|
/**
|
2018-01-04 17:26:29 +00:00
|
|
|
|
* Copyright (C) 2015-2018 Lightbend Inc. <https://www.lightbend.com>
|
2015-07-17 17:03:48 +03:00
|
|
|
|
*/
|
|
|
|
|
|
package docs.stream
|
|
|
|
|
|
|
|
|
|
|
|
import akka.stream._
|
|
|
|
|
|
import akka.stream.scaladsl._
|
|
|
|
|
|
import akka.stream.testkit._
|
|
|
|
|
|
import akka.stream.testkit.scaladsl._
|
|
|
|
|
|
import scala.util.Random
|
|
|
|
|
|
import scala.math._
|
|
|
|
|
|
import scala.concurrent.Await
|
|
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
|
import scala.collection.immutable
|
2016-02-25 14:27:45 +01:00
|
|
|
|
import akka.testkit.{ AkkaSpec, TestLatch }
|
2015-07-17 17:03:48 +03:00
|
|
|
|
|
|
|
|
|
|
class RateTransformationDocSpec extends AkkaSpec {
|
|
|
|
|
|
|
2015-12-11 14:45:24 +01:00
|
|
|
|
implicit val materializer = ActorMaterializer()
|
2015-07-17 17:03:48 +03:00
|
|
|
|
|
|
|
|
|
|
"conflate should summarize" in {
|
|
|
|
|
|
//#conflate-summarize
|
|
|
|
|
|
val statsFlow = Flow[Double]
|
2018-02-19 11:19:56 +01:00
|
|
|
|
.conflateWithSeed(immutable.Seq(_))(_ :+ _)
|
2017-10-06 10:30:28 +02:00
|
|
|
|
.map { s ⇒
|
2015-07-17 17:03:48 +03:00
|
|
|
|
val μ = s.sum / s.size
|
2017-10-06 10:30:28 +02:00
|
|
|
|
val se = s.map(x ⇒ pow(x - μ, 2))
|
2015-07-17 17:03:48 +03:00
|
|
|
|
val σ = sqrt(se.sum / se.size)
|
|
|
|
|
|
(σ, μ, s.size)
|
|
|
|
|
|
}
|
|
|
|
|
|
//#conflate-summarize
|
|
|
|
|
|
|
2017-10-06 10:30:28 +02:00
|
|
|
|
val fut = Source.fromIterator(() ⇒ Iterator.continually(Random.nextGaussian))
|
2015-07-17 17:03:48 +03:00
|
|
|
|
.via(statsFlow)
|
|
|
|
|
|
.grouped(10)
|
|
|
|
|
|
.runWith(Sink.head)
|
|
|
|
|
|
|
2018-02-19 11:19:56 +01:00
|
|
|
|
fut.futureValue
|
2015-07-17 17:03:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"conflate should sample" in {
|
|
|
|
|
|
//#conflate-sample
|
|
|
|
|
|
val p = 0.01
|
|
|
|
|
|
val sampleFlow = Flow[Double]
|
2018-02-19 11:19:56 +01:00
|
|
|
|
.conflateWithSeed(immutable.Seq(_)) {
|
2017-10-06 10:30:28 +02:00
|
|
|
|
case (acc, elem) if Random.nextDouble < p ⇒ acc :+ elem
|
|
|
|
|
|
case (acc, _) ⇒ acc
|
2015-07-17 17:03:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
.mapConcat(identity)
|
|
|
|
|
|
//#conflate-sample
|
|
|
|
|
|
|
2015-07-20 15:21:16 +03:00
|
|
|
|
val fut = Source(1 to 1000)
|
2015-07-17 17:03:48 +03:00
|
|
|
|
.map(_.toDouble)
|
|
|
|
|
|
.via(sampleFlow)
|
|
|
|
|
|
.runWith(Sink.fold(Seq.empty[Double])(_ :+ _))
|
|
|
|
|
|
|
2018-02-19 11:19:56 +01:00
|
|
|
|
fut.futureValue
|
2015-07-17 17:03:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"expand should repeat last" in {
|
|
|
|
|
|
//#expand-last
|
|
|
|
|
|
val lastFlow = Flow[Double]
|
2016-01-18 11:29:14 +01:00
|
|
|
|
.expand(Iterator.continually(_))
|
2015-07-17 17:03:48 +03:00
|
|
|
|
//#expand-last
|
|
|
|
|
|
|
|
|
|
|
|
val (probe, fut) = TestSource.probe[Double]
|
|
|
|
|
|
.via(lastFlow)
|
|
|
|
|
|
.grouped(10)
|
|
|
|
|
|
.toMat(Sink.head)(Keep.both)
|
2015-07-20 15:21:16 +03:00
|
|
|
|
.run()
|
2015-07-17 17:03:48 +03:00
|
|
|
|
|
|
|
|
|
|
probe.sendNext(1.0)
|
2018-02-19 11:19:56 +01:00
|
|
|
|
val expanded = fut.futureValue
|
2015-07-17 17:03:48 +03:00
|
|
|
|
expanded.size shouldBe 10
|
|
|
|
|
|
expanded.sum shouldBe 10
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"expand should track drift" in {
|
|
|
|
|
|
//#expand-drift
|
|
|
|
|
|
val driftFlow = Flow[Double]
|
2017-10-06 10:30:28 +02:00
|
|
|
|
.expand(i ⇒ Iterator.from(0).map(i -> _))
|
2015-07-17 17:03:48 +03:00
|
|
|
|
//#expand-drift
|
2015-10-31 14:46:10 +01:00
|
|
|
|
val latch = TestLatch(2)
|
|
|
|
|
|
val realDriftFlow = Flow[Double]
|
2017-10-06 10:30:28 +02:00
|
|
|
|
.expand(d ⇒ { latch.countDown(); Iterator.from(0).map(d -> _) })
|
2015-07-17 17:03:48 +03:00
|
|
|
|
|
|
|
|
|
|
val (pub, sub) = TestSource.probe[Double]
|
2015-10-31 14:46:10 +01:00
|
|
|
|
.via(realDriftFlow)
|
2015-07-17 17:03:48 +03:00
|
|
|
|
.toMat(TestSink.probe[(Double, Int)])(Keep.both)
|
2015-07-20 15:21:16 +03:00
|
|
|
|
.run()
|
2015-07-17 17:03:48 +03:00
|
|
|
|
|
|
|
|
|
|
sub.request(1)
|
|
|
|
|
|
pub.sendNext(1.0)
|
|
|
|
|
|
sub.expectNext((1.0, 0))
|
|
|
|
|
|
|
|
|
|
|
|
sub.requestNext((1.0, 1))
|
|
|
|
|
|
sub.requestNext((1.0, 2))
|
|
|
|
|
|
|
|
|
|
|
|
pub.sendNext(2.0)
|
2015-10-31 14:46:10 +01:00
|
|
|
|
Await.ready(latch, 1.second)
|
2015-07-17 17:03:48 +03:00
|
|
|
|
sub.requestNext((2.0, 0))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|