Class TestPublisherSubscriber<T>
java.lang.Object
io.servicetalk.concurrent.test.internal.TestPublisherSubscriber<T>
- Type Parameters:
T
- The type of data inonNext(Object)
.
- All Implemented Interfaces:
PublisherSource.Subscriber<T>
public final class TestPublisherSubscriber<T>
extends Object
implements PublisherSource.Subscriber<T>
A
PublisherSource.Subscriber
that enqueues onNext(Object)
and terminal signals while providing blocking methods
to consume these events. There are two approaches to using this class:
TestPublisherSubscriber<String> sub = new TestPublisherSubscriber<>(); // Approach 1 - verify individual items sequentially. String s = sub.takeOnNext(); // verify s sub.awaitOnComplete(); // this will verify that all onNext signals have been consumed // Approach 2 - wait for terminal, verify items in bulk. sub.awaitOnComplete(false); // wait for the terminal signal, ignore if there are unconsumed onNext signals. List<String> onNextSignals = sub.pollAllOnNext(); // verify all onNextSignals occurred in the expected order
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Block until a terminal signal is received, throws ifonError(Throwable)
and returns normally ifonComplete()
.Block until a terminal signal is received, throws ifonComplete()
and returns normally ifonError(Throwable)
.Block untilPublisherSource.Subscriber.onSubscribe(Subscription)
.void
Callback to signal completion of thePublisherSource
for thisSubscriber
.void
Callback to receive anerror
for thisSubscriber
.void
Callback to receive adata
element for thisSubscriber
.void
onSubscribe
(PublisherSource.Subscription subscription) Callback to receive aPublisherSource.Subscription
for thisSubscriber
.Consume all currently availableonNext(Object)
signals.pollOnNext
(long timeout, TimeUnit unit) Blocks for at mosttimeout
time until the nextonNext(Object)
method invocation.pollTerminal
(long timeout, TimeUnit unit) Block for a terminal event.Blocks until the nextonNext(Object)
method invocation.takeOnNext
(int n) Blocks untiln
onNext(Object)
method invocations.
-
Constructor Details
-
TestPublisherSubscriber
public TestPublisherSubscriber()Create a new instance.
-
-
Method Details
-
onSubscribe
Description copied from interface:PublisherSource.Subscriber
Callback to receive aPublisherSource.Subscription
for thisSubscriber
.See Reactive Streams specifications for the rules about how and when this method will be invoked.
- Specified by:
onSubscribe
in interfacePublisherSource.Subscriber<T>
- Parameters:
subscription
-PublisherSource.Subscription
for thisSubscriber
.
-
onNext
Description copied from interface:PublisherSource.Subscriber
Callback to receive adata
element for thisSubscriber
.See Reactive Streams specifications for the rules about how and when this method will be invoked.
- Specified by:
onNext
in interfacePublisherSource.Subscriber<T>
- Parameters:
t
- Adata
element.
-
onError
Description copied from interface:PublisherSource.Subscriber
Callback to receive anerror
for thisSubscriber
.See Reactive Streams specifications for the rules about how and when this method will be invoked.
- Specified by:
onError
in interfacePublisherSource.Subscriber<T>
- Parameters:
t
-error
for thisSubscriber
.
-
onComplete
public void onComplete()Description copied from interface:PublisherSource.Subscriber
Callback to signal completion of thePublisherSource
for thisSubscriber
.See Reactive Streams specifications for the rules about how and when this method will be invoked.
- Specified by:
onComplete
in interfacePublisherSource.Subscriber<T>
-
awaitSubscription
Block untilPublisherSource.Subscriber.onSubscribe(Subscription)
.- Returns:
- The
PublisherSource.Subscription
fromPublisherSource.Subscriber.onSubscribe(Subscription)
.
-
takeOnNext
Blocks until the nextonNext(Object)
method invocation.- Returns:
- item delivered to
onNext(Object)
.
-
takeOnNext
Blocks untiln
onNext(Object)
method invocations.- Parameters:
n
- The number ofonNext(Object)
to consume.- Returns:
- A
List
containingn
onNext(Object)
signals.
-
pollAllOnNext
Consume all currently availableonNext(Object)
signals.- Returns:
List
containing all currently availableonNext(Object)
signals.
-
pollOnNext
Blocks for at mosttimeout
time until the nextonNext(Object)
method invocation.- Parameters:
timeout
- The amount of time to wait.unit
- The units oftimeout
.- Returns:
- A
Supplier
that returns the signal delivered toonNext(Object)
, ornull
if a timeout occurred or a terminal signal has been received.
-
awaitOnError
Block until a terminal signal is received, throws ifonComplete()
and returns normally ifonError(Throwable)
. This method will verify that allonNext(Object)
signals have been consumed.- Returns:
- the exception received by
onError(Throwable)
.
-
awaitOnComplete
public void awaitOnComplete()Block until a terminal signal is received, throws ifonError(Throwable)
and returns normally ifonComplete()
. This method will verify that allonNext(Object)
signals have been consumed. -
pollTerminal
Block for a terminal event.- Parameters:
timeout
- The duration of time to wait.unit
- The unit of time to apply to the duration.- Returns:
null
if a the timeout expires before a terminal event is received. A non-null
Supplier
that returnsnull
ifonComplete()
, or theThrowable
fromonError(Throwable)
.
-