- java.lang.Object
-
- io.smallrye.mutiny.helpers.test.UniAssertSubscriber<T>
-
- Type Parameters:
T- the type of the items
- All Implemented Interfaces:
ContextSupport,UniSubscriber<T>
public class UniAssertSubscriber<T> extends java.lang.Object implements UniSubscriber<T>
AUniUniSubscriberfor testing purposes that comes with useful assertion helpers.
-
-
Constructor Summary
Constructors Constructor Description UniAssertSubscriber()Create a newUniAssertSubscriberwith no upfront cancellation and an emptyContext.UniAssertSubscriber(boolean cancelled)Create a newUniAssertSubscriberwith an upfront cancellation configuration and an emptyContext.UniAssertSubscriber(Context context, boolean cancelled)Create a newUniAssertSubscriber.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description UniAssertSubscriber<T>assertCompleted()Assert that theUnihas completed.UniAssertSubscriber<T>assertFailed()Assert that theUnihas failed.UniAssertSubscriber<T>assertFailedWith(java.lang.Class<? extends java.lang.Throwable> expectedTypeOfFailure)Assert that theUnihas failed.UniAssertSubscriber<T>assertFailedWith(java.lang.Class<? extends java.lang.Throwable> expectedTypeOfFailure, java.lang.String expectedMessage)Assert that theUnihas failed.UniAssertSubscriber<T>assertItem(T expected)Assert that theUnihas received an item.UniAssertSubscriber<T>assertNotSubscribed()Assert that theUnihas not been subscribed.UniAssertSubscriber<T>assertNotTerminated()Assert that theUnihas not terminated.UniAssertSubscriber<T>assertSignalsReceivedInOrder()Assert that signals have been received in correct order.UniAssertSubscriber<T>assertSubscribed()Assert that theUnihas been subscribed.UniAssertSubscriber<T>assertTerminated()Assert that theUnihas terminated.UniAssertSubscriber<T>awaitFailure()Awaits for a failure event.UniAssertSubscriber<T>awaitFailure(java.time.Duration duration)Awaits for a failure event.UniAssertSubscriber<T>awaitFailure(java.util.function.Consumer<java.lang.Throwable> assertion)Awaits for a failure event and validate it.UniAssertSubscriber<T>awaitFailure(java.util.function.Consumer<java.lang.Throwable> assertion, java.time.Duration duration)Awaits for a failure event and validate it.UniAssertSubscriber<T>awaitItem()Awaits for an item event.UniAssertSubscriber<T>awaitItem(java.time.Duration duration)Awaits for a item event at mostduration.UniAssertSubscriber<T>awaitSubscription()Awaits for a subscription event (the subscriber receives aUniSubscriptionfrom the upstream.UniAssertSubscriber<T>awaitSubscription(java.time.Duration duration)Awaits for a subscription event (the subscriber receives aUniSubscriptionfrom the upstream.voidcancel()Cancel the subscription.Contextcontext()Provide a context.static <T> UniAssertSubscriber<T>create()Create a newUniAssertSubscriberwith no upfront cancellation and an emptyContext.static <T> UniAssertSubscriber<T>create(Context context)Create a newUniAssertSubscriberwith no upfront cancellation and aContext.java.lang.ThrowablegetFailure()Get theUnifailure, if any.TgetItem()Get theUniitem, if any.java.lang.StringgetOnFailureThreadName()Get the name of the thread that calledonFailure(Throwable), if any.java.lang.StringgetOnItemThreadName()Get the name of the thread that calledonItem(Object), if any.java.lang.StringgetOnSubscribeThreadName()Get the name of the thread that calledonSubscribe(UniSubscription), if any.java.util.List<UniSignal>getSignals()Get theUniSignalaudit trail for this subscriber.voidonFailure(java.lang.Throwable failure)Called if the computation of the item by the subscriberUnifailed.voidonItem(T item)Event handler called once the item has been computed by the subscribedUni.voidonSubscribe(UniSubscription subscription)Event handler called once the subscribedUnihas taken into account the subscription.
-
-
-
Constructor Detail
-
UniAssertSubscriber
public UniAssertSubscriber(Context context, boolean cancelled)
Create a newUniAssertSubscriber.- Parameters:
context- the subscription context, cannot benullcancelled-truewhen the subscription shall be cancelled upfront,falseotherwise
-
UniAssertSubscriber
public UniAssertSubscriber(boolean cancelled)
Create a newUniAssertSubscriberwith an upfront cancellation configuration and an emptyContext.
-
UniAssertSubscriber
public UniAssertSubscriber()
Create a newUniAssertSubscriberwith no upfront cancellation and an emptyContext.
-
-
Method Detail
-
create
public static <T> UniAssertSubscriber<T> create()
Create a newUniAssertSubscriberwith no upfront cancellation and an emptyContext.- Type Parameters:
T- the type of the item- Returns:
- a new subscriber
-
create
public static <T> UniAssertSubscriber<T> create(Context context)
Create a newUniAssertSubscriberwith no upfront cancellation and aContext.- Type Parameters:
T- the type of the item- Parameters:
context- the context, cannot benull- Returns:
- a new subscriber
-
context
public Context context()
Description copied from interface:ContextSupportProvide a context.Since calls to this method shall only be triggered when a Mutiny pipeline uses a
withContextoperator, there is no need in general for caching the context value in a field of the implementing class. Exceptions include operators that have cross-subscriber semantics such as memoizers or broadcasters.This method is expected to be called once per
withContextoperator.- Specified by:
contextin interfaceContextSupport- Returns:
- the context, must not be
null.
-
onSubscribe
public void onSubscribe(UniSubscription subscription)
Description copied from interface:UniSubscriberEvent handler called once the subscribedUnihas taken into account the subscription. TheUnihave triggered the computation of the item.IMPORTANT:
UniSubscriber.onItem(Object)andUniSubscriber.onFailure(Throwable)would not be called before the invocation of this method.- Executor: Operate on no particular executor, except if
Uni.runSubscriptionOn(Executor)has been called - Exception: Throwing an exception cancels the subscription,
UniSubscriber.onItem(Object)andUniSubscriber.onFailure(Throwable)won't be called
- Specified by:
onSubscribein interfaceUniSubscriber<T>- Parameters:
subscription- the subscription allowing to cancel the computation.
- Executor: Operate on no particular executor, except if
-
onItem
public void onItem(T item)
Description copied from interface:UniSubscriberEvent handler called once the item has been computed by the subscribedUni.IMPORTANT: this method will be only called once per subscription. If
UniSubscriber.onFailure(Throwable)is called, this method won't be called.- Executor: Operate on no particular executor, except if
Uni.emitOn(java.util.concurrent.Executor)has been called - Exception: Throwing an exception cancels the subscription.
- Specified by:
onItemin interfaceUniSubscriber<T>- Parameters:
item- the item, may benull.
- Executor: Operate on no particular executor, except if
-
onFailure
public void onFailure(java.lang.Throwable failure)
Description copied from interface:UniSubscriberCalled if the computation of the item by the subscriberUnifailed.IMPORTANT: this method will be only called once per subscription. If
UniSubscriber.onItem(Object)is called, this method won't be called.- Executor: Operate on no particular executor, except if
Uni.emitOn(java.util.concurrent.Executor)has been called - Exception: Throwing an exception cancels the subscription.
- Specified by:
onFailurein interfaceUniSubscriber<T>- Parameters:
failure- the failure, cannot benull.
- Executor: Operate on no particular executor, except if
-
awaitItem
public UniAssertSubscriber<T> awaitItem()
Awaits for an item event. It waits at mostAssertSubscriber.DEFAULT_TIMEOUT.If the timeout expired, or if a failure event is received instead of the expected completion, the check fails.
- Returns:
- this
UniAssertSubscriber
-
awaitItem
public UniAssertSubscriber<T> awaitItem(java.time.Duration duration)
Awaits for a item event at mostduration.If the timeout expired, or if a failure event is received instead of the expected completion, the check fails.
- Parameters:
duration- the duration, must not benull- Returns:
- this
UniAssertSubscriber
-
awaitFailure
public UniAssertSubscriber<T> awaitFailure()
Awaits for a failure event. It waits at mostAssertSubscriber.DEFAULT_TIMEOUT.If the timeout expired, or if an item event is received instead of the expected failure, the check fails.
- Returns:
- this
UniAssertSubscriber
-
awaitFailure
public UniAssertSubscriber<T> awaitFailure(java.util.function.Consumer<java.lang.Throwable> assertion)
Awaits for a failure event and validate it. It waits at mostAssertSubscriber.DEFAULT_TIMEOUT.If the timeout expired, or if an item event is received instead of the expected failure, the check fails. The received failure is validated using the
assertionconsumer. The code of the consumer is expected to throw anAssertionErrorto indicate that the failure didn't pass the validation. The consumer is not called if no failures are received.- Parameters:
assertion- a check validating the received failure (if any). Must not benull- Returns:
- this
UniAssertSubscriber
-
awaitFailure
public UniAssertSubscriber<T> awaitFailure(java.time.Duration duration)
Awaits for a failure event. It waits at mostduration.If the timeout expired, or if an item event is received instead of the expected failure, the check fails.
- Parameters:
duration- the max duration to wait, must not benull- Returns:
- this
UniAssertSubscriber
-
awaitFailure
public UniAssertSubscriber<T> awaitFailure(java.util.function.Consumer<java.lang.Throwable> assertion, java.time.Duration duration)
Awaits for a failure event and validate it. It waits at mostduration.If the timeout expired, or if an item event is received instead of the expected failure, the check fails. The received failure is validated using the
assertionconsumer. The code of the consumer is expected to throw anAssertionErrorto indicate that the failure didn't pass the validation. The consumer is not called if no failures are received.- Parameters:
assertion- a check validating the received failure (if any). Must not benullduration- the max duration to wait, must not benull- Returns:
- this
UniAssertSubscriber
-
awaitSubscription
public UniAssertSubscriber<T> awaitSubscription()
Awaits for a subscription event (the subscriber receives aUniSubscriptionfrom the upstream. It waits at mostAssertSubscriber.DEFAULT_TIMEOUT.If the timeout expired, the check fails.
- Returns:
- this
UniAssertSubscriber
-
awaitSubscription
public UniAssertSubscriber<T> awaitSubscription(java.time.Duration duration)
Awaits for a subscription event (the subscriber receives aUniSubscriptionfrom the upstream. It waits at mostduration.If the timeout expired, the check fails.
- Parameters:
duration- the UniAssertSubscriber, must not benull- Returns:
- this
AssertSubscriber
-
assertCompleted
public UniAssertSubscriber<T> assertCompleted()
Assert that theUnihas completed.- Returns:
- this
UniAssertSubscriber
-
assertFailed
public UniAssertSubscriber<T> assertFailed()
Assert that theUnihas failed.- Returns:
- this
UniAssertSubscriber
-
getFailure
public java.lang.Throwable getFailure()
Get theUnifailure, if any.- Returns:
- the failure or
null
-
assertItem
public UniAssertSubscriber<T> assertItem(T expected)
Assert that theUnihas received an item.- Parameters:
expected- the expected item- Returns:
- this
UniAssertSubscriber
-
assertFailedWith
public UniAssertSubscriber<T> assertFailedWith(java.lang.Class<? extends java.lang.Throwable> expectedTypeOfFailure, java.lang.String expectedMessage)
Assert that theUnihas failed.- Parameters:
expectedTypeOfFailure- the expected failure typeexpectedMessage- a message that is expected to be contained in the failure message- Returns:
- this
UniAssertSubscriber
-
assertFailedWith
public UniAssertSubscriber<T> assertFailedWith(java.lang.Class<? extends java.lang.Throwable> expectedTypeOfFailure)
Assert that theUnihas failed.- Parameters:
expectedTypeOfFailure- the expected failure type- Returns:
- this
UniAssertSubscriber
-
getOnItemThreadName
public java.lang.String getOnItemThreadName()
Get the name of the thread that calledonItem(Object), if any.- Returns:
- the thread name
-
getOnFailureThreadName
public java.lang.String getOnFailureThreadName()
Get the name of the thread that calledonFailure(Throwable), if any.- Returns:
- the thread name
-
getOnSubscribeThreadName
public java.lang.String getOnSubscribeThreadName()
Get the name of the thread that calledonSubscribe(UniSubscription), if any.- Returns:
- the thread name
-
cancel
public void cancel()
Cancel the subscription.
-
assertTerminated
public UniAssertSubscriber<T> assertTerminated()
Assert that theUnihas terminated.- Returns:
- this
UniAssertSubscriber
-
assertNotTerminated
public UniAssertSubscriber<T> assertNotTerminated()
Assert that theUnihas not terminated.- Returns:
- this
UniAssertSubscriber
-
assertSubscribed
public UniAssertSubscriber<T> assertSubscribed()
Assert that theUnihas been subscribed.- Returns:
- this
UniAssertSubscriber
-
assertNotSubscribed
public UniAssertSubscriber<T> assertNotSubscribed()
Assert that theUnihas not been subscribed.- Returns:
- this
UniAssertSubscriber
-
getSignals
public java.util.List<UniSignal> getSignals()
Get theUniSignalaudit trail for this subscriber.- Returns:
- the signals in receive order
-
assertSignalsReceivedInOrder
public UniAssertSubscriber<T> assertSignalsReceivedInOrder()
Assert that signals have been received in correct order.An example of an legal sequence would be receiving
onSubscribe -> onItem. An example of an illegal sequence would be receivingonItem -> onSubscribe.- Returns:
- this
UniAssertSubscriber
-
-