forked from apple/swift-async-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestDeferred.swift
114 lines (107 loc) · 3.32 KB
/
TestDeferred.swift
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
@preconcurrency import XCTest
import AsyncAlgorithms
final class TestDeferred: XCTestCase {
func test_deferred() async {
let expected = [0,1,2,3,4]
let sequence = deferred {
return expected.async
}
var iterator = sequence.makeAsyncIterator()
var actual = [Int]()
while let item = await iterator.next() {
actual.append(item)
}
XCTAssertEqual(expected, actual)
let pastEnd = await iterator.next()
XCTAssertNil(pastEnd)
}
func test_deferred_remains_idle_pending_consumer() async {
let expectation = expectation(description: "pending")
expectation.isInverted = true
let _ = deferred {
AsyncStream { continuation in
expectation.fulfill()
continuation.yield(0)
continuation.finish()
}
}
wait(for: [expectation], timeout: 1.0)
}
func test_deferred_generates_new_sequence_per_consumer() async {
let expectation = expectation(description: "started")
expectation.expectedFulfillmentCount = 3
let sequence = deferred {
AsyncStream { continuation in
expectation.fulfill()
continuation.yield(0)
continuation.finish()
}
}
for await _ in sequence { }
for await _ in sequence { }
for await _ in sequence { }
wait(for: [expectation], timeout: 1.0)
}
func test_deferred_throws() async {
let expectation = expectation(description: "throws")
let sequence = deferred {
AsyncThrowingStream<Int, Error> { continuation in
continuation.finish(throwing: Failure())
}
}
var iterator = sequence.makeAsyncIterator()
do {
while let _ = try await iterator.next() { }
}
catch {
expectation.fulfill()
}
wait(for: [expectation], timeout: 1.0)
let pastEnd = try! await iterator.next()
XCTAssertNil(pastEnd)
}
func test_deferred_autoclosure() async {
let expected = [0,1,2,3,4]
let sequence = deferred(expected.async)
var iterator = sequence.makeAsyncIterator()
var actual = [Int]()
while let item = await iterator.next() {
actual.append(item)
}
XCTAssertEqual(expected, actual)
let pastEnd = await iterator.next()
XCTAssertNil(pastEnd)
}
func test_deferred_cancellation() async {
let source = Indefinite(value: 0)
let sequence = deferred(source.async)
let finished = expectation(description: "finished")
let iterated = expectation(description: "iterated")
let task = Task {
var firstIteration = false
for await _ in sequence {
if !firstIteration {
firstIteration = true
iterated.fulfill()
}
}
finished.fulfill()
}
// ensure the other task actually starts
wait(for: [iterated], timeout: 1.0)
// cancellation should ensure the loop finishes
// without regards to the remaining underlying sequence
task.cancel()
wait(for: [finished], timeout: 1.0)
}
}