xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Tests / XToolTests / InputTestSupport.swift
InputTestSupport.swift24 lines · 641 BRaw
| 1 | import Foundation |
| 2 | import Testing |
| 3 | |
| 4 | /// Releases suspended reads on timeout so a regression cannot hang the test process. |
| 5 | func 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 | } |