Class BaseTestConsumer<T,U extends BaseTestConsumer<T,U>>
- java.lang.Object
-
- io.reactivex.rxjava3.observers.BaseTestConsumer<T,U>
-
- Type Parameters:
T- the value type consumedU- the subclass of thisBaseTestConsumer
- Direct Known Subclasses:
TestObserver,TestSubscriber
public abstract class BaseTestConsumer<T,U extends BaseTestConsumer<T,U>> extends java.lang.ObjectBase class with shared infrastructure to supportTestSubscriberandTestObserver.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleancheckSubscriptionOnceprotected longcompletionsThe number of completions.protected java.util.concurrent.CountDownLatchdoneThe latch that indicates an onError or onComplete has been called.protected java.util.List<java.lang.Throwable>errorsThe list of errors received.protected java.lang.ThreadlastThreadThe last thread seen by the observer.protected java.lang.CharSequencetagThe optional tag associated with this test consumer.protected booleantimeoutIndicates that one of theawaitXmethod has timed out.protected java.util.List<T>valuesThe list of values received.
-
Constructor Summary
Constructors Constructor Description BaseTestConsumer()Constructs aBaseTestConsumerwithCountDownLatchset to 1.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description UassertComplete()Assert that thisTestObserver/TestSubscriberreceived exactly oneonCompleteevent.UassertEmpty()Assert that theTestObserver/TestSubscriberhas received aDisposable/SubscriptionviaonSubscribebut no other events.UassertError(@NonNull Predicate<java.lang.Throwable> errorPredicate)Asserts that thisTestObserver/TestSubscriberreceived exactly oneonErrorevent for which the provided predicate returnstrue.private UassertError(@NonNull Predicate<java.lang.Throwable> errorPredicate, boolean exact)UassertError(@NonNull java.lang.Class<? extends java.lang.Throwable> errorClass)Asserts that thisTestObserver/TestSubscriberreceived exactly oneonErrorevent which is an instance of the specifiederrorClassClass.UassertError(@NonNull java.lang.Throwable error)Assert that thisTestObserver/TestSubscriberreceived exactly the specifiedonErrorevent value.UassertFailure(@NonNull java.lang.Class<? extends java.lang.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.UassertNoErrors()Assert that thisTestObserver/TestSubscriberhas not received anonErrorevent.UassertNotComplete()Assert that thisTestObserver/TestSubscriberhas not received anonCompleteevent.UassertNoValues()Assert that thisTestObserver/TestSubscriberhas not received anyonNextevents.UassertResult(@NonNull T... values)Assert that the upstream signaled the specified values in order and completed normally.protected abstract UassertSubscribed()Assert that theonSubscribemethod was called exactly once.UassertValue(@NonNull Predicate<T> valuePredicate)Asserts that thisTestObserver/TestSubscriberreceived exactly oneonNextvalue for which the provided predicate returnstrue.UassertValue(T value)Assert that thisTestObserver/TestSubscriberreceived exactly oneonNextvalue which is equal to the given value with respect toObjects.equals(Object, Object).UassertValueAt(int index, @NonNull Predicate<T> valuePredicate)Asserts that thisTestObserver/TestSubscriberreceived anonNextvalue at the given index for the provided predicate returnstrue.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).UassertValueCount(int count)Assert that thisTestObserver/TestSubscriberreceived the specified numberonNextevents.UassertValues(@NonNull T... values)Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order.UassertValueSequence(@NonNull java.lang.Iterable<? extends T> sequence)Assert that theTestObserver/TestSubscriberreceived only the specified sequence of values in the same order.UassertValuesOnly(@NonNull T... values)Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order without terminating.Uawait()Awaits until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents.booleanawait(long time, @NonNull java.util.concurrent.TimeUnit unit)Awaits the specified amount of time or until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents, whichever happens first.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.UawaitDone(long time, @NonNull java.util.concurrent.TimeUnit unit)Awaits until the internal latch is counted down.protected abstract voiddispose()Cancel/dispose this test consumer.protected @NonNull java.lang.AssertionErrorfail(@NonNull java.lang.String message)Fail with the given message and add the sequence of errors as suppressed ones.protected abstract booleanisDisposed()Returns true if this test consumer was cancelled/disposed.static @NonNull java.lang.StringvalueAndClass(@Nullable java.lang.Object o)Appends the class name to a non-nullvalue or returns"null".@NonNull java.util.List<T>values()Returns a shared list of receivedonNextvalues or the singleonSuccessvalue.UwithTag(@Nullable java.lang.CharSequence tag)Set the tag displayed along with an assertion failure's other state information.
-
-
-
Field Detail
-
done
protected final java.util.concurrent.CountDownLatch done
The latch that indicates an onError or onComplete has been called.
-
values
protected final java.util.List<T> values
The list of values received.
-
errors
protected final java.util.List<java.lang.Throwable> errors
The list of errors received.
-
completions
protected long completions
The number of completions.
-
lastThread
protected java.lang.Thread lastThread
The last thread seen by the observer.
-
checkSubscriptionOnce
protected boolean checkSubscriptionOnce
-
tag
protected java.lang.CharSequence tag
The optional tag associated with this test consumer.- Since:
- 2.0.7
-
timeout
protected boolean timeout
Indicates that one of theawaitXmethod has timed out.- Since:
- 2.0.7
-
-
Method Detail
-
values
@NonNull public final @NonNull java.util.List<T> 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
@NonNull protected final @NonNull java.lang.AssertionError fail(@NonNull @NonNull java.lang.String message)
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
@NonNull public final U await() throws java.lang.InterruptedException
Awaits until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents.- Returns:
- this
- Throws:
java.lang.InterruptedException- if the current thread is interrupted while waiting
-
await
public final boolean await(long time, @NonNull @NonNull java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionAwaits 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:
java.lang.InterruptedException- if the current thread is interrupted while waiting
-
assertComplete
@NonNull public final U assertComplete()
Assert that thisTestObserver/TestSubscriberreceived exactly oneonCompleteevent.- Returns:
- this
-
assertNotComplete
@NonNull public final U assertNotComplete()
Assert that thisTestObserver/TestSubscriberhas not received anonCompleteevent.- Returns:
- this
-
assertNoErrors
@NonNull public final U assertNoErrors()
Assert that thisTestObserver/TestSubscriberhas not received anonErrorevent.- Returns:
- this
-
assertError
@NonNull public final U assertError(@NonNull @NonNull java.lang.Throwable error)
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(Class),assertError(Predicate)
-
assertError
@NonNull public final U assertError(@NonNull @NonNull java.lang.Class<? extends java.lang.Throwable> errorClass)
Asserts that thisTestObserver/TestSubscriberreceived exactly oneonErrorevent which is an instance of the specifiederrorClassClass.- Parameters:
errorClass- the errorClassto expect- Returns:
- this
-
assertError
@NonNull public final U assertError(@NonNull @NonNull Predicate<java.lang.Throwable> errorPredicate)
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
-
assertError
@NonNull private U assertError(@NonNull @NonNull Predicate<java.lang.Throwable> errorPredicate, boolean exact)
-
assertValue
@NonNull public final U assertValue(@NonNull T value)
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
@NonNull public final U assertValue(@NonNull @NonNull Predicate<T> valuePredicate)
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
@NonNull public final U assertValueAt(int index, @NonNull 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).History: 2.1.3 - experimental
- Parameters:
index- the position to assert onvalue- the value to expect- Returns:
- this
- Since:
- 2.2
-
assertValueAt
@NonNull public final U assertValueAt(int index, @NonNull @NonNull Predicate<T> valuePredicate)
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
@NonNull public static @NonNull java.lang.String valueAndClass(@Nullable @Nullable java.lang.Object o)
Appends the class name to a non-nullvalue or returns"null".- Parameters:
o- the object- Returns:
- the string representation
-
assertValueCount
@NonNull public final U assertValueCount(int count)
Assert that thisTestObserver/TestSubscriberreceived the specified numberonNextevents.- Parameters:
count- the expected number ofonNextevents- Returns:
- this
-
assertNoValues
@NonNull public final U assertNoValues()
Assert that thisTestObserver/TestSubscriberhas not received anyonNextevents.- Returns:
- this
-
assertValues
@SafeVarargs @NonNull public final U assertValues(@NonNull @NonNull T... values)
Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order.- Parameters:
values- the values expected- Returns:
- this
-
assertValuesOnly
@SafeVarargs @NonNull public final U assertValuesOnly(@NonNull @NonNull T... values)
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
@NonNull public final U assertValueSequence(@NonNull @NonNull java.lang.Iterable<? extends T> sequence)
Assert that theTestObserver/TestSubscriberreceived only the specified sequence of values in the same order.- Parameters:
sequence- the sequence of expected values in order- Returns:
- this
-
assertSubscribed
@NonNull protected abstract U assertSubscribed()
Assert that theonSubscribemethod was called exactly once.- Returns:
- this
-
assertResult
@SafeVarargs @NonNull public final U assertResult(@NonNull @NonNull T... values)
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(Class, Object...)
-
assertFailure
@SafeVarargs @NonNull public final U assertFailure(@NonNull @NonNull java.lang.Class<? extends java.lang.Throwable> error, @NonNull @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
@NonNull public final U awaitDone(long time, @NonNull @NonNull java.util.concurrent.TimeUnit unit)
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:
java.lang.RuntimeException- wrapping anInterruptedExceptionif the wait is interrupted
-
assertEmpty
@NonNull public final U assertEmpty()
Assert that theTestObserver/TestSubscriberhas received aDisposable/SubscriptionviaonSubscribebut no other events.- Returns:
- this
-
withTag
@NonNull public final U withTag(@Nullable @Nullable java.lang.CharSequence tag)
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
@NonNull public final U awaitCount(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.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.
-
-