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[]
}
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[]
}
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);
}
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));
}
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");
}
Aggregations