1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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 AsyncAlgorithms
extension AsyncSequenceValidationDiagram {
public struct Clock {
let queue: WorkQueue
init(queue: WorkQueue) {
self.queue = queue
}
}
}
public protocol TestClock: Sendable {
associatedtype Instant: TestInstant
var now: Instant { get }
func sleep(until deadline: Self.Instant, tolerance: Self.Instant.Duration?) async throws
}
public protocol TestInstant: Equatable {
associatedtype Duration
}
extension AsyncSequenceValidationDiagram.Clock {
public struct Step: DurationProtocol, Hashable, CustomStringConvertible {
internal var rawValue: Int
internal init(_ rawValue: Int) {
self.rawValue = rawValue
}
public static func + (lhs: Step, rhs: Step) -> Step {
return .init(lhs.rawValue + rhs.rawValue)
}
public static func - (lhs: Step, rhs: Step) -> Step {
.init(lhs.rawValue - rhs.rawValue)
}
public static func / (lhs: Step, rhs: Int) -> Step {
.init(lhs.rawValue / rhs)
}
public static func * (lhs: Step, rhs: Int) -> Step {
.init(lhs.rawValue * rhs)
}
public static func / (lhs: Step, rhs: Step) -> Double {
Double(lhs.rawValue) / Double(rhs.rawValue)
}
public static func < (lhs: Step, rhs: Step) -> Bool {
lhs.rawValue < rhs.rawValue
}
public static var zero: Step { .init(0) }
public static func steps(_ amount: Int) -> Step {
return Step(amount)
}
public var description: String {
return "step \(rawValue)"
}
}
public struct Instant: CustomStringConvertible {
public typealias Duration = Step
let when: Step
public func advanced(by duration: Step) -> Instant {
Instant(when: when + duration)
}
public func duration(to other: Instant) -> Step {
other.when - when
}
public static func < (lhs: Instant, rhs: Instant) -> Bool {
lhs.when < rhs.when
}
public var description: String {
// the raw value is 1 indexed in execution but we should report it as 0 indexed
return "tick \(when.rawValue - 1)"
}
}
public var now: Instant {
queue.now
}
public var minimumResolution: Step {
.steps(1)
}
public func sleep(
until deadline: Instant,
tolerance: Step? = nil
) async throws {
let token = queue.prepare()
try await withTaskCancellationHandler {
try await withUnsafeThrowingContinuation { continuation in
queue.enqueue(AsyncSequenceValidationDiagram.Context.currentJob, deadline: deadline, continuation: continuation, token: token)
}
} onCancel: {
queue.cancel(token)
}
}
}
extension AsyncSequenceValidationDiagram.Clock.Instant: TestInstant { }
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequenceValidationDiagram.Clock.Instant: InstantProtocol { }
extension AsyncSequenceValidationDiagram.Clock: TestClock { }
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequenceValidationDiagram.Clock: Clock { }
// placeholders to avoid warnings
extension AsyncSequenceValidationDiagram.Clock.Instant: Hashable { }
extension AsyncSequenceValidationDiagram.Clock.Instant: Comparable { }
|