diff --git a/context-propagation/src/test/java/io/smallrye/mutiny/context/MultiContextPropagationTest.java b/context-propagation/src/test/java/io/smallrye/mutiny/context/MultiContextPropagationTest.java index 1215ca1ef..4c41a7cda 100644 --- a/context-propagation/src/test/java/io/smallrye/mutiny/context/MultiContextPropagationTest.java +++ b/context-propagation/src/test/java/io/smallrye/mutiny/context/MultiContextPropagationTest.java @@ -252,7 +252,7 @@ public void testOnSubscribe() { }) .subscribe().withSubscriber(AssertSubscriber.create(20)); - subscriber.await() + subscriber.awaitCompletion() .assertItems(0L, 1L, 2L, 3L, 4L); } @@ -270,7 +270,7 @@ public void testSelectWhere() { }) .subscribe().withSubscriber(AssertSubscriber.create(20)); - subscriber.await() + subscriber.awaitCompletion() .assertItems(0L, 2L, 4L); } @@ -289,8 +289,7 @@ public void testScan() { .select().first(10) .subscribe().withSubscriber(AssertSubscriber.create(10)); - subscriber.await() - .assertCompleted() + subscriber.awaitCompletion() .assertItems(0, 1, 3, 6, 10, 15, 21, 28, 36, 45); } diff --git a/documentation/src/test/java/guides/operators/BroadcastProcessorTest.java b/documentation/src/test/java/guides/operators/BroadcastProcessorTest.java index b438b47e0..36172511c 100644 --- a/documentation/src/test/java/guides/operators/BroadcastProcessorTest.java +++ b/documentation/src/test/java/guides/operators/BroadcastProcessorTest.java @@ -30,7 +30,6 @@ public void test() { // end::code[] AssertSubscriber subscriber = AssertSubscriber.create(Long.MAX_VALUE); multi.subscribe().withSubscriber(subscriber) - .await() - .assertCompleted(); + .awaitCompletion(); } } diff --git a/documentation/src/test/java/guides/operators/UnicastProcessorTest.java b/documentation/src/test/java/guides/operators/UnicastProcessorTest.java index 950631e17..237b59345 100644 --- a/documentation/src/test/java/guides/operators/UnicastProcessorTest.java +++ b/documentation/src/test/java/guides/operators/UnicastProcessorTest.java @@ -28,7 +28,7 @@ public void test() { // end::code[] AssertSubscriber subscriber = AssertSubscriber.create(Long.MAX_VALUE); multi.subscribe().withSubscriber(subscriber) - .await() + .awaitCompletion() .run(() -> assertThat(subscriber.getItems()).hasSize(1000)); } } diff --git a/implementation/src/main/java/io/smallrye/mutiny/groups/UniOnFailure.java b/implementation/src/main/java/io/smallrye/mutiny/groups/UniOnFailure.java index 15de8ad5c..197f7c956 100644 --- a/implementation/src/main/java/io/smallrye/mutiny/groups/UniOnFailure.java +++ b/implementation/src/main/java/io/smallrye/mutiny/groups/UniOnFailure.java @@ -121,7 +121,13 @@ public Uni call(Function> action) { //noinspection unchecked return (Uni) uni .onItem().failWith(ignored -> failure) - .onFailure().apply(subFailure -> new CompositeException(failure, subFailure)); + .onFailure().transform(subFailure -> { + if (subFailure != failure) { + return new CompositeException(failure, subFailure); + } else { + return subFailure; + } + }); }); } diff --git a/implementation/src/main/java/io/smallrye/mutiny/helpers/spies/MultiOnCancellationSpy.java b/implementation/src/main/java/io/smallrye/mutiny/helpers/spies/MultiOnCancellationSpy.java index ee8af0127..a57829835 100644 --- a/implementation/src/main/java/io/smallrye/mutiny/helpers/spies/MultiOnCancellationSpy.java +++ b/implementation/src/main/java/io/smallrye/mutiny/helpers/spies/MultiOnCancellationSpy.java @@ -23,4 +23,16 @@ public boolean isCancelled() { public String toString() { return "MultiOnCancellationSpy{} " + super.toString(); } + + public void assertCancelled() { + if (!isCancelled()) { + throw new AssertionError("Expected downstream cancellation, but it did not happen"); + } + } + + public void assertNotCancelled() { + if (isCancelled()) { + throw new AssertionError("Did not expect to receive a downstream cancellation"); + } + } } diff --git a/implementation/src/main/java/io/smallrye/mutiny/helpers/test/AssertSubscriber.java b/implementation/src/main/java/io/smallrye/mutiny/helpers/test/AssertSubscriber.java index 014d2bdc2..4f2a85ca8 100644 --- a/implementation/src/main/java/io/smallrye/mutiny/helpers/test/AssertSubscriber.java +++ b/implementation/src/main/java/io/smallrye/mutiny/helpers/test/AssertSubscriber.java @@ -4,28 +4,39 @@ import java.time.Duration; import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; +import java.util.NoSuchElementException; +import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; /** * A {@link io.smallrye.mutiny.Multi} {@link Subscriber} for testing purposes that comes with useful assertion helpers. - * + * * @param the type of the items */ @SuppressWarnings({ "ReactiveStreamsSubscriberImplementation" }) public class AssertSubscriber implements Subscriber { /** - * Latch waiting for the completion of failure event. + * The default timeout used by {@code await} method. */ - private final CountDownLatch latch = new CountDownLatch(1); + public static Duration DEFAULT_TIMEOUT = Duration.ofSeconds(10); + + /** + * Latch waiting for the completion or failure event. + */ + private final CountDownLatch terminal = new CountDownLatch(1); + + /** + * Latch waiting for the subscription event. + */ + private final CountDownLatch subscribed = new CountDownLatch(1); /** * The subscription received from upstream. @@ -48,7 +59,6 @@ public class AssertSubscriber implements Subscriber { private final AtomicReference failure = new AtomicReference<>(); /** - * 1 * Whether the multi completed successfully. */ private final AtomicBoolean completed = new AtomicBoolean(); @@ -84,7 +94,6 @@ public AssertSubscriber(long requested, boolean cancelled) { /** * Creates a new {@link AssertSubscriber} with 0 requested items and no upfront cancellation. - * */ public AssertSubscriber() { this(0, false); @@ -101,7 +110,7 @@ public AssertSubscriber(long requested) { /** * Creates a new {@link AssertSubscriber} with 0 requested items and no upfront cancellation. - * + * * @param the items type * @return a new subscriber */ @@ -143,9 +152,20 @@ public AssertSubscriber assertFailedWith(Class expectedT return this; } + /** + * Assert that the multi has failed. + * + * @param expectedTypeOfFailure the expected failure type + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber assertFailedWith(Class expectedTypeOfFailure) { + shouldHaveFailed(hasCompleted(), getFailure(), expectedTypeOfFailure, null); + return this; + } + /** * Assert that no item has been received yet. - * + * * @return this {@link AssertSubscriber} */ public AssertSubscriber assertHasNotReceivedAnyItem() { @@ -155,7 +175,7 @@ public AssertSubscriber assertHasNotReceivedAnyItem() { /** * Assert that the multi has been subscribed. - * + * * @return this {@link AssertSubscriber} */ public AssertSubscriber assertSubscribed() { @@ -165,7 +185,7 @@ public AssertSubscriber assertSubscribed() { /** * Assert that the multi has not been subscribed. - * + * * @return this {@link AssertSubscriber} */ public AssertSubscriber assertNotSubscribed() { @@ -174,7 +194,7 @@ public AssertSubscriber assertNotSubscribed() { } /** - * Assert that the multi has been terminated. + * Assert that the multi has been terminated, i.e. received a failure or a completion event. * * @return this {@link AssertSubscriber} */ @@ -184,7 +204,7 @@ public AssertSubscriber assertTerminated() { } /** - * Assert that the multi has not been terminated. + * Assert that the multi has not been terminated, i.e. did not received a failure or a completion event. * * @return this {@link AssertSubscriber} */ @@ -205,14 +225,361 @@ public final AssertSubscriber assertItems(T... expected) { return this; } + /** + * @return get the last received item, potentially {@code null} if no items have been received. + */ + public T getLastItem() { + if (items.isEmpty()) { + return null; + } else { + return items.get(items.size() - 1); + } + } + + /** + * Asserts that the last received item is equal to {@code expected}. + * The assertion fails if no items have been received. + * + * @param expected the expected item, must not be {@code null} + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber assertLastItem(T expected) { + shouldHaveReceived(getLastItem(), expected); + return this; + } + /** * Await for the multi to be terminated. - * Wait at most 10 seconds before failing. - * + * It waits at most {@link #DEFAULT_TIMEOUT}. + * * @return this {@link AssertSubscriber} + * @deprecated Use {@link #awaitCompletion()} or {@link #awaitFailure()} instead */ + @Deprecated public AssertSubscriber await() { - return await(Duration.ofSeconds(10)); + return await(DEFAULT_TIMEOUT); + } + + /** + * Awaits for the next item. + * If no item have been received before the default timeout, an {@link AssertionError} is thrown. + * + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitNextItem() { + return awaitNextItems(1); + } + + /** + * Awaits for the next item. + * If no item have been received before the given timeout, an {@link AssertionError} is thrown. + * + * @param duration the timeout, must not be {@code null} + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitNextItem(Duration duration) { + return awaitNextItems(1, duration); + } + + /** + * Awaits for the next {@code number} items. + * If not enough items have been received before the default timeout, an {@link AssertionError} is thrown. + * + * @param number the number of item to expect, must not be 0 or negative. + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitNextItems(int number) { + return awaitNextItems(number, DEFAULT_TIMEOUT); + } + + /** + * Awaits for the next {@code number} items. + * If not enough items have been received before the given timeout, an {@link AssertionError} is thrown. + * + * @param number the number of item to expect, must not be 0 or negative. + * @param duration the timeout, must not be {@code null} + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitNextItems(int number, Duration duration) { + if (hasCompleted() || getFailure() != null) { + if (hasCompleted()) { + throw new AssertionError("Expecting a next items, but a completion event has already being received"); + } else { + throw new AssertionError( + "Expecting a next items, but a failure event has already being received: " + getFailure()); + } + } + + awaitNextItemEvents(number, duration); + + return this; + } + + /** + * Awaits for the subscriber to receive {@code number} items in total (including the ones received after calling + * this method). + * If not enough items have been received before the default timeout, an {@link AssertionError} is thrown. + * + * @param number the number of item to expect, must not be 0 or negative. + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitItems(int number) { + return awaitItems(number, DEFAULT_TIMEOUT); + } + + /** + * Awaits for the subscriber to receive {@code number} items in total (including the ones received after calling + * this method). + * If not enough items have been received before the given timeout, an {@link AssertionError} is thrown. + * + * @param number the number of item to expect, must not be 0 or negative. + * @param duration the timeout, must not be {@code null} + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitItems(int number, Duration duration) { + if (items.size() > number) { + throw new AssertionError( + "Expected the number of items to be " + number + ", but it's already " + items.size()); + } + + if (isCancelled() || hasCompleted() || getFailure() != null) { + if (items.size() != number) { + throw new AssertionError( + "Expected the number of items to be " + number + ", but received " + items.size() + + " and we received a terminal event already"); + } + return this; + } + + awaitItemEvents(number, duration); + + return this; + } + + /** + * Awaits for a completion event. + * It waits at most {@link #DEFAULT_TIMEOUT}. + *

+ * If the timeout expired, or if a failure event is received instead of the expected completion, the check fails. + * + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitCompletion() { + return awaitCompletion(DEFAULT_TIMEOUT); + } + + /** + * Awaits for a completion event at most {@code duration}. + *

+ * If the timeout expired, or if a failure event is received instead of the expected completion, the check fails. + * + * @param duration the duration, must not be {@code null} + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitCompletion(Duration duration) { + try { + awaitEvent(terminal, duration); + } catch (TimeoutException e) { + throw new AssertionError( + "No completion (or failure) event received in the last " + duration.toMillis() + " ms"); + } + + if (completed.get()) { + return this; + } + + final Throwable throwable = failure.get(); + if (throwable != null) { + throw new AssertionError("Expected a completion event but got a failure: " + throwable); + } + + // We have been interrupted. + return this; + + } + + /** + * Awaits for a failure event. + * It waits at most {@link #DEFAULT_TIMEOUT}. + *

+ * If the timeout expired, or if a completion event is received instead of the expected failure, the check fails. + * + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitFailure() { + return awaitFailure(t -> { + }); + } + + /** + * Awaits for a failure event and validate it. + * It waits at most {@link #DEFAULT_TIMEOUT}. + *

+ * If the timeout expired, or if a completion event is received instead of the expected failure, the check fails. + * The received failure is validated using the {@code assertion} consumer. The code of the consumer is expected to + * throw an {@link AssertionError} to indicate that the failure didn't pass the validation. The consumer is not + * called if no failures are received. + * + * @param assertion a check validating the received failure (if any). Must not be {@code null} + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitFailure(Consumer assertion) { + return awaitFailure(assertion, DEFAULT_TIMEOUT); + } + + /** + * Awaits for a failure event. + * It waits at most {@code duration}. + *

+ * If the timeout expired, or if a completion event is received instead of the expected failure, the check fails. + * + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitFailure(Duration duration) { + return awaitFailure(t -> { + }, duration); + } + + /** + * Awaits for a failure event and validate it. + * It waits at most {@code duration}. + *

+ * If the timeout expired, or if a completion event is received instead of the expected failure, the check fails. + * The received failure is validated using the {@code assertion} consumer. The code of the consumer is expected to + * throw an {@link AssertionError} to indicate that the failure didn't pass the validation. The consumer is not + * called if no failures are received. + * + * @param assertion a check validating the received failure (if any). Must not be {@code null} + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitFailure(Consumer assertion, Duration duration) { + try { + awaitEvent(terminal, duration); + } catch (TimeoutException e) { + throw new AssertionError( + "No completion (or failure) event received in the last " + duration.toMillis() + " ms"); + } + + if (completed.get()) { + throw new AssertionError("Expected a failure event but got a completion event."); + } + + final Throwable throwable = failure.get(); + try { + assertion.accept(throwable); + return this; + } catch (AssertionError e) { + throw new AssertionError("Received a failure event, but that failure did not pass the validation: " + e, e); + } + + } + + /** + * Awaits for a subscription event (the subscriber receives a {@link Subscription} from the upstream. + * It waits at most {@link #DEFAULT_TIMEOUT}. + *

+ * If the timeout expired, the check fails. + * + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitSubscription() { + return awaitSubscription(DEFAULT_TIMEOUT); + } + + /** + * Awaits for a subscription event (the subscriber receives a {@link Subscription} from the upstream. + * It waits at most {@code duration}. + *

+ * If the timeout expired, the check fails. + * + * @param duration the duration, must not be {@code null} + * @return this {@link AssertSubscriber} + */ + public AssertSubscriber awaitSubscription(Duration duration) { + try { + awaitEvent(subscribed, duration); + } catch (TimeoutException e) { + throw new AssertionError( + "Expecting a subscription event in the last " + duration.toMillis() + " ms, but did not get it"); + } + return this; + } + + private void awaitEvent(CountDownLatch latch, Duration duration) throws TimeoutException { + // Are we already done? + if (latch.getCount() == 0) { + return; + } + try { + if (!latch.await(duration.toMillis(), TimeUnit.MILLISECONDS)) { + throw new TimeoutException(); + } + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + + private final List eventListeners = new CopyOnWriteArrayList<>(); + + private void awaitNextItemEvents(int number, Duration duration) { + NextItemTask task = new NextItemTask<>(number, this); + int size = items.size(); + try { + task.future().get(duration.toMillis(), TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + // Terminal event received + int received = items.size() - size; + if (isCancelled()) { + throw new AssertionError( + "Expected " + number + " items, but received a cancellation event while waiting. Only " + received + + " item(s) have been received."); + } else if (hasCompleted()) { + throw new AssertionError( + "Expected " + number + " items, but received a completion event while waiting. Only " + received + + " item(s) have been received."); + } else { + throw new AssertionError( + "Expected " + number + " items, but received a failure event while waiting: " + getFailure() + ". Only " + + received + " item(s) have been received."); + } + } catch (TimeoutException e) { + // Timeout + int received = items.size() - size; + throw new AssertionError( + "Expected " + number + " items in " + duration.toMillis() + " ms, but only received " + received + + " items."); + } + } + + private void awaitItemEvents(int expected, Duration duration) { + ItemTask task = new ItemTask<>(expected, this); + try { + task.future().get(duration.toMillis(), TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + // Terminal event received + if (isCancelled()) { + throw new AssertionError( + "Expected " + expected + " items, but received a cancellation event while waiting. Only " + items.size() + + " items have been received."); + } else if (hasCompleted()) { + throw new AssertionError( + "Expected " + expected + " items, but received a completion event while waiting. Only " + items.size() + + " items have been received."); + } else { + throw new AssertionError( + "Expected " + expected + " items, but received a failure event while waiting: " + getFailure() + + ". Only " + items.size() + " items have been received."); + } + } catch (TimeoutException e) { + // Timeout + throw new AssertionError( + "Expected " + expected + " items in " + duration.toMillis() + " ms, but only received " + + items.size() + " items."); + } } /** @@ -220,15 +587,17 @@ public AssertSubscriber await() { * * @param duration the timeout duration * @return this {@link AssertSubscriber} + * @deprecated Use {@link #awaitFailure()} or {@link #awaitCompletion()} instead */ + @Deprecated public AssertSubscriber await(Duration duration) { - if (latch.getCount() == 0) { + if (terminal.getCount() == 0) { // We are done already. return this; } try { - if (!latch.await(duration.toMillis(), TimeUnit.MILLISECONDS)) { + if (!terminal.await(duration.toMillis(), TimeUnit.MILLISECONDS)) { throw new AssertionError("Expected a terminal event before the timeout."); } } catch (InterruptedException ex) { @@ -239,19 +608,21 @@ public AssertSubscriber await(Duration duration) { /** * Cancel the subscription. - * + * * @return this {@link AssertSubscriber} */ public AssertSubscriber cancel() { shouldBeSubscribed(numberOfSubscription); subscription.get().cancel(); cancelled = true; + Event ev = new Event(null, null, false, true); + eventListeners.forEach(l -> l.accept(ev)); return this; } /** * Request items. - * + * * @param req the number of items to request. * @return this {@link AssertSubscriber} */ @@ -267,12 +638,12 @@ public AssertSubscriber request(long req) { public void onSubscribe(Subscription s) { numberOfSubscription++; subscription.set(s); + subscribed.countDown(); if (upfrontCancellation) { s.cancel(); cancelled = true; // Do not request is cancelled. return; - } if (requested.get() > 0) { s.request(requested.get()); @@ -283,18 +654,24 @@ public void onSubscribe(Subscription s) { @Override public synchronized void onNext(T t) { items.add(t); + Event ev = new Event(t, null, false, false); + eventListeners.forEach(l -> l.accept(ev)); } @Override public void onError(Throwable t) { failure.set(t); - latch.countDown(); + terminal.countDown(); + Event ev = new Event(null, t, false, false); + eventListeners.forEach(l -> l.accept(ev)); } @Override public void onComplete() { completed.set(true); - latch.countDown(); + terminal.countDown(); + Event ev = new Event(null, null, true, false); + eventListeners.forEach(l -> l.accept(ev)); } /** @@ -349,4 +726,107 @@ public boolean isCancelled() { public boolean hasCompleted() { return completed.get(); } + + private void registerListener(EventListener listener) { + eventListeners.add(listener); + } + + private void unregisterListener(EventListener listener) { + eventListeners.remove(listener); + } + + private interface EventListener extends Consumer { + } + + private static class Event { + + private final Object item; + private final Throwable failure; + private final boolean completion; + private final boolean cancellation; + + private Event(Object item, Throwable failure, boolean completion, boolean cancellation) { + this.item = item; + this.failure = failure; + this.completion = completion; + this.cancellation = cancellation; + } + + public boolean isItem() { + return item != null; + } + + public boolean isCancellation() { + return cancellation; + } + + public boolean isFailure() { + return failure != null; + } + + public boolean isCompletion() { + return completion; + } + } + + private static class NextItemTask { + + private final int expected; + private final AssertSubscriber subscriber; + + public NextItemTask(int expected, AssertSubscriber subscriber) { + this.expected = expected; + this.subscriber = subscriber; + } + + public CompletableFuture future() { + CompletableFuture future = new CompletableFuture<>(); + AtomicInteger count = new AtomicInteger(this.expected); + + EventListener listener = event -> { + if (event.isItem()) { + if (count.decrementAndGet() == 0) { + future.complete(null); + } + } else if (event.isCancellation() || event.isFailure() || event.isCompletion()) { + future.completeExceptionally( + new NoSuchElementException("Received a terminal event while waiting for items")); + } + // Else wait for timeout or next event. + }; + subscriber.registerListener(listener); + return future + .whenComplete((x, f) -> subscriber.unregisterListener(listener)); + } + } + + private static class ItemTask { + + private final int expected; + private final AssertSubscriber subscriber; + + public ItemTask(int expected, AssertSubscriber subscriber) { + this.expected = expected; + this.subscriber = subscriber; + } + + public CompletableFuture future() { + CompletableFuture future = new CompletableFuture<>(); + EventListener listener = event -> { + if (event.isItem()) { + if (subscriber.items.size() >= expected) { + future.complete(null); + } + } else if (event.isCancellation() || event.isFailure() || event.isCompletion()) { + future.completeExceptionally( + new NoSuchElementException("Received a terminal event while waiting for items")); + } + // Else wait for timeout or next event. + }; + subscriber.registerListener(listener); + return future + .whenComplete((x, f) -> subscriber.unregisterListener(listener)); + } + } + } diff --git a/implementation/src/main/java/io/smallrye/mutiny/helpers/test/AssertionHelper.java b/implementation/src/main/java/io/smallrye/mutiny/helpers/test/AssertionHelper.java index aacf28dba..3c6443ef1 100644 --- a/implementation/src/main/java/io/smallrye/mutiny/helpers/test/AssertionHelper.java +++ b/implementation/src/main/java/io/smallrye/mutiny/helpers/test/AssertionHelper.java @@ -5,6 +5,7 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.util.*; +import java.util.concurrent.CancellationException; import java.util.stream.Collectors; public class AssertionHelper { @@ -37,9 +38,17 @@ static void shouldHaveFailed(boolean completed, Throwable failure, Class expe failure, expectedFailureType, getStackTrace(failure)); } - if (expectedMessage != null && !failure.getMessage().contains(expectedMessage)) { - fail("%nReceived a failure event, but expecting:%n <%s>%nto contain:%n <%s>%nbut was:%n <%s>", - failure.getMessage(), expectedMessage, getStackTrace(failure)); + if (expectedMessage != null) { + final String msg = failure.getMessage(); + if (msg == null) { + fail("%nReceived a failure event, but expecting:%n <%s>%nto contain:%n <%s>%nbut was:%n <%s>", + msg, expectedMessage, getStackTrace(failure)); + } else { + if (!msg.contains(expectedMessage)) { + fail("%nReceived a failure event, but expecting:%n <%s>%nto contain:%n <%s>%nbut was:%n <%s>", + msg, expectedMessage, getStackTrace(failure)); + } + } } } @@ -73,6 +82,15 @@ static void shouldNotBeTerminated(boolean completed, Throwable failure) { } } + static void shouldNotBeTerminatedUni(boolean completed, Throwable failure) { + if (completed) { + fail("%nExpected no terminal event, but received a completion event."); + } else if (failure != null && !(failure instanceof CancellationException)) { + fail("%nExpected no terminal event, but received a failure event: <%s>:%n<%s>", + failure, getStackTrace(failure)); + } + } + static void shouldBeTerminated(boolean completed, Throwable failure) { if (!completed && failure == null) { fail("%nExpected a terminal event (either a completion or failure event), but didn't received any."); diff --git a/implementation/src/main/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriber.java b/implementation/src/main/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriber.java index a97d72e91..7186e4b58 100644 --- a/implementation/src/main/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriber.java +++ b/implementation/src/main/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriber.java @@ -1,11 +1,14 @@ package io.smallrye.mutiny.helpers.test; +import static io.smallrye.mutiny.helpers.test.AssertSubscriber.DEFAULT_TIMEOUT; import static io.smallrye.mutiny.helpers.test.AssertionHelper.*; +import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.concurrent.CompletableFuture; +import java.util.concurrent.*; +import java.util.function.Consumer; import io.smallrye.mutiny.subscription.UniSubscriber; import io.smallrye.mutiny.subscription.UniSubscription; @@ -17,14 +20,23 @@ */ public class UniAssertSubscriber implements UniSubscriber { private volatile boolean cancelImmediatelyOnSubscription; + + // Writable from the subscribers + private final CompletableFuture completion = new CompletableFuture<>(); + private final CompletableFuture subscribed = new CompletableFuture<>(); + + // Readable from the assertions + private final CompletableFuture hasCompleted; + private final CompletableFuture hasSubscription; + private volatile UniSubscription subscription; private volatile T item; private volatile Throwable failure; - private volatile boolean completed; - private final CompletableFuture future = new CompletableFuture<>(); + private volatile boolean hasCompletedSuccessfully; private volatile String onResultThreadName; private volatile String onErrorThreadName; private volatile String onSubscribeThreadName; + private final List signals = new ArrayList<>(4); /** @@ -33,6 +45,22 @@ public class UniAssertSubscriber implements UniSubscriber { * @param cancelled {@code true} when the subscription shall be cancelled upfront, {@code false} otherwise */ public UniAssertSubscriber(boolean cancelled) { + hasCompleted = completion.whenComplete((item, failure) -> { + if (failure == null) { + this.onResultThreadName = Thread.currentThread().getName(); + this.hasCompletedSuccessfully = true; + this.item = item; + } else { + this.onErrorThreadName = Thread.currentThread().getName(); + this.failure = failure; + } + }).toCompletableFuture(); + hasSubscription = subscribed.thenApply(s -> { + this.onSubscribeThreadName = Thread.currentThread().getName(); + this.subscription = s; + return s; + }).toCompletableFuture(); + this.cancelImmediatelyOnSubscription = cancelled; } @@ -45,7 +73,7 @@ public UniAssertSubscriber() { /** * Create a new {@link UniAssertSubscriber} with no upfront cancellation. - * + * * @param the type of the item * @return a new subscriber */ @@ -56,48 +84,195 @@ public static UniAssertSubscriber create() { @Override public synchronized void onSubscribe(UniSubscription subscription) { signals.add(new OnSubscribeUniSignal(subscription)); - onSubscribeThreadName = Thread.currentThread().getName(); + subscribed.complete(subscription); if (this.cancelImmediatelyOnSubscription) { - this.subscription = subscription; subscription.cancel(); - future.cancel(false); - return; + completion.cancel(false); } - this.subscription = subscription; } @Override public synchronized void onItem(T item) { - signals.add(new OnItemUniSignal(item)); - this.completed = true; - this.item = item; - this.onResultThreadName = Thread.currentThread().getName(); - this.future.complete(item); + signals.add(new OnItemUniSignal(item)); + this.completion.complete(item); } @Override public synchronized void onFailure(Throwable failure) { signals.add(new OnFailureUniSignal(failure)); - this.failure = failure; - this.onErrorThreadName = Thread.currentThread().getName(); - this.future.completeExceptionally(failure); + this.completion.completeExceptionally(failure); + } + + /** + * Awaits for an item event. + * It waits at most {@link AssertSubscriber#DEFAULT_TIMEOUT}. + *

+ * If the timeout expired, or if a failure event is received instead of the expected completion, the check fails. + * + * @return this {@link UniAssertSubscriber} + */ + public UniAssertSubscriber awaitItem() { + return awaitItem(DEFAULT_TIMEOUT); + } + + /** + * Awaits for a item event at most {@code duration}. + *

+ * If the timeout expired, or if a failure event is received instead of the expected completion, the check fails. + * + * @param duration the duration, must not be {@code null} + * @return this {@link UniAssertSubscriber} + */ + public UniAssertSubscriber awaitItem(Duration duration) { + try { + awaitEvent(hasCompleted, duration); + } catch (TimeoutException e) { + throw new AssertionError( + "No item (or failure) event received in the last " + duration.toMillis() + " ms"); + } + + if (failure == null) { + return this; + } else { + throw new AssertionError("Expected an item event but got a failure: " + failure); + } + } + + /** + * Awaits for a failure event. + * It waits at most {@link AssertSubscriber#DEFAULT_TIMEOUT}. + *

+ * If the timeout expired, or if an item event is received instead of the expected failure, the check fails. + * + * @return this {@link UniAssertSubscriber} + */ + public UniAssertSubscriber awaitFailure() { + return awaitFailure(t -> { + }); + } + + /** + * Awaits for a failure event and validate it. + * It waits at most {@link AssertSubscriber#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 {@code assertion} consumer. The code of the consumer is expected to + * throw an {@link AssertionError} to indicate that the failure didn't pass the validation. The consumer is not + * called if no failures are received. + * + * @param assertion a check validating the received failure (if any). Must not be {@code null} + * @return this {@link UniAssertSubscriber} + */ + public UniAssertSubscriber awaitFailure(Consumer assertion) { + return awaitFailure(assertion, DEFAULT_TIMEOUT); + } + + /** + * Awaits for a failure event. + * It waits at most {@code duration}. + *

+ * If the timeout expired, or if an item event is received instead of the expected failure, the check fails. + * + * @return this {@link UniAssertSubscriber} + */ + public UniAssertSubscriber awaitFailure(Duration duration) { + return awaitFailure(t -> { + }, duration); + } + + /** + * Awaits for a failure event and validate it. + * It waits at most {@code duration}. + *

+ * 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 {@code assertion} consumer. The code of the consumer is expected to + * throw an {@link AssertionError} to indicate that the failure didn't pass the validation. The consumer is not + * called if no failures are received. + * + * @param assertion a check validating the received failure (if any). Must not be {@code null} + * @return this {@link UniAssertSubscriber} + */ + public UniAssertSubscriber awaitFailure(Consumer assertion, Duration duration) { + try { + awaitEvent(hasCompleted, duration); + } catch (TimeoutException e) { + throw new AssertionError( + "No item (or failure) event received in the last " + duration.toMillis() + " ms"); + } + + if (failure == null) { + throw new AssertionError("Expected a failure event but got an item event: " + item); + } + + try { + assertion.accept(failure); + return this; + } catch (AssertionError e) { + throw new AssertionError("Received a failure event, but that failure did not pass the validation: " + e, e); + } + + } + + /** + * Awaits for a subscription event (the subscriber receives a {@link UniSubscription} from the upstream. + * It waits at most {@link AssertSubscriber#DEFAULT_TIMEOUT}. + *

+ * If the timeout expired, the check fails. + * + * @return this {@link UniAssertSubscriber} + */ + public UniAssertSubscriber awaitSubscription() { + return awaitSubscription(DEFAULT_TIMEOUT); + } + + /** + * Awaits for a subscription event (the subscriber receives a {@link UniSubscription} from the upstream. + * It waits at most {@code duration}. + *

+ * If the timeout expired, the check fails. + * + * @param duration the UniAssertSubscriber, must not be {@code null} + * @return this {@link AssertSubscriber} + */ + public UniAssertSubscriber awaitSubscription(Duration duration) { + try { + awaitEvent(hasSubscription, duration); + } catch (TimeoutException e) { + throw new AssertionError( + "Expecting a subscription event in the last " + duration.toMillis() + " ms, but did not get it"); + } + return this; + } + + private void awaitEvent(CompletableFuture future, Duration duration) throws TimeoutException { + // Are we already done? + if (future.isDone()) { + return; + } + try { + future.get(duration.toMillis(), TimeUnit.MILLISECONDS); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + // Completed exceptionally, but completed anyway. + } } /** * Await for termination. * * @return this {@link UniAssertSubscriber} + * @deprecated Use {@link #awaitFailure()} or {@link #awaitItem()} instead. */ + @Deprecated public UniAssertSubscriber await() { - CompletableFuture fut; - synchronized (this) { - fut = this.future; - } try { - fut.join(); - } catch (Exception e) { - // Error already caught. + awaitEvent(hasCompleted, DEFAULT_TIMEOUT); + } catch (TimeoutException e) { + throw new AssertionError("Expected item or failure event in the last " + + DEFAULT_TIMEOUT.toMillis() + " ms, but didn't get any event."); } + return this; } @@ -107,7 +282,7 @@ public UniAssertSubscriber await() { * @return this {@link UniAssertSubscriber} */ public synchronized UniAssertSubscriber assertCompleted() { - shouldHaveCompleted(completed, failure, null); + shouldHaveCompleted(hasCompletedSuccessfully, failure, null); return this; } @@ -117,7 +292,7 @@ public synchronized UniAssertSubscriber assertCompleted() { * @return this {@link UniAssertSubscriber} */ public synchronized UniAssertSubscriber assertFailed() { - shouldHaveFailed(completed, failure, null, null); + shouldHaveFailed(hasCompletedSuccessfully, failure, null, null); return this; } @@ -136,6 +311,9 @@ public synchronized T getItem() { * @return the failure or {@code null} */ public synchronized Throwable getFailure() { + if (failure instanceof CancellationException) { + return null; + } return failure; } @@ -146,7 +324,7 @@ public synchronized Throwable getFailure() { * @return this {@link UniAssertSubscriber} */ public UniAssertSubscriber assertItem(T expected) { - shouldHaveCompleted(completed, failure, null); + shouldHaveCompleted(hasCompletedSuccessfully, failure, null); shouldHaveReceived(getItem(), expected); return this; } @@ -158,8 +336,20 @@ public UniAssertSubscriber assertItem(T expected) { * @param expectedMessage a message that is expected to be contained in the failure message * @return this {@link UniAssertSubscriber} */ - public UniAssertSubscriber assertFailedWith(Class expectedTypeOfFailure, String expectedMessage) { - shouldHaveFailed(completed, failure, expectedTypeOfFailure, expectedMessage); + public UniAssertSubscriber assertFailedWith(Class expectedTypeOfFailure, + String expectedMessage) { + shouldHaveFailed(hasCompletedSuccessfully, failure, expectedTypeOfFailure, expectedMessage); + return this; + } + + /** + * Assert that the {@link io.smallrye.mutiny.Uni} has failed. + * + * @param expectedTypeOfFailure the expected failure type + * @return this {@link UniAssertSubscriber} + */ + public UniAssertSubscriber assertFailedWith(Class expectedTypeOfFailure) { + shouldHaveFailed(hasCompletedSuccessfully, failure, expectedTypeOfFailure, null); return this; } @@ -208,7 +398,7 @@ public void cancel() { * @return this {@link UniAssertSubscriber} */ public UniAssertSubscriber assertTerminated() { - shouldBeTerminated(completed, failure); + shouldBeTerminated(hasCompletedSuccessfully, failure); return this; } @@ -218,7 +408,7 @@ public UniAssertSubscriber assertTerminated() { * @return this {@link UniAssertSubscriber} */ public UniAssertSubscriber assertNotTerminated() { - shouldNotBeTerminated(completed, failure); + shouldNotBeTerminatedUni(hasCompletedSuccessfully, failure); return this; } @@ -244,7 +434,7 @@ public UniAssertSubscriber assertNotSubscribed() { /** * Get the {@link UniSignal} audit trail for this subscriber. - * + * * @return the signals in receive order */ public List getSignals() { diff --git a/implementation/src/test/java/io/smallrye/mutiny/groups/MultiBroadcastTest.java b/implementation/src/test/java/io/smallrye/mutiny/groups/MultiBroadcastTest.java index de6867127..85a142aff 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/groups/MultiBroadcastTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/groups/MultiBroadcastTest.java @@ -298,9 +298,9 @@ public void testRequests() { subscriber2.request(1000); - subscriber1.await().assertCompleted(); + subscriber1.awaitCompletion(); assertThat(subscriber1.getItems()).hasSize(1000); - subscriber2.await().assertCompleted(); + subscriber2.awaitCompletion(); assertThat(subscriber2.getItems()).hasSize(1000); } } diff --git a/implementation/src/test/java/io/smallrye/mutiny/groups/MultiDisjointTest.java b/implementation/src/test/java/io/smallrye/mutiny/groups/MultiDisjointTest.java index 3dda1bb8e..ed3ec5dfe 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/groups/MultiDisjointTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/groups/MultiDisjointTest.java @@ -1,7 +1,6 @@ package io.smallrye.mutiny.groups; import static org.assertj.core.api.Assertions.assertThat; -import static org.awaitility.Awaitility.await; import java.io.IOException; import java.time.Duration; @@ -159,16 +158,16 @@ public void testFlatMapRequestsWithEmissionOnExecutor() { .subscribe().withSubscriber(AssertSubscriber.create(0)); subscriber - .assertSubscribed() + .awaitSubscription() .assertHasNotReceivedAnyItem() .request(1); - await().until(() -> subscriber.getItems().contains("A")); - - subscriber.request(2); - await().until(() -> subscriber.getItems().contains("B") && subscriber.getItems().contains("C")); + subscriber.awaitNextItem() + .request(2) + .awaitNextItems(2) + .assertItems("A", "B", "C"); subscriber.request(100); - subscriber.await().assertCompleted(); + subscriber.awaitCompletion(); } } diff --git a/implementation/src/test/java/io/smallrye/mutiny/groups/UniMemoizeTest.java b/implementation/src/test/java/io/smallrye/mutiny/groups/UniMemoizeTest.java index a1d76f99f..3a958d613 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/groups/UniMemoizeTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/groups/UniMemoizeTest.java @@ -461,9 +461,9 @@ void testSubscribersRaceWithRandomInvalidations() { public void testDrainBlockedByAwait() { Uni uni = Uni.createFrom().item(() -> 1) .memoize().indefinitely(); - uni + assertThat(uni .onItem().transform(x -> uni.await().indefinitely()) - .subscribe().withSubscriber(UniAssertSubscriber.create()).await(); + .subscribe().withSubscriber(UniAssertSubscriber.create()).awaitItem().getItem()).isEqualTo(1); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/helpers/test/AbstractSubscriberTest.java b/implementation/src/test/java/io/smallrye/mutiny/helpers/test/AbstractSubscriberTest.java index 1bd12ce01..bc6eb5bb8 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/helpers/test/AbstractSubscriberTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/helpers/test/AbstractSubscriberTest.java @@ -139,6 +139,7 @@ public void testIsCancelledWithCancellation() { assertThat(subscriber.isCancelled()).isTrue(); } + @SuppressWarnings("deprecation") @Test public void testAwaitWithTimeout() { AssertSubscriber subscriber = AssertSubscriber.create(10); @@ -147,16 +148,22 @@ public void testAwaitWithTimeout() { assertThatThrownBy(() -> await() .pollDelay(Duration.ofMillis(1)) - .atMost(Duration.ofMillis(2)).untilAsserted(subscriber::await)).isInstanceOf(ConditionTimeoutException.class); + .atMost(Duration.ofMillis(2)).untilAsserted(subscriber::await)) + .isInstanceOf(ConditionTimeoutException.class); + + assertThatThrownBy(() -> await() + .pollDelay(Duration.ofMillis(1)) + .atMost(Duration.ofMillis(2)).untilAsserted(subscriber::awaitCompletion)) + .isInstanceOf(ConditionTimeoutException.class); } @Test - public void testAwaitWithInterruption() { + public void testAwaitCompletionWithInterruption() { AssertSubscriber subscriber = AssertSubscriber.create(10); AtomicBoolean unblocked = new AtomicBoolean(); Thread thread = new Thread(() -> { - subscriber.await(Duration.ofSeconds(100)); + subscriber.awaitCompletion(Duration.ofSeconds(100)); unblocked.set(true); }); thread.start(); @@ -166,7 +173,7 @@ public void testAwaitWithInterruption() { unblocked.set(false); thread = new Thread(() -> { - subscriber.await(); + subscriber.awaitCompletion(); unblocked.set(true); }); thread.start(); @@ -181,11 +188,8 @@ public void testAwaitWhenAlreadyCompleted() { AssertSubscriber subscriber = AssertSubscriber.create(10); subscriber.onComplete(); - subscriber.await(Duration.ofSeconds(100)); - subscriber.await(); - - subscriber.assertCompleted(); - + subscriber.awaitCompletion(Duration.ofSeconds(100)); + subscriber.awaitCompletion(); } @Test @@ -194,11 +198,10 @@ public void testAwaitWhenAlreadyFailed() { AssertSubscriber subscriber = AssertSubscriber.create(10); subscriber.onError(new IOException("boom")); - subscriber.await(Duration.ofSeconds(100)); - subscriber.await(); + subscriber.awaitFailure(Duration.ofSeconds(100)); + subscriber.awaitFailure(); subscriber.assertFailedWith(IOException.class, "boom"); } - } diff --git a/implementation/src/test/java/io/smallrye/mutiny/helpers/test/AssertSubscriberTest.java b/implementation/src/test/java/io/smallrye/mutiny/helpers/test/AssertSubscriberTest.java index 0ce6c0bf4..d3649c251 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/helpers/test/AssertSubscriberTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/helpers/test/AssertSubscriberTest.java @@ -8,12 +8,27 @@ import java.io.IOException; import java.time.Duration; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; import org.junit.jupiter.api.Test; import org.reactivestreams.Subscription; +import io.smallrye.mutiny.Multi; +import io.smallrye.mutiny.TestException; +import io.smallrye.mutiny.TimeoutException; +import io.smallrye.mutiny.Uni; +import io.smallrye.mutiny.infrastructure.Infrastructure; + public class AssertSubscriberTest { + private final Duration SMALL = Duration.ofMillis(100); + private final Duration MEDIUM = Duration.ofMillis(1000); + + private final Uni smallDelay = Uni.createFrom().voidItem() + .onItem().delayIt().by(SMALL); + private final Uni mediumDelay = Uni.createFrom().voidItem() + .onItem().delayIt().by(MEDIUM); + @Test public void testItemsAndCompletion() { AssertSubscriber subscriber = AssertSubscriber.create(); @@ -144,8 +159,7 @@ public void testAwait() { subscriber.onComplete(); }).start(); - subscriber.await(); - subscriber.assertCompleted(); + subscriber.awaitCompletion(); } @Test @@ -162,8 +176,7 @@ public void testAwaitWithDuration() { subscriber.onComplete(); }).start(); - subscriber.await(Duration.ofMillis(100)); - subscriber.assertCompleted(); + subscriber.awaitCompletion(); } @Test @@ -180,7 +193,7 @@ public void testAwaitOnFailure() { subscriber.onError(new Exception("boom")); }).start(); - subscriber.await(); + subscriber.awaitFailure(); subscriber.assertFailedWith(Exception.class, "boom"); assertThatThrownBy(() -> subscriber.assertFailedWith(IllegalStateException.class, "")) @@ -201,8 +214,7 @@ public void testAwaitAlreadyCompleted() { subscriber.request(2); subscriber.onComplete(); - subscriber.await(); - subscriber.assertCompleted(); + subscriber.awaitCompletion(); } @Test @@ -214,7 +226,7 @@ public void testAwaitAlreadyFailed() { subscriber.request(2); subscriber.onError(new Exception("boom")); - subscriber.await(); + subscriber.awaitFailure(); subscriber.assertFailedWith(Exception.class, "boom"); } @@ -301,4 +313,327 @@ public void testTermination() { subscriber.assertTerminated(); } + @Test + public void testAwaitSubscription() { + // already subscribed + AssertSubscriber subscriber = Multi.createFrom().items(1) + .subscribe().withSubscriber(AssertSubscriber.create(0)); + assertThat(subscriber.awaitSubscription()).isSameAs(subscriber); + + // Delay + subscriber = Multi.createFrom().items(1) + .onSubscribe().call(x -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(0)); + assertThat(subscriber.awaitSubscription()).isSameAs(subscriber); + + subscriber = Multi.createFrom().items(1) + .onSubscribe().call(x -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(0)); + assertThat(subscriber.awaitSubscription(MEDIUM)).isSameAs(subscriber); + + // timeout + subscriber = Multi.createFrom().items(1) + .onSubscribe().call(x -> mediumDelay) + .subscribe().withSubscriber(AssertSubscriber.create(0)); + AssertSubscriber tmp = subscriber; + assertThatThrownBy(() -> tmp.awaitSubscription(SMALL)).isInstanceOf(AssertionError.class) + .hasMessageContaining("subscription").hasMessageContaining(SMALL.toMillis() + " ms"); + } + + @Test + public void testAwaitCompletion() { + AssertSubscriber subscriber = Multi.createFrom().items(1) + .subscribe().withSubscriber(AssertSubscriber.create(1)); + assertThat(subscriber.awaitCompletion()).isSameAs(subscriber); + + // Delay + subscriber = Multi.createFrom().items(1) + .onCompletion().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)); + assertThat(subscriber.awaitCompletion()).isSameAs(subscriber); + + subscriber = Multi.createFrom().items(1) + .onCompletion().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)); + assertThat(subscriber.awaitCompletion(MEDIUM)).isSameAs(subscriber); + + // timeout + subscriber = Multi.createFrom().items(1) + .onCompletion().call(() -> Uni.createFrom().voidItem() + .onItem().delayIt().by(MEDIUM)) + .subscribe().withSubscriber(AssertSubscriber.create(1)); + AssertSubscriber tmp = subscriber; + assertThatThrownBy(() -> tmp.awaitCompletion(SMALL)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("completion").hasMessageContaining(SMALL.toMillis() + " ms"); + + // Failure instead of completion + assertThatThrownBy(() -> Multi.createFrom(). failure(new TestException()) + .onFailure().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)).awaitCompletion()).isInstanceOf(AssertionError.class) + .hasMessageContaining("failure"); + } + + @Test + public void testAwaitFailure() { + AssertSubscriber subscriber = Multi.createFrom(). failure(new TestException()) + .subscribe().withSubscriber(AssertSubscriber.create(1)); + assertThat(subscriber.awaitFailure()).isSameAs(subscriber); + + assertThat( + subscriber.awaitFailure(t -> assertThat(t).isInstanceOf(TestException.class))).isSameAs(subscriber); + + AssertSubscriber tmp = subscriber; + Consumer failedValidation = t -> assertThat(t).isInstanceOf(IOException.class); + Consumer passedValidation = t -> assertThat(t).isInstanceOf(TestException.class); + + assertThatThrownBy(() -> tmp.awaitFailure(failedValidation)) + .isInstanceOf(AssertionError.class).hasMessageContaining("validation"); + + // Delay + subscriber = Multi.createFrom(). failure(new TestException()) + .onFailure().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)); + assertThat(subscriber.awaitFailure()).isSameAs(subscriber); + + subscriber = Multi.createFrom(). failure(new TestException()) + .onFailure().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)); + assertThat(subscriber.awaitFailure(passedValidation)).isSameAs(subscriber); + + subscriber = Multi.createFrom(). failure(new TestException()) + .onFailure().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)); + assertThat(subscriber.awaitFailure(MEDIUM)).isSameAs(subscriber); + + // timeout + subscriber = Multi.createFrom(). failure(new TestException()) + .onFailure().call(() -> mediumDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)); + AssertSubscriber tmp2 = subscriber; + assertThatThrownBy(() -> tmp2.awaitFailure(SMALL)).isInstanceOf(AssertionError.class) + .hasMessageContaining("failure").hasMessageContaining(SMALL.toMillis() + " ms"); + + // Completion instead of failure + assertThatThrownBy(() -> Multi.createFrom().items(1, 2) + .subscribe().withSubscriber(AssertSubscriber.create(2)) + .awaitFailure()).isInstanceOf(AssertionError.class).hasMessageContaining("completion"); + } + + @Test + public void testFailureWithNoMessage() { + Multi.createFrom().failure(new TimeoutException()) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitFailure() + .assertFailedWith(TimeoutException.class); + } + + @Test + public void testAwaitItem() { + // Already completed + assertThatThrownBy(() -> Multi.createFrom().empty() + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItem()).isInstanceOf(AssertionError.class) + .hasMessageContaining("completion").hasMessageContaining("item"); + + // Already failed + assertThatThrownBy(() -> Multi.createFrom().failure(new TestException()) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItem()).isInstanceOf(AssertionError.class) + .hasMessageContaining("failure").hasMessageContaining("item") + .hasMessageContaining(TestException.class.getName()); + + // Completion instead of item + assertThatThrownBy(() -> Multi.createFrom().empty() + .onCompletion().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItem(SMALL.multipliedBy(2))).isInstanceOf(AssertionError.class) + .hasMessageContaining("completion").hasMessageContaining("item"); + + // Failure instead of item + assertThatThrownBy(() -> Multi.createFrom().failure(new TestException()) + .onFailure().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItem(SMALL.multipliedBy(2))).isInstanceOf(AssertionError.class) + .hasMessageContaining("failure").hasMessageContaining("item"); + + // Item + Multi.createFrom().items(1) + .onItem().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItem(MEDIUM) + .assertItems(1); + + // Item group + Multi.createFrom().items(1, 2, 3) + .onItem().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItem(MEDIUM); + + // Timeout + assertThatThrownBy(() -> Multi.createFrom().item(1) + .onItem().call(() -> mediumDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItem(SMALL)).isInstanceOf(AssertionError.class) + .hasMessageContaining("item") + .hasMessageContaining(SMALL.toMillis() + " ms"); + } + + @Test + public void testAwaitNextItems() { + // Already completed + assertThatThrownBy(() -> Multi.createFrom().empty() + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItems(2)).isInstanceOf(AssertionError.class) + .hasMessageContaining("completion").hasMessageContaining("item"); + + // Already failed + assertThatThrownBy(() -> Multi.createFrom().failure(new TestException()) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItems(2)).isInstanceOf(AssertionError.class) + .hasMessageContaining("failure").hasMessageContaining("item") + .hasMessageContaining(TestException.class.getName()); + + // Completion instead of item + assertThatThrownBy(() -> Multi.createFrom().emitter(e -> e.emit(1).complete()) + .onCompletion().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItems(2, SMALL.multipliedBy(2))).isInstanceOf(AssertionError.class) + .hasMessageContaining("completion").hasMessageContaining("item").hasMessageContaining("0"); + + // Failure instead of item + assertThatThrownBy(() -> Multi.createFrom().emitter(e -> e.emit(1).fail(new TestException())) + .onFailure().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitNextItems(2, SMALL.multipliedBy(2))).isInstanceOf(AssertionError.class) + .hasMessageContaining("failure").hasMessageContaining("item").hasMessageContaining("0"); + + // Item + Multi.createFrom().items(1, 2, 3) + .onItem().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(3)) + .awaitNextItems(3, MEDIUM) + .assertItems(1, 2, 3) + .assertLastItem(3); + + // Timeout + assertThatThrownBy(() -> Multi.createFrom().emitter(e -> { + e.emit(1).emit(2); + try { + Thread.sleep(MEDIUM.toMillis()); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + e.emit(3); + }) + .emitOn(Infrastructure.getDefaultExecutor()) + .subscribe().withSubscriber(AssertSubscriber.create(3)) + .awaitNextItems(3, SMALL)).isInstanceOf(AssertionError.class) + .hasMessageContaining("item") + .hasMessageContaining(SMALL.toMillis() + " ms"); + } + + @Test + public void testAwaitItems() { + // Already completed + assertThatThrownBy(() -> Multi.createFrom().empty() + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitItems(2)).isInstanceOf(AssertionError.class) + .hasMessageContaining("terminal").hasMessageContaining("item"); + + // Already failed + assertThatThrownBy(() -> Multi.createFrom().failure(new TestException()) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitItems(2)).isInstanceOf(AssertionError.class) + .hasMessageContaining("terminal").hasMessageContaining("item"); + + // Completion instead of item + assertThatThrownBy(() -> Multi.createFrom().emitter(e -> e.emit(1).complete()) + .onCompletion().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitItems(2, SMALL.multipliedBy(2))).isInstanceOf(AssertionError.class) + .hasMessageContaining("completion").hasMessageContaining("item").hasMessageContaining("1"); + + // Failure instead of item + assertThatThrownBy(() -> Multi.createFrom().emitter(e -> e.emit(1).fail(new TestException())) + .onFailure().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(1)) + .awaitItems(2, SMALL.multipliedBy(2))).isInstanceOf(AssertionError.class) + .hasMessageContaining("failure").hasMessageContaining("item").hasMessageContaining("1"); + + // Item + Multi.createFrom().items(1, 2, 3) + .onItem().call(() -> smallDelay) + .subscribe().withSubscriber(AssertSubscriber.create(3)) + .awaitItems(3, MEDIUM) + .assertItems(1, 2, 3) + .assertLastItem(3); + + // Timeout + assertThatThrownBy(() -> Multi.createFrom().emitter(e -> { + e.emit(1).emit(2); + try { + Thread.sleep(MEDIUM.toMillis()); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + e.emit(3); + }) + .runSubscriptionOn(Infrastructure.getDefaultExecutor()) + .subscribe().withSubscriber(AssertSubscriber.create(3)) + .awaitItems(3, SMALL)).isInstanceOf(AssertionError.class) + .hasMessageContaining("item") + .hasMessageContaining(SMALL.toMillis() + " ms"); + + // Have received more items than expected. + assertThatThrownBy(() -> Multi.createFrom().items(1, 2, 3) + .subscribe().withSubscriber(AssertSubscriber.create(3)) + .awaitItems(2, SMALL)).isInstanceOf(AssertionError.class) + .hasMessageContaining("item").hasMessageContaining("2").hasMessageContaining("3"); + + // Already Cancelled + assertThatThrownBy(() -> Multi.createFrom().items(1, 2, 3) + .subscribe().withSubscriber(new AssertSubscriber<>(1, true)) + .awaitItems(2, SMALL)).isInstanceOf(AssertionError.class) + .hasMessageContaining("item").hasMessageContaining("2").hasMessageContaining("0") + .hasMessageContaining("terminal"); + + // Cancellation while waiting. + assertThatThrownBy(() -> { + AssertSubscriber subscriber = Multi.createFrom().nothing() + .subscribe().withSubscriber(new AssertSubscriber<>(1, true)); + subscriber + .run(() -> new Thread(() -> { + try { + Thread.sleep(SMALL.toMillis()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + subscriber.cancel(); + }).start()) + .awaitItems(2, MEDIUM); + }).isInstanceOf(AssertionError.class) + .hasMessageContaining("item").hasMessageContaining("2").hasMessageContaining("0") + .hasMessageContaining("terminal"); + + // Already receive the right number + Multi.createFrom().items(1, 2, 3) + .subscribe().withSubscriber(AssertSubscriber.create(4)) + .awaitItems(3) + .cancel(); + } + + @Test + public void testAssertLast() { + assertThatThrownBy( + () -> Multi.createFrom().empty().subscribe().withSubscriber(AssertSubscriber.create(1)) + .assertLastItem(1)) + .isInstanceOf(AssertionError.class); + + Multi.createFrom().items(1, 2, 3, 4) + .subscribe().withSubscriber(AssertSubscriber.create(2)) + .assertLastItem(2) + .request(2) + .assertLastItem(4); + } } diff --git a/implementation/src/test/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriberTest.java b/implementation/src/test/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriberTest.java index fdab220ba..694510020 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriberTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriberTest.java @@ -4,12 +4,15 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.IOException; +import java.time.Duration; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import org.junit.jupiter.api.Test; +import io.smallrye.mutiny.TestException; +import io.smallrye.mutiny.TimeoutException; import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.subscription.UniEmitter; @@ -31,15 +34,16 @@ void testCompletion() { .isInstanceOf(AssertionError.class); } + @SuppressWarnings("deprecation") @Test void testAwait() { UniAssertSubscriber subscriber = Uni.createFrom().item(123) .subscribe().withSubscriber(new UniAssertSubscriber<>()); + assertThat(subscriber.awaitItem().getItem()).isEqualTo(123); subscriber.await(); subscriber.assertCompleted(); subscriber.assertTerminated(); - subscriber.assertItem(123); } @Test @@ -197,8 +201,8 @@ public void testDoubleOnItem() { UniAssertSubscriber subscriber = UniAssertSubscriber.create(); subscriber.onSubscribe(() -> { }); - subscriber.onItem("Yo"); - subscriber.onItem("Yo"); + subscriber.onItem("Yo1"); + subscriber.onItem("Yo2"); assertThatThrownBy(subscriber::assertSignalsReceivedInOrder) .isInstanceOf(AssertionError.class) @@ -210,8 +214,8 @@ public void testDoubleOnFailure() { UniAssertSubscriber subscriber = UniAssertSubscriber.create(); subscriber.onSubscribe(() -> { }); - subscriber.onFailure(new RuntimeException("Yo")); - subscriber.onFailure(new RuntimeException("Yo")); + subscriber.onFailure(new RuntimeException("Yo1")); + subscriber.onFailure(new RuntimeException("Yo2")); assertThatThrownBy(subscriber::assertSignalsReceivedInOrder) .isInstanceOf(AssertionError.class) @@ -230,4 +234,113 @@ public void testBothOnFailureAndOnItem() { .isInstanceOf(AssertionError.class) .hasMessageContaining("There are both onItem and onFailure"); } + + @Test + public void testAwaitSubscription() { + // already subscribed + UniAssertSubscriber subscriber = Uni.createFrom().item(1) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + assertThat(subscriber.awaitSubscription()).isSameAs(subscriber); + + // Delay + subscriber = Uni.createFrom().item(1) + .onSubscribe().call(x -> Uni.createFrom().item(x).onItem().delayIt().by(Duration.ofSeconds(1))) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + assertThat(subscriber.awaitSubscription()).isSameAs(subscriber); + + subscriber = Uni.createFrom().item(1) + .onSubscribe().call(x -> Uni.createFrom().item(x).onItem().delayIt().by(Duration.ofSeconds(1))) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + assertThat(subscriber.awaitSubscription(Duration.ofSeconds(5))).isSameAs(subscriber); + + // timeout + subscriber = Uni.createFrom().item(1) + .onSubscribe().call(x -> Uni.createFrom().item(x).onItem().delayIt().by(Duration.ofSeconds(10))) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + UniAssertSubscriber tmp = subscriber; + assertThatThrownBy(() -> tmp.awaitSubscription(Duration.ofMillis(100))).isInstanceOf(AssertionError.class) + .hasMessageContaining("subscription").hasMessageContaining("100 ms"); + } + + @Test + public void testAwaitItem() { + UniAssertSubscriber subscriber = Uni.createFrom().item(1) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + assertThat(subscriber.awaitItem().getItem()).isEqualTo(1); + + // Delay + subscriber = Uni.createFrom().item(1) + .onItem().call(() -> Uni.createFrom().voidItem() + .onItem().delayIt().by(Duration.ofSeconds(1))) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + assertThat(subscriber.awaitItem().getItem()).isEqualTo(1); + + subscriber = Uni.createFrom().item(1) + .onItem().call(() -> Uni.createFrom().voidItem() + .onItem().delayIt().by(Duration.ofSeconds(1))) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + assertThat(subscriber.awaitItem(Duration.ofSeconds(5)).getItem()).isEqualTo(1); + + // timeout + subscriber = Uni.createFrom().item(1) + .onItem().call(() -> Uni.createFrom().voidItem() + .onItem().delayIt().by(Duration.ofSeconds(10))) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + UniAssertSubscriber tmp = subscriber; + assertThatThrownBy(() -> tmp.awaitItem(Duration.ofMillis(100))).isInstanceOf(AssertionError.class) + .hasMessageContaining("item").hasMessageContaining("100 ms"); + } + + @Test + public void testAwaitFailure() { + UniAssertSubscriber subscriber = Uni.createFrom(). failure(new TestException()) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + assertThat(subscriber.awaitFailure()).isSameAs(subscriber); + + assertThat( + subscriber.awaitFailure(t -> assertThat(t).isInstanceOf(TestException.class))).isSameAs(subscriber); + + UniAssertSubscriber tmp = subscriber; + Consumer failedValidation = t -> assertThat(t).isInstanceOf(IOException.class); + Consumer passedValidation = t -> assertThat(t).isInstanceOf(TestException.class); + + assertThatThrownBy(() -> tmp.awaitFailure(failedValidation)) + .isInstanceOf(AssertionError.class).hasMessageContaining("validation"); + + // Delay + subscriber = Uni.createFrom(). failure(new TestException()) + .onFailure().call(() -> Uni.createFrom().voidItem() + .onItem().delayIt().by(Duration.ofSeconds(1))) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + assertThat(subscriber.awaitFailure()).isSameAs(subscriber); + + subscriber = Uni.createFrom(). failure(new TestException()) + .onFailure().call(() -> Uni.createFrom().item(0) + .onItem().delayIt().by(Duration.ofSeconds(1))) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + assertThat(subscriber.awaitFailure(passedValidation)).isSameAs(subscriber); + + subscriber = Uni.createFrom(). failure(new TestException()) + .onFailure().call(() -> Uni.createFrom().voidItem() + .onItem().delayIt().by(Duration.ofSeconds(1))) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + assertThat(subscriber.awaitFailure(Duration.ofSeconds(5))).isSameAs(subscriber); + + // timeout + subscriber = Uni.createFrom(). failure(new TestException()) + .onFailure().call(() -> Uni.createFrom().voidItem() + .onItem().delayIt().by(Duration.ofSeconds(10))) + .subscribe().withSubscriber(UniAssertSubscriber.create()); + UniAssertSubscriber tmp2 = subscriber; + assertThatThrownBy(() -> tmp2.awaitFailure(Duration.ofMillis(100))).isInstanceOf(AssertionError.class) + .hasMessageContaining("failure").hasMessageContaining("100 ms"); + } + + @Test + public void testFailureWithNoMessage() { + Uni.createFrom().failure(new TimeoutException()) + .subscribe().withSubscriber(UniAssertSubscriber.create()) + .awaitFailure() + .assertFailedWith(TimeoutException.class); + } } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCollectTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCollectTest.java index c0c447b56..abe072327 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCollectTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCollectTest.java @@ -39,13 +39,13 @@ public void testCollectFirstAndLast() { items .collect().first() .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitItem() .assertItem(1); items .collect().last() .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitItem() .assertItem(3); } @@ -56,13 +56,13 @@ public void testCollectFirstAndLastDeprecated() { items .collectItems().first() .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitItem() .assertItem(1); items .collectItems().last() .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitItem() .assertItem(3); } @@ -72,13 +72,13 @@ public void testCollectWithEmpty() { items .collect().first() .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitItem() .assertItem(null); items .collect().last() .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitItem() .assertItem(null); } @@ -88,13 +88,13 @@ public void testCollectFirstAndLastOnFailure() { failing .collect().first() .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom"); failing .collect().last() .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom"); } @@ -103,7 +103,7 @@ public void testAsList() { UniAssertSubscriber> subscriber = Multi.createFrom().items(1, 2, 3) .collect().asList() .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await(); + .awaitItem(); assertThat(subscriber.getItem()).containsExactly(1, 2, 3); } @@ -113,7 +113,7 @@ public void testCollectIn() { UniAssertSubscriber> subscriber = Multi.createFrom().range(1, 10) .collect().in(LinkedList::new, LinkedList::add) .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await(); + .awaitItem(); assertThat(subscriber.getItem()).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9).isInstanceOf(LinkedList.class); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCreateFromCompletionStageTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCreateFromCompletionStageTest.java index 45c2f2fb1..8ce5f6a59 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCreateFromCompletionStageTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCreateFromCompletionStageTest.java @@ -41,7 +41,7 @@ public void testWithAsyncValue() { AssertSubscriber subscriber = Multi.createFrom() .completionStage(CompletableFuture.supplyAsync(() -> "hello")).subscribe() .withSubscriber(AssertSubscriber.create(1)); - subscriber.await().assertCompleted().assertItems("hello"); + subscriber.awaitCompletion().assertItems("hello"); } @Test @@ -58,7 +58,7 @@ public void testWithAsyncCompletionWithNull() { AssertSubscriber subscriber = Multi.createFrom() .completionStage(CompletableFuture.runAsync(() -> called.set(true))).subscribe() .withSubscriber(AssertSubscriber.create(1)); - subscriber.await().assertCompleted().assertHasNotReceivedAnyItem(); + subscriber.awaitCompletion().assertHasNotReceivedAnyItem(); assertThat(called).isTrue(); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCreateFromTimePeriodTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCreateFromTimePeriodTest.java index aacf965df..c550f5e63 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCreateFromTimePeriodTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiCreateFromTimePeriodTest.java @@ -1,7 +1,6 @@ package io.smallrye.mutiny.operators; import static org.assertj.core.api.Assertions.assertThat; -import static org.awaitility.Awaitility.await; import java.time.Duration; import java.util.List; @@ -37,8 +36,8 @@ public void testIntervalOfAFewMillis() { .onItem().transform(l -> System.currentTimeMillis()) .subscribe().withSubscriber(subscriber); - await().until(() -> subscriber.getItems().size() >= 10); - subscriber.cancel(); + subscriber.awaitNextItems(10) + .cancel(); subscriber.assertNotTerminated(); @@ -64,8 +63,8 @@ public void testWithInfraExecutorAndNoDelay() throws InterruptedException { .onItem().transform(l -> System.currentTimeMillis()) .subscribe().withSubscriber(subscriber); - await().until(() -> subscriber.getItems().size() >= 10); - subscriber.cancel(); + subscriber.awaitNextItems(10) + .cancel(); subscriber.assertNotTerminated(); @@ -90,8 +89,11 @@ public void testBackPressureOverflow() { subscriber .request(2) // request only 2 - .await() // wait until failure - .assertFailedWith(BackPressureFailure.class, "lack of requests"); + .awaitFailure(t -> { // wait until failure, and validate + assertThat(t) + .isInstanceOf(BackPressureFailure.class) + .hasMessageContaining("lack of requests"); + }); } } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiDistinctTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiDistinctTest.java index ff8746154..77b675113 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiDistinctTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiDistinctTest.java @@ -1,7 +1,6 @@ package io.smallrye.mutiny.operators; import static org.assertj.core.api.Assertions.assertThat; -import static org.awaitility.Awaitility.await; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; @@ -19,6 +18,8 @@ import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.TestException; +import io.smallrye.mutiny.helpers.spies.MultiOnCancellationSpy; +import io.smallrye.mutiny.helpers.spies.Spy; import io.smallrye.mutiny.helpers.test.AssertSubscriber; import io.smallrye.mutiny.subscription.MultiEmitter; @@ -189,9 +190,9 @@ public void testDroppedRepetitionsDeprecated() { @Test public void testSkipRepetitionsWithCancellation() { AtomicLong count = new AtomicLong(); - AtomicBoolean cancelled = new AtomicBoolean(); - AssertSubscriber subscriber = Multi.createFrom().ticks().every(Duration.ofMillis(1)) - .onCancellation().invoke(() -> cancelled.set(true)) + MultiOnCancellationSpy multi = Spy + .onCancellation(Multi.createFrom().ticks().every(Duration.ofMillis(1))); + AssertSubscriber subscriber = multi .onItem().transform(l -> { if (count.getAndIncrement() % 2 == 0) { return l; @@ -202,9 +203,9 @@ public void testSkipRepetitionsWithCancellation() { .skip().repetitions() .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - await().until(() -> subscriber.getItems().size() >= 10); - subscriber.cancel(); - assertThat(cancelled).isTrue(); + multi.assertNotCancelled(); + subscriber.awaitNextItems(10).cancel(); + multi.assertCancelled(); } @Test @@ -304,8 +305,9 @@ public void testSkipRepetitionsExceptionInEquals() { BadlyComparableStuffOnEquals item2 = new BadlyComparableStuffOnEquals(); emitter.get().emit(item1).emit(item2).complete(); subscriber - .await() - .assertFailedWith(TestException.class, "boom"); + .awaitFailure(t -> assertThat(t) + .isInstanceOf(TestException.class) + .hasMessageContaining("boom")); } @Test @@ -323,8 +325,9 @@ public void testSkipRepetitionsExceptionInComparator() { emitter.get().emit(1).emit(2).complete(); subscriber - .await() - .assertFailedWith(TestException.class, "boom"); + .awaitFailure(t -> assertThat(t) + .isInstanceOf(TestException.class) + .hasMessageContaining("boom")); } @Test diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiEmitOnTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiEmitOnTest.java index 896ad828d..4ca427188 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiEmitOnTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiEmitOnTest.java @@ -1,5 +1,6 @@ package io.smallrye.mutiny.operators; +import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; import static org.mockito.Mockito.mock; @@ -37,12 +38,12 @@ public void testWithSequenceOfItems() { .emitOn(executor) .subscribe().withSubscriber(AssertSubscriber.create()); - subscriber.request(2); - await().until(() -> subscriber.getItems().size() == 2); - subscriber.assertItems(1, 2); - subscriber.request(20); - await().until(() -> subscriber.getItems().size() == 10); - subscriber.assertItems(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + subscriber.request(2) + .awaitNextItems(2) + .assertItems(1, 2) + .request(20) + .awaitNextItems(8) + .assertItems(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); } @Test @@ -52,8 +53,9 @@ public void testWithRequest0() { .subscribe().withSubscriber(AssertSubscriber.create()); subscriber.request(0); - subscriber.await() - .assertFailedWith(IllegalArgumentException.class, "request"); + subscriber.awaitFailure(t -> assertThat(t) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("request")); } @Test diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiGroupTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiGroupTest.java index 61266d6ee..05b5ca6b5 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiGroupTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiGroupTest.java @@ -16,6 +16,7 @@ import java.util.function.Consumer; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; @@ -24,6 +25,8 @@ import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.TestException; import io.smallrye.mutiny.Uni; +import io.smallrye.mutiny.helpers.spies.MultiOnCancellationSpy; +import io.smallrye.mutiny.helpers.spies.Spy; import io.smallrye.mutiny.helpers.test.AssertSubscriber; import io.smallrye.mutiny.infrastructure.Infrastructure; import io.smallrye.mutiny.subscription.BackPressureFailure; @@ -188,13 +191,14 @@ public void testAsListsWithNegativeDuration() { @Test public void testAsListsWithDuration() { - Multi publisher = Multi.createFrom().publisher(Multi.createFrom().ticks().every(Duration.ofMillis(2))); + MultiOnCancellationSpy publisher = Spy + .onCancellation(Multi.createFrom().publisher(Multi.createFrom().ticks().every(Duration.ofMillis(2)))); AssertSubscriber> subscriber = publisher.group().intoLists().every(Duration.ofMillis(100)) .subscribe() .withSubscriber(AssertSubscriber.create(100)); - await().until(() -> subscriber.getItems().size() > 3); - subscriber.cancel(); + subscriber.awaitNextItems(3).cancel(); + publisher.assertCancelled(); } @Test @@ -204,8 +208,7 @@ public void testAsListsWithDurationWithCompletion() { AssertSubscriber> subscriber = publisher.group().intoLists().every(Duration.ofMillis(100)) .subscribe() .withSubscriber(AssertSubscriber.create(100)); - subscriber.await(); - subscriber.assertCompleted(); + subscriber.awaitCompletion(); } @Test @@ -216,7 +219,7 @@ public void testAsListsWithDurationWithFailure() { AssertSubscriber> subscriber = publisher.group().intoLists().every(Duration.ofMillis(100)) .subscribe() .withSubscriber(AssertSubscriber.create(100)); - subscriber.await(); + subscriber.awaitFailure(); subscriber.assertFailedWith(IOException.class, "boom"); } @@ -230,7 +233,8 @@ public void testAsListsWithDurationAndLackOfRequests() { .subscribe() .withSubscriber(AssertSubscriber.create(2)); - subscriber.await() + subscriber + .awaitFailure() .assertFailedWith(BackPressureFailure.class, ""); assertThat(cancelled).isTrue(); } @@ -380,8 +384,7 @@ public void testAsMultisWithDuration() { .subscribe() .withSubscriber(AssertSubscriber.create(100)); - await().until(() -> subscriber.getItems().size() > 3); - subscriber.cancel(); + subscriber.awaitNextItems(3).cancel(); } @Test @@ -405,7 +408,7 @@ public void testBasicTimeWindowWithFailure() { .onItem().transformToMultiAndConcatenate(m -> m); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() + .awaitFailure() .assertItems(1, 2, 3, 4, 5, 6) .assertFailedWith(IOException.class, "boom"); } @@ -418,10 +421,9 @@ public void testThatWindowWithDurationEmitsEmptyLists() { .onItem().transformToUniAndMerge(m -> m.collect().asList()) .subscribe().withSubscriber(subscriber); - await().until(() -> subscriber.getItems().size() == 3); + subscriber.awaitNextItems(3).cancel(); List> items = subscriber.getItems(); assertThat(items).hasSize(3).allSatisfy(list -> assertThat(list).isEmpty()); - subscriber.cancel(); } @Test @@ -812,8 +814,9 @@ public void testCancellationOnUpstream() { .group().by(l -> l % 2) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.assertSubscribed(); - await().until(() -> subscriber.getItems().size() == 2); + subscriber.awaitSubscription(); + subscriber.awaitNextItems(2).cancel(); + AssertSubscriber s1 = subscriber.getItems().get(0).subscribe() .withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); s1.assertSubscribed(); @@ -839,7 +842,7 @@ public void testCancellationOnGroup() { .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); subscriber.assertSubscribed(); - await().until(() -> subscriber.getItems().size() == 2); + subscriber.awaitNextItems(2); AssertSubscriber s1 = subscriber.getItems().get(0).subscribe() .withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); s1.assertSubscribed(); @@ -867,7 +870,7 @@ public void testImmediateCancellationOnUpstream() { assertThat(subscriber.assertNotTerminated().isCancelled()).isTrue(); } - @Test + @RepeatedTest(10) public void testGroupByWithUpstreamFailure() { AtomicReference> emitter = new AtomicReference<>(); diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnFailureRetryTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnFailureRetryTest.java index f3d5031f4..21ea6ad20 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnFailureRetryTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnFailureRetryTest.java @@ -141,7 +141,7 @@ public void testWithRetryWithBackoffWithPredicate() { .retry() .withBackOff(Duration.ofMillis(10)).atMost(3) .subscribe().withSubscriber(AssertSubscriber.create(10)) - .await() + .awaitFailure() .assertFailedWith(RuntimeException.class, "boom") .assertItems(1, 2, 3); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnOverflowTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnOverflowTest.java index a6f75af11..4c10b3330 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnOverflowTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnOverflowTest.java @@ -83,7 +83,7 @@ public void testDropStrategyWithBackPressure() { Multi.createFrom().range(1, 10) .onOverflow().drop() .subscribe(sub); - sub.await().assertCompleted().assertHasNotReceivedAnyItem(); + sub.awaitCompletion().assertHasNotReceivedAnyItem(); } @Test diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnSubscribeTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnSubscribeTest.java index 8962ba30c..229dd5b0a 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnSubscribeTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiOnSubscribeTest.java @@ -228,9 +228,10 @@ public void testThatSubscriptionIsNotPassedDownstreamUntilInvokeCallbackComplete subscriber.assertNotSubscribed(); latch.countDown(); - subscriber.await() - .assertSubscribed() - .assertCompleted().assertItems(1, 2, 3); + subscriber + .awaitSubscription() + .awaitCompletion() + .assertItems(1, 2, 3); } @Test @@ -246,9 +247,10 @@ public void testThatSubscriptionIsNotPassedDownstreamUntilProducedUniCompletes() await().until(() -> emitter.get() != null); emitter.get().complete(12345); - subscriber.await() - .assertSubscribed() - .assertCompleted().assertItems(1, 2, 3); + subscriber + .awaitSubscription() + .awaitCompletion() + .assertItems(1, 2, 3); } @@ -266,9 +268,10 @@ public void testThatSubscriptionIsNotPassedDownstreamUntilProducedUniCompletesWi await().until(() -> emitter.get() != null); emitter.get().complete(12345); - subscriber.await() - .assertSubscribed() - .assertCompleted().assertItems(1, 2, 3); + subscriber + .awaitSubscription() + .awaitCompletion() + .assertItems(1, 2, 3); } @@ -280,9 +283,8 @@ public void testThatRunSubscriptionOnEmitRequestOnSubscribe() { subscriber .request(1) - .await() - .assertItems(1, 2, 3) - .assertCompleted(); + .awaitCompletion() + .assertItems(1, 2, 3); } @Test diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiReceiveItemOnTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiReceiveItemOnTest.java index 4ad50f856..43be16649 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiReceiveItemOnTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiReceiveItemOnTest.java @@ -62,8 +62,7 @@ public void testThatItemsAreDispatchedOnTheRightThread() { .onItem().invoke(i -> itemThread.add(Thread.currentThread().getName())) .onCompletion().invoke(() -> completionThread.add(Thread.currentThread().getName())) .subscribe().withSubscriber(AssertSubscriber.create(4)) - .await() - .assertCompleted(); + .awaitCompletion(); await().until(() -> subscriber.getItems().size() == 4); assertThat(itemThread).allSatisfy(s -> assertThat(s).startsWith("test-")); @@ -79,7 +78,7 @@ public void testThatFailureAreDispatchedOnExecutor() { .onItem().invoke(i -> itemThread.add(Thread.currentThread().getName())) .onFailure().invoke(f -> failureThread.add(Thread.currentThread().getName())) .subscribe().withSubscriber(AssertSubscriber.create(4)) - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom"); assertThat(itemThread).isEmpty(); @@ -91,7 +90,7 @@ public void testWithImmediate() { Multi.createFrom().items(1, 2, 3, 4) .emitOn(Runnable::run) .subscribe().withSubscriber(AssertSubscriber.create(4)) - .await() + .awaitCompletion() .assertItems(1, 2, 3, 4); } @@ -100,8 +99,7 @@ public void testWithLargeNumberOfItems() { AssertSubscriber subscriber = Multi.createFrom().range(0, 100_000) .emitOn(executor) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() - .assertCompleted(); + .awaitCompletion(); assertThat(subscriber.getItems()).hasSize(100_000); int current = -1; @@ -116,7 +114,7 @@ public void testRunSubscriptionOn() { Multi.createFrom().items(1, 2, 3, 4) .runSubscriptionOn(executor) .subscribe().withSubscriber(AssertSubscriber.create(4)) - .await() + .awaitCompletion() .assertItems(1, 2, 3, 4); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiSelectFirstOrLast.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiSelectFirstOrLast.java index d3bea49ba..7ffa4f06b 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiSelectFirstOrLast.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiSelectFirstOrLast.java @@ -249,8 +249,7 @@ public void testLimitingInfiniteStream() { Multi.createFrom().ticks().every(Duration.ofMillis(2)) .select().first(5) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() - .assertCompleted() + .awaitCompletion() .assertItems(0L, 1L, 2L, 3L, 4L); } @@ -265,8 +264,7 @@ public void testSelectByTime() { AssertSubscriber subscriber = Multi.createFrom().range(1, 100) .select().first(Duration.ofMillis(1000)) .subscribe().withSubscriber(AssertSubscriber.create(10)) - .await() - .assertCompleted(); + .awaitCompletion(); assertThat(subscriber.getItems()).hasSize(10); } @@ -280,7 +278,7 @@ public void testSelectByTimeWithFailure() { AssertSubscriber subscriber = multi .select().first(Duration.ofMillis(1000)) .subscribe().withSubscriber(AssertSubscriber.create(100)) - .await() + .awaitFailure() .assertFailedWith(TestException.class, "boom"); assertThat(subscriber.getItems()).hasSize(4); @@ -330,8 +328,7 @@ public void subscribe(MultiSubscriber subscriber) { AssertSubscriber subscriber = rogue .select().first(Duration.ofMillis(1000)) .subscribe().withSubscriber(AssertSubscriber.create(100)) - .await() - .assertCompleted(); + .awaitCompletion(); assertThat(subscriber.getItems()).hasSize(2); } @@ -351,7 +348,7 @@ public void subscribe(MultiSubscriber subscriber) { AssertSubscriber subscriber = rogue .select().first(Duration.ofMillis(1000)) .subscribe().withSubscriber(AssertSubscriber.create(100)) - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom"); assertThat(subscriber.getItems()).hasSize(2); diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiSkipTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiSkipTest.java index ae8dedb7c..280e8d55a 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiSkipTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiSkipTest.java @@ -342,8 +342,7 @@ public void testItDoesNotRequest0() { .onItem().transform(n -> n * 10) .subscribe().withSubscriber(AssertSubscriber.create(100)) - .await() - .assertCompleted() + .awaitCompletion() .assertItems(40, 60, 80); assertThat(called).isFalse(); diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTakeTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTakeTest.java index d6cf8a4ec..ace0e7b32 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTakeTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTakeTest.java @@ -18,6 +18,7 @@ import io.smallrye.mutiny.subscription.MultiEmitter; import io.smallrye.mutiny.subscription.MultiSubscriber; +@SuppressWarnings("deprecation") public class MultiTakeTest { @Test @@ -207,8 +208,7 @@ public void testLimitingInfiniteStream() { Multi.createFrom().ticks().every(Duration.ofMillis(2)) .transform().byTakingFirstItems(5) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() - .assertCompleted() + .awaitCompletion() .assertItems(0L, 1L, 2L, 3L, 4L); } @@ -217,8 +217,7 @@ public void testTakeByTime() { AssertSubscriber subscriber = Multi.createFrom().range(1, 100).transform() .byTakingItemsFor(Duration.ofMillis(1000)) .subscribe().withSubscriber(AssertSubscriber.create(10)) - .await() - .assertCompleted(); + .awaitCompletion(); assertThat(subscriber.getItems()).hasSize(10); } @@ -232,7 +231,7 @@ public void testTakeByTimeWithFailure() { AssertSubscriber subscriber = multi .transform().byTakingItemsFor(Duration.ofMillis(1000)) .subscribe().withSubscriber(AssertSubscriber.create(100)) - .await() + .awaitFailure() .assertFailedWith(TestException.class, "boom"); assertThat(subscriber.getItems()).hasSize(4); @@ -282,8 +281,7 @@ public void subscribe(MultiSubscriber subscriber) { AssertSubscriber subscriber = rogue .transform().byTakingItemsFor(Duration.ofMillis(1000)) .subscribe().withSubscriber(AssertSubscriber.create(100)) - .await() - .assertCompleted(); + .awaitCompletion(); assertThat(subscriber.getItems()).hasSize(2); } @@ -303,7 +301,7 @@ public void subscribe(MultiSubscriber subscriber) { AssertSubscriber subscriber = rogue .transform().byTakingItemsFor(Duration.ofMillis(1000)) .subscribe().withSubscriber(AssertSubscriber.create(100)) - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom"); assertThat(subscriber.getItems()).hasSize(2); diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiToUniTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiToUniTest.java index 2b007c672..431778240 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiToUniTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiToUniTest.java @@ -114,12 +114,12 @@ public void testFromAnUniSendingResultEventInTheFuture() { .completionStage(() -> CompletableFuture.supplyAsync(count::incrementAndGet)); multi.toUni().subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitItem() .assertItem(1) .assertCompleted(); Uni.createFrom().multi(multi).subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitItem() .assertItem(2) .assertCompleted(); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTransformByMergingTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTransformByMergingTest.java index 1c227aa8d..657dcd4b4 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTransformByMergingTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTransformByMergingTest.java @@ -68,7 +68,7 @@ public void testConcurrentEmissionWithMerge() { AssertSubscriber subscriber = merged.subscribe() .withSubscriber(AssertSubscriber.create(1000)); - subscriber.await(); + subscriber.awaitCompletion(); List items = subscriber.getItems(); assertThat(Collections.singleton(items)).noneSatisfy(list -> assertThat(list).isSorted()); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTransformToMultiTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTransformToMultiTest.java index 41191c90e..53d19a433 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTransformToMultiTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/MultiTransformToMultiTest.java @@ -241,8 +241,7 @@ public void testTransformToMultiAndMergeUsingMultiFlatten() { .subscribe(subscriber); subscriber - .await() - .assertCompleted(); + .awaitCompletion(); assertThat(subscriber.getItems()).containsExactlyInAnyOrder(1, 1, 2, 2, 3, 3); } @@ -259,8 +258,7 @@ public void testTransformToMultiAndConcatenateUsingMultiFlatten() { .subscribe(subscriber); subscriber - .await() - .assertCompleted(); + .awaitCompletion(); assertThat(subscriber.getItems()).containsExactly(1, 1, 2, 2, 3, 3); } @@ -276,9 +274,7 @@ public void testTransformToMultiAndMerge() { }) .subscribe(subscriber); - subscriber - .await() - .assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).containsExactlyInAnyOrder(1, 1, 2, 2, 3, 3); } @@ -295,8 +291,7 @@ public void testTransformToMultiAndConcatenate() { .subscribe(subscriber); subscriber - .await() - .assertCompleted(); + .awaitCompletion(); assertThat(subscriber.getItems()).containsExactlyInAnyOrder(1, 1, 2, 2, 3, 3); } @@ -309,8 +304,8 @@ public void testFlatMapShortcut() { .subscribe(subscriber); subscriber - .await() - .assertItems(1, 1, 2, 2, 3, 3).assertCompleted(); + .awaitCompletion() + .assertItems(1, 1, 2, 2, 3, 3); } @Test @@ -451,7 +446,7 @@ public void testTransformToMultiWithConcurrencyAndAsyncEmission() { .merge(25) .subscribe(subscriber); - subscriber.await().assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).hasSize(10000); } @@ -881,8 +876,7 @@ public void testMaxConcurrency() { AssertSubscriber subscriber = AssertSubscriber.create(Long.MAX_VALUE); multi.subscribe().withSubscriber(subscriber); - subscriber.await() - .assertCompleted(); + subscriber.awaitCompletion(); List expected = new ArrayList<>(); for (int i = 0; i <= 99; i++) { @@ -939,8 +933,7 @@ public void testThatConcurrencyDontMissItems() { .merge(8) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await() - .assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).containsExactlyInAnyOrderElementsOf(expected); } @@ -956,8 +949,7 @@ public void testThatConcatenateDontMissItemsAndPreserveOrder() { .concatenate() .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await() - .assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).containsExactlyElementsOf(expected); } @@ -967,8 +959,7 @@ public void testFlatMapSimplePassThroughWithExecutor() { AssertSubscriber subscriber = Multi.createFrom().range(1, 1001) .flatMap(i -> Multi.createFrom().item(i).runSubscriptionOn(Infrastructure.getDefaultExecutor())) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await() - .assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).hasSize(1000); } @@ -978,8 +969,7 @@ public void testFlatMapSimplePassThroughWithoutExecutor() { .flatMap(i -> Multi.createFrom().items(i + 1, i + 2)) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await() - .assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).hasSize(4); } @@ -995,8 +985,7 @@ public void testProducingEmptyMultiWithMerge() { }) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await() - .assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).hasSize(512); } @@ -1012,8 +1001,7 @@ public void testProducingEmptyMultiWithConcatenate() { }) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await() - .assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).hasSize(512); } @@ -1029,8 +1017,7 @@ public void testProducingEmptyMultiWithIterable() { }) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await() - .assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).hasSize(512); } @@ -1049,8 +1036,7 @@ public void testProducingEmptyMultiWithMergeAndConcurrency() { .runSubscriptionOn(Infrastructure.getDefaultExecutor()) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await(Duration.ofSeconds(5)) - .assertCompleted(); + subscriber.awaitCompletion(Duration.ofSeconds(5)); assertThat(subscriber.getItems()).hasSize(max / 2); } @@ -1067,7 +1053,8 @@ public void testThatUpstreamIsCancelledIfMapperThrowsExceptionWithMerge() { }) .subscribe().withSubscriber(AssertSubscriber.create(10)); - subscriber.await() + subscriber + .awaitFailure() .assertFailedWith(IllegalArgumentException.class, "boom"); assertThat(cancelled).hasValue(1); } @@ -1087,7 +1074,8 @@ public void testThatUpstreamIsNotCancelledIfMapperProduceFailureWithMergeAndFail .merge() .subscribe().withSubscriber(AssertSubscriber.create(10)); - subscriber.await() + subscriber + .awaitFailure() .assertFailedWith(CompositeException.class, "boom"); assertThat(cancelled).hasValue(0); } @@ -1106,7 +1094,8 @@ public void testThatUpstreamIsCancelledIfMapperProduceFailureWithMergeAndNoFailu .merge() .subscribe().withSubscriber(AssertSubscriber.create(10)); - subscriber.await() + subscriber + .awaitFailure() .assertFailedWith(IllegalArgumentException.class, "boom"); assertThat(cancelled).hasValue(1); } @@ -1126,7 +1115,8 @@ public void testThatUpstreamIsNotCancelledIfMapperProduceFailureWithConcatenateA .concatenate() .subscribe().withSubscriber(AssertSubscriber.create(10)); - subscriber.await() + subscriber + .awaitFailure() .assertFailedWith(CompositeException.class, "boom"); assertThat(cancelled).hasValue(0); } @@ -1145,7 +1135,8 @@ public void testThatUpstreamIsCancelledIfMapperProduceFailureWithConcatenateAndN .concatenate() .subscribe().withSubscriber(AssertSubscriber.create(10)); - subscriber.await() + subscriber + .awaitFailure() .assertFailedWith(IllegalArgumentException.class, "boom"); assertThat(cancelled).hasValue(1); } @@ -1283,7 +1274,7 @@ public void testMergeRaceWithInnerEmissionOnAnotherThreadForSomeItems() { }).merge(1) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await().assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).hasSize(20); assertThat(subscriber.getItems()) .containsExactlyInAnyOrder(0, 1, 2, 3, 4, -5, 6, 7, 8, 9, -10, 11, 12, 13, 14, -15, 16, 17, 18, 19); @@ -1299,7 +1290,7 @@ public void testMergeRaceWithInnerEmissionOnAnotherThreadForSomeItems() { }).merge(5) .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await().assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).hasSize(20); assertThat(subscriber.getItems()) .containsExactlyInAnyOrder(0, 1, 2, 3, 4, -5, 6, 7, 8, 9, -10, 11, 12, 13, 14, -15, 16, 17, 18, 19); @@ -1318,7 +1309,7 @@ public void testConcatRaceWithInnerEmissionOnAnotherThreadForSomeItems() { }).concatenate() .subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)); - subscriber.await().assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).hasSize(20); assertThat(subscriber.getItems()) .containsExactly(0, 1, 2, 3, 4, -5, 6, 7, 8, 9, -10, 11, 12, 13, 14, -15, 16, 17, 18, 19); diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniAndTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniAndTest.java index c0c8547a6..3eb69a536 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniAndTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniAndTest.java @@ -75,7 +75,7 @@ public void testWithCombinator() { UniAssertSubscriber subscriber = Uni.combine().all().unis(uni1, uni2, uni3) .combinedWith((i1, i2, i3) -> i1 + i2 + i3) .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.await().assertItem(6); + subscriber.awaitItem().assertItem(6); } @Test @@ -142,7 +142,7 @@ public void testWithCombinatorAndDeprecatedApis() { UniAssertSubscriber subscriber = uni1.and().unis(uni2, uni3) .combinedWith((i1, i2, i3) -> i1 + i2 + i3) .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.await().assertItem(6); + subscriber.awaitItem().assertItem(6); } @Test diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniCreateFromFutureTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniCreateFromFutureTest.java index 7861c1407..563aa2aa7 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniCreateFromFutureTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniCreateFromFutureTest.java @@ -26,7 +26,7 @@ public void testThatNullValueAreAccepted() { Uni.createFrom().future(cs).subscribe().withSubscriber(subscriber); cs.complete(null); subscriber - .await() + .awaitItem() .assertCompleted().assertItem(null); } @@ -47,8 +47,9 @@ public void testWithAnAlreadyCancelledFuture() { CompletableFuture cs = new CompletableFuture<>(); cs.cancel(false); Uni.createFrom().future(cs).subscribe().withSubscriber(subscriber); - // No await - immediate failure + subscriber + .awaitFailure() .assertFailedWith(CancellationException.class, null); } @@ -59,7 +60,7 @@ public void testWithNonNullValue() { Uni.createFrom().future(cs).subscribe().withSubscriber(subscriber); cs.complete("1"); subscriber - .await() + .awaitItem() .assertCompleted().assertItem("1"); } @@ -70,7 +71,7 @@ public void testWithException() { Uni.createFrom().future(cs).subscribe().withSubscriber(subscriber); cs.completeExceptionally(new IOException("boom")); subscriber - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom"); } @@ -96,7 +97,7 @@ public void testWithExceptionThrownByAStage() { })).subscribe().withSubscriber(subscriber); cs.complete("bonjour"); subscriber - .await() + .awaitFailure() .assertFailedWith(IllegalStateException.class, "boom"); } @@ -115,7 +116,7 @@ public void testWithNonNullValueWithSupplier() { Uni.createFrom().future(() -> cs).subscribe().withSubscriber(subscriber); cs.complete("1"); subscriber - .await() + .awaitItem() .assertCompleted().assertItem("1"); } @@ -126,7 +127,7 @@ public void testWithExceptionWithSupplier() { Uni.createFrom().future(() -> cs).subscribe().withSubscriber(subscriber); cs.completeExceptionally(new IOException("boom")); subscriber - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom"); } @@ -244,7 +245,7 @@ public void testThatSubscriberCanCancelAfterEmission() { uni.subscribe().withSubscriber(subscriber); cs.complete(1); - subscriber.await(); + subscriber.awaitItem(); subscriber.cancel(); assertThat(called).isTrue(); subscriber.assertItem(1); @@ -259,7 +260,7 @@ public void testThatSubscriberCanCancelAfterEmissionWithSupplier() { uni.subscribe().withSubscriber(subscriber); cs.complete(1); subscriber - .await() + .awaitItem() .cancel(); subscriber.assertItem(1); @@ -292,7 +293,7 @@ public void testWithInterruptedFuture() { Uni.createFrom().future(cs).subscribe().withSubscriber(subscriber); cs.cancel(true); subscriber - .await() + .awaitFailure() .assertFailedWith(CancellationException.class, null); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniIfNoItemTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniIfNoItemTest.java index f5647a903..14da5710b 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniIfNoItemTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniIfNoItemTest.java @@ -27,7 +27,7 @@ public void testResultWhenTimeoutIsNotReached() { .ifNoItem().after(Duration.ofMillis(10)).recoverWithUni(Uni.createFrom().nothing()) .subscribe().withSubscriber(subscriber); - subscriber.await().assertCompleted().assertItem(1); + assertThat(subscriber.awaitItem().getItem()).isEqualTo(1); } @Test @@ -39,7 +39,7 @@ public void testTimeout() { .ifNoItem().after(Duration.ofMillis(1)).fail() .subscribe().withSubscriber(subscriber); - subscriber.await().assertFailed(); + subscriber.awaitFailure(); assertThat(subscriber.getFailure()).isInstanceOf(TimeoutException.class); } @@ -58,7 +58,7 @@ public void testTimeoutOnFailure() { .ifNoItem().after(Duration.ofMillis(1)).fail() .subscribe().withSubscriber(subscriber); - subscriber.await().assertFailed(); + subscriber.awaitFailure(); assertThat(subscriber.getFailure()).isInstanceOf(TimeoutException.class); } @@ -78,7 +78,7 @@ public void testFailureBeforeTimeout() { .ifNoItem().after(Duration.ofMillis(10000)).fail() .subscribe().withSubscriber(subscriber); - subscriber.await().assertFailed(); + subscriber.awaitFailure(); assertThat(subscriber.getFailure()).isInstanceOf(TestException.class); } @@ -87,7 +87,7 @@ public void testRecoverWithItem() { UniAssertSubscriber subscriber = Uni.createFrom(). nothing() .ifNoItem().after(Duration.ofMillis(10)).recoverWithItem(5) .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.await().assertItem(5); + subscriber.awaitItem().assertItem(5); } @Test @@ -95,7 +95,7 @@ public void testRecoverWithItemSupplier() { UniAssertSubscriber subscriber = Uni.createFrom(). nothing() .ifNoItem().after(Duration.ofMillis(10)).recoverWithItem(() -> 23) .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.await().assertItem(23); + subscriber.awaitItem().assertItem(23); } @Test @@ -103,7 +103,7 @@ public void testRecoverWithSwitchToUni() { UniAssertSubscriber subscriber = Uni.createFrom(). nothing() .ifNoItem().after(Duration.ofMillis(10)).recoverWithUni(() -> Uni.createFrom().item(15)) .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.await().assertItem(15); + subscriber.awaitItem().assertItem(15); } @Test @@ -111,7 +111,7 @@ public void testFailingWithAnotherException() { UniAssertSubscriber subscriber = Uni.createFrom(). nothing() .ifNoItem().after(Duration.ofMillis(10)).failWith(new IOException("boom")) .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.await().assertFailedWith(IOException.class, "boom"); + subscriber.awaitFailure().assertFailedWith(IOException.class, "boom"); } @Test @@ -140,8 +140,7 @@ public void testFailingOnTimeoutWithShutdownExecutor() { .subscribe().withSubscriber(UniAssertSubscriber.create()); subscriber - .await() - .assertFailed() + .awaitFailure() .assertFailedWith(RejectedExecutionException.class, ""); } @@ -184,7 +183,7 @@ public void testTimeoutWithCustomSupplierFailing() { }) .subscribe().withSubscriber(subscriber); - subscriber.await().assertFailed(); + subscriber.awaitFailure(); assertThat(subscriber.getFailure()).isInstanceOf(IllegalArgumentException.class); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnFailureTransformTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnFailureTransformTest.java index 051dee124..476cdffc0 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnFailureTransformTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnFailureTransformTest.java @@ -101,7 +101,7 @@ public void testThatMapperIsCalledOnTheRightExecutor() { }) .subscribe().withSubscriber(subscriber); - subscriber.await().assertFailedWith(BoomException.class, "BoomException"); + subscriber.awaitFailure().assertFailedWith(BoomException.class, "BoomException"); assertThat(threadName).isNotNull().doesNotHaveValue("main"); assertThat(subscriber.getOnFailureThreadName()).isEqualTo(threadName.get()); } finally { diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemDelayTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemDelayTest.java index 4a755b522..cfcbae3e4 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemDelayTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemDelayTest.java @@ -51,7 +51,7 @@ public void testDelayOnItemWithDefaultExecutor() { .onItem().delayIt() .by(Duration.ofMillis(100)).subscribe().withSubscriber(subscriber); - subscriber.await(); + subscriber.awaitItem(); long end = System.currentTimeMillis(); assertThat(end - begin).isGreaterThanOrEqualTo(100); subscriber.assertCompleted().assertItem(null); @@ -77,7 +77,7 @@ public void testDelayOnItem() { long begin = System.currentTimeMillis(); UniAssertSubscriber subscriber = UniAssertSubscriber.create(); delayed.subscribe().withSubscriber(subscriber); - subscriber.await(); + subscriber.awaitItem(); long end = System.currentTimeMillis(); assertThat(end - begin).isGreaterThanOrEqualTo(100); subscriber.assertCompleted().assertItem(null); @@ -91,10 +91,10 @@ public void testThatDelayDoNotImpactFailures() { Uni.createFrom(). failure(new Exception("boom")).onItem().delayIt() .onExecutor(executor) .by(Duration.ofMillis(100)).subscribe().withSubscriber(subscriber); - subscriber.await(); + subscriber.awaitFailure(); long end = System.currentTimeMillis(); assertThat(end - begin).isLessThan(100); - subscriber.assertFailed().assertFailedWith(Exception.class, "boom"); + subscriber.assertFailedWith(Exception.class, "boom"); } @Test diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemDelayUntilTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemDelayUntilTest.java index 10da560c7..579d5e94b 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemDelayUntilTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemDelayUntilTest.java @@ -92,7 +92,7 @@ public void testWithEmission() { await().until(() -> reference.get() != null); subscriber.assertNotTerminated(); reference.get().complete(null); - subscriber.await().assertItem(1); + subscriber.awaitItem().assertItem(1); } @Test diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureFlatMapTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureFlatMapTest.java index c25e24d21..0f3061b68 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureFlatMapTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureFlatMapTest.java @@ -72,7 +72,7 @@ public void testWithDelayedItem() { return Uni.createFrom().item(2); }).subscribe().withSubscriber(test); - test.await().assertCompleted().assertItem(2); + assertThat(test.awaitItem().getItem()).isEqualTo(2); assertThat(count).hasValue(1); } @@ -114,7 +114,7 @@ public void testWithDelayedFailure() { return Uni.createFrom().item(2); }).subscribe().withSubscriber(test); - test.await().assertCompleted().assertItem(2); + assertThat(test.awaitItem().getItem()).isEqualTo(2); assertThat(count).hasValue(1); } @@ -154,7 +154,7 @@ public void testWithImmediateItemAndThrowingException() { throw new IllegalStateException("kaboom"); }).subscribe().withSubscriber(test); - test.await().assertFailedWith(IllegalStateException.class, "kaboom"); + test.awaitFailure().assertFailedWith(IllegalStateException.class, "kaboom"); assertThat(count).hasValue(1); } @@ -168,7 +168,7 @@ public void testWithDeferredItemAndThrowingException() { throw new IllegalStateException("kaboom"); }).subscribe().withSubscriber(test); - test.await().assertFailedWith(IllegalStateException.class, "kaboom"); + test.awaitFailure().assertFailedWith(IllegalStateException.class, "kaboom"); assertThat(count).hasValue(1); } @@ -183,8 +183,8 @@ public void testWithImmediateFailureAndThrowingException() { throw new IllegalStateException("kaboom"); }).subscribe().withSubscriber(test); - test.await().assertFailedWith(CompositeException.class, "kaboom"); - test.await().assertFailedWith(CompositeException.class, "boom"); + test.awaitFailure().assertFailedWith(CompositeException.class, "kaboom"); + test.awaitFailure().assertFailedWith(CompositeException.class, "boom"); assertThat(count).hasValue(1); } @@ -197,7 +197,7 @@ public void testWithAMapperReturningNull() { called.set(true); return null; }).subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(NullPointerException.class, ""); + test.awaitFailure().assertFailedWith(NullPointerException.class, ""); assertThat(called).isTrue(); } @@ -211,7 +211,7 @@ public void testWithAMapperReturningNullAfterFailure() { called.set(true); return null; }).subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(NullPointerException.class, ""); + test.awaitFailure().assertFailedWith(NullPointerException.class, ""); assertThat(called).isTrue(); } @@ -244,7 +244,7 @@ public void testWithEmitterOnItem() { e.complete(2); }).subscribe().withSubscriber(test); - test.await().assertItem(2).assertCompleted(); + test.awaitItem().assertItem(2).assertCompleted(); } @Test @@ -257,7 +257,7 @@ public void testProduceUniWithEmitterOnItemDeprecated() { e.complete(2); }).subscribe().withSubscriber(test); - test.await().assertItem(2).assertCompleted(); + test.awaitItem().assertItem(2).assertCompleted(); } @Test @@ -269,7 +269,7 @@ public void testWithEmitterOnNull() { e.complete(2); }).subscribe().withSubscriber(test); - test.await().assertItem(2).assertCompleted(); + test.awaitItem().assertItem(2).assertCompleted(); } @Test @@ -281,7 +281,7 @@ public void testWithEmitterOnFailure() { e.complete(2); }).subscribe().withSubscriber(test); - test.await().assertItem(2).assertCompleted(); + test.awaitItem().assertItem(2).assertCompleted(); } @Test @@ -293,7 +293,7 @@ public void testWithEmitterOnItemThrowingException() { throw new IllegalStateException("bing"); }).subscribe().withSubscriber(test); - test.await().assertFailedWith(IllegalStateException.class, "bing"); + test.awaitFailure().assertFailedWith(IllegalStateException.class, "bing"); } @Test @@ -303,8 +303,8 @@ public void testWithEmitterOnFailureThrowingException() { throw new IllegalStateException("bing"); }).subscribe().withSubscriber(test); - test.await().assertFailedWith(CompositeException.class, "bing"); - test.await().assertFailedWith(CompositeException.class, "boom"); + test.awaitFailure().assertFailedWith(CompositeException.class, "bing"); + test.awaitFailure().assertFailedWith(CompositeException.class, "boom"); } } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureInvokeTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureInvokeTest.java index bbd198ee6..fa1bbef7b 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureInvokeTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureInvokeTest.java @@ -178,7 +178,7 @@ public void testThatCallbackIsCalledOnTheRightExecutorOnItem() { .onItemOrFailure().invoke((i, f) -> threadName.set(Thread.currentThread().getName())) .subscribe().withSubscriber(subscriber); - subscriber.await().assertCompleted().assertItem(1); + assertThat(subscriber.awaitItem().getItem()).isEqualTo(1); assertThat(threadName).isNotNull().doesNotHaveValue("main"); assertThat(subscriber.getOnItemThreadName()).isEqualTo(threadName.get()); } finally { @@ -202,7 +202,7 @@ public void testThatCallbackIsCalledOnTheRightExecutorOnFailure() { .onFailure().recoverWithItem(1) .subscribe().withSubscriber(subscriber); - subscriber.await().assertCompleted().assertItem(1); + assertThat(subscriber.awaitItem().getItem()).isEqualTo(1); assertThat(threadName).isNotNull().doesNotHaveValue("main"); assertThat(subscriber.getOnItemThreadName()).isEqualTo(threadName.get()); } finally { diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureMapTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureMapTest.java index c876e6909..69d07d618 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureMapTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemOrFailureMapTest.java @@ -167,7 +167,7 @@ public void testThatMapperIsCalledOnTheRightExecutorOnItem() { }) .subscribe().withSubscriber(subscriber); - subscriber.await().assertCompleted().assertItem(2); + assertThat(subscriber.awaitItem().getItem()).isEqualTo(2); assertThat(threadName).isNotNull().doesNotHaveValue("main"); assertThat(subscriber.getOnItemThreadName()).isEqualTo(threadName.get()); } finally { @@ -191,7 +191,7 @@ public void testThatMapperIsCalledOnTheRightExecutorOnFailure() { }) .subscribe().withSubscriber(subscriber); - subscriber.await().assertCompleted().assertItem(1); + assertThat(subscriber.awaitItem().getItem()).isEqualTo(1); assertThat(threadName).isNotNull().doesNotHaveValue("main"); assertThat(subscriber.getOnItemThreadName()).isEqualTo(threadName.get()); } finally { diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemProduceCompletionStageTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemProduceCompletionStageTest.java index f3002f0ee..7b4c96aa8 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemProduceCompletionStageTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemProduceCompletionStageTest.java @@ -40,7 +40,7 @@ public void testWithACompletionStageResolvedAsynchronously() { Uni uni = Uni.createFrom().item(1).onItem() .produceCompletionStage(v -> CompletableFuture.supplyAsync(() -> 42)); uni.subscribe().withSubscriber(test); - test.await().assertCompleted().assertItem(42); + assertThat(test.awaitItem().getItem()).isEqualTo(42); } @Test @@ -51,7 +51,7 @@ public void testWithACompletionStageResolvedAsynchronouslyWithAFailure() { throw new IllegalStateException("boom"); })); uni.subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(IllegalStateException.class, "boom"); + test.awaitFailure().assertFailedWith(IllegalStateException.class, "boom"); } @Test @@ -62,7 +62,7 @@ public void testThatMapperIsNotCalledOnUpstreamFailure() { called.set(true); return CompletableFuture.completedFuture(2); }).subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(Exception.class, "boom"); + test.awaitFailure().assertFailedWith(Exception.class, "boom"); assertThat(called).isFalse(); } @@ -76,7 +76,7 @@ public void testWithAMapperThrowingAnException() { throw new IllegalStateException("boom"); }) .subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(IllegalStateException.class, "boom"); + test.awaitFailure().assertFailedWith(IllegalStateException.class, "boom"); assertThat(called).isTrue(); } @@ -89,7 +89,7 @@ public void testWithAMapperReturningNull() { called.set(true); return null; }).subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(NullPointerException.class, ""); + test.awaitFailure().assertFailedWith(NullPointerException.class, ""); assertThat(called).isTrue(); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformTest.java index 58e42d5c8..953c7330f 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformTest.java @@ -110,7 +110,7 @@ public void testThatMapperIsCalledOnTheRightExecutor() { }) .subscribe().withSubscriber(subscriber); - subscriber.await().assertCompleted().assertItem(2); + assertThat(subscriber.awaitItem().getItem()).isEqualTo(2); assertThat(threadName).isNotNull().doesNotHaveValue("main"); assertThat(subscriber.getOnItemThreadName()).isEqualTo(threadName.get()); } finally { diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToMultiTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToMultiTest.java index 8807a75aa..5ef3ff956 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToMultiTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToMultiTest.java @@ -28,8 +28,7 @@ public void testTransformToMultiWithItem() { Uni.createFrom().item(1) .onItem().transformToMulti(i -> Multi.createFrom().range(i, 5)) .subscribe().withSubscriber(AssertSubscriber.create(10)) - .await() - .assertCompleted() + .awaitCompletion() .assertItems(1, 2, 3, 4); } @@ -39,8 +38,7 @@ public void testTransformToMultiWithItemDeprecated() { Uni.createFrom().item(1) .onItem().produceMulti(i -> Multi.createFrom().range(i, 5)) .subscribe().withSubscriber(AssertSubscriber.create(10)) - .await() - .assertCompleted() + .awaitCompletion() .assertItems(1, 2, 3, 4); } @@ -49,8 +47,7 @@ public void testTransformToMultiWithNull() { Uni.createFrom().voidItem() .onItem().transformToMulti(x -> Multi.createFrom().range(1, 5)) .subscribe().withSubscriber(AssertSubscriber.create(10)) - .await() - .assertCompleted() + .awaitCompletion() .assertItems(1, 2, 3, 4); } @@ -59,7 +56,7 @@ public void testTransformToMultiWithFailure() { Uni.createFrom(). failure(new IOException("boom")) .onItem().transformToMulti(x -> Multi.createFrom().range(1, 5)) .subscribe().withSubscriber(AssertSubscriber.create(10)) - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom") .assertHasNotReceivedAnyItem(); } @@ -71,7 +68,7 @@ public void testTransformToMultiWithExceptionThrownByMapper() { throw new IllegalStateException("boom"); }) .subscribe().withSubscriber(AssertSubscriber.create(10)) - .await() + .awaitFailure() .assertFailedWith(IllegalStateException.class, "boom") .assertHasNotReceivedAnyItem(); } @@ -81,7 +78,7 @@ public void testTransformToMultiWithNullReturnedByMapper() { Uni.createFrom().item(1) .onItem().transformToMulti(x -> null) .subscribe().withSubscriber(AssertSubscriber.create(10)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertHasNotReceivedAnyItem(); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToUniTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToUniTest.java index 64ac84242..067118eee 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToUniTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToUniTest.java @@ -98,7 +98,8 @@ public void testWithAnUniResolvedAsynchronously() { .transformToUni( v -> Uni.createFrom().emitter(emitter -> new Thread(() -> emitter.complete(42)).start())); uni.subscribe().withSubscriber(test); - test.await().assertCompleted().assertItem(42); + assertThat(test.awaitItem().getItem()).isEqualTo(42); + } @Test @@ -107,7 +108,7 @@ public void testWithAnUniResolvedAsynchronouslyWithAFailure() { Uni uni = Uni.createFrom().item(1).onItem().transformToUni(v -> Uni.createFrom() .emitter(emitter -> new Thread(() -> emitter.fail(new IOException("boom"))).start())); uni.subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(IOException.class, "boom"); + test.awaitFailure().assertFailedWith(IOException.class, "boom"); } @Test @@ -118,7 +119,7 @@ public void testThatMapperIsNotCalledOnUpstreamFailure() { called.set(true); return Uni.createFrom().item(2); }).subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(Exception.class, "boom"); + test.awaitFailure().assertFailedWith(Exception.class, "boom"); assertThat(called).isFalse(); } @@ -132,7 +133,7 @@ public void testWithAMapperThrowingAnException() { throw new IllegalStateException("boom"); }) .subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(IllegalStateException.class, "boom"); + test.awaitFailure().assertFailedWith(IllegalStateException.class, "boom"); assertThat(called).isTrue(); } @@ -145,7 +146,7 @@ public void testWithAMapperReturningNull() { called.set(true); return null; }).subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(NullPointerException.class, ""); + test.awaitFailure().assertFailedWith(NullPointerException.class, ""); assertThat(called).isTrue(); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToUniWithEmitterTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToUniWithEmitterTest.java index 372c87f72..5bd1b2222 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToUniWithEmitterTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnItemTransformToUniWithEmitterTest.java @@ -58,10 +58,8 @@ public void testWithAsyncEmitter() { .transformToUni((v, e) -> new Thread(() -> e.complete(count.incrementAndGet())).start()); uni.subscribe().withSubscriber(test1); uni.subscribe().withSubscriber(test2); - test1.await().assertCompleted(); - test2.await().assertCompleted(); - assertThat(test1.getItem()).isBetween(3, 4); - assertThat(test2.getItem()).isBetween(3, 4); + assertThat(test1.awaitItem().getItem()).isBetween(3, 4); + assertThat(test2.awaitItem().getItem()).isBetween(3, 4); } @Test @@ -70,7 +68,7 @@ public void testWithAsyncEmitterAndFailure() { Uni uni = Uni.createFrom().item(1).onItem() .transformToUni((v, e) -> new Thread(() -> e.fail(new IOException("boom"))).start()); uni.subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(IOException.class, "boom"); + test.awaitFailure().assertFailedWith(IOException.class, "boom"); } @Test @@ -81,7 +79,7 @@ public void testThatMapperIsNotCalledOnUpstreamFailure() { called.set(true); e.complete(2); }).subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(Exception.class, "boom"); + test.awaitFailure().assertFailedWith(Exception.class, "boom"); assertThat(called).isFalse(); } @@ -93,7 +91,7 @@ public void testWithAMapperThrowingAnException() { called.set(true); throw new IllegalStateException("boom"); }).subscribe().withSubscriber(test); - test.await().assertFailed().assertFailedWith(IllegalStateException.class, "boom"); + test.awaitFailure().assertFailedWith(IllegalStateException.class, "boom"); assertThat(called).isTrue(); } @@ -106,7 +104,7 @@ public void testWithAMapperThrowingAnExceptionAfterEmittingAValue() { e.complete(2); throw new IllegalStateException("boom"); }).subscribe().withSubscriber(test); - test.await().assertCompleted().assertItem(2); + assertThat(test.awaitItem().getItem()).isEqualTo(2); assertThat(called).isTrue(); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnSubscribeTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnSubscribeTest.java index 38c6d2e95..ba0bba97e 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnSubscribeTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnSubscribeTest.java @@ -315,8 +315,8 @@ public void testThatSubscriptionIsNotPassedDownstreamUntilInvokeCallbackComplete subscriber.assertNotSubscribed(); latch.countDown(); - subscriber.await() - .assertSubscribed() + subscriber.awaitSubscription() + .awaitItem() .assertCompleted().assertItem(1); } @@ -333,8 +333,8 @@ public void testThatSubscriptionIsNotPassedDownstreamUntilProducedUniCompletes() await().until(() -> emitter.get() != null); emitter.get().complete(12345); - subscriber.await() - .assertSubscribed() + subscriber.awaitSubscription() + .awaitItem() .assertCompleted().assertItem(1); } @@ -353,8 +353,8 @@ public void testThatSubscriptionIsNotPassedDownstreamUntilProducedUniCompletesWi await().until(() -> emitter.get() != null); emitter.get().complete(12345); - subscriber.await() - .assertSubscribed() + subscriber.awaitSubscription() + .awaitItem() .assertCompleted().assertItem(1); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnTerminationTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnTerminationTest.java index 49a984442..0406ee0bd 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnTerminationTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnTerminationTest.java @@ -36,7 +36,8 @@ public void testTerminationAfterDelayedItem() { .onTermination().invoke((r, f, c) -> terminate.set(r)) .subscribe().withSubscriber(UniAssertSubscriber.create()); subscriber - .await() + .awaitSubscription() + .awaitItem() .assertItem(1); assertThat(terminate).hasValue(1); } @@ -58,7 +59,7 @@ public void testTerminationWithoutParamsAfterDelayedItem() { .emitOn(Infrastructure.getDefaultExecutor()) .onTermination().invoke(() -> terminate.set(true)) .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.await().assertItem(1); + subscriber.awaitItem().assertItem(1); assertThat(terminate).isTrue(); } @@ -79,7 +80,7 @@ public void testTerminationAfterDelayedFailure() { .emitOn(Infrastructure.getDefaultExecutor()) .onTermination().invoke((r, f, c) -> terminate.set(f)) .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.await().assertFailedWith(TestException.class, "boom"); + subscriber.awaitFailure().assertFailedWith(TestException.class, "boom"); assertThat(terminate.get()).hasMessageContaining("boom").isInstanceOf(TestException.class); } @@ -100,7 +101,7 @@ public void testTerminationWithoutParameterAfterDelayedFailure() { .emitOn(Infrastructure.getDefaultExecutor()) .onTermination().invoke(() -> terminate.set(true)) .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.await().assertFailedWith(TestException.class, "boom"); + subscriber.awaitFailure().assertFailedWith(TestException.class, "boom"); assertThat(terminate).isTrue(); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOrTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOrTest.java index 0780e1cb6..76d5086cb 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOrTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOrTest.java @@ -106,7 +106,7 @@ public void testWithADelayedUni() { .of(Uni.createFrom().item("foo").onItem().delayIt().onExecutor(executor).by(Duration.ofMillis(10)), Uni.createFrom().item("bar").onItem().delayIt().onExecutor(executor).by(Duration.ofMillis(100))) .subscribe().withSubscriber(subscriber2); - subscriber2.await().assertCompleted().assertItem("foo"); + assertThat(subscriber2.awaitItem().getItem()).isEqualTo("foo"); } @RepeatedTest(100) @@ -172,6 +172,7 @@ public void testUniOrWithDelayedUni() { assertThat(c3.await().indefinitely()).isEqualTo("foo"); } + @SuppressWarnings("deprecation") @Test public void testUniOrWithDelayedUniAndDeprecatedApis() { Uni first = Uni.createFrom().item("foo").onItem().delayIt().onExecutor(executor) diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniRepeatTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniRepeatTest.java index 030c6891a..80777c1f5 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniRepeatTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniRepeatTest.java @@ -435,7 +435,7 @@ public void testFailurePropagationAfterFewRepeats() { .subscribe().withSubscriber(AssertSubscriber.create()); subscriber.request(10) - .await() + .awaitFailure() .assertItems(1, 2) .assertFailedWith(IllegalStateException.class, "boom"); assertThat(subscriber.getItems()).hasSize(2); @@ -455,7 +455,7 @@ public void testFailurePropagationAfterFewRepeatsWithRepeatUntil() { .subscribe().withSubscriber(AssertSubscriber.create()); subscriber.request(10) - .await() + .awaitFailure() .assertItems(1, 2) .assertFailedWith(IllegalStateException.class, "boom"); assertThat(subscriber.getItems()).hasSize(2); @@ -475,7 +475,7 @@ public void testFailurePropagationAfterFewRepeatsWithRepeatWhilst() { .subscribe().withSubscriber(AssertSubscriber.create()); subscriber.request(10) - .await() + .awaitFailure() .assertItems(1, 2) .assertFailedWith(IllegalStateException.class, "boom"); assertThat(subscriber.getItems()).hasSize(2); @@ -495,9 +495,8 @@ public void testFailurePropagationAfterMaxRepeats() { .subscribe().withSubscriber(AssertSubscriber.create()); subscriber.request(10) - .await() - .assertItems(1, 2) - .assertCompleted(); + .awaitCompletion() + .assertItems(1, 2); assertThat(subscriber.getItems()).hasSize(2); } @@ -515,9 +514,8 @@ public void testEmptyUniOnceInAWhileWithAtMost() { .subscribe().withSubscriber(AssertSubscriber.create()); subscriber.request(100) - .await() - .assertItems(1, 2, 4, 5, 7, 8, 10) - .assertCompleted(); + .awaitCompletion() + .assertItems(1, 2, 4, 5, 7, 8, 10); assertThat(count).hasValue(10); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniRunSubscriptionOnTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniRunSubscriptionOnTest.java index 2f7d5dd75..28ee17208 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniRunSubscriptionOnTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniRunSubscriptionOnTest.java @@ -24,7 +24,7 @@ public void testRunSubscriptionOnWithSupplier() { Uni.createFrom().item(() -> 1) .runSubscriptionOn(ForkJoinPool.commonPool()) .subscribe().withSubscriber(subscriber); - subscriber.await().assertItem(1); + subscriber.awaitItem().assertItem(1); assertThat(subscriber.getOnSubscribeThreadName()).isNotEqualTo(Thread.currentThread().getName()); } @@ -36,7 +36,7 @@ public void testWithWithImmediateValue() { .runSubscriptionOn(ForkJoinPool.commonPool()) .subscribe().withSubscriber(subscriber); - subscriber.await().assertItem(1); + subscriber.awaitItem().assertItem(1); assertThat(subscriber.getOnSubscribeThreadName()).isNotEqualTo(Thread.currentThread().getName()); } @@ -58,7 +58,7 @@ public void testWithTimeout() { .runSubscriptionOn(executorService) .subscribe().withSubscriber(subscriber); - subscriber.await().assertItem(1); + subscriber.awaitItem().assertItem(1); executorService.shutdownNow(); } @@ -71,7 +71,7 @@ public void callableEvaluatedTheRightTime() { .runSubscriptionOn(ForkJoinPool.commonPool()); assertThat(count).hasValue(0); - uni.subscribe().withSubscriber(UniAssertSubscriber.create()).await(); + uni.subscribe().withSubscriber(UniAssertSubscriber.create()).awaitItem(); assertThat(count).hasValue(1); } @@ -80,7 +80,7 @@ public void testWithFailure() { Uni.createFrom(). failure(new IOException("boom")) .runSubscriptionOn(Infrastructure.getDefaultExecutor()) .subscribe().withSubscriber(UniAssertSubscriber.create()) - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom"); } @@ -117,7 +117,7 @@ public void testRejectedTask() { .runSubscriptionOn(pool) .subscribe().withSubscriber(UniAssertSubscriber.create()); assertThat(called).isFalse(); - subscriber.await() + subscriber.awaitFailure() .assertFailedWith(RejectedExecutionException.class, ""); } @@ -135,7 +135,7 @@ public void subscribe(UniSubscriber subscriber) { .subscribe().withSubscriber(UniAssertSubscriber.create()); assertThat(called).isFalse(); subscriber - .await() + .awaitFailure() .assertFailedWith(IllegalArgumentException.class, "boom"); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniSerializedSubscriberTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniSerializedSubscriberTest.java index 1d8641afe..ef818a27a 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniSerializedSubscriberTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniSerializedSubscriberTest.java @@ -12,6 +12,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; @@ -204,13 +205,14 @@ public void testRaceBetweenItemAndFailureWithoutUniserializedSubscriber() { runnables.forEach(r -> new Thread(r).start()); - subscriber.await(); + Awaitility.await().untilAsserted(subscriber::assertTerminated); if (subscriber.getFailure() != null) { subscriber.assertFailed() .assertFailedWith(IOException.class, "boom"); } else { - subscriber.assertCompleted() + subscriber + .assertCompleted() .assertItem(1); } subscriber.assertSignalsReceivedInOrder(); @@ -245,7 +247,7 @@ public void testRaceBetweenItemAndFailureWithUniserializedSubscriber() { runnables.forEach(r -> new Thread(r).start()); - subscriber.await(); + Awaitility.await().untilAsserted(subscriber::assertTerminated); if (subscriber.getFailure() != null) { subscriber.assertFailed() @@ -292,8 +294,7 @@ public void testRaceBetweenMultipleItemsWithoutUniserializedSubscriber() { runnables.forEach(r -> new Thread(r).start()); - subscriber.await(); - subscriber.assertCompleted(); + subscriber.awaitItem(); assertThat(subscriber.getItem()).isBetween(1, 3); subscriber.assertSignalsReceivedInOrder(); } @@ -333,8 +334,7 @@ public void testRaceBetweenMultipleItemsWithUniserializedSubscriber() { runnables.forEach(r -> new Thread(r).start()); - subscriber.await(); - subscriber.assertCompleted(); + subscriber.awaitItem(); assertThat(subscriber.getItem()).isBetween(1, 3); subscriber.assertSignalsReceivedInOrder(); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniToMultiTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniToMultiTest.java index 2e7b15c68..88bd36339 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniToMultiTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniToMultiTest.java @@ -136,15 +136,13 @@ public void testFromAnUniSendingResultEventInTheFuture() { Multi multi = Uni.createFrom() .completionStage(() -> CompletableFuture.supplyAsync(count::incrementAndGet)).toMulti(); multi.subscribe().withSubscriber(AssertSubscriber.create(1)) - .await() - .assertItems(1) - .assertCompleted(); + .awaitCompletion() + .assertItems(1); multi.subscribe().withSubscriber(AssertSubscriber.create(0)).assertNotTerminated() .assertHasNotReceivedAnyItem() .request(1) - .await() - .assertItems(2) - .assertCompleted(); + .awaitCompletion() + .assertItems(2); } @Test @@ -153,15 +151,13 @@ public void testFromAnUniSendingResultEventInTheFuture2() { Multi multi = Multi.createFrom().uni(Uni.createFrom() .completionStage(() -> CompletableFuture.supplyAsync(count::incrementAndGet))); multi.subscribe().withSubscriber(AssertSubscriber.create(1)) - .await() - .assertItems(1) - .assertCompleted(); + .awaitCompletion() + .assertItems(1); multi.subscribe().withSubscriber(AssertSubscriber.create(0)).assertNotTerminated() .assertHasNotReceivedAnyItem() .request(1) - .await() - .assertItems(2) - .assertCompleted(); + .awaitCompletion() + .assertItems(2); } @Test @@ -169,15 +165,13 @@ public void testFromAnUniSendingNullResultEventInTheFuture() { Multi multi = Uni.createFrom() .completionStage(() -> CompletableFuture. supplyAsync(() -> null)).toMulti(); multi.subscribe().withSubscriber(AssertSubscriber.create(1)) - .await() - .assertHasNotReceivedAnyItem() - .assertCompleted(); + .awaitCompletion() + .assertHasNotReceivedAnyItem(); multi.subscribe().withSubscriber(AssertSubscriber.create(0)) .assertNotTerminated() .request(1) - .await() - .assertHasNotReceivedAnyItem() - .assertCompleted(); + .awaitCompletion() + .assertHasNotReceivedAnyItem(); } @Test @@ -185,14 +179,12 @@ public void testFromAnUniSendingNullResultEventInTheFuture2() { Multi multi = Multi.createFrom() .uni(Uni.createFrom().completionStage(() -> CompletableFuture.supplyAsync(() -> null))); multi.subscribe().withSubscriber(AssertSubscriber.create(1)) - .await() - .assertHasNotReceivedAnyItem() - .assertCompleted(); + .awaitCompletion() + .assertHasNotReceivedAnyItem(); multi.subscribe().withSubscriber(AssertSubscriber.create(0)) .assertNotTerminated() .request(1) - .await() - .assertHasNotReceivedAnyItem() - .assertCompleted(); + .awaitCompletion() + .assertHasNotReceivedAnyItem(); } } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniZipTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniZipTest.java index a3f8401c3..c14617fad 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniZipTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniZipTest.java @@ -88,7 +88,7 @@ public void testWithCombinator() { UniAssertSubscriber subscriber = Uni.combine().all().unis(uni1, uni2, uni3) .combinedWith((i1, i2, i3) -> i1 + i2 + i3) .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.await().assertItem(6); + subscriber.awaitItem().assertItem(6); } @Test diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/EmitterBasedMultiTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/EmitterBasedMultiTest.java index d38744639..dbe77a568 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/EmitterBasedMultiTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/EmitterBasedMultiTest.java @@ -103,7 +103,7 @@ public void testThatEmitterCannotEmitNull() { e.emit("a"); e.emit(null); }).subscribe().withSubscriber(AssertSubscriber.create(2)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertItems("a"); assertThat(terminated).isTrue(); @@ -113,7 +113,7 @@ public void testThatEmitterCannotEmitNull() { e.emit(null); }, BackPressureStrategy.LATEST) .subscribe().withSubscriber(AssertSubscriber.create(2)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertItems("a"); @@ -122,7 +122,7 @@ public void testThatEmitterCannotEmitNull() { e.emit(null); }, BackPressureStrategy.DROP) .subscribe().withSubscriber(AssertSubscriber.create(2)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertItems("a"); @@ -131,7 +131,7 @@ public void testThatEmitterCannotEmitNull() { e.emit(null); }, BackPressureStrategy.ERROR) .subscribe().withSubscriber(AssertSubscriber.create(2)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertItems("a"); @@ -140,7 +140,7 @@ public void testThatEmitterCannotEmitNull() { e.emit(null); }, BackPressureStrategy.IGNORE) .subscribe().withSubscriber(AssertSubscriber.create(2)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertItems("a"); @@ -154,7 +154,7 @@ public void testThatEmitterCannotPropagateNullAsFailure() { e.emit("a"); e.fail(null); }).subscribe().withSubscriber(AssertSubscriber.create(2)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertItems("a"); assertThat(terminated).isTrue(); @@ -164,7 +164,7 @@ public void testThatEmitterCannotPropagateNullAsFailure() { e.fail(null); }, BackPressureStrategy.LATEST) .subscribe().withSubscriber(AssertSubscriber.create(2)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertItems("a"); @@ -173,7 +173,7 @@ public void testThatEmitterCannotPropagateNullAsFailure() { e.fail(null); }, BackPressureStrategy.DROP) .subscribe().withSubscriber(AssertSubscriber.create(2)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertItems("a"); @@ -182,7 +182,7 @@ public void testThatEmitterCannotPropagateNullAsFailure() { e.fail(null); }, BackPressureStrategy.ERROR) .subscribe().withSubscriber(AssertSubscriber.create(2)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertItems("a"); @@ -191,7 +191,7 @@ public void testThatEmitterCannotPropagateNullAsFailure() { e.fail(null); }, BackPressureStrategy.IGNORE) .subscribe().withSubscriber(AssertSubscriber.create(2)) - .await() + .awaitFailure() .assertFailedWith(NullPointerException.class, "") .assertItems("a"); @@ -276,7 +276,9 @@ public void testSerializedWithConcurrentEmissionsAndFailure() { service.submit(r1); service.submit(r2); - subscriber.await().assertFailedWith(Exception.class, "boom"); + subscriber + .awaitFailure() + .assertFailedWith(Exception.class, "boom"); service.shutdown(); } diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/MultiFromResourceFromUniTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/MultiFromResourceFromUniTest.java index e54803c50..b25535b18 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/MultiFromResourceFromUniTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/MultiFromResourceFromUniTest.java @@ -396,9 +396,8 @@ public void testWithFakeTransactionalResource() { FakeTransactionalResource::cancel); multi.subscribe().withSubscriber(AssertSubscriber.create(20)) - .await() - .assertItems("in transaction") - .assertCompleted(); + .awaitCompletion() + .assertItems("in transaction"); assertThat(FakeTransactionalResource.last.subscribed).isTrue(); assertThat(FakeTransactionalResource.last.onCompleteSubscribed).isTrue(); @@ -474,9 +473,8 @@ public void testThatCancellationDueToPartialConsumptionCallsOnCancel() { .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() - .assertItems("0", "1", "2") - .assertCompleted(); + .awaitCompletion() + .assertItems("0", "1", "2"); assertThat(FakeTransactionalResource.last.subscribed).isTrue(); assertThat(FakeTransactionalResource.last.onCompleteSubscribed).isFalse(); @@ -486,17 +484,15 @@ public void testThatCancellationDueToPartialConsumptionCallsOnCancel() { @Test public void testThatCancellationFailureAreNotPropagated() { - Multi multi = Multi.createFrom() - .resourceFromUni(() -> FakeTransactionalResource.create(), FakeTransactionalResource::infinite) + .resourceFromUni(FakeTransactionalResource::create, FakeTransactionalResource::infinite) .withFinalizer(FakeTransactionalResource::commit, FakeTransactionalResource::rollback, r -> r.cancel().onItem().failWith(x -> new IOException("boom"))) .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() - .assertItems("0", "1", "2") - .assertCompleted(); + .awaitCompletion() + .assertItems("0", "1", "2"); assertThat(FakeTransactionalResource.last.subscribed).isTrue(); assertThat(FakeTransactionalResource.last.onCompleteSubscribed).isFalse(); @@ -507,15 +503,14 @@ public void testThatCancellationFailureAreNotPropagated() { @Test public void testThatCancellationReturningNullAreNotPropagated() { Multi multi = Multi.createFrom() - .resourceFromUni(() -> FakeTransactionalResource.create(), FakeTransactionalResource::infinite) + .resourceFromUni(FakeTransactionalResource::create, FakeTransactionalResource::infinite) .withFinalizer(FakeTransactionalResource::commit, FakeTransactionalResource::rollback, r -> null) .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() - .assertItems("0", "1", "2") - .assertCompleted(); + .awaitCompletion() + .assertItems("0", "1", "2"); assertThat(FakeTransactionalResource.last.subscribed).isTrue(); assertThat(FakeTransactionalResource.last.onCompleteSubscribed).isFalse(); @@ -533,7 +528,7 @@ public void testThatCompletionFailureArePropagated() { .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() + .awaitFailure() .assertItems("in transaction") .assertFailedWith(IOException.class, "boom"); @@ -553,7 +548,7 @@ public void testThatCompletionFailureArePropagated2() { .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() + .awaitFailure() .assertItems("in transaction") .assertFailedWith(IOException.class, "commit failed"); @@ -574,7 +569,7 @@ public void testWithOnCompletionReturningNull() { .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() + .awaitFailure() .assertItems("in transaction") .assertFailedWith(NullPointerException.class, "`null`"); @@ -592,7 +587,7 @@ public void testThatOnFailureFailureArePropagated() { FakeTransactionalResource::cancel); multi.subscribe().withSubscriber(AssertSubscriber.create(20)) - .await() + .awaitFailure() .assertFailedWith(CompositeException.class, "boom") .assertFailedWith(CompositeException.class, "rollback failed"); @@ -611,7 +606,7 @@ public void testWithOnFailureReturningNull() { FakeTransactionalResource::cancel); multi.subscribe().withSubscriber(AssertSubscriber.create(20)) - .await() + .awaitFailure() .assertFailedWith(CompositeException.class, "boom") .assertFailedWith(CompositeException.class, "`null`"); @@ -668,8 +663,7 @@ public void testOnCancellationWithSingleFinalizer() { }) .select().first(5); multi.subscribe().withSubscriber(AssertSubscriber.create(20)) - .await() - .assertCompleted() + .awaitCompletion() .assertItems(0L, 1L, 2L, 3L, 4L); assertThat(subscribed).isTrue(); } @@ -684,8 +678,7 @@ public void testThatOnCancellationIsNotCalledAfterCompletion() { FakeTransactionalResource::cancel) .subscribe(subscriber); subscriber - .await() - .assertCompleted() + .awaitCompletion() .cancel(); assertThat(FakeTransactionalResource.last.onCompleteSubscribed).isTrue(); @@ -704,7 +697,7 @@ public void testThatOnCancellationIsNotCalledAfterFailure() { FakeTransactionalResource::cancel) .subscribe(subscriber); subscriber - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom") .cancel(); diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/MultiFromResourceTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/MultiFromResourceTest.java index 4e0d77281..ec611dfe0 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/MultiFromResourceTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/multi/builders/MultiFromResourceTest.java @@ -454,9 +454,8 @@ public void testThatCancellationDueToPartialConsumptionCallsOnCancel() { .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() - .assertItems("0", "1", "2") - .assertCompleted(); + .awaitCompletion() + .assertItems("0", "1", "2"); assertThat(resource.subscribed).isTrue(); assertThat(resource.onCompleteSubscribed).isFalse(); @@ -474,9 +473,8 @@ public void testThatCancellationFailureAreNotPropagated() { .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() - .assertItems("0", "1", "2") - .assertCompleted(); + .awaitCompletion() + .assertItems("0", "1", "2"); assertThat(resource.subscribed).isTrue(); assertThat(resource.onCompleteSubscribed).isFalse(); @@ -494,9 +492,8 @@ public void testThatCancellationReturningNullAreNotPropagated() { .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() - .assertItems("0", "1", "2") - .assertCompleted(); + .awaitCompletion() + .assertItems("0", "1", "2"); assertThat(resource.subscribed).isTrue(); assertThat(resource.onCompleteSubscribed).isFalse(); @@ -515,7 +512,7 @@ public void testThatCompletionFailureArePropagated() { .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() + .awaitFailure() .assertItems("in transaction") .assertFailedWith(IOException.class, "boom"); @@ -536,7 +533,7 @@ public void testThatCompletionFailureArePropagated2() { .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() + .awaitFailure() .assertItems("in transaction") .assertFailedWith(IOException.class, "commit failed"); @@ -558,7 +555,7 @@ public void testWithOnCompletionReturningNull() { .select().first(3); multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)) - .await() + .awaitFailure() .assertItems("in transaction") .assertFailedWith(NullPointerException.class, "`null`"); @@ -578,7 +575,7 @@ public void testThatOnFailureFailureArePropagated() { FakeTransactionalResource::cancel); multi.subscribe().withSubscriber(AssertSubscriber.create(20)) - .await() + .awaitFailure() .assertFailedWith(CompositeException.class, "boom") .assertFailedWith(CompositeException.class, "rollback failed"); @@ -599,7 +596,7 @@ public void testWithOnFailureReturningNull() { FakeTransactionalResource::cancel); multi.subscribe().withSubscriber(AssertSubscriber.create(20)) - .await() + .awaitFailure() .assertFailedWith(CompositeException.class, "boom") .assertFailedWith(CompositeException.class, "`null`"); @@ -655,8 +652,7 @@ public void testOnCancellationWithSingleFinalizer() { }) .select().first(5); multi.subscribe().withSubscriber(AssertSubscriber.create(20)) - .await() - .assertCompleted() + .awaitCompletion() .assertItems(0L, 1L, 2L, 3L, 4L); assertThat(subscribed).isTrue(); } @@ -672,8 +668,7 @@ public void testThatOnCancellationIsNotCalledAfterCompletion() { FakeTransactionalResource::cancel) .subscribe(subscriber); subscriber - .await() - .assertCompleted() + .awaitCompletion() .cancel(); assertThat(resource.onCompleteSubscribed).isTrue(); @@ -692,7 +687,7 @@ public void testThatOnCancellationIsNotCalledAfterFailure() { FakeTransactionalResource::cancel) .subscribe(subscriber); subscriber - .await() + .awaitFailure() .assertFailedWith(IOException.class, "boom") .cancel(); diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/multi/processors/SerializedProcessorTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/multi/processors/SerializedProcessorTest.java index 8028ac11f..174249b93 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/multi/processors/SerializedProcessorTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/multi/processors/SerializedProcessorTest.java @@ -30,9 +30,8 @@ public void testAPI() { processor.onNext("hello"); processor.onComplete(); - subscriber.await() - .assertItems("hello") - .assertCompleted(); + subscriber.awaitCompletion() + .assertItems("hello"); } @Test @@ -44,9 +43,8 @@ public void testUnicastSerialized() { AssertSubscriber subscriber = AssertSubscriber.create(1); serialized.subscribe(subscriber); - subscriber.await() - .assertItems(1) - .assertCompleted(); + subscriber.awaitCompletion() + .assertItems(1); } @Test @@ -57,9 +55,8 @@ public void testUnicastSerializedWithEmpty() { AssertSubscriber subscriber = AssertSubscriber.create(1); serialized.subscribe(subscriber); - subscriber.await() - .assertHasNotReceivedAnyItem() - .assertCompleted(); + subscriber.awaitCompletion() + .assertHasNotReceivedAnyItem(); } @Test @@ -71,7 +68,7 @@ public void testUnicastSerializedWithFailure() { AssertSubscriber subscriber = AssertSubscriber.create(1); serialized.subscribe(subscriber); - subscriber.await() + subscriber.awaitFailure() .assertItems(1) .assertFailedWith(Exception.class, "boom"); } @@ -147,8 +144,8 @@ public void verifyOnErrorThreadSafety() { }); subscriber - .await() - .assertSubscribed() + .awaitSubscription() + .awaitFailure() .assertFailedWith(Exception.class, "boom"); } @@ -202,10 +199,7 @@ public void verifyOnNextOnCompleteThreadSafety() { new Thread(r).start(); }); - subscriber.await(); - subscriber - .assertSubscribed() - .assertCompleted(); + subscriber.awaitSubscription().awaitCompletion(); if (subscriber.getItems().size() != 0) { assertThat(subscriber.getItems()).containsExactly(1); @@ -230,10 +224,7 @@ public void verifyOnSubscribeOnCompleteThreadSafety() { new Thread(r).start(); }); - subscriber.await(); - subscriber - .assertSubscribed() - .assertCompleted(); + subscriber.awaitSubscription().awaitCompletion(); if (subscriber.getItems().size() != 0) { assertThat(subscriber.getItems()).containsExactly(1); @@ -241,7 +232,7 @@ public void verifyOnSubscribeOnCompleteThreadSafety() { } @RepeatedTest(100) - public void verifyOnSubscribeOnSubscribeThreadSafety() throws InterruptedException { + public void verifyOnSubscribeOnSubscribeThreadSafety() { final Processor processor = UnicastProcessor. create().serialized(); AssertSubscriber subscriber = AssertSubscriber.create(100); processor.subscribe(subscriber); @@ -258,14 +249,9 @@ public void verifyOnSubscribeOnSubscribeThreadSafety() throws InterruptedExcepti List runnables = Arrays.asList(r1, r2); Collections.shuffle(runnables); - runnables.forEach(r -> { - new Thread(r).start(); - }); + runnables.forEach(r -> new Thread(r).start()); - latch.await(); - - subscriber - .assertSubscribed(); + subscriber.awaitSubscription(); } @RepeatedTest(100) @@ -286,10 +272,9 @@ public void verifyOnFailureOnCompleteThreadSafety() { new Thread(r).start(); }); - subscriber.await(); - subscriber - .assertSubscribed() - .assertTerminated(); + subscriber.awaitSubscription(); + + await().untilAsserted(subscriber::assertTerminated); if (subscriber.getItems().size() != 0) { assertThat(subscriber.getItems()).containsExactly(1); @@ -311,10 +296,9 @@ public void verifyOnFailureOnFailureThreadSafety() { new Thread(r).start(); }); - subscriber.await(); subscriber - .assertSubscribed() - .assertTerminated() + .awaitSubscription() + .awaitFailure() .assertFailedWith(Exception.class, "boom"); } @@ -348,10 +332,7 @@ public void testRaceBetweenOnNextAndOnComplete() { new Thread(r).start(); }); - subscriber.await(); - subscriber - .assertSubscribed() - .assertCompleted(); + subscriber.awaitSubscription().awaitCompletion(); if (subscriber.getItems().size() != 0) { assertThat(subscriber.getItems()).contains(1); @@ -380,10 +361,7 @@ public void testRaceBetweenOnNextAndOnSubscribe() { new Thread(r).start(); }); - subscriber.await(); - subscriber - .assertSubscribed() - .assertCompleted(); + subscriber.awaitSubscription().awaitCompletion(); if (subscriber.getItems().size() != 0) { assertThat(subscriber.getItems()).containsExactly(1, 2, 3, 4, 5); diff --git a/implementation/src/test/java/io/smallrye/mutiny/subscription/SerializedSubscriberTest.java b/implementation/src/test/java/io/smallrye/mutiny/subscription/SerializedSubscriberTest.java index 6fc0d6101..815bbb3a6 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/subscription/SerializedSubscriberTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/subscription/SerializedSubscriberTest.java @@ -10,6 +10,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.RepeatedTest; @@ -296,7 +297,7 @@ public void testOnCompleteRace() { }; Arrays.asList(runnable, runnable).forEach(r -> new Thread(r).start()); - subscriber.await().assertCompleted(); + subscriber.awaitCompletion(); } @RepeatedTest(10) @@ -324,7 +325,7 @@ public void testOnNextOnCompleteRace() { Collections.shuffle(runnables); runnables.forEach(r -> new Thread(r).start()); - subscriber.await().assertCompleted(); + subscriber.awaitCompletion(); assertThat(subscriber.getItems()).hasSizeBetween(0, 1); } @@ -353,7 +354,7 @@ public void testOnNextOnErrorRace() { Collections.shuffle(runnables); runnables.forEach(r -> new Thread(r).start()); - subscriber.await().assertFailedWith(TestException.class, "boom"); + subscriber.awaitFailure().assertFailedWith(TestException.class, "boom"); assertThat(subscriber.getItems()).hasSizeBetween(0, 1); } @@ -382,7 +383,7 @@ public void testOnCompleteOnErrorRace() { Collections.shuffle(runnables); runnables.forEach(r -> new Thread(r).start()); - subscriber.await(); + Awaitility.await().until(() -> subscriber.hasCompleted() || subscriber.getFailure() != null); if (subscriber.hasCompleted()) { subscriber.assertCompleted().assertHasNotReceivedAnyItem(); } else { diff --git a/kotlin/src/test/kotlin/io/smallrye/mutiny/coroutines/DeferredAsUniTest.kt b/kotlin/src/test/kotlin/io/smallrye/mutiny/coroutines/DeferredAsUniTest.kt index a4a1dfc13..241a49108 100644 --- a/kotlin/src/test/kotlin/io/smallrye/mutiny/coroutines/DeferredAsUniTest.kt +++ b/kotlin/src/test/kotlin/io/smallrye/mutiny/coroutines/DeferredAsUniTest.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.async import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat import java.util.UUID import kotlin.test.Test @@ -25,7 +26,7 @@ class DeferredAsUniTest { deferred.asUni().subscribe().withSubscriber(subscriber) // Then - subscriber.await().assertItem(value) + assertThat(subscriber.awaitItem().item).isEqualTo(value) } } @@ -40,7 +41,7 @@ class DeferredAsUniTest { deferred.asUni().subscribe().withSubscriber(subscriber) // Then - subscriber.await().assertFailedWith(IllegalStateException::class.java, "kaboom") + subscriber.awaitFailure().assertFailedWith(IllegalStateException::class.java, "kaboom") } } @@ -59,7 +60,7 @@ class DeferredAsUniTest { deferred.cancel(CancellationException("abort")) // Then - subscriber.await().assertFailedWith(CancellationException::class.java, "abort") + subscriber.awaitFailure().assertFailedWith(CancellationException::class.java, "abort") } } @@ -74,7 +75,7 @@ class DeferredAsUniTest { deferred.asUni().subscribe().withSubscriber(subscriber) // Then - subscriber.await().assertItem(null) + assertThat(subscriber.awaitItem().item).isNull() } } } diff --git a/kotlin/src/test/kotlin/io/smallrye/mutiny/coroutines/FlowAsMultiTest.kt b/kotlin/src/test/kotlin/io/smallrye/mutiny/coroutines/FlowAsMultiTest.kt index 04d5d5a0d..21a9c15c0 100644 --- a/kotlin/src/test/kotlin/io/smallrye/mutiny/coroutines/FlowAsMultiTest.kt +++ b/kotlin/src/test/kotlin/io/smallrye/mutiny/coroutines/FlowAsMultiTest.kt @@ -26,7 +26,7 @@ class FlowAsMultiTest { // Then assertThat(collectedItems).containsExactly(item) - subscriber.await().assertItems(item) + subscriber.awaitCompletion().assertItems(item) } } @@ -45,7 +45,7 @@ class FlowAsMultiTest { // Then assertThat(collectedItems).containsExactly(*items) - subscriber.await().assertItems(*items) + subscriber.awaitCompletion().assertItems(*items) } } @@ -62,7 +62,8 @@ class FlowAsMultiTest { flow.asMulti().subscribe().withSubscriber(subscriber) // Then - subscriber.await().assertFailedWith(IllegalStateException::class.java, "boom") + subscriber.awaitFailure() + .assertFailedWith(IllegalStateException::class.java, "boom") } } @@ -87,7 +88,7 @@ class FlowAsMultiTest { Thread.sleep(350) // Then - subscriber.await().assertFailedWith(CancellationException::class.java, "abort") + subscriber.awaitFailure().assertFailedWith(CancellationException::class.java, "abort") } }