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 java.lang.Object implements PublisherSource.Subscriber<T>
APublisherSource.Subscriber
that enqueuesonNext(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 Constructor Description TestPublisherSubscriber()
Create a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
awaitOnComplete()
Block until a terminal signal is received, throws ifonError(Throwable)
and returns normally ifonComplete()
.java.lang.Throwable
awaitOnError()
Block until a terminal signal is received, throws ifonComplete()
and returns normally ifonError(Throwable)
.PublisherSource.Subscription
awaitSubscription()
Block untilPublisherSource.Subscriber.onSubscribe(Subscription)
.void
onComplete()
Callback to signal completion of thePublisherSource
for thisSubscriber
.void
onError(java.lang.Throwable t)
Callback to receive anerror
for thisSubscriber
.void
onNext(T t)
Callback to receive adata
element for thisSubscriber
.void
onSubscribe(PublisherSource.Subscription subscription)
Callback to receive aPublisherSource.Subscription
for thisSubscriber
.java.util.List<T>
pollAllOnNext()
Consume all currently availableonNext(Object)
signals.java.util.function.Supplier<T>
pollOnNext(long timeout, java.util.concurrent.TimeUnit unit)
Blocks for at mosttimeout
time until the nextonNext(Object)
method invocation.java.util.function.Supplier<java.lang.Throwable>
pollTerminal(long timeout, java.util.concurrent.TimeUnit unit)
Block for a terminal event.T
takeOnNext()
Blocks until the nextonNext(Object)
method invocation.java.util.List<T>
takeOnNext(int n)
Blocks untiln
onNext(Object)
method invocations.
-
-
-
Method Detail
-
onSubscribe
public void onSubscribe(PublisherSource.Subscription subscription)
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
public void onNext(@Nullable T t)
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
public void onError(java.lang.Throwable t)
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
public PublisherSource.Subscription awaitSubscription()
Block untilPublisherSource.Subscriber.onSubscribe(Subscription)
.- Returns:
- The
PublisherSource.Subscription
fromPublisherSource.Subscriber.onSubscribe(Subscription)
.
-
takeOnNext
@Nullable public T takeOnNext()
Blocks until the nextonNext(Object)
method invocation.- Returns:
- item delivered to
onNext(Object)
.
-
takeOnNext
public java.util.List<T> takeOnNext(int n)
Blocks untiln
onNext(Object)
method invocations.- Parameters:
n
- The number ofonNext(Object)
to consume.- Returns:
- A
List
containingn
onNext(Object)
signals.
-
pollAllOnNext
public java.util.List<T> pollAllOnNext()
Consume all currently availableonNext(Object)
signals.- Returns:
List
containing all currently availableonNext(Object)
signals.
-
pollOnNext
@Nullable public java.util.function.Supplier<T> pollOnNext(long timeout, java.util.concurrent.TimeUnit unit)
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
public java.lang.Throwable 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
@Nullable public java.util.function.Supplier<java.lang.Throwable> pollTerminal(long timeout, java.util.concurrent.TimeUnit unit)
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)
.
-
-