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.
2
Answers
If I understand your question correctly, you are looking into a way to test async/await code. If that’s correct, let’s image you have the following
DataDownloader
which will execute an async function to download something:In order to call that function we will need to use
await
, which would basically mark that the calls need to happen in a concurrency supported context. Fortunately Apple’sXCTest
has been upgraded to support that. We don’t need to wrap it in aTask
. So the trick is that in your test you can mark any function asasync
and you will be use theawait
. A simple example below how would you call theDownloader
async function in a test context: