Search in sources :

Example 1 with MultiSubscriber

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

the class MultiSelectFirstOrLastTest method testSelectByTimeWithRogueUpstreamSendingFailureAfterCompletion.

@Test
public void testSelectByTimeWithRogueUpstreamSendingFailureAfterCompletion() {
    Multi<Integer> rogue = new AbstractMulti<Integer>() {

        @Override
        public void subscribe(MultiSubscriber<? super Integer> subscriber) {
            subscriber.onItem(1);
            subscriber.onItem(2);
            subscriber.onComplete();
            subscriber.onItem(3);
            subscriber.onError(new IOException("boom"));
        }
    };
    AssertSubscriber<Integer> subscriber = rogue.select().first(Duration.ofMillis(1000)).subscribe().withSubscriber(AssertSubscriber.create(100)).awaitCompletion();
    assertThat(subscriber.getItems()).hasSize(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException) MultiSubscriber(io.smallrye.mutiny.subscription.MultiSubscriber) Test(org.junit.jupiter.api.Test)

Example 2 with MultiSubscriber

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

the class MultiSelectFirstOrLastTest method testSelectByTimeWithRogueUpstreamSendingCompletionAfterFailure.

@Test
public void testSelectByTimeWithRogueUpstreamSendingCompletionAfterFailure() {
    Multi<Integer> rogue = new AbstractMulti<Integer>() {

        @Override
        public void subscribe(MultiSubscriber<? super Integer> subscriber) {
            subscriber.onItem(1);
            subscriber.onItem(2);
            subscriber.onError(new IOException("boom"));
            subscriber.onItem(3);
            subscriber.onComplete();
        }
    };
    AssertSubscriber<Integer> subscriber = rogue.select().first(Duration.ofMillis(1000)).subscribe().withSubscriber(AssertSubscriber.create(100)).awaitFailure().assertFailedWith(IOException.class, "boom");
    assertThat(subscriber.getItems()).hasSize(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException) MultiSubscriber(io.smallrye.mutiny.subscription.MultiSubscriber) Test(org.junit.jupiter.api.Test)

Example 3 with MultiSubscriber

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

the class MultiGroupTest method testSkipGroupWithRogueFailures.

@Test
public void testSkipGroupWithRogueFailures() {
    AtomicReference<Throwable> caught = new AtomicReference<>();
    Infrastructure.setDroppedExceptionHandler(caught::set);
    AbstractMulti<Integer> rogue = new AbstractMulti<Integer>() {

        @Override
        public void subscribe(MultiSubscriber<? super Integer> subscriber) {
            subscriber.onSubscribe(mock(Subscription.class));
            subscriber.onItem(1);
            subscriber.onItem(2);
            subscriber.onItem(3);
            subscriber.onItem(4);
            subscriber.onFailure(new IOException("boom-1"));
            subscriber.onItem(5);
            subscriber.onItem(6);
            subscriber.onFailure(new IOException("boom-2"));
            subscriber.onCompletion();
        }
    };
    AssertSubscriber<List<Integer>> subscriber = rogue.group().intoLists().of(2, 4).subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
    subscriber.assertFailedWith(IOException.class, "boom-1");
    assertThat(caught.get()).hasMessageContaining("boom-2");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Subscription(org.reactivestreams.Subscription) MultiSubscriber(io.smallrye.mutiny.subscription.MultiSubscriber) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 4 with MultiSubscriber

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

the class MultiGroupTest method testOverlapGroupAsMultisWithRogueFailures.

@Test
public void testOverlapGroupAsMultisWithRogueFailures() {
    AtomicReference<Throwable> caught = new AtomicReference<>();
    Infrastructure.setDroppedExceptionHandler(caught::set);
    AbstractMulti<Integer> rogue = new AbstractMulti<Integer>() {

        @Override
        public void subscribe(MultiSubscriber<? super Integer> subscriber) {
            subscriber.onSubscribe(mock(Subscription.class));
            subscriber.onItem(1);
            subscriber.onItem(2);
            subscriber.onItem(3);
            subscriber.onItem(4);
            subscriber.onFailure(new IOException("boom-1"));
            subscriber.onItem(5);
            subscriber.onItem(6);
            subscriber.onFailure(new IOException("boom-2"));
            subscriber.onCompletion();
        }
    };
    AssertSubscriber<Integer> subscriber = rogue.group().intoMultis().of(4, 2).onItem().transformToMultiAndConcatenate(m -> m).subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
    subscriber.assertFailedWith(IOException.class, "boom-1");
    assertThat(caught.get()).hasMessageContaining("boom-2");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Subscription(org.reactivestreams.Subscription) MultiSubscriber(io.smallrye.mutiny.subscription.MultiSubscriber) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 5 with MultiSubscriber

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

the class ResourceMulti method subscribe.

@Override
public void subscribe(MultiSubscriber<? super I> subscriber) {
    R resource;
    try {
        resource = resourceSupplier.get();
        if (resource == null) {
            throw new IllegalArgumentException(ParameterValidation.SUPPLIER_PRODUCED_NULL);
        }
    } catch (Throwable e) {
        Subscriptions.fail(subscriber, e);
        return;
    }
    Publisher<? extends I> stream;
    try {
        stream = streamSupplier.apply(resource);
        if (stream == null) {
            throw new IllegalArgumentException(ParameterValidation.SUPPLIER_PRODUCED_NULL);
        }
    } catch (Throwable e) {
        try {
            Uni<Void> uni = onFailure.apply(resource, e);
            if (uni == null) {
                Subscriptions.fail(subscriber, new NullPointerException("Unable to call the finalizer - it returned `null`"));
            } else {
                uni.subscribe().with(completed -> Subscriptions.fail(subscriber, e), failed -> Subscriptions.fail(subscriber, new CompositeException(e, failed)));
            }
        } catch (Throwable ex) {
            Subscriptions.fail(subscriber, new CompositeException(e, ex));
            return;
        }
        Subscriptions.fail(subscriber, e);
        return;
    }
    ResourceSubscriber<I, R> us = new ResourceSubscriber<>(subscriber, resource, onCompletion, onFailure, onCancellation);
    stream.subscribe(us);
}
Also used : Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) AbstractMulti(io.smallrye.mutiny.operators.AbstractMulti) ContextSupport(io.smallrye.mutiny.subscription.ContextSupport) BiFunction(java.util.function.BiFunction) Publisher(org.reactivestreams.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Context(io.smallrye.mutiny.Context) Uni(io.smallrye.mutiny.Uni) Subscriptions(io.smallrye.mutiny.helpers.Subscriptions) CompositeException(io.smallrye.mutiny.CompositeException) ParameterValidation(io.smallrye.mutiny.helpers.ParameterValidation) Subscription(org.reactivestreams.Subscription) MultiSubscriber(io.smallrye.mutiny.subscription.MultiSubscriber) Uni(io.smallrye.mutiny.Uni) CompositeException(io.smallrye.mutiny.CompositeException)

Aggregations

MultiSubscriber (io.smallrye.mutiny.subscription.MultiSubscriber)13 Test (org.junit.jupiter.api.Test)12 IOException (java.io.IOException)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Subscription (org.reactivestreams.Subscription)9 RepeatedTest (org.junit.jupiter.api.RepeatedTest)8 AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)3 Subscriber (org.reactivestreams.Subscriber)3 CompositeException (io.smallrye.mutiny.CompositeException)1 Context (io.smallrye.mutiny.Context)1 Uni (io.smallrye.mutiny.Uni)1 ParameterValidation (io.smallrye.mutiny.helpers.ParameterValidation)1 Subscriptions (io.smallrye.mutiny.helpers.Subscriptions)1 Infrastructure (io.smallrye.mutiny.infrastructure.Infrastructure)1 AbstractMulti (io.smallrye.mutiny.operators.AbstractMulti)1 ContextSupport (io.smallrye.mutiny.subscription.ContextSupport)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1