Compare commits

..
3 Commits
Author SHA1 Message Date
Philippe Hausler cca423ff03 Remove the disable of availability and mark methods as available to clock/instant/duration (#165)
* Remove the disable of availability and mark methods as available to clock/instant/duration

* Rework availability to be more permissive for tests

* Use hand coded values instead of defines (only available via Darwin versions of Dispatch)

* Adjust additional test availability

* Correct debounce test availability guard

* Update build instructions to indicate Xcode requirements

* Ensure the clock property of the validation diagram is only available for targets that can support it
2022-06-16 13:06:20 -07:00
Thibault Wittemberg 9a3e94d440 compacted: rename unit tests (#166) 2022-06-15 23:28:45 -07:00
Philippe Hausler 07b8d74cc7 [SAA-0002] Merge (#156) 2022-06-13 11:04:03 -07:00
20 changed files with 241 additions and 92 deletions

No files matched your search

+103
View File
@@ -0,0 +1,103 @@
# Merge
* Proposal: [SAA-0002](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0002-merge.md)
* Authors: [Philippe Hausler](https://github.com/phausler)
* Status: **Implemented**
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Asyncmerge2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncMerge3Sequence.swift) |
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestMerge.swift)]
* Decision Notes:
* Bugs:
## Introduction
In the category of combinations of asynchronous sequences there are a few different potential behaviors. This category all take two or more `AsyncSequence` types and produce one `AsyncSequence`. One fundamental behavior is taking all values produced by the inputs and resuming the iteration of the singular downstream `AsyncSequence` with those values. This shape is called merge.
## Detailed Design
Merge takes two or more asynchronous sequences sharing the same element type and combines them into one singular asynchronous sequence of those elements.
```swift
let appleFeed = URL(string: "http://www.example.com/ticker?symbol=AAPL")!.lines.map { "AAPL: " + $0 }
let nasdaqFeed = URL(string:"http://www.example.com/ticker?symbol=^IXIC")!.lines.map { "^IXIC: " + $0 }
for try await ticker in merge(appleFeed, nasdaqFeed) {
print(ticker)
}
```
Given some sample inputs the following merged events can be expected.
| Timestamp | appleFeed | nasdaqFeed | merged output |
| ----------- | --------- | ---------- | --------------- |
| 11:40 AM | 173.91 | | AAPL: 173.91 |
| 12:25 AM | | 14236.78 | ^IXIC: 14236.78 |
| 12:40 AM | | 14218.34 | ^IXIC: 14218.34 |
| 1:15 PM | 173.00 | | AAPL: 173.00 |
This function family and the associated family of return types are prime candidates for variadic generics. Until that proposal is accepted, these will be implemented in terms of two- and three-base sequence cases.
```swift
public func merge<Base1: AsyncSequence, Base2: AsyncSequence>(_ base1: Base1, _ base2: Base2) -> AsyncMerge2Sequence<Base1, Base2>
public func merge<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>(_ base1: Base1, _ base2: Base2, _ base3: Base3) -> AsyncMerge3Sequence<Base1, Base2, Base3>
public struct AsyncMerge2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Sendable
where
Base1.Element == Base2.Element,
Base1: Sendable, Base2: Sendable,
Base1.Element: Sendable, Base2.Element: Sendable,
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable {
public typealias Element = Base1.Element
public struct Iterator: AsyncIteratorProtocol {
public mutating func next() async rethrows -> Element?
}
public func makeAsyncIterator() -> Iterator
}
public struct AsyncMerge3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: Sendable
where
Base1.Element == Base2.Element, Base1.Element == Base3.Element,
Base1: Sendable, Base2: Sendable, Base3: Sendable
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable, Base3.AsyncIterator: Sendable {
public typealias Element = Base1.Element
public struct Iterator: AsyncIteratorProtocol {
public mutating func next() async rethrows -> Element?
}
public func makeAsyncIterator() -> Iterator
}
```
The `merge(_:...)` function takes two or more asynchronous sequences as arguments and produces an `AsyncMergeSequence` which is an asynchronous sequence.
Since the bases comprising the `AsyncMergeSequence` must be iterated concurrently to produce the latest value, those sequences must be able to be sent to child tasks. This means that a prerequisite of the bases must be that the base asynchronous sequences, their iterators, and the elements they produce must be `Sendable`.
When iterating a `AsyncMergeSequence`, the sequence terminates when all of the base asynchronous sequences terminate, since this means there is no potential for any further elements to be produced.
The throwing behavior of `AsyncMergeSequence` is that if any of the bases throw, then the composed asynchronous sequence throws on its iteration. If at any point an error is thrown by any base, the other iterations are cancelled and the thrown error is immediately thrown to the consuming iteration.
### Naming
Since the inherent behavior of `merge(_:...)` merges values from multiple streams into a singular asynchronous sequence, the naming is intended to be quite literal. There are precedent terms of art in other frameworks and libraries (listed in the comparison section). Other naming takes the form of "withLatestFrom". This was disregarded since the "with" prefix is often most associated with the passing of a closure and some sort of contextual concept; `withUnsafePointer` or `withUnsafeContinuation` are prime examples.
### Comparison with other libraries
**ReactiveX** ReactiveX has an [API definition of Merge](https://reactivex.io/documentation/operators/merge.html) as a top level function for merging Observables.
**Combine** Combine has an [API definition of merge(with:)](https://developer.apple.com/documentation/combine/publisher/merge(with:)-7qt71/) as an operator style method for merging Publishers.
## Effect on API resilience
### `@frozen` and `@inlinable`
These types utilize rethrowing mechanisms that are awaiting an implementation in the compiler for supporting implementation based rethrows. So none of them are marked as frozen or marked as inlinable. This feature (discussed as `rethrows(unsafe)` or `rethrows(SourceOfRethrowyness)` has not yet been reviewed or implemented. The current implementation takes liberties with an internal protocol to accomplish this task. Future revisions will remove that protocol trick to replace it with proper rethrows semantics at the actual call site. The types are expected to be stable boundaries to prevent that workaround for the compilers yet to be supported rethrowing (or TaskGroup rethrowing) mechanisms. As soon as that feature is resolved; a more detailed investigation on performance impact of inlining and frozen should be done before 1.0.
## Alternatives considered
It was considered to have merge be shaped as an extension method on `AsyncSequence` however that infers a "primary-ness" of one `AsyncSequence` over another. Since the behavior of this as a global function (which infers no preference to one side or another) it was decided that having symmetry between the asynchronous version and the synchronous version inferred the right connotations.
+2 -12
View File
@@ -25,20 +25,10 @@ let package = Package(
.systemLibrary(name: "_CAsyncSequenceValidationSupport"),
.target(
name: "AsyncAlgorithms_XCTest",
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation"],
swiftSettings: [
.unsafeFlags([
"-Xfrontend", "-disable-availability-checking"
])
]),
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation"]),
.testTarget(
name: "AsyncAlgorithmsTests",
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation", "AsyncAlgorithms_XCTest"],
swiftSettings: [
.unsafeFlags([
"-Xfrontend", "-disable-availability-checking"
])
]),
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation", "AsyncAlgorithms_XCTest"]),
]
)
+2 -13
View File
@@ -87,28 +87,17 @@ Finally, add `import AsyncAlgorithms` to your source code.
## Getting Started
⚠️ Please note that this package currently requires a recent [Swift Trunk Development toolchain](https://www.swift.org/download/#trunk-development-main). More information on how to use custom toolchains with Xcode can be viewed [here](https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/AlternativeToolchains.html).
⚠️ Please note that this package requires Xcode 14 on macOS hosts. Previous versions of Xcode do not contain the required Swift version.
### Building/Testing Using Xcode on macOS
1. Download the most recent development Xcode toolchain.
2. Install the package
4. Select the development toolchain in Xcode
4. Open the `swift-async-algorithms` package directory in Xcode
5. Build or Test in Xcode as normal
⚠️ Note: `swift test` does not currently work properly with custom toolchains for this package.
1. In the `swift-async-algorithms` directory run `swift build` or `swift test` accordingly
### Building/Testing on Linux
1. Download the most recent development toolchain for your Linux distribution
2. Decompress the archive to a path in which the `swift` executable is in the binary search path environment variable (`$PATH`)
3. In the `swift-async-algorithms` directory run `swift build` or `swift test` accordingly
### Building with Swift 5.6
1. `git checkout swift-5.6`
2. run `swift build` or `swift test` accordingly
## Source Stability
@@ -11,7 +11,6 @@
import _CAsyncSequenceValidationSupport
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
@resultBuilder
public struct AsyncSequenceValidationDiagram : Sendable {
public struct Component<T> {
@@ -97,15 +96,20 @@ public struct AsyncSequenceValidationDiagram : Sendable {
}
let queue: WorkQueue
let _clock: Clock
public var inputs: InputList
public let clock: Clock
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public var clock: Clock {
_clock
}
internal init() {
let queue = WorkQueue()
self.queue = queue
self.inputs = InputList(queue: queue)
self.clock = Clock(queue: queue)
self._clock = Clock(queue: queue)
}
}
+25 -4
View File
@@ -11,7 +11,6 @@
import AsyncAlgorithms
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequenceValidationDiagram {
public struct Clock {
let queue: WorkQueue
@@ -22,8 +21,20 @@ extension AsyncSequenceValidationDiagram {
}
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequenceValidationDiagram.Clock: Clock {
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
@@ -66,7 +77,7 @@ extension AsyncSequenceValidationDiagram.Clock: Clock {
}
}
public struct Instant: InstantProtocol, CustomStringConvertible {
public struct Instant: CustomStringConvertible {
public typealias Duration = Step
let when: Step
@@ -111,3 +122,13 @@ extension AsyncSequenceValidationDiagram.Clock: Clock {
}
}
}
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 { }
@@ -9,7 +9,6 @@
//
//===----------------------------------------------------------------------===//
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequenceValidationDiagram {
struct Failure: Error, Equatable { }
@@ -9,7 +9,6 @@
//
//===----------------------------------------------------------------------===//
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequenceValidationDiagram {
public struct ExpectationResult: Sendable {
public struct Event: Sendable {
@@ -9,7 +9,6 @@
//
//===----------------------------------------------------------------------===//
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequenceValidationDiagram {
public struct Specification: Sendable {
public let specification: String
@@ -11,7 +11,6 @@
import _CAsyncSequenceValidationSupport
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
struct Job: Hashable, @unchecked Sendable {
let job: JobRef
@@ -20,7 +20,6 @@ import _CAsyncSequenceValidationSupport
#endif
#if canImport(Darwin)
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
return nil
@@ -34,7 +33,6 @@ func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
#error("TODO: Port TaskDriver threading to windows")
#endif
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
final class TaskDriver {
let work: (TaskDriver) -> Void
let queue: WorkQueue
+3 -5
View File
@@ -19,22 +19,20 @@ internal func _swiftJobRun(
_ executor: UnownedSerialExecutor
) -> ()
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public protocol AsyncSequenceValidationTest: Sendable {
var inputs: [AsyncSequenceValidationDiagram.Specification] { get }
var output: AsyncSequenceValidationDiagram.Specification { get }
func test<C: Clock>(with clock: C, activeTicks: [C.Instant], output: AsyncSequenceValidationDiagram.Specification, _ event: (String) -> Void) async throws
func test<C: TestClock>(with clock: C, activeTicks: [C.Instant], output: AsyncSequenceValidationDiagram.Specification, _ event: (String) -> Void) async throws
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequenceValidationDiagram {
struct Test<Operation: AsyncSequence>: AsyncSequenceValidationTest, @unchecked Sendable where Operation.Element == String {
let inputs: [Specification]
let sequence: Operation
let output: Specification
func test<C: _Concurrency.Clock>(with clock: C, activeTicks: [C.Instant], output: Specification, _ event: (String) -> Void) async throws {
func test<C: TestClock>(with clock: C, activeTicks: [C.Instant], output: Specification, _ event: (String) -> Void) async throws {
var iterator = sequence.makeAsyncIterator()
do {
for tick in activeTicks {
@@ -265,7 +263,7 @@ extension AsyncSequenceValidationDiagram {
@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test
) throws -> (ExpectationResult, [ExpectationFailure]) {
let diagram = AsyncSequenceValidationDiagram()
let clock = diagram.clock
let clock = diagram._clock
let test = build(diagram)
for index in 0..<test.inputs.count {
// fault in all inputs
@@ -9,21 +9,18 @@
//
//===----------------------------------------------------------------------===//
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public protocol AsyncSequenceValidationTheme {
func token(_ character: Character, inValue: Bool) -> AsyncSequenceValidationDiagram.Token
func description(for token: AsyncSequenceValidationDiagram.Token) -> String
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequenceValidationTheme where Self == AsyncSequenceValidationDiagram.ASCIITheme {
public static var ascii: AsyncSequenceValidationDiagram.ASCIITheme {
return AsyncSequenceValidationDiagram.ASCIITheme()
}
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequenceValidationDiagram {
public enum Token: Sendable {
case step
@@ -9,7 +9,6 @@
//
//===----------------------------------------------------------------------===//
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
struct WorkQueue: Sendable {
enum Item: CustomStringConvertible, Comparable {
case blocked(Token, AsyncSequenceValidationDiagram.Clock.Instant, UnsafeContinuation<Void, Error>)
+20 -11
View File
@@ -22,7 +22,6 @@ func concatCharacters(_ array: [String]) -> String {
}
final class TestChunk: XCTestCase {
func test_signal_equalChunks() {
validate {
"ABC- DEF- GHI- |"
@@ -113,7 +112,8 @@ final class TestChunk: XCTestCase {
}
}
func test_time_equalChunks() {
func test_time_equalChunks() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"ABC- DEF- GHI- |"
$0.inputs[0].chunked(by: .repeating(every: .steps(4), clock: $0.clock)).map(concatCharacters)
@@ -121,7 +121,8 @@ final class TestChunk: XCTestCase {
}
}
func test_time_unequalChunks() {
func test_time_unequalChunks() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"AB------ A------- ABCDEFG- |"
$0.inputs[0].chunked(by: .repeating(every: .steps(8), clock: $0.clock)).map(concatCharacters)
@@ -129,7 +130,8 @@ final class TestChunk: XCTestCase {
}
}
func test_time_emptyChunks() {
func test_time_emptyChunks() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"-- 1- --|"
$0.inputs[0].chunked(by: .repeating(every: .steps(2), clock: $0.clock)).map(concatCharacters)
@@ -137,7 +139,8 @@ final class TestChunk: XCTestCase {
}
}
func test_time_error() {
func test_time_error() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"AB^"
$0.inputs[0].chunked(by: .repeating(every: .steps(5), clock: $0.clock)).map(concatCharacters)
@@ -145,7 +148,8 @@ final class TestChunk: XCTestCase {
}
}
func test_time_unsignaledTrailingChunk() {
func test_time_unsignaledTrailingChunk() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"111-111|"
$0.inputs[0].chunked(by: .repeating(every: .steps(4), clock: $0.clock)).map(sumCharacters)
@@ -153,7 +157,8 @@ final class TestChunk: XCTestCase {
}
}
func test_timeAndCount_timeAlwaysPrevails() {
func test_timeAndCount_timeAlwaysPrevails() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"AB------ A------- ABCDEFG- |"
$0.inputs[0].chunks(ofCount: 42, or: .repeating(every: .steps(8), clock: $0.clock)).map(concatCharacters)
@@ -161,7 +166,8 @@ final class TestChunk: XCTestCase {
}
}
func test_timeAndCount_countAlwaysPrevails() {
func test_timeAndCount_countAlwaysPrevails() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"AB --A-B -|"
$0.inputs[0].chunks(ofCount: 2, or: .repeating(every: .steps(8), clock: $0.clock)).map(concatCharacters)
@@ -169,7 +175,8 @@ final class TestChunk: XCTestCase {
}
}
func test_timeAndCount_countResetsAfterCount() {
func test_timeAndCount_countResetsAfterCount() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"ABCDE --- ABCDE |"
$0.inputs[0].chunks(ofCount: 5, or: .repeating(every: .steps(8), clock: $0.clock)).map(concatCharacters)
@@ -177,7 +184,8 @@ final class TestChunk: XCTestCase {
}
}
func test_timeAndCount_countResetsAfterSignal() {
func test_timeAndCount_countResetsAfterSignal() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"AB------ ABCDE |"
$0.inputs[0].chunks(ofCount: 5, or: .repeating(every: .steps(8), clock: $0.clock)).map(concatCharacters)
@@ -185,7 +193,8 @@ final class TestChunk: XCTestCase {
}
}
func test_timeAndCount_error() {
func test_timeAndCount_error() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"ABC^"
$0.inputs[0].chunks(ofCount: 5, or: .repeating(every: .steps(8), clock: $0.clock)).map(concatCharacters)
@@ -13,7 +13,7 @@
import AsyncAlgorithms
final class TestCompacted: XCTestCase {
func test_compacting() async {
func test_compacted_is_equivalent_to_compactMap_when_input_as_nil_elements() async {
let source = [1, 2, nil, 3, 4, nil, 5]
let expected = source.compactMap { $0 }
let sequence = source.async.compacted()
@@ -24,7 +24,7 @@ final class TestCompacted: XCTestCase {
XCTAssertEqual(expected, actual)
}
func test_compacting_past_end() async {
func test_compacted_produces_nil_next_element_when_iteration_is_finished() async {
let source = [1, 2, nil, 3, 4, nil, 5]
let expected = source.compactMap { $0 }
let sequence = source.async.compacted()
@@ -38,7 +38,7 @@ final class TestCompacted: XCTestCase {
XCTAssertNil(pastEnd)
}
func test_compactingNonNils() async {
func test_compacted_is_equivalent_to_compactMap_when_input_as_no_nil_elements() async {
let source: [Int?] = [1, 2, 3, 4, 5]
let expected = source.compactMap { $0 }
let sequence = source.async.compacted()
@@ -49,7 +49,7 @@ final class TestCompacted: XCTestCase {
XCTAssertEqual(expected, actual)
}
func test_throwing() async throws {
func test_compacted_throws_when_root_sequence_throws() async throws {
let sequence = [1, nil, 3, 4, 5, nil, 7].async.map { try throwOn(4, $0) }.compacted()
var iterator = sequence.makeAsyncIterator()
var collected = [Int]()
@@ -66,7 +66,7 @@ final class TestCompacted: XCTestCase {
XCTAssertNil(pastEnd)
}
func test_cancellation() async {
func test_compacted_finishes_when_iteration_task_is_cancelled() async {
let value: String? = "test"
let source = Indefinite(value: value)
let sequence = source.async.compacted()
+10 -5
View File
@@ -13,7 +13,8 @@ import XCTest
import AsyncAlgorithms
final class TestDebounce: XCTestCase {
func test_delayingValues() {
func test_delayingValues() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcd----e---f-g----|"
$0.inputs[0].debounce(for: .steps(3), clock: $0.clock)
@@ -21,7 +22,8 @@ final class TestDebounce: XCTestCase {
}
}
func test_delayingValues_dangling_last() {
func test_delayingValues_dangling_last() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcd----e---f-g-|"
$0.inputs[0].debounce(for: .steps(3), clock: $0.clock)
@@ -30,7 +32,8 @@ final class TestDebounce: XCTestCase {
}
func test_finishDoesntDebounce() {
func test_finishDoesntDebounce() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"a|"
$0.inputs[0].debounce(for: .steps(3), clock: $0.clock)
@@ -38,7 +41,8 @@ final class TestDebounce: XCTestCase {
}
}
func test_throwDoesntDebounce() {
func test_throwDoesntDebounce() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"a^"
$0.inputs[0].debounce(for: .steps(3), clock: $0.clock)
@@ -46,7 +50,8 @@ final class TestDebounce: XCTestCase {
}
}
func test_noValues() {
func test_noValues() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"----|"
$0.inputs[0].debounce(for: .steps(3), clock: $0.clock)
@@ -18,7 +18,11 @@ final class TestTaskSelect: XCTestCase {
let firstValue = await Task.select(Task {
return 1
}, Task {
try! await Task.sleep(until: .now + .seconds(2), clock: .continuous)
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
try! await Task.sleep(until: .now + .seconds(2), clock: .continuous)
} else {
try! await Task.sleep(nanoseconds: 2_000_000_000)
}
return 2
}).value
XCTAssertEqual(firstValue, 1)
@@ -26,7 +30,11 @@ final class TestTaskSelect: XCTestCase {
func test_second() async {
let firstValue = await Task.select(Task {
try! await Task.sleep(until: .now + .seconds(2), clock: .continuous)
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
try! await Task.sleep(until: .now + .seconds(2), clock: .continuous)
} else {
try! await Task.sleep(nanoseconds: 2_000_000_000)
}
return 1
}, Task {
return 2
@@ -37,7 +45,11 @@ final class TestTaskSelect: XCTestCase {
func test_throwing() async {
do {
_ = try await Task.select(Task { () async throws -> Int in
try await Task.sleep(until: .now + .seconds(2), clock: .continuous)
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
try await Task.sleep(until: .now + .seconds(2), clock: .continuous)
} else {
try await Task.sleep(nanoseconds: 2_000_000_000)
}
return 1
}, Task { () async throws -> Int in
throw NSError(domain: NSCocoaErrorDomain, code: -1, userInfo: nil)
@@ -59,7 +71,11 @@ final class TestTaskSelect: XCTestCase {
firstCancelled.fulfill()
} operation: { () -> Int in
firstReady.fulfill()
try? await Task.sleep(until: .now + .seconds(2), clock: .continuous)
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
try? await Task.sleep(until: .now + .seconds(2), clock: .continuous)
} else {
try? await Task.sleep(nanoseconds: 2_000_000_000)
}
return 1
}
}, Task {
@@ -67,7 +83,11 @@ final class TestTaskSelect: XCTestCase {
secondCancelled.fulfill()
} operation: { () -> Int in
secondReady.fulfill()
try? await Task.sleep(until: .now + .seconds(2), clock: .continuous)
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
try? await Task.sleep(until: .now + .seconds(2), clock: .continuous)
} else {
try? await Task.sleep(nanoseconds: 2_000_000_000)
}
return 1
}
})
+28 -14
View File
@@ -13,7 +13,8 @@ import XCTest
import AsyncAlgorithms
final class TestThrottle: XCTestCase {
func test_rate_0() {
func test_rate_0() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(0), clock: $0.clock)
@@ -21,7 +22,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_rate_0_leading_edge() {
func test_rate_0_leading_edge() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(0), clock: $0.clock, latest: false)
@@ -29,7 +31,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_rate_1() {
func test_rate_1() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock)
@@ -37,7 +40,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_rate_1_leading_edge() {
func test_rate_1_leading_edge() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock, latest: false)
@@ -45,7 +49,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_rate_2() {
func test_rate_2() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
@@ -53,7 +58,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_rate_2_leading_edge() {
func test_rate_2_leading_edge() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
@@ -61,7 +67,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_rate_3() {
func test_rate_3() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
@@ -69,7 +76,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_rate_3_leading_edge() {
func test_rate_3_leading_edge() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: false)
@@ -77,7 +85,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_throwing() {
func test_throwing() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdef^hijk|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
@@ -85,7 +94,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_throwing_leading_edge() {
func test_throwing_leading_edge() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdef^hijk|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
@@ -93,7 +103,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_emission_2_rate_1() {
func test_emission_2_rate_1() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"-a-b-c-d-e-f-g-h-i-j-k-|"
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock)
@@ -101,7 +112,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_emission_2_rate_2() {
func test_emission_2_rate_2() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"-a-b-c-d-e-f-g-h-i-j-k-|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
@@ -109,7 +121,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_emission_3_rate_2() {
func test_emission_3_rate_2() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"--a--b--c--d--e--f--g|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
@@ -117,7 +130,8 @@ final class TestThrottle: XCTestCase {
}
}
func test_emission_2_rate_3() {
func test_emission_2_rate_3() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"-a-b-c-d-e-f-g-h-i-j-k-|"
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
+8 -4
View File
@@ -14,28 +14,32 @@ import AsyncAlgorithms
import AsyncSequenceValidation
final class TestTimer: XCTestCase {
func test_tick1() {
func test_tick1() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
AsyncTimerSequence(interval: .steps(1), clock: $0.clock).map { _ in "x" }
"xxxxxxx[;|]"
}
}
func test_tick2() {
func test_tick2() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
AsyncTimerSequence(interval: .steps(2), clock: $0.clock).map { _ in "x" }
"-x-x-x-[;|]"
}
}
func test_tick3() {
func test_tick3() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
AsyncTimerSequence(interval: .steps(3), clock: $0.clock).map { _ in "x" }
"--x--x-[;|]"
}
}
func test_tick2_event_skew3() {
func test_tick2_event_skew3() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate { 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)))
@@ -292,7 +292,8 @@ final class TestValidationDiagram: XCTestCase {
}
}
func test_delayNext_into_emptyTick() {
func test_delayNext_into_emptyTick() throws {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"xx|"
LaggingAsyncSequence($0.inputs[0], delayBy: .steps(3), using: $0.clock)
@@ -309,6 +310,7 @@ final class TestValidationDiagram: XCTestCase {
}
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
struct LaggingAsyncSequence<Base: AsyncSequence, C: Clock> : AsyncSequence {
typealias Element = Base.Element