ios - XCTWaiter.wait() 超时有时似乎需要更长的时间

标签 ios swift wait xctest xctwaiter

To add delays in my tests I implemented this:

func execute(after: TimeInterval, testBlock: () -> Void) {
    let result = XCTWaiter.wait(for: [expectation(description: "Delayed Test")], timeout: after)

    if result == XCTWaiter.Result.timedOut {
        testBlock()
    } else {
        XCTFail("Delay interrupted.")
    }
}

然后我写了一个测试:

func testExecute() {
    var i = 1

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.40) {
        i = 2
    }

    execute(after: 0.20) {
        XCTAssert(i == 1)
    }

    execute(after: 0.15) {
        XCTAssert(i == 1)   // Fails once every three or four runs.
    }

    execute(after: 0.06) {  // Never fails.
        XCTAssert(i == 2)
    }
}

为什么第二个 XCTAssert() 经常失败?

这是唯一在我的模拟器上运行的东西。您预计会有一些抖动,但这不应该保持在 1/60 秒系统时钟周期的 1 倍或 2 倍吗?

最佳答案

事实证明,延迟可能会花费相当长的时间(在 2011 年的实验中长达 200 毫秒:http://atastypixel.com/blog/experiments-with-precise-timing-in-ios/)。

使用此 execute(after:testBlock:) 函数时必须有足够的余量。

关于ios - XCTWaiter.wait() 超时有时似乎需要更长的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50250993/

相关文章:

c - c 中的 fork() 和 execvp() 命令

jQuery 等待元素属性有值

ios - 在 IOS 中打开 CBZ/CBR 文件

ios - 如何在不在屏幕上闪烁顶部和底部之间的任何呈现的 VC 的情况下关闭带有动画的模态视图 Controller 堆栈?

iOS 文档文件夹不是目录和/或丢失

ios - 更改按钮状态时显示/隐藏 subview

ios - 使用未解析的标识符 'displayAlertMessage'

ios - AdMob 忽略 iPhone 应用程序的 if/else 语句

ios - 当用户在文本字段中键入 @ 符号时,有没有办法为电子邮件添加域建议

Java线程: wait and notify methods