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 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/viainvalid reference
SubscriptiononSubscribebut 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 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 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 UassertValuesOnly(T... values) Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order without terminating.final Uawait()Awaits until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents.final booleanAwaits the specified amount of time or until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents, whichever happens first.final UawaitCount(int atLeast) Await until theTestObserver/TestSubscriberreceives the given number of items or terminates by sleeping 10 milliseconds at a time up to 5000 milliseconds of timeout.final UAwaits until the internal latch is counted down.protected abstract voiddispose()Cancel/dispose this test consumer.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.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
-
-
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
-
fail
Fail with the given message and add the sequence of errors as suppressed ones.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.
- Parameters:
message- the message to use- Returns:
- AssertionError the prepared AssertionError instance
-
await
Awaits 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:
-
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
Awaits 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/viainvalid reference
SubscriptiononSubscribebut 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
Await 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
-
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.
-