Class TestPublisher<T>
- All Implemented Interfaces:
Publisher<T>,PublisherProbe<T>
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
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface reactor.test.publisher.PublisherProbe
PublisherProbe.DefaultPublisherProbe<T> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract TestPublisher<T>Asserts that this publisher has had at least one subscriber that has been cancelled.abstract TestPublisher<T>assertCancelled(int n) Asserts that this publisher has had at least n subscribers that have been cancelled.abstract TestPublisher<T>assertMaxRequested(long n) Assert that the current maximum request of all this publisher's subscribers is <=n.abstract TestPublisher<T>assertMinRequested(long n) Assert that the current minimum request of all this publisher's subscribers is >=n.abstract TestPublisher<T>Asserts that this publisher has had no subscriber with request overflow.abstract TestPublisher<T>Asserts that this publisher has no subscribers.abstract TestPublisher<T>Asserts that this publisher has had no cancelled subscribers.abstract TestPublisher<T>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.abstract TestPublisher<T>Asserts that this publisher has subscribers.abstract TestPublisher<T>assertSubscribers(int n) Asserts that this publisher has exactly n subscribers.abstract TestPublisher<T>complete()Triggerscompletionof this publisher.static <T> TestPublisher<T>create()Create a standard hotTestPublisher.static <T> TestPublisher<T>Create a coldTestPublisher, which can be subscribed to by multiple subscribers.static <T> TestPublisher<T>Create a coldTestPublisher, which can be subscribed to by multiple subscribers.static <T> TestPublisher<T>createColdNonCompliant(boolean errorOnOverflow, TestPublisher.Violation firstViolation, TestPublisher.Violation... otherViolations) Create a coldTestPublisher, which can be subscribed to by multiple subscribers.static <T> TestPublisher<T>createNoncompliant(TestPublisher.Violation first, TestPublisher.Violation... rest) Create anoncomplianthotTestPublisherwith a given set of reactive streams spec violations that will be overlooked.final TestPublisher<T>Combine emitting items and completing this publisher.abstract TestPublisher<T>Triggers anerrorsignal to the subscribers.flux()Convenience method to wrap thisTestPublisherto aFlux.mono()Convenience method to wrap thisTestPublisherto aMono.abstract TestPublisher<T>Send 1onNextsignal to the subscribers.final TestPublisher<T>Send 1-nonNextsignals to the subscribers.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface reactor.test.publisher.PublisherProbe
assertWasCancelled, assertWasNotCancelled, assertWasNotRequested, assertWasNotSubscribed, assertWasRequested, assertWasSubscribed, subscribeCount, wasCancelled, wasRequested, wasSubscribed
-
Constructor Details
-
TestPublisher
public TestPublisher()
-
-
Method Details
-
create
Create a standard hotTestPublisher.- 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 anoncomplianthotTestPublisherwith a given set of reactive streams spec violations that will be overlooked.- Type Parameters:
T- the type of the publisher- Parameters:
first- the first allowedTestPublisher.Violationrest- additional optional violations- Returns:
- the new noncompliant
TestPublisher
-
createCold
Create a coldTestPublisher, which can be subscribed to by multiple subscribers. It buffers thenext(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
Create a coldTestPublisher, which can be subscribed to by multiple subscribers. It buffers thenext(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 coldTestPublisher, which can be subscribed to by multiple subscribers. It buffers thenext(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 thanrequestedcan be set viaerrorOnOverflow.- 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
Convenience method to wrap thisTestPublisherto aFlux.- Specified by:
fluxin interfacePublisherProbe<T>- Returns:
- a
Fluxversion of the probe.
-
mono
Convenience method to wrap thisTestPublisherto aMono.- Specified by:
monoin interfacePublisherProbe<T>- Returns:
- a
Monoversion of the probe.
-
assertMinRequested
Assert that the current minimum request of all this publisher's subscribers is >=n.- Parameters:
n- the expected minimum request- Returns:
- this
TestPublisherfor chaining.
-
assertMaxRequested
Assert that the current maximum request of all this publisher's subscribers is <=n. Can beLong.MAX_VALUEin case a subscriber has made an unbounded request.- Parameters:
n- the expected maximum request includingLong.MAX_VALUE- Returns:
- this
TestPublisherfor chaining.
-
assertSubscribers
Asserts that this publisher has subscribers.- Returns:
- this
TestPublisherfor chaining.
-
assertSubscribers
Asserts that this publisher has exactly n subscribers.- Parameters:
n- the expected number of subscribers- Returns:
- this
TestPublisherfor chaining.
-
assertNoSubscribers
Asserts that this publisher has no subscribers.- Returns:
- this
TestPublisherfor chaining.
-
assertCancelled
Asserts that this publisher has had at least one subscriber that has been cancelled.- Returns:
- this
TestPublisherfor chaining.
-
assertCancelled
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
TestPublisherfor chaining.
-
assertNotCancelled
Asserts that this publisher has had no cancelled subscribers.- Returns:
- this
TestPublisherfor chaining.
-
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
TestPublisherfor chaining.
-
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
TestPublisherfor chaining.
-
next
Send 1onNextsignal to the subscribers.- Parameters:
value- the item to emit (can be null if the relevantTestPublisher.Violationis set)- Returns:
- this
TestPublisherfor chaining.
-
error
Triggers anerrorsignal to the subscribers.- Parameters:
t- theThrowableto trigger- Returns:
- this
TestPublisherfor chaining.
-
complete
Triggerscompletionof this publisher.- Returns:
- this
TestPublisherfor chaining.
-
next
Send 1-nonNextsignals to the subscribers.- Parameters:
first- the first item to emitrest- the optional remaining items to emit- Returns:
- this
TestPublisherfor chaining. - See Also:
-
emit
Combine emitting items and completing this publisher.- Parameters:
values- the values to emit to subscribers- Returns:
- this
TestPublisherfor chaining. - See Also:
-