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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
//===----------------------------------------------------------------------===//
//
// 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
final class TestThrowingChannel: XCTestCase {
func test_asyncThrowingChannel_delivers_elements_when_several_producers_and_several_consumers() async throws {
let sents = (1...10)
let expected = Set(sents)
// Given: an AsyncThrowingChannel
let sut = AsyncThrowingChannel<Int, Error>()
// When: sending elements from tasks in a group
Task {
await withTaskGroup(of: Void.self) { group in
for sent in sents {
group.addTask {
await sut.send(sent)
}
}
}
}
// When: receiving those elements from tasks in a group
let collected = try await withThrowingTaskGroup(of: Int.self, returning: Set<Int>.self) { group in
for _ in sents {
group.addTask {
var iterator = sut.makeAsyncIterator()
let received = try await iterator.next()
return received!
}
}
var collected = Set<Int>()
for try await element in group {
collected.update(with: element)
}
return collected
}
// Then: all elements are sent and received
XCTAssertEqual(collected, expected)
}
func test_asyncThrowingChannel_resumes_producers_and_discards_additional_elements_when_finish_is_called() async throws {
// Given: an AsyncThrowingChannel
let sut = AsyncThrowingChannel<Int, Error>()
// Given: 2 suspended send operations
let task1 = Task {
await sut.send(1)
}
let task2 = Task {
await sut.send(2)
}
// When: finishing the channel
sut.finish()
// Then: the send operations are resumed
_ = await (task1.value, task2.value)
// When: sending an extra value
await sut.send(3)
// Then: the operation and the iteration are immediately resumed
var collected = [Int]()
for try await element in sut {
collected.append(element)
}
XCTAssertTrue(collected.isEmpty)
}
func test_asyncThrowingChannel_resumes_producers_and_discards_additional_elements_when_fail_is_called() async throws {
// Given: an AsyncThrowingChannel
let sut = AsyncThrowingChannel<Int, Error>()
// Given: 2 suspended send operations
let task1 = Task {
await sut.send(1)
}
let task2 = Task {
await sut.send(2)
}
// When: failing the channel
sut.fail(Failure())
// Then: the send operations are resumed
_ = await (task1.value, task2.value)
// When: sending an extra value
await sut.send(3)
// Then: the send operation is resumed
// Then: the iteration is resumed with a failure
var collected = [Int]()
do {
for try await element in sut {
collected.append(element)
}
} catch {
XCTAssertTrue(collected.isEmpty)
XCTAssertEqual(error as? Failure, Failure())
}
// When: requesting a next value
var iterator = sut.makeAsyncIterator()
let pastFailure = try await iterator.next()
// Then: the past failure is nil
XCTAssertNil(pastFailure)
}
func test_asyncThrowingChannel_resumes_consumers_when_finish_is_called() async throws {
// Given: an AsyncThrowingChannel
let sut = AsyncThrowingChannel<Int, Error>()
// Given: 2 suspended iterations
let task1 = Task<Int?, Error> {
var iterator = sut.makeAsyncIterator()
return try await iterator.next()
}
let task2 = Task<Int?, Error> {
var iterator = sut.makeAsyncIterator()
return try await iterator.next()
}
// When: finishing the channel
sut.finish()
// Then: the iterations are resumed with nil values
let (collected1, collected2) = try await (task1.value, task2.value)
XCTAssertNil(collected1)
XCTAssertNil(collected2)
// When: requesting a next value
var iterator = sut.makeAsyncIterator()
let pastEnd = try await iterator.next()
// Then: the past end is nil
XCTAssertNil(pastEnd)
}
func test_asyncThrowingChannel_resumes_consumer_when_fail_is_called() async throws {
// Given: an AsyncThrowingChannel
let sut = AsyncThrowingChannel<Int, Error>()
// Given: suspended iteration
let task = Task<Int?, Error> {
var iterator = sut.makeAsyncIterator()
do {
_ = try await iterator.next()
XCTFail("We expect the above call to throw")
} catch {
XCTAssertEqual(error as? Failure, Failure())
}
return try await iterator.next()
}
// When: failing the channel
sut.fail(Failure())
// Then: the iterations are resumed with the error and the next element is nil
do {
let collected = try await task.value
XCTAssertNil(collected)
} catch {
XCTFail("The task should not fail, the past failure element should be nil, not a failure.")
}
}
func test_asyncThrowingChannel_resumes_consumers_when_fail_is_called() async throws {
// Given: an AsyncThrowingChannel
let sut = AsyncThrowingChannel<Int, Error>()
// Given: 2 suspended iterations
let task1 = Task<Int?, Error> {
var iterator = sut.makeAsyncIterator()
return try await iterator.next()
}
let task2 = Task<Int?, Error> {
var iterator = sut.makeAsyncIterator()
return try await iterator.next()
}
// When: failing the channel
sut.fail(Failure())
// Then: the iterations are resumed with the error
do {
_ = try await task1.value
} catch {
XCTAssertEqual(error as? Failure, Failure())
}
do {
_ = try await task2.value
} catch {
XCTAssertEqual(error as? Failure, Failure())
}
// When: requesting a next value
var iterator = sut.makeAsyncIterator()
let pastFailure = try await iterator.next()
// Then: the past failure is nil
XCTAssertNil(pastFailure)
}
func test_asyncThrowingChannel_resumes_consumer_with_error_when_already_failed() async throws {
// Given: an AsyncThrowingChannel that is failed
let sut = AsyncThrowingChannel<Int, Error>()
sut.fail(Failure())
var iterator = sut.makeAsyncIterator()
// When: requesting the next element
do {
_ = try await iterator.next()
} catch {
// Then: the iteration is resumed with the error
XCTAssertEqual(error as? Failure, Failure())
}
// When: requesting the next element past failure
do {
let pastFailure = try await iterator.next()
// Then: the past failure is nil
XCTAssertNil(pastFailure)
} catch {
XCTFail("The past failure should not throw")
}
}
func test_asyncThrowingChannel_resumes_producer_when_task_is_cancelled() async throws {
let send1IsResumed = expectation(description: "The first send operation is resumed")
// Given: an AsyncThrowingChannel
let sut = AsyncThrowingChannel<Int, Error>()
// Given: 2 suspended send operations
let task1 = Task {
await sut.send(1)
send1IsResumed.fulfill()
}
let task2 = Task {
await sut.send(2)
}
// When: cancelling the first task
task1.cancel()
// Then: the first sending operation is resumed
await fulfillment(of: [send1IsResumed], timeout: 1.0)
// When: collecting elements
var iterator = sut.makeAsyncIterator()
let collected = try await iterator.next()
// Then: the second operation resumes and the iteration receives the element
_ = await task2.value
XCTAssertEqual(collected, 2)
}
func test_asyncThrowingChannel_resumes_consumer_when_task_is_cancelled() async throws {
// Given: an AsyncThrowingChannel
let sut = AsyncThrowingChannel<Int, Error>()
// Given: 2 suspended iterations
let task1 = Task<Int?, Error> {
var iterator = sut.makeAsyncIterator()
return try await iterator.next()
}
let task2 = Task<Int?, Error> {
var iterator = sut.makeAsyncIterator()
return try await iterator.next()
}
// When: cancelling the first task
task1.cancel()
// Then: the first iteration is resumed with a nil element
let collected1 = try await task1.value
XCTAssertNil(collected1)
// When: sending an element
await sut.send(1)
// Then: the second iteration is resumed with the element
let collected2 = try await task2.value
XCTAssertEqual(collected2, 1)
}
}
|