mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-25 04:27:48 +00:00
Validate AsyncSequence specification SE-0323 (#60)
This commit is contained in:
1 parent
d90945abc8
commit
9377b4de58
5 files changed
+118
-4
No files matched your search
@@ -30,6 +30,9 @@ extension AsyncSequenceValidationDiagram {
|
||||
case unexpectedValue(String)
|
||||
case unexpectedFinish
|
||||
case unexpectedFailure(Error)
|
||||
|
||||
case specificationViolationGotValueAfterIteration(String)
|
||||
case specificationViolationGotFailureAfterIteration(Error)
|
||||
}
|
||||
public var when: Clock.Instant
|
||||
public var kind: Kind
|
||||
@@ -62,6 +65,10 @@ extension AsyncSequenceValidationDiagram {
|
||||
return "unexpected finish"
|
||||
case .unexpectedFailure:
|
||||
return "unexpected failure"
|
||||
case .specificationViolationGotValueAfterIteration(let actual):
|
||||
return "specification violation got \"\(actual)\" after iteration terminated"
|
||||
case .specificationViolationGotFailureAfterIteration(let error):
|
||||
return "specification violation got failure after iteration terminated"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,8 +32,20 @@ extension AsyncSequenceValidationDiagram {
|
||||
let output: String
|
||||
|
||||
func test(_ event: (String) -> Void) async throws {
|
||||
for try await item in sequence {
|
||||
event(item)
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
do {
|
||||
while let item = try await iterator.next() {
|
||||
event(item)
|
||||
}
|
||||
do {
|
||||
if let pastEnd = try await iterator.next(){
|
||||
Context.specificationFailures.append(ExpectationFailure(when: Context.clock!.now, kind: .specificationViolationGotValueAfterIteration(pastEnd)))
|
||||
}
|
||||
} catch {
|
||||
Context.specificationFailures.append(ExpectationFailure(when: Context.clock!.now, kind: .specificationViolationGotFailureAfterIteration(error)))
|
||||
}
|
||||
} catch {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,6 +68,8 @@ extension AsyncSequenceValidationDiagram {
|
||||
static var driver: TaskDriver?
|
||||
|
||||
static var currentJob: Job?
|
||||
|
||||
static var specificationFailures = [ExpectationFailure]()
|
||||
}
|
||||
|
||||
enum ActualResult {
|
||||
@@ -84,7 +98,8 @@ extension AsyncSequenceValidationDiagram {
|
||||
actual: [(Clock.Instant, Result<String?, Error>)]
|
||||
) -> (ExpectationResult, [ExpectationFailure]) {
|
||||
let result = ExpectationResult(expected: expected, actual: actual)
|
||||
var failures = [ExpectationFailure]()
|
||||
var failures = Context.specificationFailures
|
||||
Context.specificationFailures.removeAll()
|
||||
|
||||
let actualTimes = actual.map { when, _ in when }
|
||||
let expectedTimes = expected.map { when, _ in when }
|
||||
@@ -238,6 +253,7 @@ extension AsyncSequenceValidationDiagram {
|
||||
|
||||
let actual = ManagedCriticalState([(Clock.Instant, Result<String?, Error>)]())
|
||||
Context.clock = clock
|
||||
Context.specificationFailures.removeAll()
|
||||
// This all needs to be isolated from potential Tasks (the caller function might be async!)
|
||||
Context.driver = TaskDriver(queue: diagram.queue) { driver in
|
||||
swift_task_enqueueGlobal_hook = { job, original in
|
||||
|
||||
@@ -239,7 +239,7 @@ struct WorkQueue: Sendable {
|
||||
}
|
||||
if state.items[token]?.isCancelled == true {
|
||||
let item: Item = .work(token, {
|
||||
continuation.resume(throwing: CancellationError())
|
||||
continuation.resume(returning: nil) // the input sequences should not throw cancellation errors
|
||||
})
|
||||
state.queues[job, default: []].append(item)
|
||||
state.items[token] = item
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension AsyncSequence {
|
||||
func violatingSpecification(returningPastEndIteration element: Element) -> SpecificationViolatingSequence<Self> {
|
||||
SpecificationViolatingSequence(self, kind: .producing(element))
|
||||
}
|
||||
|
||||
func violatingSpecification(throwingPastEndIteration error: Error) -> SpecificationViolatingSequence<Self> {
|
||||
SpecificationViolatingSequence(self, kind: .throwing(error))
|
||||
}
|
||||
}
|
||||
|
||||
struct SpecificationViolatingSequence<Base: AsyncSequence> {
|
||||
enum Kind {
|
||||
case producing(Base.Element)
|
||||
case throwing(Error)
|
||||
}
|
||||
|
||||
let base: Base
|
||||
let kind: Kind
|
||||
|
||||
init(_ base: Base, kind: Kind) {
|
||||
self.base = base
|
||||
self.kind = kind
|
||||
}
|
||||
}
|
||||
|
||||
extension SpecificationViolatingSequence: AsyncSequence {
|
||||
typealias Element = Base.Element
|
||||
|
||||
struct Iterator: AsyncIteratorProtocol {
|
||||
var iterator: Base.AsyncIterator
|
||||
let kind: Kind
|
||||
var finished = false
|
||||
var violated = false
|
||||
|
||||
mutating func next() async throws -> Element? {
|
||||
if finished {
|
||||
if violated {
|
||||
return nil
|
||||
}
|
||||
violated = true
|
||||
switch kind {
|
||||
case .producing(let element): return element
|
||||
case .throwing(let error): throw error
|
||||
}
|
||||
}
|
||||
do {
|
||||
if let value = try await iterator.next() {
|
||||
return value
|
||||
}
|
||||
finished = true
|
||||
return nil
|
||||
} catch {
|
||||
finished = true
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func makeAsyncIterator() -> Iterator {
|
||||
Iterator(iterator: base.makeAsyncIterator(), kind: kind)
|
||||
}
|
||||
}
|
||||
@@ -232,4 +232,22 @@ final class TestValidationDiagram: XCTestCase {
|
||||
"[a-]b|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_diagram_specification_produce_past_end() {
|
||||
expectFailures(["specification violation got \"d\" after iteration terminated at tick 9"])
|
||||
validate {
|
||||
"a--b--c--|"
|
||||
$0.inputs[0].violatingSpecification(returningPastEndIteration: "d")
|
||||
"a--b--c--|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_diagram_specification_throw_past_end() {
|
||||
expectFailures(["specification violation got failure after iteration terminated at tick 9"])
|
||||
validate {
|
||||
"a--b--c--|"
|
||||
$0.inputs[0].violatingSpecification(throwingPastEndIteration: Failure())
|
||||
"a--b--c--|"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user