Search code examples
iosswiftunit-testingasync-awaitcombine

Can you use async/await in swift along with a test scheduler?


We use the Pointfree Combine Schedulers, specifically the TestScheduler, extensively in our unit tests.

Is there a way to use this same library, or a similar test scheduler library, with the new async/await in Swift? Basically, can you programmatically move and advance through time in the reactive streams produced by async/await?

Why might we want this? As Pointfree describes:

This scheduler is useful for testing how the flow of time effects publishers that use asynchronous operators, such as debounce, throttle, delay, timeout, receive(on:), subscribe(on:) and more

It appears that Swift has recently introduced Swift Async Algorithms that include support for many of these operators. How does the community test these operators?

A similar question was asked a few months back, but the solution seems to propose waiting for the actual amount of time. Obviously if you have a 10 or 30 second timeout, one does not want to literally wait 10 or 30 seconds in their test.


Solution

  • The authors of https://github.com/pointfreeco/combine-schedulers have released https://github.com/pointfreeco/swift-clocks, which serves the same purpose for AsyncSequence.