-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathAsyncHandleEventsSequenceTests.swift
More file actions
158 lines (126 loc) · 5.39 KB
/
AsyncHandleEventsSequenceTests.swift
File metadata and controls
158 lines (126 loc) · 5.39 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
//
// AsyncHandleEventsSequenceTests.swift
//
//
// Created by Thibault Wittemberg on 01/01/2022.
//
@testable import AsyncExtensions
import XCTest
final class AsyncHandleEventsSequenceTests: XCTestCase {
func test_iteration_calls_blocks_when_not_cancelled_and_no_error() async throws {
let received = ManagedCriticalState([String]())
let sourceSequence = [1, 2, 3, 4, 5].async
let sut = sourceSequence.handleEvents {
received.withCriticalRegion { $0.append("start") }
} onElement: { element in
received.withCriticalRegion { $0.append("\(element)") }
} onCancel: {
received.withCriticalRegion { $0.append("cancelled") }
} onFinish: { completion in
received.withCriticalRegion { $0.append("finish \(completion)") }
}
for try await _ in sut {}
XCTAssertEqual(received.criticalState, ["start", "1", "2", "3", "4", "5", "finish finished"])
}
func test_onStart_calls_onFinish_when_throwing() async throws {
let received = ManagedCriticalState([String]())
let sourceSequence = [1, 2, 3, 4, 5].async
let sut = sourceSequence.handleEvents {
received.withCriticalRegion { $0.append("start") }
throw NSError(domain: "error", code: 0)
} onElement: { element in
received.withCriticalRegion { $0.append("\(element)") }
} onCancel: {
received.withCriticalRegion { $0.append("cancelled") }
} onFinish: { completion in
switch completion {
case .finished:
received.withCriticalRegion { $0.append("finish finished") }
case let .failure(error):
received.withCriticalRegion { $0.append("finish error \((error as NSError).code)") }
}
}
do {
for try await _ in sut {}
XCTFail("The stream should have thrown")
} catch {
XCTAssertEqual((error as NSError).code, 0)
XCTAssertEqual(received.criticalState, ["start", "finish error 0"])
}
}
func test_iteration_calls_onCancel_when_task_is_cancelled() {
let firstElementHasBeenReceivedExpectation = expectation(description: "First element has been emitted")
let taskHasBeenCancelledExpectation = expectation(description: "The task has been cancelled")
let onCancelHasBeenCalledExpectation = expectation(description: "OnCancel has been called")
let received = ManagedCriticalState([String]())
let sourceSequence = AsyncCurrentValueSubject<Int>(1)
let sut = sourceSequence.handleEvents {
received.withCriticalRegion { $0.append("start") }
} onElement: { element in
received.withCriticalRegion { $0.append("\(element)") }
} onCancel: {
received.withCriticalRegion { $0.append("cancelled") }
onCancelHasBeenCalledExpectation.fulfill()
} onFinish: { completion in
received.withCriticalRegion { $0.append("finish \(completion)") }
}
let task = Task {
for try await element in sut {
if element == 1 {
firstElementHasBeenReceivedExpectation.fulfill()
}
wait(for: [taskHasBeenCancelledExpectation], timeout: 1)
}
}
wait(for: [firstElementHasBeenReceivedExpectation], timeout: 1)
task.cancel()
taskHasBeenCancelledExpectation.fulfill()
wait(for: [onCancelHasBeenCalledExpectation], timeout: 1)
XCTAssertEqual(received.criticalState, ["start", "1", "cancelled"])
}
func test_iteration_calls_onFinish_with_failure_when_sequence_fails() async throws {
let onFinishHasBeenCalledExpectation = expectation(description: "OnFinish has been called")
let received = ManagedCriticalState([String]())
let expectedError = MockError(code: Int.random(in: 0...100))
let sourceSequence = AsyncFailSequence<Int>(expectedError)
let sut = sourceSequence.handleEvents {
received.withCriticalRegion { $0.append("start") }
} onElement: { element in
received.withCriticalRegion { $0.append("\(element)") }
} onCancel: {
received.withCriticalRegion { $0.append("cancelled") }
} onFinish: { completion in
if case let .failure(error) = completion {
XCTAssertEqual(error as? MockError, expectedError)
}
onFinishHasBeenCalledExpectation.fulfill()
}
do {
for try await _ in sut {}
} catch {
XCTAssertEqual(error as? MockError, expectedError)
}
await waitForExpectations(timeout: 1)
}
func test_iteration_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 sut = (0...1_000_000)
let handledSequence = sut.async.handleEvents()
let task = Task {
var firstElement: Int?
for try await element in handledSequence {
firstElement = element
canCancelExpectation.fulfill()
wait(for: [hasCancelExceptation], timeout: 5)
}
XCTAssertEqual(firstElement, 0)
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
}
}