Search in sources :

Example 1 with Context

use of io.smallrye.mutiny.Context in project smallrye-mutiny by smallrye.

the class ContextPassingTest method sampleUsageAttachedContext.

@Test
void sampleUsageAttachedContext() {
    Multi<Integer> pipeline = Multi.createFrom().range(1, 10);
    String customerId = "1234";
    // tag::contextAttachedSampleUsage[]
    Context context = Context.of("X-CUSTOMER-ID", customerId);
    pipeline.attachContext().onItem().transformToUniAndMerge(item -> makeRequest(item.get(), item.context().get("X-CUSTOMER-ID"))).subscribe().with(context, item -> handleResponse(item), err -> handleFailure(err));
// end::contextAttachedSampleUsage[]
}
Also used : Context(io.smallrye.mutiny.Context) Test(org.junit.jupiter.api.Test)

Example 2 with Context

use of io.smallrye.mutiny.Context in project smallrye-mutiny by smallrye.

the class ContextPassingTest method emitterUsage.

@Test
void emitterUsage() {
    Context context = Context.of("X-SPAN-ID", "1234");
    // tag::emitterUsage[]
    Multi.createFrom().emitter(emitter -> {
        String customerId = emitter.context().get("X-SPAN-ID");
        for (int i = 0; i < 10; i++) {
            emitter.emit("@" + i + " [" + customerId + "]");
        }
        emitter.complete();
    });
// end::emitterUsage[]
}
Also used : Context(io.smallrye.mutiny.Context) Test(org.junit.jupiter.api.Test)

Example 3 with Context

use of io.smallrye.mutiny.Context in project smallrye-mutiny by smallrye.

the class BlockingIterable method create.

private SubscriberIterator<T> create() {
    Queue<T> queue = null;
    // Create the instance of queue, check for failure and `null` values.
    try {
        queue = supplier.get();
    } catch (Throwable e) {
        propagateFailure(e);
    }
    if (queue == null) {
        throw new IllegalStateException(SUPPLIER_PRODUCED_NULL);
    }
    Context context = null;
    try {
        context = contextSupplier.get();
    } catch (Throwable e) {
        propagateFailure(e);
    }
    if (context == null) {
        throw new IllegalStateException(SUPPLIER_PRODUCED_NULL);
    }
    return new SubscriberIterator<>(queue, batchSize, context);
}
Also used : Context(io.smallrye.mutiny.Context)

Example 4 with Context

use of io.smallrye.mutiny.Context in project smallrye-mutiny by smallrye.

the class DeferredMultiWithContext method subscribe.

@Override
public void subscribe(MultiSubscriber<? super T> downstream) {
    Context context;
    if (downstream instanceof ContextSupport) {
        context = ((ContextSupport) downstream).context();
    } else {
        context = Context.empty();
    }
    Multi<? extends T> multi;
    try {
        multi = mapper.apply(context);
        if (multi == null) {
            throw new NullPointerException(ParameterValidation.MAPPER_RETURNED_NULL);
        }
    } catch (Throwable failure) {
        Subscriptions.fail(downstream, failure);
        return;
    }
    multi.subscribe(Infrastructure.onMultiSubscription(multi, downstream));
}
Also used : Context(io.smallrye.mutiny.Context) ContextSupport(io.smallrye.mutiny.subscription.ContextSupport)

Example 5 with Context

use of io.smallrye.mutiny.Context 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)

Aggregations

Context (io.smallrye.mutiny.Context)15 Test (org.junit.jupiter.api.Test)9 UniSubscription (io.smallrye.mutiny.subscription.UniSubscription)5 RepeatedTest (org.junit.jupiter.api.RepeatedTest)5 IOException (java.io.IOException)4 ContextSupport (io.smallrye.mutiny.subscription.ContextSupport)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Multi (io.smallrye.mutiny.Multi)2 UniEmitter (io.smallrye.mutiny.subscription.UniEmitter)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Uni (io.smallrye.mutiny.Uni)1 UniSubscriber (io.smallrye.mutiny.subscription.UniSubscriber)1 Assertions (org.junit.jupiter.api.Assertions)1