skip to Main Content

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


  1. 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:

    struct DataDownloader {
        func downloadData() async throws -> Data {
            ...
        }
    }
    

    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’s XCTest has been upgraded to support that. We don’t need to wrap it in a Task. So the trick is that in your test you can mark any function as async and you will be use the await. A simple example below how would you call the Downloader async function in a test context:

    class DataDownloaderTests: XCTestCase {
        func testDataDownload() async throws {
            let dataDownloader = DataDownloader()
            let downloadedData = try await dataDownloader.downloadData()
    
            XCTAssertNil(resizedImage.size)
        }
    }
    
    Login or Signup to reply.
  2. func AsyncFunction() async -> Int {
      let result = await AsyncOperation()
      return result
    }
    
    let scheduler = TestScheduler()
    let result = try scheduler.start {
      return try AsyncFunction().wait()
    }
    
    XCTAssertEqual(result, 42)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search