Files
swift-async-algorithms/Tests/AsyncAlgorithmsTests/TestTimer.swift
T
Philippe Hausler 5bfab6f07a 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
2022-02-18 16:44:07 -08:00

48 lines
1.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// 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|]"
}
}
}