public abstract class TestPublisher<T> extends Object implements 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.
Modifier and Type | Class and Description |
---|---|
static class |
TestPublisher.Violation
Possible misbehavior for a
TestPublisher . |
PublisherProbe.DefaultPublisherProbe<T>
Constructor and Description |
---|
TestPublisher() |
Modifier and Type | Method and Description |
---|---|
abstract TestPublisher<T> |
assertCancelled()
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> |
assertNoRequestOverflow()
Asserts that this publisher has had no subscriber with request overflow.
|
abstract TestPublisher<T> |
assertNoSubscribers()
Asserts that this publisher has no subscribers.
|
abstract TestPublisher<T> |
assertNotCancelled()
Asserts that this publisher has had no cancelled subscribers.
|
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.
|
abstract TestPublisher<T> |
assertSubscribers()
Asserts that this publisher has subscribers.
|
abstract TestPublisher<T> |
assertSubscribers(int n)
Asserts that this publisher has exactly n subscribers.
|
abstract TestPublisher<T> |
complete()
Triggers
completion of this publisher. |
static <T> TestPublisher<T> |
create()
Create a standard hot
TestPublisher . |
static <T> TestPublisher<T> |
createCold()
Create a cold
TestPublisher , which can be subscribed to by multiple
subscribers. |
static <T> TestPublisher<T> |
createColdNonBuffering()
Create a cold
TestPublisher , which can be subscribed to by multiple
subscribers. |
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. |
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. |
TestPublisher<T> |
emit(T... values)
Combine emitting items and completing this publisher.
|
abstract TestPublisher<T> |
error(Throwable t)
Triggers an
error signal to the subscribers. |
abstract Flux<T> |
flux()
Convenience method to wrap this
TestPublisher to a Flux . |
abstract Mono<T> |
mono()
Convenience method to wrap this
TestPublisher to a Mono . |
abstract TestPublisher<T> |
next(T value)
Send 1
onNext signal to the subscribers. |
TestPublisher<T> |
next(T first,
T... rest)
Send 1-n
onNext signals to the subscribers. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
assertWasCancelled, assertWasNotCancelled, assertWasNotRequested, assertWasNotSubscribed, assertWasRequested, assertWasSubscribed, empty, of, subscribeCount, wasCancelled, wasRequested, wasSubscribed
public static <T> TestPublisher<T> create()
TestPublisher
.T
- the type of the publisherTestPublisher
public static <T> TestPublisher<T> createNoncompliant(TestPublisher.Violation first, TestPublisher.Violation... rest)
noncompliant
hot TestPublisher
with a given set of reactive streams spec violations that will be overlooked.T
- the type of the publisherfirst
- the first allowed TestPublisher.Violation
rest
- additional optional violationsTestPublisher
public static <T> TestPublisher<T> createCold()
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.
T
- the type of the publisherTestPublisher
public static <T> TestPublisher<T> createColdNonBuffering()
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.
T
- the type of the publisherTestPublisher
public static <T> TestPublisher<T> createColdNonCompliant(boolean errorOnOverflow, TestPublisher.Violation firstViolation, TestPublisher.Violation... otherViolations)
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.Violation
s. In addition,
its behavior when there is more data than requested
can be set via errorOnOverflow
.
T
- the type of the publishererrorOnOverflow
- whether to throw an exception if there are more values than request (true) or buffer values until request becomes available (false)TestPublisher
public abstract Flux<T> flux()
TestPublisher
to a Flux
.flux
in interface PublisherProbe<T>
Flux
version of the probe.public abstract Mono<T> mono()
TestPublisher
to a Mono
.mono
in interface PublisherProbe<T>
Mono
version of the probe.public abstract TestPublisher<T> assertMinRequested(long n)
n
.n
- the expected minimum requestTestPublisher
for chaining.public abstract TestPublisher<T> assertMaxRequested(long n)
n
. Can be Long.MAX_VALUE
in case a subscriber has made
an unbounded request.n
- the expected maximum request including Long.MAX_VALUE
TestPublisher
for chaining.public abstract TestPublisher<T> assertSubscribers()
TestPublisher
for chaining.public abstract TestPublisher<T> assertSubscribers(int n)
n
- the expected number of subscribersTestPublisher
for chaining.public abstract TestPublisher<T> assertNoSubscribers()
TestPublisher
for chaining.public abstract TestPublisher<T> assertCancelled()
TestPublisher
for chaining.public abstract TestPublisher<T> assertCancelled(int n)
n
- the expected number of subscribers to have been cancelled.TestPublisher
for chaining.public abstract TestPublisher<T> assertNotCancelled()
TestPublisher
for chaining.public abstract TestPublisher<T> assertRequestOverflow()
TestPublisher
for chaining.public abstract TestPublisher<T> assertNoRequestOverflow()
TestPublisher
for chaining.public abstract TestPublisher<T> next(@Nullable T value)
onNext
signal to the subscribers.value
- the item to emit (can be null if the relevant TestPublisher.Violation
is set)TestPublisher
for chaining.public abstract TestPublisher<T> error(Throwable t)
error
signal to the subscribers.t
- the Throwable
to triggerTestPublisher
for chaining.public abstract TestPublisher<T> complete()
completion
of this publisher.TestPublisher
for chaining.@SafeVarargs public final TestPublisher<T> next(@Nullable T first, T... rest)
onNext
signals to the subscribers.first
- the first item to emitrest
- the optional remaining items to emitTestPublisher
for chaining.next
@SafeVarargs public final TestPublisher<T> emit(T... values)
values
- the values to emit to subscribersTestPublisher
for chaining.next
,
complete