-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThresherTests.swift
197 lines (155 loc) · 5.84 KB
/
ThresherTests.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
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
import XCTest
import Foundation
import Combine
@testable import Thresher
final class ThresherTests: XCTestCase {
var directory: URL {
return URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.appendingPathComponent("Fixtures")
}
func test_schedule() {
let scheduler = TestScheduler()
var didRun = false
scheduler.schedule {
didRun = true
}
scheduler.advance()
XCTAssert(didRun)
}
func test_schedule_after_advance_by() {
let scheduler = TestScheduler()
var didRun = false
let now = DispatchTime.now()
scheduler.schedule(after: TestScheduler.SchedulerTimeType(now + 100), tolerance: 1, options: nil) { didRun = true }
scheduler.advance(by: 101)
XCTAssert(didRun)
}
func test_schedule_after_advance_to() {
let scheduler = TestScheduler()
var didRun = false
let now = DispatchTime.now()
scheduler.schedule(after: TestScheduler.SchedulerTimeType(now + 100), tolerance: 1, options: nil) { didRun = true }
scheduler.advance(to: TestScheduler.SchedulerTimeType(now + 101))
XCTAssert(didRun)
}
func test_schedule_in_order() {
let scheduler = TestScheduler()
var ints: [Int] = []
let now = DispatchTime.now()
scheduler.schedule(after: TestScheduler.SchedulerTimeType(now + 200), tolerance: 1, options: nil) { ints.append(200) }
scheduler.schedule(after: TestScheduler.SchedulerTimeType(now + 100), tolerance: 1, options: nil) { ints.append(100) }
scheduler.schedule(after: TestScheduler.SchedulerTimeType(now + 300), tolerance: 1, options: nil) { ints.append(300) }
scheduler.run()
XCTAssertEqual(ints, [100, 200, 300])
}
func test_schedule_nested() {
let scheduler = TestScheduler()
var ints: [Int] = []
var cancellables: [AnyCancellable] = []
scheduler.schedule {
ints.append(1)
}
scheduler.schedule(after: scheduler.now.advanced(by: .seconds(10))) {
ints.append(2)
scheduler.schedule(after: scheduler.now.advanced(by: .seconds(20))) {
ints.append(3)
}
}
scheduler.schedule {
ints.append(4)
}
scheduler.schedule(after: scheduler.now.advanced(by: .seconds(5)), interval: .seconds(10)) {
ints.append(5) // 10 sec interval
}
.store(in: &cancellables)
scheduler.advance(by: 30)
XCTAssertEqual(ints, [1, 4, 5, 2, 5, 5, 3])
}
func test_passthrough_subject() {
let scheduler = TestScheduler()
let subject = PassthroughSubject<Int, Never>()
var ints: [Int] = []
var cancellables: [AnyCancellable] = []
subject
.receive(on: scheduler)
.sink { value in
print(value)
ints.append(value)
}
.store(in: &cancellables)
// the act of subscription is also scheduled on the scheduler
// so we need to advance the scheduler to make sure the subription
// has occured
scheduler.advance()
subject.send(1)
subject.send(2)
// the subscriber hasn't received any values yet
// as it hasn't been scheduled
XCTAssert(ints.isEmpty)
scheduler.run()
XCTAssertEqual(ints, [1, 2])
}
func test_passthrough_subject_single_values() {
let scheduler = TestScheduler()
let subject = PassthroughSubject<Int, Never>()
var ints: [Int] = []
var cancellables: [AnyCancellable] = []
subject
.receive(on: scheduler)
.sink { value in
ints.append(value)
}
.store(in: &cancellables)
// the act of subscription is also scheduled on the scheduler
// so we need to advance the scheduler to make sure the subription
// has occured
scheduler.advance()
subject.send(1)
scheduler.advance()
XCTAssertEqual(ints, [1])
subject.send(2)
scheduler.advance()
XCTAssertEqual(ints, [1, 2])
}
func test_image_loading() {
let url = directory.appendingPathComponent("thresher.jpg")
let scheduler = TestScheduler()
var image: UIImage? = nil
var cancellables: [AnyCancellable] = []
loadImage(from: url)
.receive(on: scheduler)
.sink { image = $0 }
.store(in: &cancellables)
// schedule the subscription
scheduler.advance()
// wait for URLSession dataTaskPublisher to do its thing
wait(seconds: 2)
// process subscription results
scheduler.advance()
XCTAssertNotNil(image)
}
private func loadImage(from url: URL) -> AnyPublisher<UIImage?, Never> {
return URLSession.shared.dataTaskPublisher(for: url)
.map(\.data)
.map(UIImage.init(data:))
.catch { _ in Empty<UIImage?, Never>() }
.eraseToAnyPublisher()
}
private func wait(seconds: TimeInterval) {
let expectation = XCTestExpectation(description: "waiting")
DispatchQueue.global().asyncAfter(deadline: .now() + seconds) {
expectation.fulfill()
}
wait(for: [expectation], timeout: seconds + 1)
}
static var allTests = [
("test_schedule", test_schedule),
("test_schedule_after_advance_by", test_schedule_after_advance_by),
("test_schedule_after_advance_to", test_schedule_after_advance_to),
("test_schedule_in_order", test_schedule_in_order),
("test_passthrough_subject", test_passthrough_subject),
("test_passthrough_subject_single_values", test_passthrough_subject_single_values),
("test_image_loading", test_image_loading),
]
}