mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-30 04:58:44 +00:00
Apply formatting
This commit is contained in:
1 parent
ba33a225e9
commit
d5b49aab17
97 files changed
+5078
-4440
No files matched your search
@@ -11,7 +11,7 @@
|
||||
|
||||
import XCTest
|
||||
|
||||
fileprivate enum _XCTAssertion {
|
||||
private enum _XCTAssertion {
|
||||
case equal
|
||||
case equalWithAccuracy
|
||||
case identical
|
||||
@@ -30,9 +30,9 @@ fileprivate enum _XCTAssertion {
|
||||
case fail
|
||||
case throwsError
|
||||
case noThrow
|
||||
|
||||
|
||||
var name: String? {
|
||||
switch(self) {
|
||||
switch self {
|
||||
case .equal: return "XCTAssertEqual"
|
||||
case .equalWithAccuracy: return "XCTAssertEqual"
|
||||
case .identical: return "XCTAssertIdentical"
|
||||
@@ -55,18 +55,18 @@ fileprivate enum _XCTAssertion {
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate enum _XCTAssertionResult {
|
||||
private enum _XCTAssertionResult {
|
||||
case success
|
||||
case expectedFailure(String?)
|
||||
case unexpectedFailure(Swift.Error)
|
||||
|
||||
|
||||
var isExpected: Bool {
|
||||
switch self {
|
||||
case .unexpectedFailure(_): return false
|
||||
default: return true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func failureDescription(_ assertion: _XCTAssertion) -> String {
|
||||
let explanation: String
|
||||
switch self {
|
||||
@@ -75,23 +75,28 @@ fileprivate enum _XCTAssertionResult {
|
||||
case .expectedFailure(_): explanation = "failed"
|
||||
case .unexpectedFailure(let error): explanation = "threw error \"\(error)\""
|
||||
}
|
||||
|
||||
if let name = assertion.name {
|
||||
return "\(name) \(explanation)"
|
||||
} else {
|
||||
|
||||
guard let name = assertion.name else {
|
||||
return explanation
|
||||
}
|
||||
return "\(name) \(explanation)"
|
||||
}
|
||||
}
|
||||
|
||||
private func _XCTEvaluateAssertion(_ assertion: _XCTAssertion, message: () -> String, file: StaticString, line: UInt, expression: () throws -> _XCTAssertionResult) {
|
||||
private func _XCTEvaluateAssertion(
|
||||
_ assertion: _XCTAssertion,
|
||||
message: () -> String,
|
||||
file: StaticString,
|
||||
line: UInt,
|
||||
expression: () throws -> _XCTAssertionResult
|
||||
) {
|
||||
let result: _XCTAssertionResult
|
||||
do {
|
||||
result = try expression()
|
||||
} catch {
|
||||
result = .unexpectedFailure(error)
|
||||
}
|
||||
|
||||
|
||||
switch result {
|
||||
case .success:
|
||||
return
|
||||
@@ -100,22 +105,34 @@ private func _XCTEvaluateAssertion(_ assertion: _XCTAssertion, message: () -> St
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func _XCTAssertEqual<T>(_ expression1: () throws -> T, _ expression2: () throws -> T, _ equal: (T, T) -> Bool, _ message: () -> String, file: StaticString = #filePath, line: UInt = #line) {
|
||||
private func _XCTAssertEqual<T>(
|
||||
_ expression1: () throws -> T,
|
||||
_ expression2: () throws -> T,
|
||||
_ equal: (T, T) -> Bool,
|
||||
_ message: () -> String,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) {
|
||||
_XCTEvaluateAssertion(.equal, message: message, file: file, line: line) {
|
||||
let (value1, value2) = (try expression1(), try expression2())
|
||||
if equal(value1, value2) {
|
||||
return .success
|
||||
} else {
|
||||
guard equal(value1, value2) else {
|
||||
return .expectedFailure("(\"\(value1)\") is not equal to (\"\(value2)\")")
|
||||
}
|
||||
return .success
|
||||
}
|
||||
}
|
||||
|
||||
public func XCTAssertEqual<A: Equatable, B: Equatable>(_ expression1: @autoclosure () throws -> (A, B), _ expression2: @autoclosure () throws -> (A, B), _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
|
||||
public func XCTAssertEqual<A: Equatable, B: Equatable>(
|
||||
_ expression1: @autoclosure () throws -> (A, B),
|
||||
_ expression2: @autoclosure () throws -> (A, B),
|
||||
_ message: @autoclosure () -> String = "",
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) {
|
||||
_XCTAssertEqual(expression1, expression2, { $0 == $1 }, message, file: file, line: line)
|
||||
}
|
||||
|
||||
fileprivate func ==<A: Equatable, B: Equatable>(_ lhs: [(A, B)], _ rhs: [(A, B)]) -> Bool {
|
||||
private func == <A: Equatable, B: Equatable>(_ lhs: [(A, B)], _ rhs: [(A, B)]) -> Bool {
|
||||
guard lhs.count == rhs.count else {
|
||||
return false
|
||||
}
|
||||
@@ -127,15 +144,27 @@ fileprivate func ==<A: Equatable, B: Equatable>(_ lhs: [(A, B)], _ rhs: [(A, B)]
|
||||
return true
|
||||
}
|
||||
|
||||
public func XCTAssertEqual<A: Equatable, B: Equatable>(_ expression1: @autoclosure () throws -> [(A, B)], _ expression2: @autoclosure () throws -> [(A, B)], _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
|
||||
public func XCTAssertEqual<A: Equatable, B: Equatable>(
|
||||
_ expression1: @autoclosure () throws -> [(A, B)],
|
||||
_ expression2: @autoclosure () throws -> [(A, B)],
|
||||
_ message: @autoclosure () -> String = "",
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) {
|
||||
_XCTAssertEqual(expression1, expression2, { $0 == $1 }, message, file: file, line: line)
|
||||
}
|
||||
|
||||
public func XCTAssertEqual<A: Equatable, B: Equatable, C: Equatable>(_ expression1: @autoclosure () throws -> (A, B, C), _ expression2: @autoclosure () throws -> (A, B, C), _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
|
||||
public func XCTAssertEqual<A: Equatable, B: Equatable, C: Equatable>(
|
||||
_ expression1: @autoclosure () throws -> (A, B, C),
|
||||
_ expression2: @autoclosure () throws -> (A, B, C),
|
||||
_ message: @autoclosure () -> String = "",
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) {
|
||||
_XCTAssertEqual(expression1, expression2, { $0 == $1 }, message, file: file, line: line)
|
||||
}
|
||||
|
||||
fileprivate func ==<A: Equatable, B: Equatable, C: Equatable>(_ lhs: [(A, B, C)], _ rhs: [(A, B, C)]) -> Bool {
|
||||
private func == <A: Equatable, B: Equatable, C: Equatable>(_ lhs: [(A, B, C)], _ rhs: [(A, B, C)]) -> Bool {
|
||||
guard lhs.count == rhs.count else {
|
||||
return false
|
||||
}
|
||||
@@ -147,48 +176,58 @@ fileprivate func ==<A: Equatable, B: Equatable, C: Equatable>(_ lhs: [(A, B, C)]
|
||||
return true
|
||||
}
|
||||
|
||||
public func XCTAssertEqual<A: Equatable, B: Equatable, C: Equatable>(_ expression1: @autoclosure () throws -> [(A, B, C)], _ expression2: @autoclosure () throws -> [(A, B, C)], _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
|
||||
public func XCTAssertEqual<A: Equatable, B: Equatable, C: Equatable>(
|
||||
_ expression1: @autoclosure () throws -> [(A, B, C)],
|
||||
_ expression2: @autoclosure () throws -> [(A, B, C)],
|
||||
_ message: @autoclosure () -> String = "",
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) {
|
||||
_XCTAssertEqual(expression1, expression2, { $0 == $1 }, message, file: file, line: line)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
||||
internal func XCTAssertThrowsError<T>(
|
||||
_ expression: @autoclosure () async throws -> T,
|
||||
file: StaticString = #file,
|
||||
line: UInt = #line,
|
||||
verify: (Error) -> Void = { _ in }
|
||||
_ expression: @autoclosure () async throws -> T,
|
||||
file: StaticString = #file,
|
||||
line: UInt = #line,
|
||||
verify: (Error) -> Void = { _ in }
|
||||
) async {
|
||||
do {
|
||||
_ = try await expression()
|
||||
XCTFail("Expression did not throw error", file: file, line: line)
|
||||
} catch {
|
||||
verify(error)
|
||||
}
|
||||
do {
|
||||
_ = try await expression()
|
||||
XCTFail("Expression did not throw error", file: file, line: line)
|
||||
} catch {
|
||||
verify(error)
|
||||
}
|
||||
}
|
||||
|
||||
class WaiterDelegate: NSObject, XCTWaiterDelegate {
|
||||
let state: ManagedCriticalState<UnsafeContinuation<Void, Never>?> = ManagedCriticalState(nil)
|
||||
|
||||
|
||||
init(_ continuation: UnsafeContinuation<Void, Never>) {
|
||||
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) {
|
||||
|
||||
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 }
|
||||
@@ -200,7 +239,13 @@ class WaiterDelegate: NSObject, XCTWaiterDelegate {
|
||||
|
||||
extension XCTestCase {
|
||||
@_disfavoredOverload
|
||||
func fulfillment(of expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false, file: StaticString = #file, line: Int = #line) async {
|
||||
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)
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
struct Failure: Error, Equatable { }
|
||||
struct Failure: Error, Equatable {}
|
||||
|
||||
func throwOn<T: Equatable>(_ toThrowOn: T, _ value: T) throws -> T {
|
||||
if value == toThrowOn {
|
||||
|
||||
@@ -17,9 +17,9 @@ public struct Gate: Sendable {
|
||||
case open
|
||||
case pending(UnsafeContinuation<Void, Never>)
|
||||
}
|
||||
|
||||
|
||||
let state = ManagedCriticalState(State.closed)
|
||||
|
||||
|
||||
public func `open`() {
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<Void, Never>? in
|
||||
switch state {
|
||||
@@ -34,7 +34,7 @@ public struct Gate: Sendable {
|
||||
}
|
||||
}?.resume()
|
||||
}
|
||||
|
||||
|
||||
public func enter() async {
|
||||
var other: UnsafeContinuation<Void, Never>?
|
||||
await withUnsafeContinuation { (continuation: UnsafeContinuation<Void, Never>) in
|
||||
@@ -45,7 +45,7 @@ public struct Gate: Sendable {
|
||||
return nil
|
||||
case .open:
|
||||
state = .closed
|
||||
return continuation
|
||||
return continuation
|
||||
case .pending(let existing):
|
||||
other = existing
|
||||
state = .pending(continuation)
|
||||
|
||||
@@ -13,7 +13,7 @@ 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 {
|
||||
@@ -21,7 +21,7 @@ public struct GatedSequence<Element> {
|
||||
}
|
||||
gates[index].open()
|
||||
}
|
||||
|
||||
|
||||
public init(_ elements: [Element]) {
|
||||
self.elements = elements
|
||||
self.gates = elements.map { _ in Gate() }
|
||||
@@ -31,11 +31,11 @@ public struct GatedSequence<Element> {
|
||||
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
|
||||
@@ -45,10 +45,10 @@ extension GatedSequence: AsyncSequence {
|
||||
return element
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
Iterator(elements: elements, gates: gates)
|
||||
}
|
||||
}
|
||||
|
||||
extension GatedSequence: Sendable where Element: Sendable { }
|
||||
extension GatedSequence: Sendable where Element: Sendable {}
|
||||
@@ -11,11 +11,11 @@
|
||||
|
||||
struct Indefinite<Element: Sendable>: Sequence, IteratorProtocol, Sendable {
|
||||
let value: Element
|
||||
|
||||
|
||||
func next() -> Element? {
|
||||
return value
|
||||
}
|
||||
|
||||
|
||||
func makeIterator() -> Indefinite<Element> {
|
||||
self
|
||||
}
|
||||
|
||||
@@ -14,78 +14,78 @@ import AsyncAlgorithms
|
||||
public struct ManualClock: Clock {
|
||||
public struct Step: DurationProtocol {
|
||||
fileprivate var rawValue: Int
|
||||
|
||||
|
||||
fileprivate init(_ rawValue: Int) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
|
||||
public static func + (lhs: ManualClock.Step, rhs: ManualClock.Step) -> ManualClock.Step {
|
||||
return .init(lhs.rawValue + rhs.rawValue)
|
||||
}
|
||||
|
||||
|
||||
public static func - (lhs: ManualClock.Step, rhs: ManualClock.Step) -> ManualClock.Step {
|
||||
.init(lhs.rawValue - rhs.rawValue)
|
||||
}
|
||||
|
||||
|
||||
public static func / (lhs: ManualClock.Step, rhs: Int) -> ManualClock.Step {
|
||||
.init(lhs.rawValue / rhs)
|
||||
}
|
||||
|
||||
|
||||
public static func * (lhs: ManualClock.Step, rhs: Int) -> ManualClock.Step {
|
||||
.init(lhs.rawValue * rhs)
|
||||
}
|
||||
|
||||
|
||||
public static func / (lhs: ManualClock.Step, rhs: ManualClock.Step) -> Double {
|
||||
Double(lhs.rawValue) / Double(rhs.rawValue)
|
||||
}
|
||||
|
||||
|
||||
public static func < (lhs: ManualClock.Step, rhs: ManualClock.Step) -> Bool {
|
||||
lhs.rawValue < rhs.rawValue
|
||||
}
|
||||
|
||||
|
||||
public static var zero: ManualClock.Step { .init(0) }
|
||||
|
||||
|
||||
public static func steps(_ amount: Int) -> Step {
|
||||
return Step(amount)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public struct Instant: InstantProtocol, CustomStringConvertible {
|
||||
public typealias Duration = Step
|
||||
|
||||
|
||||
internal let rawValue: Int
|
||||
|
||||
|
||||
internal init(_ rawValue: Int) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
|
||||
public static func < (lhs: ManualClock.Instant, rhs: ManualClock.Instant) -> Bool {
|
||||
return lhs.rawValue < rhs.rawValue
|
||||
}
|
||||
|
||||
|
||||
public func advanced(by duration: ManualClock.Step) -> ManualClock.Instant {
|
||||
.init(rawValue + duration.rawValue)
|
||||
}
|
||||
|
||||
|
||||
public func duration(to other: ManualClock.Instant) -> ManualClock.Step {
|
||||
.init(other.rawValue - rawValue)
|
||||
}
|
||||
|
||||
|
||||
public var description: String {
|
||||
return "tick \(rawValue)"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fileprivate struct Wakeup {
|
||||
let generation: Int
|
||||
let continuation: UnsafeContinuation<Void, Error>
|
||||
let deadline: Instant
|
||||
}
|
||||
|
||||
|
||||
fileprivate enum Scheduled: Hashable, Comparable, CustomStringConvertible {
|
||||
case cancelled(Int)
|
||||
case wakeup(Wakeup)
|
||||
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
switch self {
|
||||
case .cancelled(let generation):
|
||||
@@ -94,14 +94,14 @@ public struct ManualClock: Clock {
|
||||
hasher.combine(wakeup.generation)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
case .cancelled: return "Cancelled wakeup"
|
||||
case .wakeup(let wakeup): return "Wakeup at \(wakeup.deadline)"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static func == (_ lhs: Scheduled, _ rhs: Scheduled) -> Bool {
|
||||
switch (lhs, rhs) {
|
||||
case (.cancelled(let lhsGen), .cancelled(let rhsGen)):
|
||||
@@ -114,7 +114,7 @@ public struct ManualClock: Clock {
|
||||
return lhs.generation == rhs.generation
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static func < (lhs: ManualClock.Scheduled, rhs: ManualClock.Scheduled) -> Bool {
|
||||
switch (lhs, rhs) {
|
||||
case (.cancelled(let lhsGen), .cancelled(let rhsGen)):
|
||||
@@ -127,14 +127,14 @@ public struct ManualClock: Clock {
|
||||
return lhs.generation < rhs.generation
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var deadline: Instant? {
|
||||
switch self {
|
||||
case .cancelled: return nil
|
||||
case .wakeup(let wakeup): return wakeup.deadline
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func resume() {
|
||||
switch self {
|
||||
case .wakeup(let wakeup):
|
||||
@@ -144,54 +144,52 @@ public struct ManualClock: Clock {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fileprivate struct State {
|
||||
var generation = 0
|
||||
var scheduled = Set<Scheduled>()
|
||||
var now = Instant(0)
|
||||
var hasSleepers = false
|
||||
}
|
||||
|
||||
|
||||
fileprivate let state = ManagedCriticalState(State())
|
||||
|
||||
|
||||
public var now: Instant {
|
||||
state.withCriticalRegion { $0.now }
|
||||
}
|
||||
|
||||
|
||||
public var minimumResolution: Step { return .zero }
|
||||
|
||||
public init() { }
|
||||
|
||||
public init() {}
|
||||
|
||||
fileprivate func cancel(_ generation: Int) {
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<Void, Error>? in
|
||||
if let existing = state.scheduled.remove(.cancelled(generation)) {
|
||||
switch existing {
|
||||
case .wakeup(let wakeup):
|
||||
return wakeup.continuation
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
guard let existing = state.scheduled.remove(.cancelled(generation)) else {
|
||||
// insert the cancelled state for when it comes in to be scheduled as a wakeup
|
||||
state.scheduled.insert(.cancelled(generation))
|
||||
return nil
|
||||
}
|
||||
switch existing {
|
||||
case .wakeup(let wakeup):
|
||||
return wakeup.continuation
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}?.resume(throwing: CancellationError())
|
||||
}
|
||||
|
||||
|
||||
var hasSleepers: Bool {
|
||||
state.withCriticalRegion { $0.hasSleepers }
|
||||
}
|
||||
|
||||
|
||||
public func advance() {
|
||||
let pending = state.withCriticalRegion { state -> Set<Scheduled> in
|
||||
state.now = state.now.advanced(by: .steps(1))
|
||||
let pending = state.scheduled.filter { item in
|
||||
if let deadline = item.deadline {
|
||||
return deadline <= state.now
|
||||
} else {
|
||||
guard let deadline = item.deadline else {
|
||||
return false
|
||||
}
|
||||
return deadline <= state.now
|
||||
}
|
||||
state.scheduled.subtract(pending)
|
||||
if pending.count > 0 {
|
||||
@@ -203,42 +201,40 @@ public struct ManualClock: Clock {
|
||||
item.resume()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public func advance(by steps: Step) {
|
||||
for _ in 0..<steps.rawValue {
|
||||
advance()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fileprivate func schedule(_ generation: Int, continuation: UnsafeContinuation<Void, Error>, deadline: Instant) {
|
||||
let resumption = state.withCriticalRegion { state -> (UnsafeContinuation<Void, Error>, Result<Void, Error>)? in
|
||||
let wakeup = Wakeup(generation: generation, continuation: continuation, deadline: deadline)
|
||||
if let existing = state.scheduled.remove(.wakeup(wakeup)) {
|
||||
switch existing {
|
||||
case .wakeup:
|
||||
fatalError()
|
||||
case .cancelled:
|
||||
// dont bother adding it back because it has been cancelled before we got here
|
||||
return (continuation, .failure(CancellationError()))
|
||||
}
|
||||
} else {
|
||||
guard let existing = state.scheduled.remove(.wakeup(wakeup)) else {
|
||||
// there is no cancelled placeholder so let it run free
|
||||
if deadline > state.now {
|
||||
// the deadline is in the future so run it then
|
||||
state.hasSleepers = true
|
||||
state.scheduled.insert(.wakeup(wakeup))
|
||||
return nil
|
||||
} else {
|
||||
guard deadline > state.now else {
|
||||
// the deadline is now or in the past so run it immediately
|
||||
return (continuation, .success(()))
|
||||
}
|
||||
// the deadline is in the future so run it then
|
||||
state.hasSleepers = true
|
||||
state.scheduled.insert(.wakeup(wakeup))
|
||||
return nil
|
||||
}
|
||||
switch existing {
|
||||
case .wakeup:
|
||||
fatalError()
|
||||
case .cancelled:
|
||||
// dont bother adding it back because it has been cancelled before we got here
|
||||
return (continuation, .failure(CancellationError()))
|
||||
}
|
||||
}
|
||||
if let resumption = resumption {
|
||||
resumption.0.resume(with: resumption.1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public func sleep(until deadline: Instant, tolerance: Step? = nil) async throws {
|
||||
let generation = state.withCriticalRegion { state -> Int in
|
||||
defer { state.generation += 1 }
|
||||
|
||||
@@ -13,7 +13,7 @@ final class ReportingSequence<Element>: Sequence, IteratorProtocol {
|
||||
enum Event: Equatable, CustomStringConvertible {
|
||||
case next
|
||||
case makeIterator
|
||||
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
case .next: return "next()"
|
||||
@@ -21,14 +21,14 @@ final class ReportingSequence<Element>: Sequence, IteratorProtocol {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var events = [Event]()
|
||||
var elements: [Element]
|
||||
|
||||
init(_ elements: [Element]) {
|
||||
self.elements = elements
|
||||
}
|
||||
|
||||
|
||||
func next() -> Element? {
|
||||
events.append(.next)
|
||||
guard elements.count > 0 else {
|
||||
@@ -36,7 +36,7 @@ final class ReportingSequence<Element>: Sequence, IteratorProtocol {
|
||||
}
|
||||
return elements.removeFirst()
|
||||
}
|
||||
|
||||
|
||||
func makeIterator() -> ReportingSequence {
|
||||
events.append(.makeIterator)
|
||||
return self
|
||||
@@ -47,7 +47,7 @@ final class ReportingAsyncSequence<Element: Sendable>: AsyncSequence, AsyncItera
|
||||
enum Event: Equatable, CustomStringConvertible {
|
||||
case next
|
||||
case makeAsyncIterator
|
||||
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
case .next: return "next()"
|
||||
@@ -55,14 +55,14 @@ final class ReportingAsyncSequence<Element: Sendable>: AsyncSequence, AsyncItera
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var events = [Event]()
|
||||
var elements: [Element]
|
||||
|
||||
init(_ elements: [Element]) {
|
||||
self.elements = elements
|
||||
}
|
||||
|
||||
|
||||
func next() async -> Element? {
|
||||
events.append(.next)
|
||||
guard elements.count > 0 else {
|
||||
@@ -70,7 +70,7 @@ final class ReportingAsyncSequence<Element: Sendable>: AsyncSequence, AsyncItera
|
||||
}
|
||||
return elements.removeFirst()
|
||||
}
|
||||
|
||||
|
||||
func makeAsyncIterator() -> ReportingAsyncSequence {
|
||||
events.append(.makeAsyncIterator)
|
||||
return self
|
||||
|
||||
@@ -17,17 +17,17 @@ public struct Validator<Element: Sendable>: Sendable {
|
||||
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(_ apply: (inout State) -> Void) {
|
||||
@@ -45,7 +45,7 @@ public struct Validator<Element: Sendable>: Sendable {
|
||||
}
|
||||
}?.resume()
|
||||
}
|
||||
|
||||
|
||||
internal func step() async {
|
||||
await withUnsafeContinuation { (continuation: UnsafeContinuation<Void, Never>) in
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<Void, Never>? in
|
||||
@@ -64,17 +64,20 @@ public struct Validator<Element: Sendable>: Sendable {
|
||||
}
|
||||
|
||||
let onEvent: (@Sendable (Result<Element?, Error>) async -> Void)?
|
||||
|
||||
|
||||
init(onEvent: @Sendable @escaping (Result<Element?, Error>) async -> Void) {
|
||||
|
||||
self.onEvent = onEvent
|
||||
}
|
||||
|
||||
|
||||
public init() {
|
||||
self.onEvent = nil
|
||||
}
|
||||
|
||||
public func test<S: AsyncSequence>(_ sequence: S, onFinish: @Sendable @escaping (inout S.AsyncIterator) async -> Void) where S.Element == Element {
|
||||
|
||||
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()
|
||||
@@ -97,18 +100,18 @@ public struct Validator<Element: Sendable>: Sendable {
|
||||
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
|
||||
|
||||
@@ -13,7 +13,7 @@ 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))
|
||||
}
|
||||
@@ -24,10 +24,10 @@ struct SpecificationViolatingSequence<Base: AsyncSequence> {
|
||||
case producing(Base.Element)
|
||||
case throwing(Error)
|
||||
}
|
||||
|
||||
|
||||
let base: Base
|
||||
let kind: Kind
|
||||
|
||||
|
||||
init(_ base: Base, kind: Kind) {
|
||||
self.base = base
|
||||
self.kind = kind
|
||||
@@ -36,13 +36,13 @@ struct SpecificationViolatingSequence<Base: AsyncSequence> {
|
||||
|
||||
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 {
|
||||
@@ -66,7 +66,7 @@ extension SpecificationViolatingSequence: AsyncSequence {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func makeAsyncIterator() -> Iterator {
|
||||
Iterator(iterator: base.makeAsyncIterator(), kind: kind)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user