2015-04-24 11:45:03 +03:00
|
|
|
/**
|
2018-01-04 17:26:29 +00:00
|
|
|
* Copyright (C) 2015-2018 Lightbend Inc. <https://www.lightbend.com>
|
2015-04-24 11:45:03 +03:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2015-04-24 11:45:03 +03:00
|
|
|
package docs.stream
|
|
|
|
|
|
|
|
|
|
import akka.stream._
|
|
|
|
|
import akka.stream.scaladsl._
|
|
|
|
|
import akka.stream.testkit._
|
|
|
|
|
import akka.stream.testkit.scaladsl._
|
2015-06-19 10:03:55 +03:00
|
|
|
import scala.util._
|
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
import scala.concurrent._
|
2016-02-25 14:27:45 +01:00
|
|
|
import akka.testkit.{ AkkaSpec, TestProbe }
|
2015-06-19 10:03:55 +03:00
|
|
|
import akka.pattern
|
2015-04-24 11:45:03 +03:00
|
|
|
|
|
|
|
|
class StreamTestKitDocSpec extends AkkaSpec {
|
|
|
|
|
|
2015-12-11 14:45:24 +01:00
|
|
|
implicit val materializer = ActorMaterializer()
|
2015-04-24 11:45:03 +03:00
|
|
|
|
2015-06-19 10:03:55 +03:00
|
|
|
"strict collection" in {
|
|
|
|
|
//#strict-collection
|
|
|
|
|
val sinkUnderTest = Flow[Int].map(_ * 2).toMat(Sink.fold(0)(_ + _))(Keep.right)
|
2015-04-24 11:45:03 +03:00
|
|
|
|
2015-06-19 10:03:55 +03:00
|
|
|
val future = Source(1 to 4).runWith(sinkUnderTest)
|
2016-08-31 18:20:05 +02:00
|
|
|
val result = Await.result(future, 3.seconds)
|
2015-06-19 10:03:55 +03:00
|
|
|
assert(result == 20)
|
|
|
|
|
//#strict-collection
|
2015-04-24 11:45:03 +03:00
|
|
|
}
|
|
|
|
|
|
2015-06-19 10:03:55 +03:00
|
|
|
"grouped part of infinite stream" in {
|
|
|
|
|
//#grouped-infinite
|
2015-07-15 11:23:22 +02:00
|
|
|
import system.dispatcher
|
|
|
|
|
import akka.pattern.pipe
|
|
|
|
|
|
2015-06-19 10:03:55 +03:00
|
|
|
val sourceUnderTest = Source.repeat(1).map(_ * 2)
|
|
|
|
|
|
2016-08-31 18:20:05 +02:00
|
|
|
val future = sourceUnderTest.take(10).runWith(Sink.seq)
|
|
|
|
|
val result = Await.result(future, 3.seconds)
|
2015-06-19 10:03:55 +03:00
|
|
|
assert(result == Seq.fill(10)(2))
|
|
|
|
|
//#grouped-infinite
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"folded stream" in {
|
|
|
|
|
//#folded-stream
|
|
|
|
|
val flowUnderTest = Flow[Int].takeWhile(_ < 5)
|
|
|
|
|
|
|
|
|
|
val future = Source(1 to 10).via(flowUnderTest).runWith(Sink.fold(Seq.empty[Int])(_ :+ _))
|
2016-08-31 18:20:05 +02:00
|
|
|
val result = Await.result(future, 3.seconds)
|
2015-06-19 10:03:55 +03:00
|
|
|
assert(result == (1 to 4))
|
|
|
|
|
//#folded-stream
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"pipe to test probe" in {
|
|
|
|
|
//#pipeto-testprobe
|
2015-07-15 11:23:22 +02:00
|
|
|
import system.dispatcher
|
|
|
|
|
import akka.pattern.pipe
|
|
|
|
|
|
2015-06-19 10:03:55 +03:00
|
|
|
val sourceUnderTest = Source(1 to 4).grouped(2)
|
|
|
|
|
|
|
|
|
|
val probe = TestProbe()
|
2016-08-31 18:20:05 +02:00
|
|
|
sourceUnderTest.runWith(Sink.seq).pipeTo(probe.ref)
|
|
|
|
|
probe.expectMsg(3.seconds, Seq(Seq(1, 2), Seq(3, 4)))
|
2015-06-19 10:03:55 +03:00
|
|
|
//#pipeto-testprobe
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"sink actor ref" in {
|
|
|
|
|
//#sink-actorref
|
2015-06-22 13:54:49 +03:00
|
|
|
case object Tick
|
2015-11-04 11:43:11 +02:00
|
|
|
val sourceUnderTest = Source.tick(0.seconds, 200.millis, Tick)
|
2015-06-19 10:03:55 +03:00
|
|
|
|
|
|
|
|
val probe = TestProbe()
|
|
|
|
|
val cancellable = sourceUnderTest.to(Sink.actorRef(probe.ref, "completed")).run()
|
|
|
|
|
|
2015-06-22 13:54:49 +03:00
|
|
|
probe.expectMsg(1.second, Tick)
|
2015-06-19 10:03:55 +03:00
|
|
|
probe.expectNoMsg(100.millis)
|
2016-08-31 18:20:05 +02:00
|
|
|
probe.expectMsg(3.seconds, Tick)
|
2015-06-19 10:03:55 +03:00
|
|
|
cancellable.cancel()
|
2016-08-31 18:20:05 +02:00
|
|
|
probe.expectMsg(3.seconds, "completed")
|
2015-06-19 10:03:55 +03:00
|
|
|
//#sink-actorref
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"source actor ref" in {
|
|
|
|
|
//#source-actorref
|
|
|
|
|
val sinkUnderTest = Flow[Int].map(_.toString).toMat(Sink.fold("")(_ + _))(Keep.right)
|
|
|
|
|
|
|
|
|
|
val (ref, future) = Source.actorRef(8, OverflowStrategy.fail)
|
|
|
|
|
.toMat(sinkUnderTest)(Keep.both).run()
|
|
|
|
|
|
|
|
|
|
ref ! 1
|
|
|
|
|
ref ! 2
|
|
|
|
|
ref ! 3
|
|
|
|
|
ref ! akka.actor.Status.Success("done")
|
|
|
|
|
|
2016-08-31 18:20:05 +02:00
|
|
|
val result = Await.result(future, 3.seconds)
|
2015-06-19 10:03:55 +03:00
|
|
|
assert(result == "123")
|
|
|
|
|
//#source-actorref
|
|
|
|
|
}
|
2015-04-24 11:45:03 +03:00
|
|
|
|
2015-06-19 10:03:55 +03:00
|
|
|
"test sink probe" in {
|
2015-04-24 11:45:03 +03:00
|
|
|
//#test-sink-probe
|
2015-06-19 10:03:55 +03:00
|
|
|
val sourceUnderTest = Source(1 to 4).filter(_ % 2 == 0).map(_ * 2)
|
|
|
|
|
|
|
|
|
|
sourceUnderTest
|
2015-04-24 11:45:03 +03:00
|
|
|
.runWith(TestSink.probe[Int])
|
|
|
|
|
.request(2)
|
|
|
|
|
.expectNext(4, 8)
|
|
|
|
|
.expectComplete()
|
|
|
|
|
//#test-sink-probe
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-19 10:03:55 +03:00
|
|
|
"test source probe" in {
|
|
|
|
|
//#test-source-probe
|
|
|
|
|
val sinkUnderTest = Sink.cancelled
|
|
|
|
|
|
|
|
|
|
TestSource.probe[Int]
|
|
|
|
|
.toMat(sinkUnderTest)(Keep.left)
|
|
|
|
|
.run()
|
|
|
|
|
.expectCancellation()
|
|
|
|
|
//#test-source-probe
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"injecting failure" in {
|
|
|
|
|
//#injecting-failure
|
|
|
|
|
val sinkUnderTest = Sink.head[Int]
|
|
|
|
|
|
|
|
|
|
val (probe, future) = TestSource.probe[Int]
|
|
|
|
|
.toMat(sinkUnderTest)(Keep.both)
|
|
|
|
|
.run()
|
|
|
|
|
probe.sendError(new Exception("boom"))
|
|
|
|
|
|
2016-08-31 18:20:05 +02:00
|
|
|
Await.ready(future, 3.seconds)
|
2015-06-19 10:03:55 +03:00
|
|
|
val Failure(exception) = future.value.get
|
|
|
|
|
assert(exception.getMessage == "boom")
|
|
|
|
|
//#injecting-failure
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"test source and a sink" in {
|
|
|
|
|
import system.dispatcher
|
|
|
|
|
//#test-source-and-sink
|
2017-10-06 10:30:28 +02:00
|
|
|
val flowUnderTest = Flow[Int].mapAsyncUnordered(2) { sleep ⇒
|
2015-06-19 10:03:55 +03:00
|
|
|
pattern.after(10.millis * sleep, using = system.scheduler)(Future.successful(sleep))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val (pub, sub) = TestSource.probe[Int]
|
|
|
|
|
.via(flowUnderTest)
|
|
|
|
|
.toMat(TestSink.probe[Int])(Keep.both)
|
|
|
|
|
.run()
|
|
|
|
|
|
|
|
|
|
sub.request(n = 3)
|
|
|
|
|
pub.sendNext(3)
|
|
|
|
|
pub.sendNext(2)
|
|
|
|
|
pub.sendNext(1)
|
|
|
|
|
sub.expectNextUnordered(1, 2, 3)
|
|
|
|
|
|
|
|
|
|
pub.sendError(new Exception("Power surge in the linear subroutine C-47!"))
|
2015-09-17 12:57:07 +02:00
|
|
|
val ex = sub.expectError()
|
2015-06-19 10:03:55 +03:00
|
|
|
assert(ex.getMessage.contains("C-47"))
|
|
|
|
|
//#test-source-and-sink
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-24 11:45:03 +03:00
|
|
|
}
|