-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathBaseTestConsumer.java
611 lines (550 loc) · 19.5 KB
/
BaseTestConsumer.java
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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 io.reactivex.rxjava3.observers;
import java.util.*;
import java.util.concurrent.*;
import io.reactivex.rxjava3.exceptions.CompositeException;
import io.reactivex.rxjava3.functions.Predicate;
import io.reactivex.rxjava3.internal.functions.*;
import io.reactivex.rxjava3.internal.util.*;
/**
* Base class with shared infrastructure to support TestSubscriber and TestObserver.
* @param <T> the value type consumed
* @param <U> the subclass of this BaseTestConsumer
*/
public abstract class BaseTestConsumer<T, U extends BaseTestConsumer<T, U>> {
/** The latch that indicates an onError or onComplete has been called. */
protected final CountDownLatch done;
/** The list of values received. */
protected final List<T> values;
/** The list of errors received. */
protected final List<Throwable> errors;
/** The number of completions. */
protected long completions;
/** The last thread seen by the observer. */
protected Thread lastThread;
protected boolean checkSubscriptionOnce;
/**
* The optional tag associated with this test consumer.
* @since 2.0.7
*/
protected CharSequence tag;
/**
* Indicates that one of the awaitX method has timed out.
* @since 2.0.7
*/
protected boolean timeout;
public BaseTestConsumer() {
this.values = new VolatileSizeArrayList<T>();
this.errors = new VolatileSizeArrayList<Throwable>();
this.done = new CountDownLatch(1);
}
/**
* Returns a shared list of received onNext values.
* <p>
* Note that accessing the items via certain methods of the {@link List}
* interface while the upstream is still actively emitting
* more items may result in a {@code ConcurrentModificationException}.
* <p>
* The {@link List#size()} method will return the number of items
* already received by this TestObserver/TestSubscriber in a thread-safe
* manner that can be read via {@link List#get(int)}) method
* (index range of 0 to {@code List.size() - 1}).
* <p>
* A view of the returned List can be created via {@link List#subList(int, int)}
* by using the bounds 0 (inclusive) to {@link List#size()} (exclusive) which,
* when accessed in a read-only fashion, should be also thread-safe and not throw any
* {@code ConcurrentModificationException}.
* @return a list of received onNext values
*/
public final List<T> values() {
return values;
}
/**
* Fail with the given message and add the sequence of errors as suppressed ones.
* <p>Note this is deliberately the only fail method. Most of the times an assertion
* would fail but it is possible it was due to an exception somewhere. This construct
* will capture those potential errors and report it along with the original failure.
*
* @param message the message to use
* @return AssertionError the prepared AssertionError instance
*/
protected final AssertionError fail(String message) {
StringBuilder b = new StringBuilder(64 + message.length());
b.append(message);
b.append(" (")
.append("latch = ").append(done.getCount()).append(", ")
.append("values = ").append(values.size()).append(", ")
.append("errors = ").append(errors.size()).append(", ")
.append("completions = ").append(completions)
;
if (timeout) {
b.append(", timeout!");
}
if (isDisposed()) {
b.append(", disposed!");
}
CharSequence tag = this.tag;
if (tag != null) {
b.append(", tag = ")
.append(tag);
}
b
.append(')')
;
AssertionError ae = new AssertionError(b.toString());
if (!errors.isEmpty()) {
if (errors.size() == 1) {
ae.initCause(errors.get(0));
} else {
CompositeException ce = new CompositeException(errors);
ae.initCause(ce);
}
}
return ae;
}
/**
* Awaits until this TestObserver/TestSubscriber receives an onError or onComplete events.
* @return this
* @throws InterruptedException if the current thread is interrupted while waiting
*/
@SuppressWarnings("unchecked")
public final U await() throws InterruptedException {
if (done.getCount() == 0) {
return (U)this;
}
done.await();
return (U)this;
}
/**
* Awaits the specified amount of time or until this TestObserver/TestSubscriber
* receives an onError or onComplete events, whichever happens first.
* @param time the waiting time
* @param unit the time unit of the waiting time
* @return true if the TestObserver/TestSubscriber terminated, false if timeout happened
* @throws InterruptedException if the current thread is interrupted while waiting
*/
public final boolean await(long time, TimeUnit unit) throws InterruptedException {
boolean d = done.getCount() == 0 || (done.await(time, unit));
timeout = !d;
return d;
}
// assertion methods
/**
* Assert that this TestObserver/TestSubscriber received exactly one onComplete event.
* @return this
*/
@SuppressWarnings("unchecked")
public final U assertComplete() {
long c = completions;
if (c == 0) {
throw fail("Not completed");
} else
if (c > 1) {
throw fail("Multiple completions: " + c);
}
return (U)this;
}
/**
* Assert that this TestObserver/TestSubscriber has not received any onComplete event.
* @return this
*/
@SuppressWarnings("unchecked")
public final U assertNotComplete() {
long c = completions;
if (c == 1) {
throw fail("Completed!");
} else
if (c > 1) {
throw fail("Multiple completions: " + c);
}
return (U)this;
}
/**
* Assert that this TestObserver/TestSubscriber has not received any onError event.
* @return this
*/
@SuppressWarnings("unchecked")
public final U assertNoErrors() {
int s = errors.size();
if (s != 0) {
throw fail("Error(s) present: " + errors);
}
return (U)this;
}
/**
* Assert that this TestObserver/TestSubscriber received exactly the specified onError event value.
*
* <p>The comparison is performed via Objects.equals(); since most exceptions don't
* implement equals(), this assertion may fail. Use the {@link #assertError(Class)}
* overload to test against the class of an error instead of an instance of an error
* or {@link #assertError(Predicate)} to test with different condition.
* @param error the error to check
* @return this
* @see #assertError(Class)
* @see #assertError(Predicate)
*/
public final U assertError(Throwable error) {
return assertError(Functions.equalsWith(error));
}
/**
* Asserts that this TestObserver/TestSubscriber received exactly one onError event which is an
* instance of the specified errorClass class.
* @param errorClass the error class to expect
* @return this
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public final U assertError(Class<? extends Throwable> errorClass) {
return (U)assertError((Predicate)Functions.isInstanceOf(errorClass));
}
/**
* Asserts that this TestObserver/TestSubscriber received exactly one onError event for which
* the provided predicate returns true.
* @param errorPredicate
* the predicate that receives the error Throwable
* and should return true for expected errors.
* @return this
*/
@SuppressWarnings("unchecked")
public final U assertError(Predicate<Throwable> errorPredicate) {
int s = errors.size();
if (s == 0) {
throw fail("No errors");
}
boolean found = false;
for (Throwable e : errors) {
try {
if (errorPredicate.test(e)) {
found = true;
break;
}
} catch (Throwable ex) {
throw ExceptionHelper.wrapOrThrow(ex);
}
}
if (found) {
if (s != 1) {
throw fail("Error present but other errors as well");
}
} else {
throw fail("Error not present");
}
return (U)this;
}
/**
* Assert that this TestObserver/TestSubscriber received exactly one onNext value which is equal to
* the given value with respect to Objects.equals.
* @param value the value to expect
* @return this
*/
@SuppressWarnings("unchecked")
public final U assertValue(T value) {
int s = values.size();
if (s != 1) {
throw fail("expected: " + valueAndClass(value) + " but was: " + values);
}
T v = values.get(0);
if (!Objects.equals(value, v)) {
throw fail("expected: " + valueAndClass(value) + " but was: " + valueAndClass(v));
}
return (U)this;
}
/**
* Asserts that this TestObserver/TestSubscriber received exactly one onNext value for which
* the provided predicate returns true.
* @param valuePredicate
* the predicate that receives the onNext value
* and should return true for the expected value.
* @return this
*/
@SuppressWarnings("unchecked")
public final U assertValue(Predicate<T> valuePredicate) {
assertValueAt(0, valuePredicate);
if (values.size() > 1) {
throw fail("Value present but other values as well");
}
return (U)this;
}
/**
* Asserts that this TestObserver/TestSubscriber received an onNext value at the given index
* which is equal to the given value with respect to null-safe Object.equals.
* <p>History: 2.1.3 - experimental
* @param index the position to assert on
* @param value the value to expect
* @return this
* @since 2.2
*/
@SuppressWarnings("unchecked")
public final U assertValueAt(int index, T value) {
int s = values.size();
if (s == 0) {
throw fail("No values");
}
if (index >= s) {
throw fail("Invalid index: " + index);
}
T v = values.get(index);
if (!Objects.equals(value, v)) {
throw fail("expected: " + valueAndClass(value) + " but was: " + valueAndClass(v));
}
return (U)this;
}
/**
* Asserts that this TestObserver/TestSubscriber received an onNext value at the given index
* for the provided predicate returns true.
* @param index the position to assert on
* @param valuePredicate
* the predicate that receives the onNext value
* and should return true for the expected value.
* @return this
*/
@SuppressWarnings("unchecked")
public final U assertValueAt(int index, Predicate<T> valuePredicate) {
int s = values.size();
if (s == 0) {
throw fail("No values");
}
if (index >= values.size()) {
throw fail("Invalid index: " + index);
}
boolean found = false;
try {
if (valuePredicate.test(values.get(index))) {
found = true;
}
} catch (Throwable ex) {
throw ExceptionHelper.wrapOrThrow(ex);
}
if (!found) {
throw fail("Value not present");
}
return (U)this;
}
/**
* Appends the class name to a non-null value.
* @param o the object
* @return the string representation
*/
public static String valueAndClass(Object o) {
if (o != null) {
return o + " (class: " + o.getClass().getSimpleName() + ")";
}
return "null";
}
/**
* Assert that this TestObserver/TestSubscriber received the specified number onNext events.
* @param count the expected number of onNext events
* @return this
*/
@SuppressWarnings("unchecked")
public final U assertValueCount(int count) {
int s = values.size();
if (s != count) {
throw fail("Value counts differ; expected: " + count + " but was: " + s);
}
return (U)this;
}
/**
* Assert that this TestObserver/TestSubscriber has not received any onNext events.
* @return this
*/
public final U assertNoValues() {
return assertValueCount(0);
}
/**
* Assert that the TestObserver/TestSubscriber received only the specified values in the specified order.
* @param values the values expected
* @return this
*/
@SuppressWarnings("unchecked")
public final U assertValues(T... values) {
int s = this.values.size();
if (s != values.length) {
throw fail("Value count differs; expected: " + values.length + " " + Arrays.toString(values)
+ " but was: " + s + " " + this.values);
}
for (int i = 0; i < s; i++) {
T v = this.values.get(i);
T u = values[i];
if (!Objects.equals(u, v)) {
throw fail("Values at position " + i + " differ; expected: " + valueAndClass(u) + " but was: " + valueAndClass(v));
}
}
return (U)this;
}
/**
* Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating.
* <p>History: 2.1.4 - experimental
* @param values the values expected
* @return this
* @since 2.2
*/
@SafeVarargs
public final U assertValuesOnly(T... values) {
return assertSubscribed()
.assertValues(values)
.assertNoErrors()
.assertNotComplete();
}
/**
* Assert that the TestObserver/TestSubscriber received only the specified sequence of values in the same order.
* @param sequence the sequence of expected values in order
* @return this
*/
@SuppressWarnings("unchecked")
public final U assertValueSequence(Iterable<? extends T> sequence) {
int i = 0;
Iterator<T> actualIterator = values.iterator();
Iterator<? extends T> expectedIterator = sequence.iterator();
boolean actualNext;
boolean expectedNext;
for (;;) {
expectedNext = expectedIterator.hasNext();
actualNext = actualIterator.hasNext();
if (!actualNext || !expectedNext) {
break;
}
T u = expectedIterator.next();
T v = actualIterator.next();
if (!Objects.equals(u, v)) {
throw fail("Values at position " + i + " differ; expected: " + valueAndClass(u) + " but was: " + valueAndClass(v));
}
i++;
}
if (actualNext) {
throw fail("More values received than expected (" + i + ")");
}
if (expectedNext) {
throw fail("Fewer values received than expected (" + i + ")");
}
return (U)this;
}
/**
* Assert that the onSubscribe method was called exactly once.
* @return this
*/
protected abstract U assertSubscribed();
/**
* Assert that the upstream signalled the specified values in order and
* completed normally.
* @param values the expected values, asserted in order
* @return this
* @see #assertFailure(Class, Object...)
*/
@SafeVarargs
public final U assertResult(T... values) {
return assertSubscribed()
.assertValues(values)
.assertNoErrors()
.assertComplete();
}
/**
* Assert that the upstream signalled the specified values in order
* and then failed with a specific class or subclass of Throwable.
* @param error the expected exception (parent) class
* @param values the expected values, asserted in order
* @return this
*/
@SafeVarargs
public final U assertFailure(Class<? extends Throwable> error, T... values) {
return assertSubscribed()
.assertValues(values)
.assertError(error)
.assertNotComplete();
}
/**
* Awaits until the internal latch is counted down.
* <p>If the wait times out or gets interrupted, the TestObserver/TestSubscriber is cancelled.
* @param time the waiting time
* @param unit the time unit of the waiting time
* @return this
* @throws RuntimeException wrapping an InterruptedException if the wait is interrupted
*/
@SuppressWarnings("unchecked")
public final U awaitDone(long time, TimeUnit unit) {
try {
if (!done.await(time, unit)) {
timeout = true;
dispose();
}
} catch (InterruptedException ex) {
dispose();
throw ExceptionHelper.wrapOrThrow(ex);
}
return (U)this;
}
/**
* Assert that the TestObserver/TestSubscriber has received a Disposable but no other events.
* @return this
*/
public final U assertEmpty() {
return assertSubscribed()
.assertNoValues()
.assertNoErrors()
.assertNotComplete();
}
/**
* Set the tag displayed along with an assertion failure's
* other state information.
* <p>History: 2.0.7 - experimental
* @param tag the string to display (null won't print any tag)
* @return this
* @since 2.1
*/
@SuppressWarnings("unchecked")
public final U withTag(CharSequence tag) {
this.tag = tag;
return (U)this;
}
/**
* Await until the TestObserver/TestSubscriber receives the given
* number of items or terminates by sleeping 10 milliseconds at a time
* up to 5000 milliseconds of timeout.
* <p>History: 2.0.7 - experimental
* @param atLeast the number of items expected at least
* @return this
* @since 2.1
*/
@SuppressWarnings("unchecked")
public final U awaitCount(int atLeast) {
long start = System.currentTimeMillis();
long timeoutMillis = 5000;
for (;;) {
if (System.currentTimeMillis() - start >= timeoutMillis) {
timeout = true;
break;
}
if (done.getCount() == 0L) {
break;
}
if (values.size() >= atLeast) {
break;
}
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
return (U)this;
}
/**
* Returns true if this test consumer was cancelled/disposed.
* @return true if this test consumer was cancelled/disposed.
*/
protected abstract boolean isDisposed();
/**
* Cancel/dispose this test consumer.
*/
protected abstract void dispose();
}