2014-12-08 17:29:40 +01:00
|
|
|
package docs.stream.cookbook
|
|
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
import akka.NotUsed
|
2014-12-08 17:29:40 +01:00
|
|
|
import akka.stream.scaladsl.{ Flow, Sink, Source }
|
2015-04-24 11:45:03 +03:00
|
|
|
import akka.stream.testkit._
|
2014-12-08 17:29:40 +01:00
|
|
|
import scala.concurrent.duration._
|
2015-10-31 14:46:10 +01:00
|
|
|
import akka.testkit.TestLatch
|
|
|
|
|
import scala.concurrent.Await
|
2014-12-08 17:29:40 +01:00
|
|
|
|
|
|
|
|
class RecipeSimpleDrop extends RecipeSpec {
|
|
|
|
|
|
|
|
|
|
"Recipe for simply dropping elements for a faster stream" must {
|
|
|
|
|
|
|
|
|
|
"work" in {
|
|
|
|
|
|
|
|
|
|
//#simple-drop
|
2016-01-20 10:00:37 +02:00
|
|
|
val droppyStream: Flow[Message, Message, NotUsed] =
|
2016-01-22 15:22:30 +01:00
|
|
|
Flow[Message].conflate((lastMessage, newMessage) => newMessage)
|
2014-12-08 17:29:40 +01:00
|
|
|
//#simple-drop
|
2015-10-31 14:46:10 +01:00
|
|
|
val latch = TestLatch(2)
|
|
|
|
|
val realDroppyStream =
|
2016-01-22 15:22:30 +01:00
|
|
|
Flow[Message].conflate((lastMessage, newMessage) => { latch.countDown(); newMessage })
|
2014-12-08 17:29:40 +01:00
|
|
|
|
2015-04-24 11:45:03 +03:00
|
|
|
val pub = TestPublisher.probe[Message]()
|
|
|
|
|
val sub = TestSubscriber.manualProbe[Message]()
|
2015-12-17 11:48:30 +02:00
|
|
|
val messageSource = Source.fromPublisher(pub)
|
|
|
|
|
val sink = Sink.fromSubscriber(sub)
|
2014-12-08 17:29:40 +01:00
|
|
|
|
2015-10-31 14:46:10 +01:00
|
|
|
messageSource.via(realDroppyStream).to(sink).run()
|
2014-12-08 17:29:40 +01:00
|
|
|
|
|
|
|
|
val subscription = sub.expectSubscription()
|
|
|
|
|
sub.expectNoMsg(100.millis)
|
|
|
|
|
|
2015-04-24 11:45:03 +03:00
|
|
|
pub.sendNext("1")
|
|
|
|
|
pub.sendNext("2")
|
|
|
|
|
pub.sendNext("3")
|
2014-12-08 17:29:40 +01:00
|
|
|
|
2015-10-31 14:46:10 +01:00
|
|
|
Await.ready(latch, 1.second)
|
|
|
|
|
|
2014-12-08 17:29:40 +01:00
|
|
|
subscription.request(1)
|
|
|
|
|
sub.expectNext("3")
|
|
|
|
|
|
2015-04-24 11:45:03 +03:00
|
|
|
pub.sendComplete()
|
2014-12-08 17:29:40 +01:00
|
|
|
subscription.request(1)
|
|
|
|
|
sub.expectComplete()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|