|
| 1 | +import Foundation |
| 2 | +import XCTest |
| 3 | + |
| 4 | +extension Task where Success == Never, Failure == Never { |
| 5 | + static func sleep(seconds: Double) async throws { |
| 6 | + let nanoseconds = UInt64(seconds * Double(NSEC_PER_SEC)) |
| 7 | + try await Task.sleep(nanoseconds: nanoseconds) |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +public actor AsyncExpectation { |
| 12 | + enum State { |
| 13 | + case pending |
| 14 | + case fulfilled |
| 15 | + case timedOut |
| 16 | + } |
| 17 | + public typealias AsyncExpectationContinuation = CheckedContinuation<Void, Error> |
| 18 | + public let expectationDescription: String |
| 19 | + public var isInverted: Bool |
| 20 | + public var expectedFulfillmentCount: Int |
| 21 | + |
| 22 | + private var fulfillmentCount: Int = 0 |
| 23 | + private var continuation: AsyncExpectationContinuation? |
| 24 | + private var state: State = .pending |
| 25 | + |
| 26 | + private var satisified: Bool { |
| 27 | + fulfillmentCount == expectedFulfillmentCount |
| 28 | + } |
| 29 | + |
| 30 | + public init(description: String, |
| 31 | + isInverted: Bool = false, |
| 32 | + expectedFulfillmentCount: Int = 1) { |
| 33 | + expectationDescription = description |
| 34 | + self.isInverted = isInverted |
| 35 | + self.expectedFulfillmentCount = expectedFulfillmentCount |
| 36 | + } |
| 37 | + |
| 38 | + public static func expectation(description: String, |
| 39 | + isInverted: Bool = false, |
| 40 | + expectedFulfillmentCount: Int = 1) -> AsyncExpectation { |
| 41 | + AsyncExpectation(description: description, |
| 42 | + isInverted: isInverted, |
| 43 | + expectedFulfillmentCount: expectedFulfillmentCount) |
| 44 | + } |
| 45 | + |
| 46 | + public func fulfill(file: StaticString = #filePath, line: UInt = #line) { |
| 47 | + guard state != .fulfilled else { return } |
| 48 | + |
| 49 | + |
| 50 | + guard !isInverted else { |
| 51 | + XCTFail("Inverted expectation fulfilled: \(expectationDescription)", file: file, line: line) |
| 52 | + finish() |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + fulfillmentCount += 1 |
| 57 | + if fulfillmentCount == expectedFulfillmentCount { |
| 58 | + state = .fulfilled |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @MainActor |
| 63 | + public static func waitForExpectations(_ expectations: [AsyncExpectation], |
| 64 | + timeout: Double = 1.0, |
| 65 | + file: StaticString = #filePath, |
| 66 | + line: UInt = #line) async throws { |
| 67 | + guard !expectations.isEmpty else { return } |
| 68 | + |
| 69 | + // check if all expectations are already satisfied and skip sleeping |
| 70 | + var count = 0 |
| 71 | + for exp in expectations { |
| 72 | + if await exp.satisified { |
| 73 | + count += 1 |
| 74 | + } |
| 75 | + } |
| 76 | + if count == expectations.count { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + let timeout = Task { |
| 81 | + try await Task.sleep(seconds: timeout) |
| 82 | + for exp in expectations { |
| 83 | + await exp.timeOut(file: file, line: line) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + await withThrowingTaskGroup(of: Void.self) { group in |
| 88 | + for exp in expectations { |
| 89 | + group.addTask { |
| 90 | + try await exp.wait() |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + timeout.cancel() |
| 96 | + } |
| 97 | + |
| 98 | + private func wait() async throws { |
| 99 | + try await withTaskCancellationHandler(handler: { |
| 100 | + Task { |
| 101 | + await cancel() |
| 102 | + } |
| 103 | + }, operation: { |
| 104 | + try await withCheckedThrowingContinuation { (continuation: AsyncExpectationContinuation) in |
| 105 | + self.continuation = continuation |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + private func timeOut(file: StaticString = #filePath, |
| 111 | + line: UInt = #line) async { |
| 112 | + if !satisified && !isInverted { |
| 113 | + state = .timedOut |
| 114 | + XCTFail("Expectation timed out: \(expectationDescription)", file: file, line: line) |
| 115 | + } |
| 116 | + finish() |
| 117 | + } |
| 118 | + |
| 119 | + private func cancel() { |
| 120 | + continuation?.resume(throwing: CancellationError()) |
| 121 | + continuation = nil |
| 122 | + } |
| 123 | + |
| 124 | + private func finish() { |
| 125 | + continuation?.resume(returning: ()) |
| 126 | + continuation = nil |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments