Skip to content

Instantly share code, notes, and snippets.

@descorp
Created October 19, 2019 21:40
Show Gist options
  • Save descorp/3f1fbead209ab791ff7af9d4458d4bf8 to your computer and use it in GitHub Desktop.
Save descorp/3f1fbead209ab791ff7af9d4458d4bf8 to your computer and use it in GitHub Desktop.
extension XCTest {
func expectToEventually(_ test: @autoclosure () -> Bool, timeout: TimeInterval = 1.0, message: String = "") {
let runLoop = RunLoop.current
let timeoutDate = Date(timeIntervalSinceNow: timeout)
repeat {
// 1
if test() {
return
}
// 2
runLoop.run(until: Date(timeIntervalSinceNow: 0.01))
} while Date().compare(timeoutDate) == .orderedAscending // 3
// 4
XCTFail(message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment