mirror of
https://github.com/xtool-org/xtool.git
synced 2026-11-16 05:26:45 +01:00
25 lines
642 B
Swift
25 lines
642 B
Swift
import Foundation
|
|
import Testing
|
|
|
|
/// Releases suspended reads on timeout so a regression cannot hang the test process.
|
|
func withInputTestTimeout<T>(
|
|
_ timeout: Duration = .seconds(30),
|
|
operation: () async throws -> T,
|
|
cleanup: @escaping @Sendable () async -> Void,
|
|
) async throws -> T {
|
|
let watchdog = Task {
|
|
do {
|
|
try await Task.sleep(for: timeout)
|
|
} catch {
|
|
return
|
|
}
|
|
Issue.record("Input operation exceeded timeout of \(timeout)")
|
|
await cleanup()
|
|
}
|
|
defer {
|
|
watchdog.cancel()
|
|
await watchdog.value
|
|
}
|
|
return try await operation()
|
|
}
|