简体   繁体   English

您可以在 swift 中使用 async/await 以及测试调度程序吗?

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

We use the Pointfree Combine Schedulers , specifically theTestScheduler , extensively in our unit tests.我们在单元测试中广泛使用Pointfree Combine Scheduler ,特别是TestScheduler

Is there a way to use this same library, or a similar test scheduler library, with the new async/await in Swift?有没有办法在 Swift 中使用这个相同的库或类似的测试调度程序库以及新的异步/等待 Basically, can you programmatically move and advance through time in the reactive streams produced by async/await?基本上,您能否以编程方式在 async/await 生成的反应流中随时间移动和前进?

Why might we want this?我们为什么要这个? As Pointfree describes:正如 Pointfree 所描述的:

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此调度程序可用于测试时间流如何影响使用异步运算符的发布者,例如 debounce、throttle、delay、timeout、receive(on:)、subscribe(on:) 等

It appears that Swift has recently introduced Swift Async Algorithms that include support for many of these operators. Swift 似乎最近引入了Swift 异步算法,其中包括对其中许多运算符的支持。 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.显然,如果您有 10 或 30 秒的超时,那么人们不想在他们的测试中真正等待 10 或 30 秒。

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:如果这是正确的,让我们假设您有以下DataDownloader ,它将执行一个异步函数来下载一些东西:

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.为了调用该函数,我们需要使用await ,这基本上标志着调用需要在并发支持的上下文中发生。 Fortunately Apple's XCTest has been upgraded to support that.幸运的是,Apple 的XCTest已经升级以支持它。 We don't need to wrap it in a Task .我们不需要将它包装在Task中。 So the trick is that in your test you can mark any function as async and you will be use the await .所以诀窍是在你的测试中你可以将任何函数标记为async并且你将使用await A simple example below how would you call the Downloader async function in a test context:下面是一个简单的示例,您将如何在测试上下文中调用Downloader异步函数:

class DataDownloaderTests: XCTestCase {
    func testDataDownload() async throws {
        let dataDownloader = DataDownloader()
        let downloadedData = try await dataDownloader.downloadData()

        XCTAssertNil(resizedImage.size)
    }
}
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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM