mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-25 04:27:48 +00:00
Add a timer based async sequence (#54)
* Support cancellation for marble diagrams * Add a timer based async sequence * Test skewing events by sleeps inside the map * A less confusing skew cancellation tick
This commit is contained in:
1 parent
17bca92f90
commit
5bfab6f07a
7 files changed
+157
No files matched your search
@@ -0,0 +1,71 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 AsyncTimerSequence<C: Clock>: AsyncSequence {
|
||||
public typealias Element = C.Instant
|
||||
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
var clock: C?
|
||||
let interval: C.Instant.Duration
|
||||
let tolerance: C.Instant.Duration?
|
||||
var last: C.Instant?
|
||||
|
||||
init(interval: C.Instant.Duration, tolerance: C.Instant.Duration?, clock: C) {
|
||||
self.clock = clock
|
||||
self.interval = interval
|
||||
self.tolerance = tolerance
|
||||
}
|
||||
|
||||
func nextDeadline(_ clock: C) -> C.Instant {
|
||||
let now = clock.now
|
||||
let last = self.last ?? now
|
||||
let next = last.advanced(by: interval)
|
||||
if next < now {
|
||||
return last.advanced(by: interval * Int(((next.duration(to: now)) / interval).rounded(.up)))
|
||||
} else {
|
||||
return next
|
||||
}
|
||||
}
|
||||
|
||||
public mutating func next() async -> C.Instant? {
|
||||
guard let clock = clock else {
|
||||
return nil
|
||||
}
|
||||
let next = nextDeadline(clock)
|
||||
do {
|
||||
try await clock.sleep(until: next, tolerance: tolerance)
|
||||
} catch {
|
||||
self.clock = nil
|
||||
return nil
|
||||
}
|
||||
let now = clock.now
|
||||
last = next
|
||||
return now
|
||||
}
|
||||
}
|
||||
|
||||
let clock: C
|
||||
let interval: C.Instant.Duration
|
||||
let tolerance: C.Instant.Duration?
|
||||
|
||||
public init(interval: C.Instant.Duration, tolerance: C.Instant.Duration? = nil, clock: C) {
|
||||
self.clock = clock
|
||||
self.interval = interval
|
||||
self.tolerance = tolerance
|
||||
}
|
||||
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
Iterator(interval: interval, tolerance: tolerance, clock: clock)
|
||||
}
|
||||
}
|
||||
|
||||
extension AsyncTimerSequence: Sendable { }
|
||||
extension AsyncTimerSequence.Iterator: Sendable { }
|
||||
@@ -33,12 +33,14 @@ extension MarbleDiagram {
|
||||
case value(String, String.Index)
|
||||
case failure(Error, String.Index)
|
||||
case finish(String.Index)
|
||||
case cancel(String.Index)
|
||||
|
||||
var results: [Result<String?, Error>] {
|
||||
switch self {
|
||||
case .value(let value, _): return [.success(value)]
|
||||
case .failure(let failure, _): return [.failure(failure)]
|
||||
case .finish: return [.success(nil)]
|
||||
case .cancel: return []
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +49,7 @@ extension MarbleDiagram {
|
||||
case .value(_, let index): return index
|
||||
case .failure(_, let index): return index
|
||||
case .finish(let index): return index
|
||||
case .cancel(let index): return index
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,6 +90,15 @@ extension MarbleDiagram {
|
||||
} else {
|
||||
string?.append(ch)
|
||||
}
|
||||
case .cancel:
|
||||
if string == nil {
|
||||
if grouping == 0 {
|
||||
when = when.advanced(by: .steps(1))
|
||||
}
|
||||
emissions.append((when, .cancel(index)))
|
||||
} else {
|
||||
string?.append(ch)
|
||||
}
|
||||
case .beginValue:
|
||||
string = ""
|
||||
case .endValue:
|
||||
|
||||
@@ -13,6 +13,13 @@ import CMarableDiagram
|
||||
|
||||
@resultBuilder
|
||||
public struct MarbleDiagram : Sendable {
|
||||
public static func buildBlock<Operation: AsyncSequence>(
|
||||
_ sequence: Operation,
|
||||
_ output: String
|
||||
) -> some MarbleDiagramTest where Operation.Element == String {
|
||||
return Test(inputs: [], sequence: sequence, output: output)
|
||||
}
|
||||
|
||||
public static func buildBlock<Operation: AsyncSequence>(
|
||||
_ input: String,
|
||||
_ sequence: Operation,
|
||||
|
||||
@@ -217,6 +217,13 @@ extension MarbleDiagram {
|
||||
}
|
||||
|
||||
let parsedOutput = try Event.parse(test.output, theme: theme)
|
||||
let cancelEvents = Set(parsedOutput.filter { when, event in
|
||||
switch event {
|
||||
case .cancel: return true
|
||||
default: return false
|
||||
}
|
||||
}.map { when, _ in return when })
|
||||
|
||||
var expected = [(Clock.Instant, Result<String?, Error>)]()
|
||||
for (when, event) in parsedOutput {
|
||||
for result in event.results {
|
||||
@@ -262,6 +269,9 @@ extension MarbleDiagram {
|
||||
// Next make sure to iterate a decent amount past the end of the maximum
|
||||
// scheduled things (that way we ensure any reasonable errors are caught)
|
||||
for _ in 0..<(end.when.rawValue * 2) {
|
||||
if cancelEvents.contains(diagram.queue.now.advanced(by: .steps(1))) {
|
||||
runner.cancel()
|
||||
}
|
||||
diagram.queue.advance()
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ extension MarbleDiagram {
|
||||
case step
|
||||
case error
|
||||
case finish
|
||||
case cancel
|
||||
case beginValue
|
||||
case endValue
|
||||
case beginGroup
|
||||
@@ -38,6 +39,7 @@ extension MarbleDiagram {
|
||||
case "-": return .step
|
||||
case "^": return .error
|
||||
case "|": return .finish
|
||||
case ";": return .cancel
|
||||
case "'": return inValue ? .endValue : .beginValue
|
||||
case "[": return .beginGroup
|
||||
case "]": return .endGroup
|
||||
|
||||
@@ -92,6 +92,14 @@ final class TestMarbleDiagram: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_cancel_event() {
|
||||
marbleDiagram {
|
||||
"a--b- - c--|"
|
||||
$0.inputs[0]
|
||||
"a--b-[;|]"
|
||||
}
|
||||
}
|
||||
|
||||
func test_diagram_failure_mismatch_value() {
|
||||
expectFailures(["expected \"X\" but got \"C\" at tick 6"])
|
||||
marbleDiagram {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
import MarbleDiagram
|
||||
|
||||
final class TestTimer: XCTestCase {
|
||||
func test_tick1() {
|
||||
marbleDiagram {
|
||||
AsyncTimerSequence(interval: .steps(1), clock: $0.clock).map { _ in "x" }
|
||||
"xxxxxxx[;|]"
|
||||
}
|
||||
}
|
||||
|
||||
func test_tick2() {
|
||||
marbleDiagram {
|
||||
AsyncTimerSequence(interval: .steps(2), clock: $0.clock).map { _ in "x" }
|
||||
"-x-x-x-[;|]"
|
||||
}
|
||||
}
|
||||
|
||||
func test_tick3() {
|
||||
marbleDiagram {
|
||||
AsyncTimerSequence(interval: .steps(3), clock: $0.clock).map { _ in "x" }
|
||||
"--x--x-[;|]"
|
||||
}
|
||||
}
|
||||
|
||||
func test_tick2_event_skew3() {
|
||||
marbleDiagram { diagram in
|
||||
AsyncTimerSequence(interval: .steps(2), clock: diagram.clock).map { [diagram] (_) -> String in
|
||||
try? await diagram.clock.sleep(until: diagram.clock.now.advanced(by: .steps(3)))
|
||||
return "x"
|
||||
}
|
||||
"----x--x-[;x|]"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user