Skip to content

Commit e639e5c

Browse files
authored
Ensure tests work in deployments that host as swift 5.7 (#285)
1 parent 7ac31f9 commit e639e5c

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,12 @@ final class TestThroughput: XCTestCase {
9090
zip($0, $1, $2)
9191
}
9292
}
93-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
9493
func test_debounce() async {
94+
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
9595
await measureSequenceThroughput(source: (1...).async) {
96-
$0.debounce(for: .zero, clock: ContinuousClock())
96+
$0.debounce(for: .zero, clock: ContinuousClock())
9797
}
98+
}
9899
}
99100
}
100101
#endif

Tests/AsyncAlgorithmsTests/Support/Asserts.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,47 @@ internal func XCTAssertThrowsError<T>(
165165
verify(error)
166166
}
167167
}
168+
169+
class WaiterDelegate: NSObject, XCTWaiterDelegate {
170+
let state: ManagedCriticalState<UnsafeContinuation<Void, Never>?> = ManagedCriticalState(nil)
171+
172+
init(_ continuation: UnsafeContinuation<Void, Never>) {
173+
state.withCriticalRegion { $0 = continuation }
174+
}
175+
176+
func waiter(_ waiter: XCTWaiter, didFulfillInvertedExpectation expectation: XCTestExpectation) {
177+
resume()
178+
}
179+
180+
func waiter(_ waiter: XCTWaiter, didTimeoutWithUnfulfilledExpectations unfulfilledExpectations: [XCTestExpectation]) {
181+
resume()
182+
}
183+
184+
func waiter(_ waiter: XCTWaiter, fulfillmentDidViolateOrderingConstraintsFor expectation: XCTestExpectation, requiredExpectation: XCTestExpectation) {
185+
resume()
186+
}
187+
188+
func nestedWaiter(_ waiter: XCTWaiter, wasInterruptedByTimedOutWaiter outerWaiter: XCTWaiter) {
189+
190+
}
191+
192+
func resume() {
193+
let continuation = state.withCriticalRegion { continuation in
194+
defer { continuation = nil }
195+
return continuation
196+
}
197+
continuation?.resume()
198+
}
199+
}
200+
201+
extension XCTestCase {
202+
@_disfavoredOverload
203+
func fulfillment(of expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false, file: StaticString = #file, line: Int = #line) async {
204+
return await withUnsafeContinuation { continuation in
205+
let delegate = WaiterDelegate(continuation)
206+
let waiter = XCTWaiter(delegate: delegate)
207+
waiter.wait(for: expectations, timeout: timeout, enforceOrder: enforceOrder)
208+
delegate.resume()
209+
}
210+
}
211+
}

0 commit comments

Comments
 (0)