-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathAsyncMergeSequenceTests.swift
More file actions
309 lines (235 loc) · 9.65 KB
/
AsyncMergeSequenceTests.swift
File metadata and controls
309 lines (235 loc) · 9.65 KB
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
//
// AsyncMergeSequenceTests.swift
//
//
// Created by Thibault Wittemberg on 01/01/2022.
//
import AsyncExtensions
import XCTest
private struct TimedAsyncSequence<Element>: AsyncSequence, AsyncIteratorProtocol {
typealias Element = Element
typealias AsyncIterator = TimedAsyncSequence
private let intervalInMills: [UInt64]
private var iterator: Array<Element>.Iterator
private var index = 0
private let indexOfError: Int?
init(intervalInMills: [UInt64], sequence: [Element], indexOfError: Int? = nil) {
self.intervalInMills = intervalInMills
self.iterator = sequence.makeIterator()
self.indexOfError = indexOfError
}
mutating func next() async throws -> Element? {
if let indexOfError = self.indexOfError, self.index == indexOfError {
throw MockError(code: 1)
}
if self.index < self.intervalInMills.count {
try await Task.sleep(nanoseconds: self.intervalInMills[index] * 1_000_000)
self.index += 1
}
return self.iterator.next()
}
func makeAsyncIterator() -> AsyncIterator {
self
}
}
final class AsyncMergeSequenceTests: XCTestCase {
func testMerge_merges_sequences_according_to_the_timeline_using_asyncSequences() async throws {
// -- 0 ------------------------------- 1000 ----------------------------- 2000 -
// --------------- 500 --------------------------------- 1500 -------------------
// -- a ----------- d ------------------ b --------------- e --------------- c --
//
// output should be: a, d, b, e, c
let expectedElements = ["a", "d", "b", "e", "c"]
let asyncSequence1 = TimedAsyncSequence(intervalInMills: [0, 1000, 1000], sequence: ["a", "b", "c"])
let asyncSequence2 = TimedAsyncSequence(intervalInMills: [500, 1000], sequence: ["d", "e"])
let sut = merge(asyncSequence1, asyncSequence2)
var receivedElements = [String]()
var iterator = sut.makeAsyncIterator()
while let element = try await iterator.next() {
try await Task.sleep(nanoseconds: 110_000_000)
receivedElements.append(element)
}
XCTAssertEqual(receivedElements, expectedElements)
let pastEnd = try await iterator.next()
XCTAssertNil(pastEnd)
}
func testMerge_merges_four_sequences() async {
let asyncSequence1 = [1, 2, 3, 4, 5]
let asyncSequence2 = [10, 20, 30, 40, 50]
let asyncSequence3 = [100, 200, 300, 400, 500]
let asyncSequence4 = [1000, 2000, 3000, 4000, 5000]
let expectedElements = asyncSequence1 + asyncSequence2 + asyncSequence3 + asyncSequence4
let sut = merge(asyncSequence1.async, asyncSequence2.async, asyncSequence3.async, asyncSequence4.async)
var receivedElements = [Int]()
var iterator = sut.makeAsyncIterator()
while let element = await iterator.next() {
receivedElements.append(element)
}
XCTAssertEqual(receivedElements.sorted(), expectedElements)
let pastEnd = await iterator.next()
XCTAssertNil(pastEnd)
}
func testMerge_merges_sequences_according_to_the_timeline_using_streams() {
let canSend2Expectation = expectation(description: "2 can be sent")
let canSend3Expectation = expectation(description: "3 can be sent")
let canSend4Expectation = expectation(description: "4 can be sent")
let canSend5Expectation = expectation(description: "5 can be sent")
let canSend6Expectation = expectation(description: "6 can be sent")
let canSendFinishExpectation = expectation(description: "finish can be sent")
let mergedSequenceIsFinisedExpectation = expectation(description: "The merged sequence is finished")
let stream1 = AsyncCurrentValueSubject<Int>(1)
let stream2 = AsyncPassthroughSubject<Int>()
let stream3 = AsyncPassthroughSubject<Int>()
let sut = merge(stream1, stream2, stream3)
Task {
var receivedElements = [Int]()
for await element in sut {
receivedElements.append(element)
if element == 1 {
canSend2Expectation.fulfill()
}
if element == 2 {
canSend3Expectation.fulfill()
}
if element == 3 {
canSend4Expectation.fulfill()
}
if element == 4 {
canSend5Expectation.fulfill()
}
if element == 5 {
canSend6Expectation.fulfill()
}
if element == 6 {
canSendFinishExpectation.fulfill()
}
}
XCTAssertEqual(receivedElements, [1, 2, 3, 4, 5, 6])
mergedSequenceIsFinisedExpectation.fulfill()
}
wait(for: [canSend2Expectation], timeout: 1)
stream2.send(2)
wait(for: [canSend3Expectation], timeout: 1)
stream3.send(3)
wait(for: [canSend4Expectation], timeout: 1)
stream3.send(4)
wait(for: [canSend5Expectation], timeout: 1)
stream2.send(5)
wait(for: [canSend6Expectation], timeout: 1)
stream1.send(6)
wait(for: [canSendFinishExpectation], timeout: 1)
stream1.send(Termination.finished)
stream2.send(Termination.finished)
stream3.send(Termination.finished)
wait(for: [mergedSequenceIsFinisedExpectation], timeout: 1)
}
func testMerge_returns_empty_sequence_when_all_sequences_are_empty() async {
var receivedResult = [Int]()
let asyncSequence1 = AsyncEmptySequence<Int>()
let asyncSequence2 = AsyncEmptySequence<Int>()
let asyncSequence3 = AsyncEmptySequence<Int>()
let sut = merge(asyncSequence1, asyncSequence2, asyncSequence3)
for await element in sut {
receivedResult.append(element)
}
XCTAssertTrue(receivedResult.isEmpty)
}
func testMerge_returns_original_sequence_when_one_sequence_is_empty() async {
let expectedResult = [1, 2, 3]
var receivedResult = [Int]()
let asyncSequence1 = expectedResult.async
let asyncSequence2 = AsyncEmptySequence<Int>()
let sut = merge(asyncSequence1, asyncSequence2)
for await element in sut {
receivedResult.append(element)
}
XCTAssertEqual(receivedResult, expectedResult)
}
func testMerge_propagates_error() {
let canSend2Expectation = expectation(description: "2 can be sent")
let canSend3Expectation = expectation(description: "3 can be sent")
let mergedSequenceIsFinishedExpectation = expectation(description: "The merged sequence is finished")
let stream1 = AsyncThrowingCurrentValueSubject<Int, Error>(1)
let stream2 = AsyncPassthroughSubject<Int>()
let sut = merge(stream1, stream2)
Task {
var receivedElements = [Int]()
do {
for try await element in sut {
receivedElements.append(element)
if element == 1 {
canSend2Expectation.fulfill()
}
if element == 2 {
canSend3Expectation.fulfill()
}
}
} catch {
XCTAssertEqual(receivedElements, [1, 2])
mergedSequenceIsFinishedExpectation.fulfill()
}
}
wait(for: [canSend2Expectation], timeout: 1)
stream2.send(2)
wait(for: [canSend3Expectation], timeout: 1)
stream1.send(.failure(MockError(code: 1)))
wait(for: [mergedSequenceIsFinishedExpectation], timeout: 1)
}
func testMerge_finishes_when_task_is_cancelled() {
let canCancelExpectation = expectation(description: "The first element has been emitted")
let hasCancelExceptation = expectation(description: "The task has been cancelled")
let taskHasFinishedExpectation = expectation(description: "The task has finished")
let asyncSequence1 = TimedAsyncSequence(intervalInMills: [100, 100, 100], sequence: [1, 2, 3])
let asyncSequence2 = TimedAsyncSequence(intervalInMills: [50, 100, 100, 100], sequence: [6, 7, 8, 9])
let asyncSequence3 = TimedAsyncSequence(intervalInMills: [1, 399], sequence: [10, 11])
let sut = merge(asyncSequence1, asyncSequence2, asyncSequence3)
let task = Task {
var firstElement: Int?
for try await element in sut {
firstElement = element
canCancelExpectation.fulfill()
await fulfillment(of: [hasCancelExceptation], timeout: 5)
}
XCTAssertEqual(firstElement, 10)
taskHasFinishedExpectation.fulfill()
}
wait(for: [canCancelExpectation], timeout: 5) // one element has been emitted, we can cancel the task
task.cancel()
hasCancelExceptation.fulfill() // we can release the lock in the for loop
wait(for: [taskHasFinishedExpectation], timeout: 5) // task has been cancelled and has finished
}
func testMerge_finishes_when_task_is_cancelled_while_waiting_for_an_element() {
let firstElementHasBeenReceivedExpectation = expectation(description: "The first elemenet has been received")
let canIterateExpectation = expectation(description: "We can iterate")
let hasCancelExceptation = expectation(description: "The iteration is cancelled")
let asyncSequence1 = AsyncCurrentValueSubject<Int>(1)
let asyncSequence2 = AsyncPassthroughSubject<Int>()
let sut = merge(asyncSequence1, asyncSequence2)
let task = Task {
var iterator = sut.makeAsyncIterator()
canIterateExpectation.fulfill()
while let _ = await iterator.next() {
firstElementHasBeenReceivedExpectation.fulfill()
}
hasCancelExceptation.fulfill()
}
wait(for: [canIterateExpectation], timeout: 1)
wait(for: [firstElementHasBeenReceivedExpectation], timeout: 1)
task.cancel()
wait(for: [hasCancelExceptation], timeout: 1)
}
func testMerge_finishes_when_empty_array_of_base() {
let sut = AsyncMergeSequence<AsyncStream<Int>>([])
let hasFinishedExpectation = expectation(description: "Merge has finished")
let task = Task {
var received = [Int]()
for try await element in sut {
received.append(element)
}
XCTAssertTrue(received.isEmpty)
hasFinishedExpectation.fulfill()
}
wait(for: [hasFinishedExpectation], timeout: 1)
task.cancel()
}
}