2014-12-08 17:29:40 +01:00
|
|
|
package docs.stream.cookbook
|
|
|
|
|
|
|
|
|
|
import akka.stream.scaladsl._
|
|
|
|
|
import akka.stream.testkit.StreamTestKit
|
|
|
|
|
import akka.stream.testkit.StreamTestKit.{ SubscriberProbe, PublisherProbe }
|
|
|
|
|
import akka.util.ByteString
|
|
|
|
|
|
|
|
|
|
class RecipeKeepAlive extends RecipeSpec {
|
|
|
|
|
|
|
|
|
|
"Recipe for injecting keepalive messages" must {
|
|
|
|
|
|
|
|
|
|
"work" in {
|
|
|
|
|
|
|
|
|
|
type Tick = Unit
|
|
|
|
|
|
|
|
|
|
val tickPub = PublisherProbe[Tick]()
|
|
|
|
|
val dataPub = PublisherProbe[ByteString]()
|
|
|
|
|
val sub = SubscriberProbe[ByteString]()
|
|
|
|
|
val ticks = Source(tickPub)
|
|
|
|
|
|
|
|
|
|
val dataStream = Source(dataPub)
|
|
|
|
|
val keepaliveMessage = ByteString(11)
|
|
|
|
|
val sink = Sink(sub)
|
|
|
|
|
|
|
|
|
|
//#inject-keepalive
|
2015-01-28 14:19:50 +01:00
|
|
|
val keepAliveStream: Source[ByteString, Unit] = ticks
|
2014-12-08 17:29:40 +01:00
|
|
|
.conflate(seed = (tick) => keepaliveMessage)((msg, newTick) => msg)
|
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
val graph = FlowGraph.closed() { implicit builder =>
|
|
|
|
|
import FlowGraph.Implicits._
|
|
|
|
|
val unfairMerge = builder.add(MergePreferred[ByteString](1))
|
2014-12-08 17:29:40 +01:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
dataStream ~> unfairMerge.preferred
|
|
|
|
|
// If data is available then no keepalive is injected
|
|
|
|
|
keepAliveStream ~> unfairMerge ~> sink
|
2014-12-08 17:29:40 +01:00
|
|
|
}
|
|
|
|
|
//#inject-keepalive
|
|
|
|
|
|
|
|
|
|
graph.run()
|
|
|
|
|
|
|
|
|
|
val manualTicks = new StreamTestKit.AutoPublisher(tickPub)
|
|
|
|
|
val manualData = new StreamTestKit.AutoPublisher(dataPub)
|
|
|
|
|
|
|
|
|
|
val subscription = sub.expectSubscription()
|
|
|
|
|
|
|
|
|
|
manualTicks.sendNext(())
|
|
|
|
|
|
|
|
|
|
// pending data will overcome the keepalive
|
|
|
|
|
manualData.sendNext(ByteString(1))
|
|
|
|
|
manualData.sendNext(ByteString(2))
|
|
|
|
|
manualData.sendNext(ByteString(3))
|
|
|
|
|
|
|
|
|
|
subscription.request(1)
|
|
|
|
|
sub.expectNext(ByteString(1))
|
|
|
|
|
subscription.request(2)
|
|
|
|
|
sub.expectNext(ByteString(2))
|
|
|
|
|
sub.expectNext(ByteString(3))
|
|
|
|
|
|
|
|
|
|
subscription.request(1)
|
|
|
|
|
sub.expectNext(keepaliveMessage)
|
|
|
|
|
|
|
|
|
|
subscription.request(1)
|
|
|
|
|
manualTicks.sendNext(())
|
|
|
|
|
sub.expectNext(keepaliveMessage)
|
|
|
|
|
|
|
|
|
|
manualData.sendComplete()
|
|
|
|
|
manualTicks.sendComplete()
|
|
|
|
|
|
|
|
|
|
sub.expectComplete()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|