diff --git a/Tests/AsyncAlgorithmsTests/Asserts.swift b/Tests/AsyncAlgorithmsTests/Support/Asserts.swift similarity index 100% rename from Tests/AsyncAlgorithmsTests/Asserts.swift rename to Tests/AsyncAlgorithmsTests/Support/Asserts.swift diff --git a/Tests/AsyncAlgorithmsTests/Support/Gate.swift b/Tests/AsyncAlgorithmsTests/Support/Gate.swift new file mode 100644 index 0000000..6bf4137 --- /dev/null +++ b/Tests/AsyncAlgorithmsTests/Support/Gate.swift @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Async Algorithms open source project +// +// Copyright (c) 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import AsyncAlgorithms + +public struct Gate: Sendable { + enum State { + case closed + case open + case pending(UnsafeContinuation) + } + + let state = ManagedCriticalState(State.closed) + + public func `open`() { + state.withCriticalRegion { state -> UnsafeContinuation? in + switch state { + case .closed: + state = .open + return nil + case .open: + return nil + case .pending(let continuation): + state = .closed + return continuation + } + }?.resume() + } + + public func enter() async { + var other: UnsafeContinuation? + await withUnsafeContinuation { (continuation: UnsafeContinuation) in + state.withCriticalRegion { state -> UnsafeContinuation? in + switch state { + case .closed: + state = .pending(continuation) + return nil + case .open: + state = .closed + return continuation + case .pending(let existing): + other = existing + state = .pending(continuation) + return nil + } + }?.resume() + } + other?.resume() + } +} diff --git a/Tests/AsyncAlgorithmsTests/Support/GatedSequence.swift b/Tests/AsyncAlgorithmsTests/Support/GatedSequence.swift new file mode 100644 index 0000000..50ede10 --- /dev/null +++ b/Tests/AsyncAlgorithmsTests/Support/GatedSequence.swift @@ -0,0 +1,55 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Async Algorithms open source project +// +// Copyright (c) 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +public struct GatedSequence { + let elements: [Element] + let gates: [Gate] + var index = 0 + + public mutating func advance() { + defer { index += 1 } + guard index < gates.count else { + return + } + gates[index].open() + } + + public init(_ elements: [Element]) { + self.elements = elements + self.gates = elements.map { _ in Gate() } + } +} + +extension GatedSequence: AsyncSequence { + public struct Iterator: AsyncIteratorProtocol { + var gatedElements: [(Element, Gate)] + + init(elements: [Element], gates: [Gate]) { + gatedElements = Array(zip(elements, gates)) + } + + public mutating func next() async -> Element? { + guard gatedElements.count > 0 else { + return nil + } + let (element, gate) = gatedElements.removeFirst() + await gate.enter() + return element + } + } + + public func makeAsyncIterator() -> Iterator { + Iterator(elements: elements, gates: gates) + } +} + +extension GatedSequence: Sendable where Element: Sendable { } +extension GatedSequence.Iterator: Sendable where Element: Sendable { } diff --git a/Tests/AsyncAlgorithmsTests/Support/Validator.swift b/Tests/AsyncAlgorithmsTests/Support/Validator.swift new file mode 100644 index 0000000..eb5bc13 --- /dev/null +++ b/Tests/AsyncAlgorithmsTests/Support/Validator.swift @@ -0,0 +1,122 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Async Algorithms open source project +// +// Copyright (c) 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import AsyncAlgorithms + +public struct Validator: Sendable { + private enum Ready { + case idle + case ready + case pending(UnsafeContinuation) + } + + private struct State: Sendable { + var collected = [Element]() + var failure: Error? + var ready: Ready = .idle + } + + private struct Envelope: @unchecked Sendable { + var contents: Contents + } + + private let state = ManagedCriticalState(State()) + + private func ready() { + state.withCriticalRegion { state -> UnsafeContinuation? in + switch state.ready { + case .idle: + state.ready = .ready + return nil + case .pending(let continuation): + state.ready = .idle + return continuation + case .ready: + return nil + } + }?.resume() + } + + private func step() async { + await withUnsafeContinuation { (continuation: UnsafeContinuation) in + state.withCriticalRegion { state -> UnsafeContinuation? in + switch state.ready { + case .ready: + state.ready = .idle + return continuation + case .idle: + state.ready = .pending(continuation) + return nil + case .pending: + fatalError() + } + }?.resume() + } + } + + public func test(_ sequence: S, onFinish: @Sendable @escaping (inout S.AsyncIterator) async -> Void) where S.Element == Element { + let envelope = Envelope(contents: sequence) + Task { + var iterator = envelope.contents.makeAsyncIterator() + ready() + do { + while let item = try await iterator.next() { + state.withCriticalRegion { state -> UnsafeContinuation? in + state.collected.append(item) + switch state.ready { + case .idle: + state.ready = .ready + return nil + case .pending(let continuation): + state.ready = .idle + return continuation + case .ready: + return nil + } + }?.resume() + } + } catch { + state.withCriticalRegion { state -> UnsafeContinuation? in + state.failure = error + switch state.ready { + case .idle: + state.ready = .ready + return nil + case .pending(let continuation): + state.ready = .idle + return continuation + case .ready: + return nil + } + }?.resume() + } + ready() + await onFinish(&iterator) + } + } + + public func validate() async -> [Element] { + await step() + return current + } + + public var current: [Element] { + return state.withCriticalRegion { state in + return state.collected + } + } + + public var failure: Error? { + return state.withCriticalRegion { state in + return state.failure + } + } +} diff --git a/Tests/AsyncAlgorithmsTests/TestValidator.swift b/Tests/AsyncAlgorithmsTests/TestValidator.swift new file mode 100644 index 0000000..9c5ef9c --- /dev/null +++ b/Tests/AsyncAlgorithmsTests/TestValidator.swift @@ -0,0 +1,138 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Async Algorithms open source project +// +// Copyright (c) 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import XCTest +import AsyncAlgorithms + +final class TestValidator: XCTestCase { + func test_gate() async { + let gate = Gate() + let state = ManagedCriticalState(false) + let entered = expectation(description: "entered") + Task { + await gate.enter() + state.withCriticalRegion { $0 = true } + entered.fulfill() + } + XCTAssertFalse(state.withCriticalRegion { $0 }) + gate.open() + wait(for: [entered], timeout: 1.0) + XCTAssertTrue(state.withCriticalRegion { $0 }) + } + + func test_gatedSequence() async { + var gated = GatedSequence([1, 2, 3]) + let expectations = [ + expectation(description: "item 1"), + expectation(description: "item 2"), + expectation(description: "item 3") + ] + let started = expectation(description: "started") + let finished = expectation(description: "finished") + let state = ManagedCriticalState([Int]()) + let seq = gated + Task { + var iterator = seq.makeAsyncIterator() + var index = 0 + started.fulfill() + while let value = await iterator.next() { + state.withCriticalRegion { + $0.append(value) + } + expectations[index].fulfill() + index += 1 + } + finished.fulfill() + } + wait(for: [started], timeout: 1.0) + XCTAssertEqual(state.withCriticalRegion { $0 }, []) + gated.advance() + wait(for: [expectations[0]], timeout: 1.0) + XCTAssertEqual(state.withCriticalRegion { $0 }, [1]) + gated.advance() + wait(for: [expectations[1]], timeout: 1.0) + XCTAssertEqual(state.withCriticalRegion { $0 }, [1, 2]) + gated.advance() + wait(for: [expectations[2]], timeout: 1.0) + XCTAssertEqual(state.withCriticalRegion { $0 }, [1, 2, 3]) + wait(for: [finished], timeout: 1.0) + } + + func test_gatedSequence_throwing() async { + var gated = GatedSequence([1, 2, 3]) + let expectations = [ + expectation(description: "item 1") + ] + let started = expectation(description: "started") + let finished = expectation(description: "finished") + let state = ManagedCriticalState([Int]()) + let failure = ManagedCriticalState(nil) + let seq = gated.map { try throwOn(2, $0) } + Task { + var iterator = seq.makeAsyncIterator() + var index = 0 + started.fulfill() + do { + while let value = try await iterator.next() { + state.withCriticalRegion { + $0.append(value) + } + expectations[index].fulfill() + index += 1 + } + } catch { + failure.withCriticalRegion { $0 = error } + } + finished.fulfill() + } + wait(for: [started], timeout: 1.0) + XCTAssertEqual(state.withCriticalRegion { $0 }, []) + gated.advance() + wait(for: [expectations[0]], timeout: 1.0) + XCTAssertEqual(state.withCriticalRegion { $0 }, [1]) + gated.advance() + XCTAssertEqual(state.withCriticalRegion { $0 }, [1]) + wait(for: [finished], timeout: 1.0) + XCTAssertEqual(state.withCriticalRegion { $0 }, [1]) + XCTAssertEqual(failure.withCriticalRegion { $0 as? Failure }, Failure()) + } + + func test_validator() async { + var a = GatedSequence([1, 2, 3]) + let finished = expectation(description: "finished") + let sequence = a.map { $0 + 1 } + let validator = Validator() + validator.test(sequence) { iterator in + let pastEnd = await iterator.next() + XCTAssertNil(pastEnd) + finished.fulfill() + } + var value = await validator.validate() + XCTAssertEqual(value, []) + a.advance() + + value = await validator.validate() + XCTAssertEqual(value, [2]) + a.advance() + + value = await validator.validate() + XCTAssertEqual(value, [2, 3]) + a.advance() + + value = await validator.validate() + XCTAssertEqual(value, [2, 3, 4]) + a.advance() + + wait(for: [finished], timeout: 1.0) + value = validator.current + XCTAssertEqual(value, [2, 3, 4]) + } +}