Class TestPublisher<T>

java.lang.Object
reactor.test.publisher.TestPublisher<T>
All Implemented Interfaces:
Publisher<T>, PublisherProbe<T>

public abstract class TestPublisher<T> extends Object implements Publisher<T>, PublisherProbe<T>
A Publisher that you can directly manipulate, triggering onNext, onComplete and onError events, for testing purposes. You can assert the state of the publisher using its assertXXX methods, usually inside a StepVerifier's then callback.

The TestPublisher can also be made more lenient towards the RS spec and allow "spec violations", as enumerated in TestPublisher.Violation. Use the createNoncompliant(Violation, Violation...) factory method to create such a misbehaving publisher.

TestPublisher are generally hot, directly propagating signals to currently subscribed downstreams and only replaying the first termination signal to subsequent subscribers. TestPublishers are also generally not safe to use from multiple parallel threads.

Author:
Simon Basle
  • Constructor Details

    • TestPublisher

      public TestPublisher()
  • Method Details

    • create

      public static <T> TestPublisher<T> create()
      Create a standard hot TestPublisher.
      Type Parameters:
      T - the type of the publisher
      Returns:
      the new TestPublisher
    • createNoncompliant

      public static <T> TestPublisher<T> createNoncompliant(TestPublisher.Violation first, TestPublisher.Violation... rest)
      Create a noncompliant hot TestPublisher with a given set of reactive streams spec violations that will be overlooked.
      Type Parameters:
      T - the type of the publisher
      Parameters:
      first - the first allowed TestPublisher.Violation
      rest - additional optional violations
      Returns:
      the new noncompliant TestPublisher
    • createCold

      public static <T> TestPublisher<T> createCold()
      Create a cold TestPublisher, which can be subscribed to by multiple subscribers. It buffers the next(Object) events and tracks how many elements have been seen by each subscriber in order to correctly replay the buffer.

      The publisher honors backpressure, holding off emitting newest items from the buffer if the subscriber doesn't have enough request.

      Type Parameters:
      T - the type of the publisher
      Returns:
      the new TestPublisher
    • createColdNonBuffering

      public static <T> TestPublisher<T> createColdNonBuffering()
      Create a cold TestPublisher, which can be subscribed to by multiple subscribers. It buffers the next(Object) events and tracks how many elements have been seen by each subscriber in order to correctly replay the buffer.

      The returned publisher will emit an overflow error if a new subscriber's first request is lower than the current buffer size, or if a new element is pushed to a registered subscriber that has zero pending demand.

      Type Parameters:
      T - the type of the publisher
      Returns:
      the new TestPublisher
    • createColdNonCompliant

      public static <T> TestPublisher<T> createColdNonCompliant(boolean errorOnOverflow, TestPublisher.Violation firstViolation, TestPublisher.Violation... otherViolations)
      Create a cold TestPublisher, which can be subscribed to by multiple subscribers. It buffers the next(Object) events and tracks how many elements have been seen by each subscriber in order to correctly replay the buffer.

      The returned publisher will be non-compliant to the spec according to one or more TestPublisher.Violations. In addition, its behavior when there is more data than requested can be set via errorOnOverflow.

      Type Parameters:
      T - the type of the publisher
      Parameters:
      errorOnOverflow - whether to throw an exception if there are more values than request (true) or buffer values until request becomes available (false)
      Returns:
      the new TestPublisher
    • flux

      public abstract Flux<T> flux()
      Convenience method to wrap this TestPublisher to a Flux.
      Specified by:
      flux in interface PublisherProbe<T>
      Returns:
      a Flux version of the probe.
    • mono

      public abstract Mono<T> mono()
      Convenience method to wrap this TestPublisher to a Mono.
      Specified by:
      mono in interface PublisherProbe<T>
      Returns:
      a Mono version of the probe.
    • assertMinRequested

      public abstract TestPublisher<T> assertMinRequested(long n)
      Assert that the current minimum request of all this publisher's subscribers is >= n.
      Parameters:
      n - the expected minimum request
      Returns:
      this TestPublisher for chaining.
    • assertMaxRequested

      public abstract TestPublisher<T> assertMaxRequested(long n)
      Assert that the current maximum request of all this publisher's subscribers is <= n. Can be Long.MAX_VALUE in case a subscriber has made an unbounded request.
      Parameters:
      n - the expected maximum request including Long.MAX_VALUE
      Returns:
      this TestPublisher for chaining.
    • assertSubscribers

      public abstract TestPublisher<T> assertSubscribers()
      Asserts that this publisher has subscribers.
      Returns:
      this TestPublisher for chaining.
    • assertSubscribers

      public abstract TestPublisher<T> assertSubscribers(int n)
      Asserts that this publisher has exactly n subscribers.
      Parameters:
      n - the expected number of subscribers
      Returns:
      this TestPublisher for chaining.
    • assertNoSubscribers

      public abstract TestPublisher<T> assertNoSubscribers()
      Asserts that this publisher has no subscribers.
      Returns:
      this TestPublisher for chaining.
    • assertCancelled

      public abstract TestPublisher<T> assertCancelled()
      Asserts that this publisher has had at least one subscriber that has been cancelled.
      Returns:
      this TestPublisher for chaining.
    • assertCancelled

      public abstract TestPublisher<T> assertCancelled(int n)
      Asserts that this publisher has had at least n subscribers that have been cancelled.
      Parameters:
      n - the expected number of subscribers to have been cancelled.
      Returns:
      this TestPublisher for chaining.
    • assertNotCancelled

      public abstract TestPublisher<T> assertNotCancelled()
      Asserts that this publisher has had no cancelled subscribers.
      Returns:
      this TestPublisher for chaining.
    • assertRequestOverflow

      public abstract TestPublisher<T> assertRequestOverflow()
      Asserts that this publisher has had subscriber that saw request overflow, that is received an onNext event despite having a requested amount of 0 at the time.
      Returns:
      this TestPublisher for chaining.
    • assertNoRequestOverflow

      public abstract TestPublisher<T> assertNoRequestOverflow()
      Asserts that this publisher has had no subscriber with request overflow. Request overflow is receiving an onNext event despite having a requested amount of 0 at that time.
      Returns:
      this TestPublisher for chaining.
    • next

      public abstract TestPublisher<T> next(@Nullable T value)
      Send 1 onNext signal to the subscribers.
      Parameters:
      value - the item to emit (can be null if the relevant TestPublisher.Violation is set)
      Returns:
      this TestPublisher for chaining.
    • error

      public abstract TestPublisher<T> error(Throwable t)
      Triggers an error signal to the subscribers.
      Parameters:
      t - the Throwable to trigger
      Returns:
      this TestPublisher for chaining.
    • complete

      public abstract TestPublisher<T> complete()
      Triggers completion of this publisher.
      Returns:
      this TestPublisher for chaining.
    • next

      @SafeVarargs public final TestPublisher<T> next(@Nullable T first, T... rest)
      Send 1-n onNext signals to the subscribers.
      Parameters:
      first - the first item to emit
      rest - the optional remaining items to emit
      Returns:
      this TestPublisher for chaining.
      See Also:
    • emit

      @SafeVarargs public final TestPublisher<T> emit(T... values)
      Combine emitting items and completing this publisher.
      Parameters:
      values - the values to emit to subscribers
      Returns:
      this TestPublisher for chaining.
      See Also: