Class BaseTestConsumer<T, U extends BaseTestConsumer<T,U>>
- Type Parameters:
T- the value type consumedU- the subclass of thisBaseTestConsumer
- Direct Known Subclasses:
TestObserver, TestSubscriber
TestSubscriber and TestObserver.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected longThe number of completions.protected final CountDownLatchThe latch that indicates an onError or onComplete has been called.The list of errors received.protected ThreadThe last thread seen by the observer.protected final CountDownLatchThe latch that indicates the onSubscribe has been called and completed within this test consumer.protected CharSequenceThe optional tag associated with this test consumer.protected booleanIndicates that one of theawaitXmethod has timed out.The list of values received. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aBaseTestConsumerwithCountDownLatchset to 1. -
Method Summary
Modifier and TypeMethodDescriptionfinal UAssert that thisTestObserver/TestSubscriberreceived exactly oneonCompleteevent.final UAssert that theTestObserver/TestSubscriberhas received aDisposable/SubscriptionviaonSubscribebut no other events.final UassertError(@NonNull Predicate<Throwable> errorPredicate) Asserts that thisTestObserver/TestSubscriberreceived exactly oneonErrorevent for which the provided predicate returnstrue.final UassertError(@NonNull Class<? extends Throwable> errorClass) Asserts that thisTestObserver/TestSubscriberreceived exactly oneonErrorevent which is an instance of the specifiederrorClassClass.final UassertError(@NonNull Throwable error) Assert that thisTestObserver/TestSubscriberreceived exactly the specifiedonErrorevent value.final UassertFailure(@NonNull Class<? extends Throwable> error, T... values) Assert that the upstream signaled the specified values in order and then failed with a specific class or subclass ofThrowable.final UAssert that thisTestObserver/TestSubscriberhas not received anonErrorevent.final UAssert that thisTestObserver/TestSubscriberhas not received anonCompleteevent.final UAsserts that some awaitX method has not timed out.final UAssert that thisTestObserver/TestSubscriberhas not received anyonNextevents.final UassertResult(T... values) Assert that the upstream signaled the specified values in order and completed normally.protected abstract UAssert that theonSubscribemethod was called exactly once.final UAsserts that some awaitX method has timed out.final UassertValue(@NonNull Predicate<T> valuePredicate) Asserts that thisTestObserver/TestSubscriberreceived exactly oneonNextvalue for which the provided predicate returnstrue.final UassertValue(T value) Assert that thisTestObserver/TestSubscriberreceived exactly oneonNextvalue which is equal to the given value with respect toObjects.equals(Object, Object).final UassertValueAt(int index, @NonNull Predicate<T> valuePredicate) Asserts that thisTestObserver/TestSubscriberreceived anonNextvalue at the given index for the provided predicate returnstrue.final UassertValueAt(int index, T value) Asserts that thisTestObserver/TestSubscriberreceived anonNextvalue at the given index which is equal to the given value with respect tonull-safeObjects.equals(Object, Object).final UassertValueCount(int count) Assert that thisTestObserver/TestSubscriberreceived the specified numberonNextevents.final UassertValues(T... values) Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order.final UassertValueSequence(@NonNull Iterable<? extends T> sequence) Assert that theTestObserver/TestSubscriberreceived only the specified sequence of values in the same order.final UassertValueSet(T... values) Assert that the upstream signaled any of the given values in any order.final UassertValuesOnly(T... values) Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order without terminating.final Uawait()Wait until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents.final booleanAwaits the specified amount of time or until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents, whichever happens first.final UawaitCount(int atLeast) Wait until theTestObserver/TestSubscriberreceives the given number of items or terminates by sleeping 10 milliseconds at a time up to 5000 milliseconds of timeout.final UawaitCount(int atLeast, long timeoutMillis, long sleepMillis) Wait until theTestObserver/TestSubscriberreceives the given number of items or terminates by sleepingsleepMillisat a time up to a total oftimeoutMillis.final UWait until the internal latch is counted down.final UawaitOnSubscribe(long timeout, TimeUnit unit) Waits at most the given amount of time for the upstream to call#onSubscribe(Subscription).final UClears the timeout flag set by the await methods when they timed out.protected abstract voiddispose()Cancel/dispose this test consumer.errors()Returns a shared list of receivedonErrorerrors or the singleonErrorvalue.protected final @NonNull AssertionErrorFail with the given message and add the sequence of errors as suppressed ones.protected abstract booleanReturns true if this test consumer was cancelled/disposed.final booleanReturns true if an await timed out.toString()Appends the class name to a non-nullvalue or returns"null".values()Returns a shared list of receivedonNextvalues or the singleonSuccessvalue.final UwithTag(@Nullable CharSequence tag) Set the tag displayed along with an assertion failure's other state information.
-
Field Details
-
done
The latch that indicates an onError or onComplete has been called. -
values
-
errors
-
completions
protected long completionsThe number of completions. -
lastThread
The last thread seen by the observer. -
checkSubscriptionOnce
protected boolean checkSubscriptionOnce -
tag
-
timeout
protected boolean timeoutIndicates that one of theawaitXmethod has timed out.- Since:
- 2.0.7
-
onSubscribeReady
The latch that indicates the onSubscribe has been called and completed within this test consumer.- Since:
- 4.0.0
-
-
Constructor Details
-
BaseTestConsumer
public BaseTestConsumer()Constructs aBaseTestConsumerwithCountDownLatchset to 1.
-
-
Method Details
-
values
Returns a shared list of receivedonNextvalues or the singleonSuccessvalue.Note that accessing the items via certain methods of the
Listinterface while the upstream is still actively emitting more items may result in aConcurrentModificationException.The
List.size()method will return the number of items already received by thisTestObserver/TestSubscriberin a thread-safe manner that can be read viaList.get(int)) method (index range of 0 toList.size() - 1).A view of the returned List can be created via
List.subList(int, int)by using the bounds 0 (inclusive) toList.size()(exclusive) which, when accessed in a read-only fashion, should be also thread-safe and not throw anyConcurrentModificationException.- Returns:
- a list of received onNext values
-
errors
Returns a shared list of receivedonErrorerrors or the singleonErrorvalue.Note that accessing the items via certain methods of the
Listinterface while the upstream is still actively emitting more items may result in aConcurrentModificationException.The
List.size()method will return the number of items already received by thisTestObserver/TestSubscriberin a thread-safe manner that can be read viaList.get(int)) method (index range of 0 toList.size() - 1).A view of the returned List can be created via
List.subList(int, int)by using the bounds 0 (inclusive) toList.size()(exclusive) which, when accessed in a read-only fashion, should be also thread-safe and not throw anyConcurrentModificationException.- Returns:
- a list of received onError values
- Since:
- 4.0.0
-
fail
Fail with the given message and add the sequence of errors as suppressed ones.Note this is deliberately the only fail method. Usually 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.
- Parameters:
message- the message to use- Returns:
- AssertionError the prepared AssertionError instance
-
toString
-
await
Wait until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents.- Returns:
- this
- Throws:
InterruptedException- if the current thread is interrupted while waiting
-
await
Awaits the specified amount of time or until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents, whichever happens first.- Parameters:
time- the waiting timeunit- the time unit of the waiting time- Returns:
- true if the
TestObserver/TestSubscriberterminated, false if timeout happened - Throws:
InterruptedException- if the current thread is interrupted while waiting
-
assertComplete
-
assertNotComplete
-
assertNoErrors
-
assertError
Assert that thisTestObserver/TestSubscriberreceived exactly the specifiedonErrorevent value.The comparison is performed via
Objects.equals(Object, Object); since most exceptions don't implement equals(), this assertion may fail. Use theassertError(Class)overload to test against the class of an error instead of an instance of an error orassertError(Predicate)to test with different condition.- Parameters:
error- the error to check- Returns:
- this
- See Also:
-
assertError
-
assertError
Asserts that thisTestObserver/TestSubscriberreceived exactly oneonErrorevent for which the provided predicate returnstrue.- Parameters:
errorPredicate- the predicate that receives the errorThrowableand should returntruefor expected errors.- Returns:
- this
-
assertValue
Assert that thisTestObserver/TestSubscriberreceived exactly oneonNextvalue which is equal to the given value with respect toObjects.equals(Object, Object).- Parameters:
value- the value to expect- Returns:
- this
-
assertValue
Asserts that thisTestObserver/TestSubscriberreceived exactly oneonNextvalue for which the provided predicate returnstrue.- Parameters:
valuePredicate- the predicate that receives theonNextvalue and should returntruefor the expected value.- Returns:
- this
-
assertValueAt
Asserts that thisTestObserver/TestSubscriberreceived anonNextvalue at the given index which is equal to the given value with respect tonull-safeObjects.equals(Object, Object).History: 2.1.3 - experimental
- Parameters:
index- the position to assert onvalue- the value to expect- Returns:
- this
- Since:
- 2.2
-
assertValueAt
Asserts that thisTestObserver/TestSubscriberreceived anonNextvalue at the given index for the provided predicate returnstrue.- Parameters:
index- the position to assert onvaluePredicate- the predicate that receives theonNextvalue and should returntruefor the expected value.- Returns:
- this
-
valueAndClass
-
assertValueCount
-
assertNoValues
-
assertValues
Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order.- Parameters:
values- the values expected- Returns:
- this
-
assertValuesOnly
Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order without terminating.History: 2.1.4 - experimental
- Parameters:
values- the values expected- Returns:
- this
- Since:
- 2.2
-
assertValueSequence
-
assertSubscribed
-
assertResult
Assert that the upstream signaled the specified values in order and completed normally.- Parameters:
values- the expected values, asserted in order- Returns:
- this
- See Also:
-
assertValueSet
Assert that the upstream signaled any of the given values in any order.- Parameters:
values- the values to check in the received values list- Returns:
- this
- Since:
- 4.0.0
-
assertFailure
@SafeVarargs @NonNull public final U assertFailure(@NonNull @NonNull Class<? extends Throwable> error, @NonNull T... values) Assert that the upstream signaled the specified values in order and then failed with a specific class or subclass ofThrowable.- Parameters:
error- the expected exception (parent)Classvalues- the expected values, asserted in order- Returns:
- this
-
awaitDone
Wait until the internal latch is counted down.If the wait times out or gets interrupted, the
TestObserver/TestSubscriberis cancelled.- Parameters:
time- the waiting timeunit- the time unit of the waiting time- Returns:
- this
- Throws:
RuntimeException- wrapping anInterruptedExceptionif the wait is interrupted
-
assertEmpty
Assert that theTestObserver/TestSubscriberhas received aDisposable/SubscriptionviaonSubscribebut no other events.- Returns:
- this
-
withTag
Set the tag displayed along with an assertion failure's other state information.History: 2.0.7 - experimental
- Parameters:
tag- the string to display (nullwon't print any tag)- Returns:
- this
- Since:
- 2.1
-
awaitCount
Wait until theTestObserver/TestSubscriberreceives the given number of items or terminates by sleeping 10 milliseconds at a time up to 5000 milliseconds of timeout.History: 2.0.7 - experimental
- Parameters:
atLeast- the number of items expected at least- Returns:
- this
- Since:
- 2.1
-
awaitCount
Wait until theTestObserver/TestSubscriberreceives the given number of items or terminates by sleepingsleepMillisat a time up to a total oftimeoutMillis.- Parameters:
atLeast- the number of items expected at leasttimeoutMillis- the maximum amount to wait for the countsleepMillis- how much to sleep at once when waiting for the items- Returns:
- this
- Since:
- 4.0.0
-
isTimeout
public final boolean isTimeout()Returns true if an await timed out.- Returns:
- true if one of the timeout-based await methods has timed out.
- Since:
- 4.0.0
- See Also:
-
clearTimeout
Clears the timeout flag set by the await methods when they timed out.- Returns:
- this
- Since:
- 4.0.0
- See Also:
-
assertTimeout
Asserts that some awaitX method has timed out.- Returns:
- this
- Since:
- 4.0.0
-
assertNoTimeout
Asserts that some awaitX method has not timed out.- Returns:
- this
- Since:
- 4.0.0
-
awaitOnSubscribe
public final U awaitOnSubscribe(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException Waits at most the given amount of time for the upstream to call#onSubscribe(Subscription).- Parameters:
timeout- the amout to wait for the callunit- the time unit- Returns:
- this
- Throws:
InterruptedException- if the wait is interruptedTimeoutException- if the wait timed out on its own- Since:
- 4.0.0
-
isDisposed
protected abstract boolean isDisposed()Returns true if this test consumer was cancelled/disposed.- Returns:
- true if this test consumer was cancelled/disposed.
-
dispose
protected abstract void dispose()Cancel/dispose this test consumer.
-