Class UniAssertSubscriber<T>
- 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>
AUni
UniSubscriber
for testing purposes that comes with useful assertion helpers.
-
-
Constructor Summary
Constructors Constructor Description UniAssertSubscriber()
Create a newUniAssertSubscriber
with no upfront cancellation and an emptyContext
.UniAssertSubscriber(boolean cancelled)
Create a newUniAssertSubscriber
with 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 Deprecated Methods Modifier and Type Method Description UniAssertSubscriber<T>
assertCompleted()
Assert that theUni
has completed.UniAssertSubscriber<T>
assertFailed()
Assert that theUni
has failed.UniAssertSubscriber<T>
assertFailedWith(java.lang.Class<? extends java.lang.Throwable> expectedTypeOfFailure)
Assert that theUni
has failed.UniAssertSubscriber<T>
assertFailedWith(java.lang.Class<? extends java.lang.Throwable> expectedTypeOfFailure, java.lang.String expectedMessage)
Assert that theUni
has failed.UniAssertSubscriber<T>
assertItem(T expected)
Assert that theUni
has received an item.UniAssertSubscriber<T>
assertNotSubscribed()
Assert that theUni
has not been subscribed.UniAssertSubscriber<T>
assertNotTerminated()
Assert that theUni
has not terminated.UniAssertSubscriber<T>
assertSignalsReceivedInOrder()
Assert that signals have been received in correct order.UniAssertSubscriber<T>
assertSubscribed()
Assert that theUni
has been subscribed.UniAssertSubscriber<T>
assertTerminated()
Assert that theUni
has terminated.UniAssertSubscriber<T>
await()
Deprecated.UseawaitFailure()
orawaitItem()
instead.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 aUniSubscription
from the upstream.UniAssertSubscriber<T>
awaitSubscription(java.time.Duration duration)
Awaits for a subscription event (the subscriber receives aUniSubscription
from the upstream.void
cancel()
Cancel the subscription.Context
context()
Provide a context.static <T> UniAssertSubscriber<T>
create()
Create a newUniAssertSubscriber
with no upfront cancellation and an emptyContext
.static <T> UniAssertSubscriber<T>
create(Context context)
Create a newUniAssertSubscriber
with no upfront cancellation and aContext
.java.lang.Throwable
getFailure()
Get theUni
failure, if any.T
getItem()
Get theUni
item, if any.java.lang.String
getOnFailureThreadName()
Get the name of the thread that calledonFailure(Throwable)
, if any.java.lang.String
getOnItemThreadName()
Get the name of the thread that calledonItem(Object)
, if any.java.lang.String
getOnSubscribeThreadName()
Get the name of the thread that calledonSubscribe(UniSubscription)
, if any.java.util.List<UniSignal>
getSignals()
Get theUniSignal
audit trail for this subscriber.void
onFailure(java.lang.Throwable failure)
Called if the computation of the item by the subscriberUni
failed.void
onItem(T item)
Event handler called once the item has been computed by the subscribedUni
.void
onSubscribe(UniSubscription subscription)
Event handler called once the subscribedUni
has taken into account the subscription.
-
-
-
Constructor Detail
-
UniAssertSubscriber
public UniAssertSubscriber(Context context, boolean cancelled)
Create a newUniAssertSubscriber
.- Parameters:
context
- the subscription context, cannot benull
cancelled
-true
when the subscription shall be cancelled upfront,false
otherwise
-
UniAssertSubscriber
public UniAssertSubscriber(boolean cancelled)
Create a newUniAssertSubscriber
with an upfront cancellation configuration and an emptyContext
.
-
UniAssertSubscriber
public UniAssertSubscriber()
Create a newUniAssertSubscriber
with no upfront cancellation and an emptyContext
.
-
-
Method Detail
-
create
public static <T> UniAssertSubscriber<T> create()
Create a newUniAssertSubscriber
with 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 newUniAssertSubscriber
with 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:ContextSupport
Provide a context.Since calls to this method shall only be triggered when a Mutiny pipeline uses a
withContext
operator, 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
withContext
operator.- Specified by:
context
in interfaceContextSupport
- Returns:
- the context, must not be
null
.
-
onSubscribe
public void onSubscribe(UniSubscription subscription)
Description copied from interface:UniSubscriber
Event handler called once the subscribedUni
has taken into account the subscription. TheUni
have 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:
onSubscribe
in 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:UniSubscriber
Event handler called once the item has been computed by the subscribedUni
. IMPORTANT: this method will be only called once per subscription. IfUniSubscriber.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:
onItem
in 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:UniSubscriber
Called if the computation of the item by the subscriberUni
failed. IMPORTANT: this method will be only called once per subscription. IfUniSubscriber.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:
onFailure
in 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
assertion
consumer. The code of the consumer is expected to throw anAssertionError
to 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
assertion
consumer. The code of the consumer is expected to throw anAssertionError
to 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
duration
- the max duration to wait, must not benull
- Returns:
- this
UniAssertSubscriber
-
awaitSubscription
public UniAssertSubscriber<T> awaitSubscription()
Awaits for a subscription event (the subscriber receives aUniSubscription
from 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 aUniSubscription
from the upstream. It waits at mostduration
.If the timeout expired, the check fails.
- Parameters:
duration
- the UniAssertSubscriber, must not benull
- Returns:
- this
AssertSubscriber
-
await
@Deprecated public UniAssertSubscriber<T> await()
Deprecated.UseawaitFailure()
orawaitItem()
instead.Await for termination.- Returns:
- this
UniAssertSubscriber
-
assertCompleted
public UniAssertSubscriber<T> assertCompleted()
Assert that theUni
has completed.- Returns:
- this
UniAssertSubscriber
-
assertFailed
public UniAssertSubscriber<T> assertFailed()
Assert that theUni
has failed.- Returns:
- this
UniAssertSubscriber
-
getFailure
public java.lang.Throwable getFailure()
Get theUni
failure, if any.- Returns:
- the failure or
null
-
assertItem
public UniAssertSubscriber<T> assertItem(T expected)
Assert that theUni
has 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 theUni
has 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 theUni
has 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 theUni
has terminated.- Returns:
- this
UniAssertSubscriber
-
assertNotTerminated
public UniAssertSubscriber<T> assertNotTerminated()
Assert that theUni
has not terminated.- Returns:
- this
UniAssertSubscriber
-
assertSubscribed
public UniAssertSubscriber<T> assertSubscribed()
Assert that theUni
has been subscribed.- Returns:
- this
UniAssertSubscriber
-
assertNotSubscribed
public UniAssertSubscriber<T> assertNotSubscribed()
Assert that theUni
has not been subscribed.- Returns:
- this
UniAssertSubscriber
-
getSignals
public java.util.List<UniSignal> getSignals()
Get theUniSignal
audit 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
-
-