forked from Polidea/RxBluetoothKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObservableSpec+QueueSubscribeOn.swift
316 lines (268 loc) · 13.7 KB
/
ObservableSpec+QueueSubscribeOn.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
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
314
315
316
// The MIT License (MIT)
//
// Copyright (c) 2016 Polidea
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import Foundation
import Quick
import Nimble
@testable
import RxBluetoothKit
import RxTest
import RxSwift
class ObservableQueueSubscribeOnSpec: QuickSpec {
func createRecords<T>(records: (Int, Event<T>)...) -> [Recorded<Event<T>>] {
var array = [Recorded<Event<T>>]()
for (time, event) in records {
array.append(Recorded(time: time, value: event))
}
return array
}
override func spec() {
var testScheduler: TestScheduler!
beforeEach {
testScheduler = TestScheduler(initialClock: 0, resolution: 1.0, simulateProcessingDelay: false)
}
describe("queueSubscribeOn extension") {
var serializedQueue: SerializedSubscriptionQueue!
var users: [ScheduledObservable<Int>]!
beforeEach {
serializedQueue = SerializedSubscriptionQueue(scheduler: testScheduler)
}
context("when there are two users registering cold subscriptions on queue") {
beforeEach {
users = []
let ob1 = testScheduler.createColdObservable(
self.createRecords(records: (000, .next(-1)),
(100, .next(0)),
(250, .next(2)),
(300, .completed)))
users.append(testScheduler.scheduleObservable {
ob1.queueSubscribe(on: serializedQueue).asObservable()
})
let ob2 = testScheduler.createColdObservable(
self.createRecords(records: (000, .next(-1)),
(050, .next(1)),
(100, .completed)))
users.append(testScheduler.scheduleObservable {
ob2.queueSubscribe(on: serializedQueue).asObservable()
})
}
it("should register events only for first user before its complete event is emitted") {
testScheduler.advanceTo(users[0].subscribeTime + 250)
expect(users[0].observer.events.count).to(equal(3))
expect(users[1].observer.events.count).to(equal(0))
}
it("should register first event for second user when first one finished emitting") {
testScheduler.advanceTo(users[0].subscribeTime + 300)
expect(users[0].observer.events.count).to(equal(4))
expect(users[1].observer.events.count).to(equal(1))
}
it("should collect all events for both users after disposal of both users") {
testScheduler.advanceTo(users[1].disposeTime)
expect(users[0].observer.events.count).to(equal(4))
expect(users[1].observer.events.count).to(equal(3))
}
}
context("when there are two users registering hot subscriptions on queue") {
beforeEach {
users = []
let ob1 = testScheduler.createHotObservable(
self.createRecords(records: (000, .next(-1)),
(205, .next(0)),
(250, .next(2)),
(400, .completed)))
users.append(testScheduler.scheduleObservable {
ob1.queueSubscribe(on: serializedQueue).asObservable()
})
let ob2 = testScheduler.createHotObservable(
self.createRecords(records: (000, .next(-1)),
(250, .next(1)),
(500, .next(2)),
(650, .next(3)),
(800, .completed)))
users.append(testScheduler.scheduleObservable {
ob2.queueSubscribe(on: serializedQueue).asObservable()
})
}
context("before first user completes its stream") {
beforeEach {
testScheduler.advanceTo(250)
}
it("should register two events for first user and none for second one") {
expect(users[0].observer.events.count).to(equal(2))
expect(users[1].observer.events.count).to(equal(0))
}
it("should skip first user's first event because it's emitted before subscription") {
expect(users[0].observer.events[0].value.isStopEvent).to(beFalse())
expect(users[0].observer.events[0].value.element).to(equal(0))
}
}
context("after first user completes its stream") {
beforeEach {
testScheduler.advanceTo(400)
}
it("should register three events for first user and none for second one") {
expect(users[0].observer.events.count).to(equal(3))
expect(users[1].observer.events.count).to(equal(0))
}
it("should register last event of first user as complete of stream") {
expect(users[0].observer.events.last!.value == .completed).to(beTrue())
}
}
context("after last user completes its stream") {
beforeEach {
testScheduler.advanceTo(800)
}
it("should register all possible events for both users") {
expect(users[0].observer.events.count).to(equal(3))
expect(users[1].observer.events.count).to(equal(3))
}
it("should register event for second user as .Next(2)") {
expect(users[1].observer.events.first?.value.element).to(equal(2))
}
}
}
context("when there are four users with queued subscriptions") {
var isSubscribed: [Bool]!
enum SomeError: Error { case error }
beforeEach {
users = []
isSubscribed = []
// First user
let ob1 = testScheduler.createColdObservable(
self.createRecords(records: (300, Event<Int>.completed)))
isSubscribed.append(false)
users.append(testScheduler.scheduleObservable {
Observable.deferred {
isSubscribed[0] = true
return ob1.asObservable()
}.queueSubscribe(on: serializedQueue)
})
// Second user
let ob2 = testScheduler.createColdObservable(
self.createRecords(records: (250, Event<Int>.error(SomeError.error))))
isSubscribed.append(false)
users.append(testScheduler.scheduleObservable {
Observable.deferred {
isSubscribed[1] = true
return ob2.asObservable()
}.queueSubscribe(on: serializedQueue)
})
// Third user (it should be disposed even before subscription)
let ob3 = testScheduler.createColdObservable(
self.createRecords(records: (100, Event<Int>.completed)))
isSubscribed.append(false)
users.append(testScheduler.scheduleObservable(time: ObservableScheduleTimes(createTime: 0, subscribeTime: 200, disposeTime: 500)) {
Observable.deferred {
isSubscribed[2] = true
return ob3.asObservable()
}.queueSubscribe(on: serializedQueue)
})
// Fourth user
let ob4 = testScheduler.createColdObservable(
self.createRecords(records: (100, Event<Int>.completed)))
isSubscribed.append(false)
users.append(testScheduler.scheduleObservable {
Observable.deferred {
isSubscribed[3] = true
return ob4.asObservable()
}.queueSubscribe(on: serializedQueue)
})
}
context("before first user is subscribed") {
beforeEach {
testScheduler.advanceTo(users[0].time.before.subscribeTime)
}
it("shouldn't subscribe any user") {
expect(isSubscribed[0]).to(beFalse())
expect(isSubscribed[1]).to(beFalse())
expect(isSubscribed[2]).to(beFalse())
expect(isSubscribed[3]).to(beFalse())
}
}
context("after first user is subscribed") {
beforeEach {
testScheduler.advanceTo(users[0].subscribeTime)
}
it("should subscribe only first user") {
expect(isSubscribed[0]).to(beTrue())
expect(isSubscribed[1]).to(beFalse())
expect(isSubscribed[2]).to(beFalse())
expect(isSubscribed[3]).to(beFalse())
}
it("shouldn't register any events for first user") {
expect(users[0].events.count).to(equal(0))
}
}
context("after first user completes its stream") {
beforeEach {
testScheduler.advanceTo(users[0].subscribeTime + 300)
}
it("should register second user") {
expect(isSubscribed[0]).to(beTrue())
expect(isSubscribed[1]).to(beTrue())
expect(isSubscribed[2]).to(beFalse())
expect(isSubscribed[3]).to(beFalse())
}
it("should register only complete event for first user") {
expect(users[0].events.first!.value == .completed).to(beTrue())
}
it("shouldn't register any event for second user") {
expect(users[1].events.count).to(equal(0))
}
}
context("after second user completes its stream") {
beforeEach {
testScheduler.advanceTo(users[0].subscribeTime + 300 + 250)
}
it("should skip registration of third user and register fourth user immidiately") {
expect(isSubscribed[0]).to(beTrue())
expect(isSubscribed[1]).to(beTrue())
expect(isSubscribed[2]).to(beFalse())
expect(isSubscribed[3]).to(beTrue())
}
it("should register one event for second user") {
expect(users[1].events.first!.value == .error(SomeError.error)).to(beTrue())
}
it("shouldn't register any event for fourth user") {
expect(users[3].events.count).to(equal(0))
}
}
context("after all events are processed") {
beforeEach {
testScheduler.start()
}
it("should subscribe to all events except thrid one") {
expect(isSubscribed[0]).to(beTrue())
expect(isSubscribed[1]).to(beTrue())
expect(isSubscribed[2]).to(beFalse())
expect(isSubscribed[3]).to(beTrue())
}
it("should contain valid number of events for every user") {
expect(users[0].events.count).to(equal(1))
expect(users[1].events.count).to(equal(1))
expect(users[2].events.count).to(equal(0))
expect(users[3].events.count).to(equal(1))
}
}
}
}
}
}