Search code examples
scalaakkaakka-streambackpressure

How to create a Source which is not affected by backpressure


I want to test some Akka streams features like conflate. For this, I need to construct a source which is not affected by backpressure within a simple unit test. Naive attempts like

Source.tick(1.milli, 1.milli, "tick").map(_ => Random.nextDouble())

do not work because of backpressure. OTOH going over HTTP is probably overkill.

How would I create a simple Source for a unit test which is not affected by backpressure?


Solution

  • You could use Source.actorRef which is - by design - not backpressure-enabled. See the example below:

      val actorRef: ActorRef = Source.actorRef(0, OverflowStrategy.dropNew)
          .map(_ => Random.nextDouble())
          .to(yourSink).run()
    
      system.scheduler.schedule(1.milli, 1.milli, actorRef, "tick")(system.dispatcher)
    

    The bufferSize parameter and the overflow strategy have been chosen randomly here, you'll need to adjust them to the needs of your test.

    More info on Source.actorRef can be found in the docs.