Testing with Mock Duration?

Hello,

I am building a library/DSL on top of Akka Streams to easily turn an HTTP resource into a stream in given certain timing.

One of the tests is that given a periodic cadence (e.g., every 3 minutes), asset that a stream with no failures yields events in that cadence.

This test is not difficult to write, but it takes a long time to run, especially if one were to use ScalaCheck to create many different periods. I am wondering if there is a way to mock time to test Akka Streams. It looks from the test page in the docs that many Akka Streams tests are time-based.

Can’t you param this out? I mean you can do a def myThing(time: Duration = 3.minutes) and in the tests you override the param to 300.millis for example. You probably don’t want to test the delay/throttle/etc which is already tested in the main lib. You just want to test if the param is working (and hope that on the top of your code others will use with the right params).

In general it is a good idea to factor out all of the timings, to your code. (For ex I never use System.currentMillis, I wrap it to a class, and I carry that class explicitly to where I need the current time, this makes the tests more consistent, bcs I can fake “now”.)

That is kind of what I ended up doing - I mocked the scheduler so now it creates a data structure with the tasks that are meant to be done.

Basically I have a function (call it mine) that takes some description about a task and how often to execute it and returns a Source with the result of that task. The mocked scheduler has a run(dur: FiniteDuration) API that executes all tasks that are meant to be executed within that duration. This way, I can call mine, run the stream and collect the number of results, and observe that the expected number of results have been computed regardless of possible failure with the task logic or an external dependency (e.g., if the task involves an HTTP request or a database hit)