Last active
November 4, 2024 21:29
-
-
Save janodev/32217b09f307da8c96e2cf629c31a8eb to your computer and use it in GitHub Desktop.
waitForExpectation
This file contains 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
import Foundation | |
import Testing | |
func waitForExpectation( | |
timeout: Duration, | |
description: String, | |
fileID: String = #fileID, | |
filePath: String = #filePath, | |
line: Int = #line, | |
column: Int = #column, | |
_ expectation: @escaping () -> Bool | |
) async { | |
let startTime = ContinuousClock.now | |
var fulfilled = false | |
await confirmation( | |
"Waiting for expectation: \(description)", | |
expectedCount: 1, | |
sourceLocation: SourceLocation(fileID: fileID, filePath: filePath, line: line, column: column) | |
) { confirm in | |
while !fulfilled && ContinuousClock.now - startTime < timeout { | |
if expectation() { | |
fulfilled = true | |
confirm() | |
break | |
} | |
await Task.yield() | |
} | |
if !fulfilled { | |
Issue.record("Expectation not fulfilled within \(timeout) seconds: \(description)") | |
} | |
} | |
} | |
try await waitForExpectation(timeout: .milliseconds(100), description: "Successful load check") { | |
let isSuccess: Bool = ... | |
return isSuccess | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey,
just stumbled upon this. You build this since Swift Testing does not support async code which is run in an own Task, right?
Example which works:
Example which won't work:
Even when using
@Test(.timeout)
it won't work, right?