Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

xtool.git / Tests / XToolTests / InputTestSupport.swift

InputTestSupport.swift24 lines · 641 BRaw
1import Foundation
2import Testing
3
4/// Releases suspended reads on timeout so a regression cannot hang the test process.
5func withInputTestTimeout<T>(
6 _ timeout: Duration = .seconds(5),
7 operation: () async throws -> T,
8 cleanup: @escaping @Sendable () async -> Void,
9) async throws -> T {
10 let watchdog = Task {
11 do {
12 try await Task.sleep(for: timeout)
13 } catch {
14 return
15 }
16 Issue.record("Input operation exceeded timeout of \(timeout)")
17 await cleanup()
18 }
19 defer {
20 watchdog.cancel()
21 await watchdog.value
22 }
23 return try await operation()
24}