T
- the value type consumedU
- the subclass of this BaseTestConsumerpublic abstract class BaseTestConsumer<T,U extends BaseTestConsumer<T,U>> extends java.lang.Object implements Disposable
Modifier and Type | Field and Description |
---|---|
protected boolean |
checkSubscriptionOnce |
protected long |
completions
The number of completions.
|
protected java.util.concurrent.CountDownLatch |
done
The latch that indicates an onError or onComplete has been called.
|
protected java.util.List<java.lang.Throwable> |
errors
The list of errors received.
|
protected int |
establishedFusionMode |
protected int |
initialFusionMode |
protected java.lang.Thread |
lastThread
The last thread seen by the observer.
|
protected java.util.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/TestSubscriber has received a Disposable but no other events.
|
U |
assertError(java.lang.Class<? extends java.lang.Throwable> errorClass)
Asserts that this TestObserver/TestSubscriber received exactly one onError event which is an
instance of the specified errorClass class.
|
U |
assertError(Predicate<java.lang.Throwable> errorPredicate)
Asserts that this TestObserver/TestSubscriber received exactly one onError event for which
the provided predicate returns true.
|
U |
assertError(java.lang.Throwable error)
Assert that this TestObserver/TestSubscriber received exactly the specified onError event value.
|
U |
assertErrorMessage(java.lang.String message)
Assert that there is a single error and it has the given message.
|
U |
assertFailure(java.lang.Class<? extends java.lang.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 |
assertFailure(Predicate<java.lang.Throwable> errorPredicate,
T... values)
Assert that the upstream signalled the specified values in order and then failed
with a Throwable for which the provided predicate returns true.
|
U |
assertFailureAndMessage(java.lang.Class<? extends java.lang.Throwable> error,
java.lang.String message,
T... values)
Assert that the upstream signalled the specified values in order,
then failed with a specific class or subclass of Throwable
and with the given exact error message.
|
U |
assertNever(Predicate<? super T> valuePredicate)
Asserts that this TestObserver/TestSubscriber did not receive any onNext value for which
the provided predicate returns true.
|
U |
assertNever(T value)
Assert that this TestObserver/TestSubscriber did not receive an onNext value which is equal to
the given value with respect to Objects.equals.
|
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.
|
abstract U |
assertNotSubscribed()
Assert that the onSubscribe method hasn't been called at all.
|
U |
assertNotTerminated()
Assert that the TestObserver/TestSubscriber has not terminated (i.e., the terminal latch is still non-zero).
|
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.
|
abstract U |
assertSubscribed()
Assert that the onSubscribe method was called exactly once.
|
U |
assertTerminated()
Assert that the TestObserver/TestSubscriber terminated (i.e., the terminal latch reached zero).
|
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 |
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(java.lang.Iterable<? extends T> sequence)
Assert that the TestObserver/TestSubscriber received only the specified sequence of values in the same order.
|
U |
assertValueSet(java.util.Collection<? extends T> expected)
Assert that the TestObserver/TestSubscriber received only the specified values in any order.
|
U |
await()
Awaits until this TestObserver/TestSubscriber receives an onError or onComplete events.
|
boolean |
await(long time,
java.util.concurrent.TimeUnit unit)
Awaits the specified amount of time or until this TestObserver/TestSubscriber
receives an onError or onComplete events, whichever happens first.
|
U |
awaitDone(long time,
java.util.concurrent.TimeUnit unit)
Awaits until the internal latch is counted down.
|
boolean |
awaitTerminalEvent()
Waits until the any terminal event has been received by this TestObserver/TestSubscriber
or returns false if the wait has been interrupted.
|
boolean |
awaitTerminalEvent(long duration,
java.util.concurrent.TimeUnit unit)
Awaits the specified amount of time or until this TestObserver/TestSubscriber
receives an onError or onComplete events, whichever happens first.
|
long |
completions()
Returns the number of times onComplete was called.
|
int |
errorCount()
Returns the number of onError exceptions received.
|
java.util.List<java.lang.Throwable> |
errors()
Returns a shared list of received onError exceptions.
|
protected java.lang.AssertionError |
fail(java.lang.String message)
Fail with the given message and add the sequence of errors as suppressed ones.
|
java.util.List<java.util.List<java.lang.Object>> |
getEvents()
Returns a list of 3 other lists: the first inner list contains the plain
values received; the second list contains the potential errors
and the final list contains the potential completions as Notifications.
|
boolean |
isTerminated()
Returns true if TestObserver/TestSubscriber received any onError or onComplete events.
|
java.lang.Thread |
lastThread()
Returns the last thread which called the onXXX methods of this TestObserver/TestSubscriber.
|
static java.lang.String |
valueAndClass(java.lang.Object o)
Appends the class name to a non-null value.
|
int |
valueCount()
Returns the number of onNext values received.
|
java.util.List<T> |
values()
Returns a shared list of received onNext values.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
dispose, isDisposed
protected final java.util.concurrent.CountDownLatch done
protected final java.util.List<T> values
protected final java.util.List<java.lang.Throwable> errors
protected long completions
protected java.lang.Thread lastThread
protected boolean checkSubscriptionOnce
protected int initialFusionMode
protected int establishedFusionMode
public final java.lang.Thread lastThread()
public final java.util.List<T> values()
public final java.util.List<java.lang.Throwable> errors()
public final long completions()
public final boolean isTerminated()
public final int valueCount()
public final int errorCount()
protected final java.lang.AssertionError fail(java.lang.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 java.lang.InterruptedException
java.lang.InterruptedException
- if the current thread is interrupted while waitingawaitTerminalEvent()
public final boolean await(long time, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
time
- the waiting timeunit
- the time unit of the waiting timejava.lang.InterruptedException
- if the current thread is interrupted while waitingawaitTerminalEvent(long, TimeUnit)
public final U assertComplete()
public final U assertNotComplete()
public final U assertNoErrors()
public final U assertError(java.lang.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(java.lang.Class<? extends java.lang.Throwable> errorClass)
errorClass
- the error class to expectpublic final U assertError(Predicate<java.lang.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 assertNever(T value)
value
- the value to expect not being receivedpublic 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 assertNever(Predicate<? super T> valuePredicate)
valuePredicate
- the predicate that receives the onNext value
and should return true for the expected value.public 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 java.lang.String valueAndClass(java.lang.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 expectedassertValueSet(Collection)
public final U assertValueSet(java.util.Collection<? extends T> expected)
This helps asserting when the order of the values is not guaranteed, i.e., when merging asynchronous streams.
expected
- the collection of values expected in any orderpublic final U assertValueSequence(java.lang.Iterable<? extends T> sequence)
sequence
- the sequence of expected values in orderpublic final U assertTerminated()
public final U assertNotTerminated()
public final boolean awaitTerminalEvent()
public final boolean awaitTerminalEvent(long duration, java.util.concurrent.TimeUnit unit)
duration
- the waiting timeunit
- the time unit of the waiting timepublic final U assertErrorMessage(java.lang.String message)
message
- the message expectedpublic final java.util.List<java.util.List<java.lang.Object>> getEvents()
public abstract U assertSubscribed()
public abstract U assertNotSubscribed()
public final U assertResult(T... values)
values
- the expected values, asserted in orderassertFailure(Class, Object...)
,
assertFailure(Predicate, Object...)
,
assertFailureAndMessage(Class, String, Object...)
public final U assertFailure(java.lang.Class<? extends java.lang.Throwable> error, T... values)
error
- the expected exception (parent) classvalues
- the expected values, asserted in orderpublic final U assertFailure(Predicate<java.lang.Throwable> errorPredicate, T... values)
errorPredicate
- the predicate that receives the error Throwable
and should return true for expected errors.values
- the expected values, asserted in orderpublic final U assertFailureAndMessage(java.lang.Class<? extends java.lang.Throwable> error, java.lang.String message, T... values)
error
- the expected exception (parent) classmessage
- the expected failure messagevalues
- the expected values, asserted in orderpublic final U awaitDone(long time, java.util.concurrent.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 timejava.lang.RuntimeException
- wrapping an InterruptedException if the wait is interruptedpublic final U assertEmpty()