mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-27 04:44:21 +00:00
Add a validator testing support mechanism (#24)
* Add a validator testing support mechanism * Move the remaining support items into the test support directory * Add rudimentary tests to validate the validator
This commit is contained in:
1 parent
ad2d2fca7e
commit
27e6645329
5 files changed
+373
No files matched your search
File renamed without changes.
@@ -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<Void, Never>)
|
||||
}
|
||||
|
||||
let state = ManagedCriticalState(State.closed)
|
||||
|
||||
public func `open`() {
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<Void, Never>? 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<Void, Never>?
|
||||
await withUnsafeContinuation { (continuation: UnsafeContinuation<Void, Never>) in
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<Void, Never>? 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()
|
||||
}
|
||||
}
|
||||
@@ -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<Element> {
|
||||
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 { }
|
||||
@@ -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<Element: Sendable>: Sendable {
|
||||
private enum Ready {
|
||||
case idle
|
||||
case ready
|
||||
case pending(UnsafeContinuation<Void, Never>)
|
||||
}
|
||||
|
||||
private struct State: Sendable {
|
||||
var collected = [Element]()
|
||||
var failure: Error?
|
||||
var ready: Ready = .idle
|
||||
}
|
||||
|
||||
private struct Envelope<Contents>: @unchecked Sendable {
|
||||
var contents: Contents
|
||||
}
|
||||
|
||||
private let state = ManagedCriticalState(State())
|
||||
|
||||
private func ready() {
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<Void, Never>? 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<Void, Never>) in
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<Void, Never>? 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<S: AsyncSequence>(_ 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<Void, Never>? 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<Void, Never>? 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Error?>(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<Int>()
|
||||
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])
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user