Files
xtool-mirror/Tests/XToolTests/InputTestSupport.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()
}