Search in sources :

Example 1 with UniSubscription

use of io.smallrye.mutiny.subscription.UniSubscription in project smallrye-mutiny by smallrye.

the class UniCallbackSubscriber method onFailure.

@Override
public final void onFailure(Throwable t) {
    UniSubscription sub = SUBSCRIPTION_UPDATER.getAndSet(this, CANCELLED);
    if (sub == CANCELLED) {
        // Already cancelled, do nothing
        return;
    }
    onFailureCallback.accept(t);
}
Also used : UniSubscription(io.smallrye.mutiny.subscription.UniSubscription)

Example 2 with UniSubscription

use of io.smallrye.mutiny.subscription.UniSubscription in project smallrye-mutiny by smallrye.

the class UniSerializedSubscriberTest method testDroppedExceptionsWhenOnFailureCalledMultipleTimes.

@Test
public void testDroppedExceptionsWhenOnFailureCalledMultipleTimes() {
    AtomicReference<Throwable> received = new AtomicReference<>();
    AtomicReference<Throwable> captured = new AtomicReference<>();
    Infrastructure.setDroppedExceptionHandler(captured::set);
    AtomicReference<UniSubscriber<? super Integer>> sub = new AtomicReference<>();
    AbstractUni<Integer> uni = new AbstractUni<Integer>() {

        @Override
        public void subscribe(UniSubscriber<? super Integer> subscriber) {
            sub.set(subscriber);
        }
    };
    UniSerializedSubscriber.subscribe(uni, new UniSubscriber<Integer>() {

        @Override
        public Context context() {
            return Context.empty();
        }

        @Override
        public void onSubscribe(UniSubscription subscription) {
        // Do nothing
        }

        @Override
        public void onItem(Integer item) {
        // Do nothing
        }

        @Override
        public void onFailure(Throwable failure) {
            received.set(failure);
        }
    });
    sub.get().onSubscribe(mock(UniSubscription.class));
    sub.get().onFailure(new IOException("I/O"));
    assertThat(captured.get()).isNull();
    assertThat(received.get()).isInstanceOf(IOException.class).hasMessageContaining("I/O");
    sub.get().onFailure(new IllegalStateException("boom"));
    assertThat(captured.get()).isNotNull().isInstanceOf(IllegalStateException.class).hasMessageContaining("boom");
    assertThat(received.get()).isInstanceOf(IOException.class).hasMessageContaining("I/O");
}
Also used : Context(io.smallrye.mutiny.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) UniSubscription(io.smallrye.mutiny.subscription.UniSubscription) IOException(java.io.IOException) UniSubscriber(io.smallrye.mutiny.subscription.UniSubscriber) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 3 with UniSubscription

use of io.smallrye.mutiny.subscription.UniSubscription in project smallrye-mutiny by smallrye.

the class UniOnSubscribeTest method testOnSubscribed.

@Test
public void testOnSubscribed() {
    AtomicInteger count = new AtomicInteger();
    AtomicReference<UniSubscription> reference = new AtomicReference<>();
    Uni<Integer> uni = Uni.createFrom().item(1).onSubscription().invoke(s -> {
        reference.set(s);
        count.incrementAndGet();
    });
    UniAssertSubscriber<Integer> subscriber = UniAssertSubscriber.create();
    assertThat(count).hasValue(0);
    assertThat(reference).hasValue(null);
    uni.subscribe().withSubscriber(subscriber);
    assertThat(count).hasValue(1);
    assertThat(reference).doesNotHaveValue(null);
    uni.subscribe().withSubscriber(subscriber);
    assertThat(count).hasValue(2);
    assertThat(reference).doesNotHaveValue(null);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) UniSubscription(io.smallrye.mutiny.subscription.UniSubscription) Test(org.junit.jupiter.api.Test)

Example 4 with UniSubscription

use of io.smallrye.mutiny.subscription.UniSubscription in project smallrye-mutiny by smallrye.

the class UniOnSubscribeTest method testCallWithSupplier.

@Test
public void testCallWithSupplier() {
    AtomicInteger count = new AtomicInteger();
    AtomicReference<UniSubscription> sub = new AtomicReference<>();
    Uni<Integer> uni = Uni.createFrom().item(1).onSubscription().call(() -> {
        count.incrementAndGet();
        return Uni.createFrom().nullItem().onSubscription().invoke(sub::set);
    });
    UniAssertSubscriber<Integer> subscriber = UniAssertSubscriber.create();
    assertThat(count).hasValue(0);
    assertThat(sub).hasValue(null);
    uni.subscribe().withSubscriber(subscriber);
    assertThat(count).hasValue(1);
    assertThat(sub).doesNotHaveValue(null);
    uni.subscribe().withSubscriber(subscriber);
    assertThat(count).hasValue(2);
    assertThat(sub).doesNotHaveValue(null);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) UniSubscription(io.smallrye.mutiny.subscription.UniSubscription) Test(org.junit.jupiter.api.Test)

Example 5 with UniSubscription

use of io.smallrye.mutiny.subscription.UniSubscription in project smallrye-mutiny by smallrye.

the class UniOnSubscribeTest method testDelayedCallAfterFailure.

@Test
public void testDelayedCallAfterFailure() {
    AtomicInteger count = new AtomicInteger();
    AtomicReference<UniSubscription> reference = new AtomicReference<>();
    AtomicReference<UniSubscription> sub = new AtomicReference<>();
    Uni<Object> uni = Uni.createFrom().failure(new IOException("boom")).onSubscription().call(s -> {
        reference.set(s);
        count.incrementAndGet();
        return Uni.createFrom().emitter(e -> {
            new Thread(() -> {
                await().during(100, TimeUnit.MILLISECONDS);
                e.complete("yo");
            }).start();
        }).onSubscription().invoke(sub::set);
    });
    UniAssertSubscriber<Object> subscriber = UniAssertSubscriber.create();
    assertThat(count).hasValue(0);
    assertThat(reference).hasValue(null);
    assertThat(sub).hasValue(null);
    uni.subscribe().withSubscriber(subscriber);
    assertThat(count).hasValue(1);
    assertThat(reference).doesNotHaveValue(null);
    assertThat(sub).doesNotHaveValue(null);
    uni.subscribe().withSubscriber(subscriber);
    assertThat(count).hasValue(2);
    assertThat(reference).doesNotHaveValue(null);
    assertThat(sub).doesNotHaveValue(null);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) UniSubscription(io.smallrye.mutiny.subscription.UniSubscription) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

UniSubscription (io.smallrye.mutiny.subscription.UniSubscription)12 Test (org.junit.jupiter.api.Test)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 Context (io.smallrye.mutiny.Context)5 IOException (java.io.IOException)5 RepeatedTest (org.junit.jupiter.api.RepeatedTest)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 UniEmitter (io.smallrye.mutiny.subscription.UniEmitter)2 UniSubscriber (io.smallrye.mutiny.subscription.UniSubscriber)2 TimeoutException (io.smallrye.mutiny.TimeoutException)1 CompletionException (java.util.concurrent.CompletionException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1