pekko/akka-docs/rst/scala/code/docs/stream/cookbook/RecipeSimpleDrop.scala

51 lines
1.3 KiB
Scala
Raw Normal View History

2014-12-08 17:29:40 +01:00
package docs.stream.cookbook
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._
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
val droppyStream: Flow[Message, Message, NotUsed] =
Flow[Message].conflate((lastMessage, newMessage) => newMessage)
2014-12-08 17:29:40 +01:00
//#simple-drop
val latch = TestLatch(2)
val realDroppyStream =
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]()
val messageSource = Source.fromPublisher(pub)
val sink = Sink.fromSubscriber(sub)
2014-12-08 17:29:40 +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
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()
}
}
}