Last active
September 4, 2022 14:40
-
-
Save JARinteractive/7fb33b6b0043f365ddfd to your computer and use it in GitHub Desktop.
wait for condition to be true
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/JARinteractive/7fb33b6b0043f365ddfd | |
import Foundation | |
import XCTest | |
@discardableResult | |
public func AssertEventuallyTrue(file: StaticString = #file, line: UInt = #line, _ checkSuccess: @autoclosure () -> Bool) -> Bool { | |
return AssertEventuallyTrue(10.0, file: file, line: line, checkSuccess) | |
} | |
@discardableResult | |
public func AssertEventuallyTrue(_ timeout: Double, file: StaticString = #file, line: UInt = #line, _ checkSuccess: @autoclosure () -> Bool) -> Bool { | |
let startDate = NSDate() | |
var success = false | |
while !success && abs(startDate.timeIntervalSinceNow) < timeout { | |
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.01)) | |
success = checkSuccess() | |
} | |
if !success { XCTFail("Timeout occurred while waiting for condition (waitUntil)", file: file, line: line) } | |
return success | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add
@discardableResult
if you want those missing assignment warnings to go away