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.

  • Field Details

    • 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 AtomicReference<org.reactivestreams.Subscription> upstream
      Holds the current subscription if any.
    • missedRequested

      private final AtomicLong missedRequested
      Holds the requested amount until a subscription arrives.
  • Constructor Details

    • TestSubscriber

      public TestSubscriber()
      Constructs a non-forwarding TestSubscriber with an initial request value of Long.MAX_VALUE.
    • TestSubscriber

      public TestSubscriber(long initialRequest)
      Constructs a non-forwarding TestSubscriber with the specified initial request value.

      The TestSubscriber doesn't validate the initialRequest 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 forwarding TestSubscriber but leaves the requesting to the wrapped Subscriber.
      Parameters:
      downstream - the actual Subscriber to forward events to
    • TestSubscriber

      public TestSubscriber(@NonNull @NonNull org.reactivestreams.Subscriber<? super T> actual, long initialRequest)
      Constructs a forwarding TestSubscriber with the specified initial request amount and an actual Subscriber 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 actual Subscriber to forward events to
      initialRequest - the initial request amount
  • Method Details

    • create

      @NonNull public static <T> @NonNull TestSubscriber<T> create()
      Creates a TestSubscriber with Long.MAX_VALUE initial request amount.
      Type Parameters:
      T - the value type
      Returns:
      the new TestSubscriber instance.
      See Also:
    • create

      @NonNull public static <T> @NonNull TestSubscriber<T> create(long initialRequested)
      Creates a TestSubscriber 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 forwarding TestSubscriber.
      Type Parameters:
      T - the value type received
      Parameters:
      delegate - the actual Subscriber 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 in Subscriber.onNext(Object) is established before calling Subscription.request(long). In practice this means no initialization should happen after the request() call and additional behavior is thread safe in respect to onNext.
      Specified by:
      onSubscribe in interface FlowableSubscriber<T>
      Specified by:
      onSubscribe in interface org.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 interface org.reactivestreams.Subscriber<T>
    • onError

      public void onError(@NonNull @NonNull Throwable t)
      Specified by:
      onError in interface org.reactivestreams.Subscriber<T>
    • onComplete

      public void onComplete()
      Specified by:
      onComplete in interface org.reactivestreams.Subscriber<T>
    • request

      public final void request(long n)
      Specified by:
      request in interface org.reactivestreams.Subscription
    • cancel

      public final void cancel()
      Specified by:
      cancel in interface org.reactivestreams.Subscription
    • isCancelled

      public final boolean isCancelled()
      Returns true if this TestSubscriber 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 class BaseTestConsumer<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 class BaseTestConsumer<T,TestSubscriber<T>>
      Returns:
      true if this test consumer was cancelled/disposed.
    • hasSubscription

      public final boolean hasSubscription()
      Returns true if this TestSubscriber received a Subscription via onSubscribe(Subscription).
      Returns:
      true if this TestSubscriber received a Subscription via onSubscribe(Subscription)
    • assertSubscribed

      protected final TestSubscriber<T> assertSubscribed()
      Assert that the onSubscribe(Subscription) method was called exactly once.
      Specified by:
      assertSubscribed in class BaseTestConsumer<T,TestSubscriber<T>>
      Returns:
      this
    • requestMore

      public final TestSubscriber<T> requestMore(long n)
      Calls request(long) and returns this.

      History: 2.0.1 - experimental

      Parameters:
      n - the request amount
      Returns:
      this
      Since:
      2.1