T
- the value type consumedU
- the subclass of this BaseTestConsumerpublic abstract class BaseTestConsumer<T,U extends BaseTestConsumer<T,U>> extends Object
Modifier and Type | Field and Description |
---|---|
protected boolean |
checkSubscriptionOnce |
protected long |
completions
The number of completions.
|
protected CountDownLatch |
done
The latch that indicates an onError or onComplete has been called.
|
protected List<Throwable> |
errors
The list of errors received.
|
protected Thread |
lastThread
The last thread seen by the observer.
|
protected CharSequence |
tag
The optional tag associated with this test consumer.
|
protected boolean |
timeout
Indicates that one of the awaitX method has timed out.
|
protected List<T> |
values
The list of values received.
|
Constructor and Description |
---|
BaseTestConsumer() |
Modifier and Type | Method and Description |
---|---|
U |
assertComplete()
Assert that this TestObserver/TestSubscriber received exactly one onComplete event.
|
U |
assertEmpty()
Assert that the TestObserver/TestSubscriber has received a Disposable but no other events.
|
U |
assertError(Class<? extends Throwable> errorClass)
Asserts that this TestObserver/TestSubscriber received exactly one onError event which is an
instance of the specified errorClass class.
|
U |
assertError(Predicate<Throwable> errorPredicate)
Asserts that this TestObserver/TestSubscriber received exactly one onError event for which
the provided predicate returns true.
|
U |
assertError(Throwable error)
Assert that this TestObserver/TestSubscriber received exactly the specified onError event value.
|
U |
assertFailure(Class<? extends Throwable> error,
T... values)
Assert that the upstream signalled the specified values in order
and then failed with a specific class or subclass of Throwable.
|
U |
assertNoErrors()
Assert that this TestObserver/TestSubscriber has not received any onError event.
|
U |
assertNotComplete()
Assert that this TestObserver/TestSubscriber has not received any onComplete event.
|
U |
assertNoValues()
Assert that this TestObserver/TestSubscriber has not received any onNext events.
|
U |
assertResult(T... values)
Assert that the upstream signalled the specified values in order and
completed normally.
|
protected abstract U |
assertSubscribed()
Assert that the onSubscribe method was called exactly once.
|
U |
assertValue(Predicate<T> valuePredicate)
Asserts that this TestObserver/TestSubscriber received exactly one onNext value for which
the provided predicate returns true.
|
U |
assertValue(T value)
Assert that this TestObserver/TestSubscriber received exactly one onNext value which is equal to
the given value with respect to Objects.equals.
|
U |
assertValueAt(int index,
Predicate<T> valuePredicate)
Asserts that this TestObserver/TestSubscriber received an onNext value at the given index
for the provided predicate returns true.
|
U |
assertValueAt(int index,
T value)
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.
|
U |
assertValueCount(int count)
Assert that this TestObserver/TestSubscriber received the specified number onNext events.
|
U |
assertValues(T... values)
Assert that the TestObserver/TestSubscriber received only the specified values in the specified order.
|
U |
assertValueSequence(Iterable<? extends T> sequence)
Assert that the TestObserver/TestSubscriber received only the specified sequence of values in the same order.
|
U |
assertValuesOnly(T... values)
Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating.
|
U |
await()
Awaits until this TestObserver/TestSubscriber receives an onError or onComplete events.
|
boolean |
await(long time,
TimeUnit unit)
Awaits the specified amount of time or until this TestObserver/TestSubscriber
receives an onError or onComplete events, whichever happens first.
|
U |
awaitCount(int atLeast)
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.
|
U |
awaitDone(long time,
TimeUnit unit)
Awaits until the internal latch is counted down.
|
protected abstract void |
dispose()
Cancel/dispose this test consumer.
|
protected AssertionError |
fail(String message)
Fail with the given message and add the sequence of errors as suppressed ones.
|
protected abstract boolean |
isDisposed()
Returns true if this test consumer was cancelled/disposed.
|
static String |
valueAndClass(Object o)
Appends the class name to a non-null value.
|
List<T> |
values()
Returns a shared list of received onNext values.
|
U |
withTag(CharSequence tag)
Set the tag displayed along with an assertion failure's
other state information.
|
protected final CountDownLatch done
protected long completions
protected Thread lastThread
protected boolean checkSubscriptionOnce
protected CharSequence tag
protected boolean timeout
public final List<T> values()
Note that accessing the items via certain methods of the List
interface while the upstream is still actively emitting
more items may result in a ConcurrentModificationException
.
The 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 List.get(int)
) method
(index range of 0 to List.size() - 1
).
A view of the returned List can be created via List.subList(int, int)
by using the bounds 0 (inclusive) to List.size()
(exclusive) which,
when accessed in a read-only fashion, should be also thread-safe and not throw any
ConcurrentModificationException
.
protected final AssertionError fail(String message)
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.
message
- the message to usepublic final U await() throws InterruptedException
InterruptedException
- if the current thread is interrupted while waitingpublic final boolean await(long time, TimeUnit unit) throws InterruptedException
time
- the waiting timeunit
- the time unit of the waiting timeInterruptedException
- if the current thread is interrupted while waitingpublic final U assertComplete()
public final U assertNotComplete()
public final U assertNoErrors()
public final U assertError(Throwable error)
The comparison is performed via Objects.equals(); since most exceptions don't
implement equals(), this assertion may fail. Use the assertError(Class)
overload to test against the class of an error instead of an instance of an error
or assertError(Predicate)
to test with different condition.
error
- the error to checkassertError(Class)
,
assertError(Predicate)
public final U assertError(Class<? extends Throwable> errorClass)
errorClass
- the error class to expectpublic final U assertError(Predicate<Throwable> errorPredicate)
errorPredicate
- the predicate that receives the error Throwable
and should return true for expected errors.public final U assertValue(T value)
value
- the value to expectpublic final U assertValue(Predicate<T> valuePredicate)
valuePredicate
- the predicate that receives the onNext value
and should return true for the expected value.public final U assertValueAt(int index, T value)
History: 2.1.3 - experimental
index
- the position to assert onvalue
- the value to expectpublic final U assertValueAt(int index, Predicate<T> valuePredicate)
index
- the position to assert onvaluePredicate
- the predicate that receives the onNext value
and should return true for the expected value.public static String valueAndClass(Object o)
o
- the objectpublic final U assertValueCount(int count)
count
- the expected number of onNext eventspublic final U assertNoValues()
public final U assertValues(T... values)
values
- the values expectedpublic final U assertValuesOnly(T... values)
History: 2.1.4 - experimental
values
- the values expectedpublic final U assertValueSequence(Iterable<? extends T> sequence)
sequence
- the sequence of expected values in orderprotected abstract U assertSubscribed()
public final U assertResult(T... values)
values
- the expected values, asserted in orderassertFailure(Class, Object...)
public final U assertFailure(Class<? extends Throwable> error, T... values)
error
- the expected exception (parent) classvalues
- the expected values, asserted in orderpublic final U awaitDone(long time, TimeUnit unit)
If the wait times out or gets interrupted, the TestObserver/TestSubscriber is cancelled.
time
- the waiting timeunit
- the time unit of the waiting timeRuntimeException
- wrapping an InterruptedException if the wait is interruptedpublic final U assertEmpty()
public final U withTag(CharSequence tag)
History: 2.0.7 - experimental
tag
- the string to display (null won't print any tag)public final U awaitCount(int atLeast)
History: 2.0.7 - experimental
atLeast
- the number of items expected at leastprotected abstract boolean isDisposed()
protected abstract void dispose()