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
ASubscriber
implementation that records events and allows making assertions about them.You can override the
onSubscribe(Subscription)
,onNext(Object)
,onError(Throwable)
andonComplete()
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 Classes Modifier and Type Class Description (package private) static class
TestSubscriber.EmptySubscriber
A subscriber that ignores all events and does not report errors.
-
Field Summary
Fields Modifier and Type Field Description private boolean
cancelled
Makes sure the incoming Subscriptions get cancelled immediately.private org.reactivestreams.Subscriber<? super T>
downstream
The actual subscriber to forward events to.private java.util.concurrent.atomic.AtomicLong
missedRequested
Holds the requested amount until a subscription arrives.private java.util.concurrent.atomic.AtomicReference<org.reactivestreams.Subscription>
upstream
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
Constructors Constructor Description TestSubscriber()
Constructs a non-forwardingTestSubscriber
with an initial request value ofLong.MAX_VALUE
.TestSubscriber(long initialRequest)
Constructs a non-forwardingTestSubscriber
with the specified initial request value.TestSubscriber(@NonNull org.reactivestreams.Subscriber<? super T> downstream)
Constructs a forwardingTestSubscriber
but leaves the requesting to the wrappedSubscriber
.TestSubscriber(@NonNull org.reactivestreams.Subscriber<? super T> actual, long initialRequest)
Constructs a forwardingTestSubscriber
with the specified initial request amount and an actualSubscriber
to forward events to.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected TestSubscriber<T>
assertSubscribed()
Assert that theonSubscribe(Subscription)
method was called exactly once.void
cancel()
static <T> @NonNull TestSubscriber<T>
create()
Creates aTestSubscriber
withLong.MAX_VALUE
initial request amount.static <T> @NonNull TestSubscriber<T>
create(long initialRequested)
Creates aTestSubscriber
with the given initial request amount.static <T> TestSubscriber<T>
create(@NonNull org.reactivestreams.Subscriber<? super T> delegate)
Constructs a forwardingTestSubscriber
.protected void
dispose()
Cancel/dispose this test consumer.boolean
hasSubscription()
boolean
isCancelled()
Returns true if thisTestSubscriber
has been cancelled.protected boolean
isDisposed()
Returns true if this test consumer was cancelled/disposed.void
onComplete()
void
onError(@NonNull java.lang.Throwable t)
void
onNext(T t)
protected void
onStart()
Called after the onSubscribe is called and handled.void
onSubscribe(@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)
.void
request(long n)
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 Detail
-
downstream
private final org.reactivestreams.Subscriber<? super T> downstream
The actual subscriber to forward events to.
-
cancelled
private volatile boolean cancelled
Makes sure the incoming Subscriptions get cancelled immediately.
-
upstream
private final java.util.concurrent.atomic.AtomicReference<org.reactivestreams.Subscription> upstream
Holds the current subscription if any.
-
missedRequested
private final java.util.concurrent.atomic.AtomicLong missedRequested
Holds the requested amount until a subscription arrives.
-
-
Constructor Detail
-
TestSubscriber
public TestSubscriber()
Constructs a non-forwardingTestSubscriber
with an initial request value ofLong.MAX_VALUE
.
-
TestSubscriber
public TestSubscriber(long initialRequest)
Constructs a non-forwardingTestSubscriber
with the specified initial request value.The
TestSubscriber
doesn't validate theinitialRequest
amount so one can test sources with invalid values as well.- Parameters:
initialRequest
- the initial request amount
-
TestSubscriber
public TestSubscriber(@NonNull @NonNull org.reactivestreams.Subscriber<? super T> downstream)
Constructs a forwardingTestSubscriber
but leaves the requesting to the wrappedSubscriber
.- Parameters:
downstream
- the actualSubscriber
to forward events to
-
TestSubscriber
public TestSubscriber(@NonNull @NonNull org.reactivestreams.Subscriber<? super T> actual, long initialRequest)
Constructs a forwardingTestSubscriber
with the specified initial request amount and an actualSubscriber
to forward events to.The
TestSubscriber
doesn't validate the initialRequest value so one can test sources with invalid values as well.- Parameters:
actual
- the actualSubscriber
to forward events toinitialRequest
- the initial request amount
-
-
Method Detail
-
create
@NonNull public static <T> @NonNull TestSubscriber<T> create()
Creates aTestSubscriber
withLong.MAX_VALUE
initial request amount.- Type Parameters:
T
- the value type- Returns:
- the new
TestSubscriber
instance. - See Also:
create(long)
-
create
@NonNull public static <T> @NonNull TestSubscriber<T> create(long initialRequested)
Creates aTestSubscriber
with the given initial request amount.- Type Parameters:
T
- the value type- Parameters:
initialRequested
- the initial requested amount- Returns:
- the new
TestSubscriber
instance.
-
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 actualSubscriber
to forward events to- Returns:
- the new TestObserver instance
-
onSubscribe
public void onSubscribe(@NonNull @NonNull org.reactivestreams.Subscription s)
Description copied from interface:FlowableSubscriber
Implementors 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:
onSubscribe
in interfaceFlowableSubscriber<T>
- Specified by:
onSubscribe
in interfaceorg.reactivestreams.Subscriber<T>
-
onStart
protected void onStart()
Called after the onSubscribe is called and handled.
-
onNext
public void onNext(@NonNull T t)
- Specified by:
onNext
in interfaceorg.reactivestreams.Subscriber<T>
-
onError
public void onError(@NonNull @NonNull java.lang.Throwable t)
- Specified by:
onError
in interfaceorg.reactivestreams.Subscriber<T>
-
onComplete
public void onComplete()
- Specified by:
onComplete
in interfaceorg.reactivestreams.Subscriber<T>
-
request
public final void request(long n)
- Specified by:
request
in interfaceorg.reactivestreams.Subscription
-
cancel
public final void cancel()
- Specified by:
cancel
in interfaceorg.reactivestreams.Subscription
-
isCancelled
public final boolean isCancelled()
Returns true if thisTestSubscriber
has been cancelled.- Returns:
- true if this
TestSubscriber
has been cancelled
-
dispose
protected final void dispose()
Description copied from class:BaseTestConsumer
Cancel/dispose this test consumer.- Specified by:
dispose
in classBaseTestConsumer<T,TestSubscriber<T>>
-
isDisposed
protected final boolean isDisposed()
Description copied from class:BaseTestConsumer
Returns true if this test consumer was cancelled/disposed.- Specified by:
isDisposed
in classBaseTestConsumer<T,TestSubscriber<T>>
- Returns:
- true if this test consumer was cancelled/disposed.
-
hasSubscription
public final boolean hasSubscription()
- Returns:
- true if this
TestSubscriber
received aSubscription
viaonSubscribe(Subscription)
-
assertSubscribed
protected final TestSubscriber<T> assertSubscribed()
Assert that theonSubscribe(Subscription)
method was called exactly once.- Specified by:
assertSubscribed
in classBaseTestConsumer<T,TestSubscriber<T>>
- Returns:
- this
-
requestMore
public final TestSubscriber<T> requestMore(long n)
Callsrequest(long)
and returns this.History: 2.0.1 - experimental
- Parameters:
n
- the request amount- Returns:
- this
- Since:
- 2.1
-
-