Package io.reactivex.rxjava3.subscribers
Class TestSubscriber<T>
java.lang.Object
io.reactivex.rxjava3.observers.BaseTestConsumer<T,TestSubscriber<T>>
io.reactivex.rxjava3.subscribers.TestSubscriber<T>
- Type Parameters:
T- the value type
- All Implemented Interfaces:
FlowableSubscriber<T>,org.reactivestreams.Subscriber<T>,org.reactivestreams.Subscription
public class TestSubscriber<T>
extends BaseTestConsumer<T,TestSubscriber<T>>
implements FlowableSubscriber<T>, org.reactivestreams.Subscription
A
Subscriber implementation that records events and allows making assertions about them.
You can override the onSubscribe(Subscription), onNext(Object), onError(Throwable) and
onComplete() methods but not the others (this is by design).
When calling the default request method, you are requesting on behalf of the
wrapped actual Subscriber if any.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enumA subscriber that ignores all events and does not report errors. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanMakes sure the incoming Subscriptions get cancelled immediately.private final org.reactivestreams.Subscriber<? super T> The actual subscriber to forward events to.private final AtomicLongHolds the requested amount until a subscription arrives.private final AtomicReference<org.reactivestreams.Subscription> Holds the current subscription if any.Fields inherited from class io.reactivex.rxjava3.observers.BaseTestConsumer
checkSubscriptionOnce, completions, done, errors, lastThread, tag, timeout, values -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a non-forwardingTestSubscriberwith an initial request value ofLong.MAX_VALUE.TestSubscriber(long initialRequest) Constructs a non-forwardingTestSubscriberwith the specified initial request value.TestSubscriber(@NonNull org.reactivestreams.Subscriber<? super T> downstream) Constructs a forwardingTestSubscriberbut leaves the requesting to the wrappedSubscriber.TestSubscriber(@NonNull org.reactivestreams.Subscriber<? super T> actual, long initialRequest) Constructs a forwardingTestSubscriberwith the specified initial request amount and an actualSubscriberto forward events to. -
Method Summary
Modifier and TypeMethodDescriptionprotected final TestSubscriber<T> Assert that theonSubscribe(Subscription)method was called exactly once.final voidcancel()static <T> @NonNull TestSubscriber<T> create()Creates aTestSubscriberwithLong.MAX_VALUEinitial request amount.static <T> @NonNull TestSubscriber<T> create(long initialRequested) Creates aTestSubscriberwith the given initial request amount.static <T> TestSubscriber<T> Constructs a forwardingTestSubscriber.protected final voiddispose()Cancel/dispose this test consumer.final booleanfinal booleanReturns true if thisTestSubscriberhas been cancelled.protected final booleanReturns true if this test consumer was cancelled/disposed.voidvoidvoidprotected voidonStart()Called after the onSubscribe is called and handled.voidonSubscribe(@NonNull org.reactivestreams.Subscription s) Implementors of this method should make sure everything that needs to be visible inSubscriber.onNext(Object)is established before callingSubscription.request(long).final voidrequest(long n) final TestSubscriber<T> requestMore(long n) Callsrequest(long)and returns this.Methods inherited from class io.reactivex.rxjava3.observers.BaseTestConsumer
assertComplete, assertEmpty, assertError, assertError, assertError, assertFailure, assertNoErrors, assertNotComplete, assertNoValues, assertResult, assertValue, assertValue, assertValueAt, assertValueAt, assertValueCount, assertValues, assertValueSequence, assertValuesOnly, await, await, awaitCount, awaitDone, fail, valueAndClass, values, withTag
-
Field Details
-
downstream
The actual subscriber to forward events to. -
cancelled
private volatile boolean cancelledMakes sure the incoming Subscriptions get cancelled immediately. -
upstream
Holds the current subscription if any. -
missedRequested
Holds the requested amount until a subscription arrives.
-
-
Constructor Details
-
TestSubscriber
public TestSubscriber()Constructs a non-forwardingTestSubscriberwith an initial request value ofLong.MAX_VALUE. -
TestSubscriber
public TestSubscriber(long initialRequest) Constructs a non-forwardingTestSubscriberwith the specified initial request value.The
TestSubscriberdoesn't validate theinitialRequestamount so one can test sources with invalid values as well.- Parameters:
initialRequest- the initial request amount
-
TestSubscriber
Constructs a forwardingTestSubscriberbut leaves the requesting to the wrappedSubscriber.- Parameters:
downstream- the actualSubscriberto forward events to
-
TestSubscriber
public TestSubscriber(@NonNull @NonNull org.reactivestreams.Subscriber<? super T> actual, long initialRequest) Constructs a forwardingTestSubscriberwith the specified initial request amount and an actualSubscriberto forward events to.The
TestSubscriberdoesn't validate the initialRequest value so one can test sources with invalid values as well.- Parameters:
actual- the actualSubscriberto forward events toinitialRequest- the initial request amount
-
-
Method Details
-
create
Creates aTestSubscriberwithLong.MAX_VALUEinitial request amount.- Type Parameters:
T- the value type- Returns:
- the new
TestSubscriberinstance. - See Also:
-
create
Creates aTestSubscriberwith the given initial request amount.- Type Parameters:
T- the value type- Parameters:
initialRequested- the initial requested amount- Returns:
- the new
TestSubscriberinstance.
-
create
public static <T> TestSubscriber<T> create(@NonNull @NonNull org.reactivestreams.Subscriber<? super T> delegate) Constructs a forwardingTestSubscriber.- Type Parameters:
T- the value type received- Parameters:
delegate- the actualSubscriberto forward events to- Returns:
- the new TestObserver instance
-
onSubscribe
Description copied from interface:FlowableSubscriberImplementors of this method should make sure everything that needs to be visible inSubscriber.onNext(Object)is established before callingSubscription.request(long). In practice this means no initialization should happen after therequest()call and additional behavior is thread safe in respect toonNext.- Specified by:
onSubscribein interfaceFlowableSubscriber<T>- Specified by:
onSubscribein interfaceorg.reactivestreams.Subscriber<T>
-
onStart
protected void onStart()Called after the onSubscribe is called and handled. -
onNext
- Specified by:
onNextin interfaceorg.reactivestreams.Subscriber<T>
-
onError
- Specified by:
onErrorin interfaceorg.reactivestreams.Subscriber<T>
-
onComplete
public void onComplete()- Specified by:
onCompletein interfaceorg.reactivestreams.Subscriber<T>
-
request
public final void request(long n) - Specified by:
requestin interfaceorg.reactivestreams.Subscription
-
cancel
public final void cancel()- Specified by:
cancelin interfaceorg.reactivestreams.Subscription
-
isCancelled
public final boolean isCancelled()Returns true if thisTestSubscriberhas been cancelled.- Returns:
- true if this
TestSubscriberhas been cancelled
-
dispose
protected final void dispose()Description copied from class:BaseTestConsumerCancel/dispose this test consumer.- Specified by:
disposein classBaseTestConsumer<T,TestSubscriber<T>>
-
isDisposed
protected final boolean isDisposed()Description copied from class:BaseTestConsumerReturns true if this test consumer was cancelled/disposed.- Specified by:
isDisposedin classBaseTestConsumer<T,TestSubscriber<T>> - Returns:
- true if this test consumer was cancelled/disposed.
-
hasSubscription
public final boolean hasSubscription()- Returns:
- true if this
TestSubscriberreceived aSubscriptionviaonSubscribe(Subscription)
-
assertSubscribed
Assert that theonSubscribe(Subscription)method was called exactly once.- Specified by:
assertSubscribedin classBaseTestConsumer<T,TestSubscriber<T>> - Returns:
- this
-
requestMore
Callsrequest(long)and returns this.History: 2.0.1 - experimental
- Parameters:
n- the request amount- Returns:
- this
- Since:
- 2.1
-