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.Subscriberthat 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 voidawaitOnComplete()Block until a terminal signal is received, throws ifonError(Throwable)and returns normally ifonComplete().java.lang.ThrowableawaitOnError()Block until a terminal signal is received, throws ifonComplete()and returns normally ifonError(Throwable).PublisherSource.SubscriptionawaitSubscription()Block untilPublisherSource.Subscriber.onSubscribe(Subscription).voidonComplete()Callback to signal completion of thePublisherSourcefor thisSubscriber.voidonError(java.lang.Throwable t)Callback to receive anerrorfor thisSubscriber.voidonNext(T t)Callback to receive adataelement for thisSubscriber.voidonSubscribe(PublisherSource.Subscription subscription)Callback to receive aPublisherSource.Subscriptionfor 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 mosttimeouttime 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.TtakeOnNext()Blocks until the nextonNext(Object)method invocation.java.util.List<T>takeOnNext(int n)Blocks untilnonNext(Object)method invocations.
-
-
-
Method Detail
-
onSubscribe
public void onSubscribe(PublisherSource.Subscription subscription)
Description copied from interface:PublisherSource.SubscriberCallback to receive aPublisherSource.Subscriptionfor thisSubscriber.See Reactive Streams specifications for the rules about how and when this method will be invoked.
- Specified by:
onSubscribein interfacePublisherSource.Subscriber<T>- Parameters:
subscription-PublisherSource.Subscriptionfor thisSubscriber.
-
onNext
public void onNext(@Nullable T t)Description copied from interface:PublisherSource.SubscriberCallback to receive adataelement for thisSubscriber.See Reactive Streams specifications for the rules about how and when this method will be invoked.
- Specified by:
onNextin interfacePublisherSource.Subscriber<T>- Parameters:
t- Adataelement.
-
onError
public void onError(java.lang.Throwable t)
Description copied from interface:PublisherSource.SubscriberCallback to receive anerrorfor thisSubscriber.See Reactive Streams specifications for the rules about how and when this method will be invoked.
- Specified by:
onErrorin interfacePublisherSource.Subscriber<T>- Parameters:
t-errorfor thisSubscriber.
-
onComplete
public void onComplete()
Description copied from interface:PublisherSource.SubscriberCallback to signal completion of thePublisherSourcefor thisSubscriber.See Reactive Streams specifications for the rules about how and when this method will be invoked.
- Specified by:
onCompletein interfacePublisherSource.Subscriber<T>
-
awaitSubscription
public PublisherSource.Subscription awaitSubscription()
Block untilPublisherSource.Subscriber.onSubscribe(Subscription).- Returns:
- The
PublisherSource.SubscriptionfromPublisherSource.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 untilnonNext(Object)method invocations.- Parameters:
n- The number ofonNext(Object)to consume.- Returns:
- A
ListcontainingnonNext(Object)signals.
-
pollAllOnNext
public java.util.List<T> pollAllOnNext()
Consume all currently availableonNext(Object)signals.- Returns:
Listcontaining all currently availableonNext(Object)signals.
-
pollOnNext
@Nullable public java.util.function.Supplier<T> pollOnNext(long timeout, java.util.concurrent.TimeUnit unit)
Blocks for at mosttimeouttime until the nextonNext(Object)method invocation.- Parameters:
timeout- The amount of time to wait.unit- The units oftimeout.- Returns:
- A
Supplierthat returns the signal delivered toonNext(Object), ornullif 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:
nullif a the timeout expires before a terminal event is received. A non-nullSupplierthat returnsnullifonComplete(), or theThrowablefromonError(Throwable).
-
-