From 8a339975f13965c35b52dddfb60dad35be455968 Mon Sep 17 00:00:00 2001 From: Philippe Hausler Date: Tue, 15 Aug 2023 11:14:33 -0700 Subject: [PATCH] Ensure tests work in deployments that host as swift 5.7 --- .../Performance/TestThroughput.swift | 5 ++- .../Support/Asserts.swift | 44 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift b/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift index d8003ca8..4ea06e06 100644 --- a/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift +++ b/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift @@ -90,11 +90,12 @@ final class TestThroughput: XCTestCase { zip($0, $1, $2) } } - @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) func test_debounce() async { + if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) { await measureSequenceThroughput(source: (1...).async) { - $0.debounce(for: .zero, clock: ContinuousClock()) + $0.debounce(for: .zero, clock: ContinuousClock()) } + } } } #endif diff --git a/Tests/AsyncAlgorithmsTests/Support/Asserts.swift b/Tests/AsyncAlgorithmsTests/Support/Asserts.swift index c9cdb968..d891cf91 100644 --- a/Tests/AsyncAlgorithmsTests/Support/Asserts.swift +++ b/Tests/AsyncAlgorithmsTests/Support/Asserts.swift @@ -165,3 +165,47 @@ internal func XCTAssertThrowsError( verify(error) } } + +class WaiterDelegate: NSObject, XCTWaiterDelegate { + let state: ManagedCriticalState?> = ManagedCriticalState(nil) + + init(_ continuation: UnsafeContinuation) { + state.withCriticalRegion { $0 = continuation } + } + + func waiter(_ waiter: XCTWaiter, didFulfillInvertedExpectation expectation: XCTestExpectation) { + resume() + } + + func waiter(_ waiter: XCTWaiter, didTimeoutWithUnfulfilledExpectations unfulfilledExpectations: [XCTestExpectation]) { + resume() + } + + func waiter(_ waiter: XCTWaiter, fulfillmentDidViolateOrderingConstraintsFor expectation: XCTestExpectation, requiredExpectation: XCTestExpectation) { + resume() + } + + func nestedWaiter(_ waiter: XCTWaiter, wasInterruptedByTimedOutWaiter outerWaiter: XCTWaiter) { + + } + + func resume() { + let continuation = state.withCriticalRegion { continuation in + defer { continuation = nil } + return continuation + } + continuation?.resume() + } +} + +extension XCTestCase { + @_disfavoredOverload + func fulfillment(of expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false, file: StaticString = #file, line: Int = #line) async { + return await withUnsafeContinuation { continuation in + let delegate = WaiterDelegate(continuation) + let waiter = XCTWaiter(delegate: delegate) + waiter.wait(for: expectations, timeout: timeout, enforceOrder: enforceOrder) + delegate.resume() + } + } +}