forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSubscriber.java
More file actions
542 lines (499 loc) · 18.9 KB
/
TestSubscriber.java
File metadata and controls
542 lines (499 loc) · 18.9 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.observers;
import java.util.*;
import java.util.concurrent.*;
import rx.*;
import rx.Observer;
import rx.exceptions.CompositeException;
/**
* A {@code TestSubscriber} is a variety of {@link Subscriber} that you can use for unit testing, to perform
* assertions, inspect received events, or wrap a mocked {@code Subscriber}.
* @param <T> the value type
*/
public class TestSubscriber<T> extends Subscriber<T> {
private final TestObserver<T> testObserver;
private final CountDownLatch latch = new CountDownLatch(1);
private volatile Thread lastSeenThread;
/** The shared no-op observer. */
private static final Observer<Object> INERT = new Observer<Object>() {
@Override
public void onCompleted() {
// do nothing
}
@Override
public void onError(Throwable e) {
// do nothing
}
@Override
public void onNext(Object t) {
// do nothing
}
};
/**
* Constructs a TestSubscriber with the initial request to be requested from upstream.
*
* @param initialRequest the initial request value, negative value will revert to the default unbounded behavior
* @since 1.1.0
*/
@SuppressWarnings("unchecked")
public TestSubscriber(long initialRequest) {
this((Observer<T>)INERT, initialRequest);
}
/**
* Constructs a TestSubscriber with the initial request to be requested from upstream
* and a delegate Observer to wrap.
*
* @param initialRequest the initial request value, negative value will revert to the default unbounded behavior
* @param delegate the Observer instance to wrap
* @throws NullPointerException if delegate is null
* @since 1.1.0
*/
public TestSubscriber(Observer<T> delegate, long initialRequest) {
if (delegate == null) {
throw new NullPointerException();
}
this.testObserver = new TestObserver<T>(delegate);
if (initialRequest >= 0L) {
this.request(initialRequest);
}
}
/**
* Constructs a TestSubscriber which requests Long.MAX_VALUE and delegates events to
* the given Subscriber.
* @param delegate the subscriber to delegate to.
* @throws NullPointerException if delegate is null
* @since 1.1.0
*/
public TestSubscriber(Subscriber<T> delegate) {
this(delegate, -1);
}
/**
* Constructs a TestSubscriber which requests Long.MAX_VALUE and delegates events to
* the given Observer.
* @param delegate the observer to delegate to.
* @throws NullPointerException if delegate is null
* @since 1.1.0
*/
public TestSubscriber(Observer<T> delegate) {
this(delegate, -1);
}
/**
* Constructs a TestSubscriber with an initial request of Long.MAX_VALUE and no delegation.
*/
public TestSubscriber() {
this(-1);
}
/**
* Factory method to construct a TestSubscriber with an initial request of Long.MAX_VALUE and no delegation.
* @param <T> the value type
* @return the created TestSubscriber instance
* @since 1.1.0
*/
public static <T> TestSubscriber<T> create() {
return new TestSubscriber<T>();
}
/**
* Factory method to construct a TestSubscriber with the given initial request amount and no delegation.
* @param <T> the value type
* @param initialRequest the initial request amount, negative values revert to the default unbounded mode
* @return the created TestSubscriber instance
* @since 1.1.0
*/
public static <T> TestSubscriber<T> create(long initialRequest) {
return new TestSubscriber<T>(initialRequest);
}
/**
* Factory method to construct a TestSubscriber which delegates events to the given Observer and
* issues the given initial request amount.
* @param <T> the value type
* @param delegate the observer to delegate events to
* @param initialRequest the initial request amount, negative values revert to the default unbounded mode
* @return the created TestSubscriber instance
* @throws NullPointerException if delegate is null
* @since 1.1.0
*/
public static <T> TestSubscriber<T> create(Observer<T> delegate, long initialRequest) {
return new TestSubscriber<T>(delegate, initialRequest);
}
/**
* Factory method to construct a TestSubscriber which delegates events to the given Subscriber and
* an issues an initial request of Long.MAX_VALUE.
* @param <T> the value type
* @param delegate the subscriber to delegate events to
* @return the created TestSubscriber instance
* @throws NullPointerException if delegate is null
* @since 1.1.0
*/
public static <T> TestSubscriber<T> create(Subscriber<T> delegate) {
return new TestSubscriber<T>(delegate);
}
/**
* Factory method to construct a TestSubscriber which delegates events to the given Observer and
* an issues an initial request of Long.MAX_VALUE.
* @param <T> the value type
* @param delegate the observer to delegate events to
* @return the created TestSubscriber instance
* @throws NullPointerException if delegate is null
* @since 1.1.0
*/
public static <T> TestSubscriber<T> create(Observer<T> delegate) {
return new TestSubscriber<T>(delegate);
}
/**
* Notifies the Subscriber that the {@code Observable} has finished sending push-based notifications.
* <p>
* The {@code Observable} will not call this method if it calls {@link #onError}.
*/
@Override
public void onCompleted() {
try {
lastSeenThread = Thread.currentThread();
testObserver.onCompleted();
} finally {
latch.countDown();
}
}
/**
* Returns the {@link Notification}s representing each time this {@link Subscriber} was notified of sequence
* completion via {@link #onCompleted}, as a {@link List}.
*
* @return a list of Notifications representing calls to this Subscriber's {@link #onCompleted} method
*/
public List<Notification<T>> getOnCompletedEvents() {
return testObserver.getOnCompletedEvents();
}
/**
* Notifies the Subscriber that the {@code Observable} has experienced an error condition.
* <p>
* If the {@code Observable} calls this method, it will not thereafter call {@link #onNext} or
* {@link #onCompleted}.
*
* @param e
* the exception encountered by the Observable
*/
@Override
public void onError(Throwable e) {
try {
lastSeenThread = Thread.currentThread();
testObserver.onError(e);
} finally {
latch.countDown();
}
}
/**
* Returns the {@link Throwable}s this {@link Subscriber} was notified of via {@link #onError} as a
* {@link List}.
*
* @return a list of the Throwables that were passed to this Subscriber's {@link #onError} method
*/
public List<Throwable> getOnErrorEvents() {
return testObserver.getOnErrorEvents();
}
/**
* Provides the Subscriber with a new item to observe.
* <p>
* The {@code Observable} may call this method 0 or more times.
* <p>
* The {@code Observable} will not call this method again after it calls either {@link #onCompleted} or
* {@link #onError}.
*
* @param t
* the item emitted by the Observable
*/
@Override
public void onNext(T t) {
lastSeenThread = Thread.currentThread();
testObserver.onNext(t);
}
/**
* Allows calling the protected {@link #request(long)} from unit tests.
*
* @param n the maximum number of items you want the Observable to emit to the Subscriber at this time, or
* {@code Long.MAX_VALUE} if you want the Observable to emit items at its own pace
*/
public void requestMore(long n) {
request(n);
}
/**
* Returns the sequence of items observed by this {@link Subscriber}, as an ordered {@link List}.
*
* @return a list of items observed by this Subscriber, in the order in which they were observed
*/
public List<T> getOnNextEvents() {
return testObserver.getOnNextEvents();
}
/**
* Asserts that a particular sequence of items was received by this {@link Subscriber} in order.
*
* @param items
* the sequence of items expected to have been observed
* @throws AssertionError
* if the sequence of items observed does not exactly match {@code items}
*/
public void assertReceivedOnNext(List<T> items) {
testObserver.assertReceivedOnNext(items);
}
/**
* Asserts that a single terminal event occurred, either {@link #onCompleted} or {@link #onError}.
*
* @throws AssertionError
* if not exactly one terminal event notification was received
*/
public void assertTerminalEvent() {
testObserver.assertTerminalEvent();
}
/**
* Asserts that this {@code Subscriber} is unsubscribed.
*
* @throws AssertionError
* if this {@code Subscriber} is not unsubscribed
*/
public void assertUnsubscribed() {
if (!isUnsubscribed()) {
throw new AssertionError("Not unsubscribed.");
}
}
/**
* Asserts that this {@code Subscriber} has received no {@code onError} notifications.
*
* @throws AssertionError
* if this {@code Subscriber} has received one or more {@code onError} notifications
*/
public void assertNoErrors() {
List<Throwable> onErrorEvents = getOnErrorEvents();
if (onErrorEvents.size() > 0) {
AssertionError ae = new AssertionError("Unexpected onError events: " + getOnErrorEvents().size());
if (onErrorEvents.size() == 1) {
ae.initCause(getOnErrorEvents().get(0));
} else {
ae.initCause(new CompositeException(onErrorEvents));
}
throw ae;
}
}
/**
* Blocks until this {@link Subscriber} receives a notification that the {@code Observable} is complete
* (either an {@code onCompleted} or {@code onError} notification).
*
* @throws RuntimeException
* if the Subscriber is interrupted before the Observable is able to complete
*/
public void awaitTerminalEvent() {
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted", e);
}
}
/**
* Blocks until this {@link Subscriber} receives a notification that the {@code Observable} is complete
* (either an {@code onCompleted} or {@code onError} notification), or until a timeout expires.
*
* @param timeout
* the duration of the timeout
* @param unit
* the units in which {@code timeout} is expressed
* @throws RuntimeException
* if the Subscriber is interrupted before the Observable is able to complete
*/
public void awaitTerminalEvent(long timeout, TimeUnit unit) {
try {
latch.await(timeout, unit);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted", e);
}
}
/**
* Blocks until this {@link Subscriber} receives a notification that the {@code Observable} is complete
* (either an {@code onCompleted} or {@code onError} notification), or until a timeout expires; if the
* Subscriber is interrupted before either of these events take place, this method unsubscribes the
* Subscriber from the Observable). If timeout expires then the Subscriber is unsubscribed from the Observable.
*
* @param timeout
* the duration of the timeout
* @param unit
* the units in which {@code timeout} is expressed
*/
public void awaitTerminalEventAndUnsubscribeOnTimeout(long timeout, TimeUnit unit) {
try {
boolean result = latch.await(timeout, unit);
if (!result) {
// timeout occurred
unsubscribe();
}
} catch (InterruptedException e) {
unsubscribe();
}
}
/**
* Returns the last thread that was in use when an item or notification was received by this
* {@link Subscriber}.
*
* @return the {@code Thread} on which this Subscriber last received an item or notification from the
* Observable it is subscribed to
*/
public Thread getLastSeenThread() {
return lastSeenThread;
}
/**
* Asserts that there is exactly one completion event.
*
* @throws AssertionError if there were zero, or more than one, onCompleted events
* @since 1.1.0
*/
public void assertCompleted() {
int s = testObserver.getOnCompletedEvents().size();
if (s == 0) {
throw new AssertionError("Not completed!");
} else
if (s > 1) {
throw new AssertionError("Completed multiple times: " + s);
}
}
/**
* Asserts that there is no completion event.
*
* @throws AssertionError if there were one or more than one onCompleted events
* @since 1.1.0
*/
public void assertNotCompleted() {
int s = testObserver.getOnCompletedEvents().size();
if (s == 1) {
throw new AssertionError("Completed!");
} else
if (s > 1) {
throw new AssertionError("Completed multiple times: " + s);
}
}
/**
* Asserts that there is exactly one error event which is a subclass of the given class.
*
* @param clazz the class to check the error against.
* @throws AssertionError if there were zero, or more than one, onError events, or if the single onError
* event did not carry an error of a subclass of the given class
* @since 1.1.0
*/
public void assertError(Class<? extends Throwable> clazz) {
List<Throwable> err = testObserver.getOnErrorEvents();
if (err.size() == 0) {
throw new AssertionError("No errors");
} else
if (err.size() > 1) {
AssertionError ae = new AssertionError("Multiple errors: " + err.size());
ae.initCause(new CompositeException(err));
throw ae;
} else
if (!clazz.isInstance(err.get(0))) {
AssertionError ae = new AssertionError("Exceptions differ; expected: " + clazz + ", actual: " + err.get(0));
ae.initCause(err.get(0));
throw ae;
}
}
/**
* Asserts that there is a single onError event with the exact exception.
*
* @param throwable the throwable to check
* @throws AssertionError if there were zero, or more than one, onError events, or if the single onError
* event did not carry an error that matches the specified throwable
* @since 1.1.0
*/
public void assertError(Throwable throwable) {
List<Throwable> err = testObserver.getOnErrorEvents();
if (err.size() == 0) {
throw new AssertionError("No errors");
} else
if (err.size() > 1) {
AssertionError ae = new AssertionError("Multiple errors: " + err.size());
ae.initCause(new CompositeException(err));
throw ae;
} else
if (!throwable.equals(err.get(0))) {
AssertionError ae = new AssertionError("Exceptions differ; expected: " + throwable + ", actual: " + err.get(0));
ae.initCause(err.get(0));
throw ae;
}
}
/**
* Asserts that there are no onError and onCompleted events.
*
* @throws AssertionError if there was either an onError or onCompleted event
* @since 1.1.0
*/
public void assertNoTerminalEvent() {
List<Throwable> err = testObserver.getOnErrorEvents();
int s = testObserver.getOnCompletedEvents().size();
if (err.size() > 0 || s > 0) {
if (err.isEmpty()) {
throw new AssertionError("Found " + err.size() + " errors and " + s + " completion events instead of none");
} else
if (err.size() == 1) {
AssertionError ae = new AssertionError("Found " + err.size() + " errors and " + s + " completion events instead of none");
ae.initCause(err.get(0));
throw ae;
} else {
AssertionError ae = new AssertionError("Found " + err.size() + " errors and " + s + " completion events instead of none");
ae.initCause(new CompositeException(err));
throw ae;
}
}
}
/**
* Asserts that there are no onNext events received.
*
* @throws AssertionError if there were any onNext events
* @since 1.1.0
*/
public void assertNoValues() {
int s = testObserver.getOnNextEvents().size();
if (s > 0) {
throw new AssertionError("No onNext events expected yet some received: " + s);
}
}
/**
* Asserts that the given number of onNext events are received.
*
* @param count the expected number of onNext events
* @throws AssertionError if there were more or fewer onNext events than specified by {@code count}
* @since 1.1.0
*/
public void assertValueCount(int count) {
int s = testObserver.getOnNextEvents().size();
if (s != count) {
throw new AssertionError("Number of onNext events differ; expected: " + count + ", actual: " + s);
}
}
/**
* Asserts that the received onNext events, in order, are the specified items.
*
* @param values the items to check
* @throws AssertionError if the items emitted do not exactly match those specified by {@code values}
* @since 1.1.0
*/
public void assertValues(T... values) {
assertReceivedOnNext(Arrays.asList(values));
}
/**
* Asserts that there is only a single received onNext event and that it marks the emission of a specific item.
*
* @param value the item to check
* @throws AssertionError if the Observable does not emit only the single item specified by {@code value}
* @since 1.1.0
*/
public void assertValue(T value) {
assertReceivedOnNext(Collections.singletonList(value));
}
}