Search in sources :

Example 56 with Multi

use of io.smallrye.mutiny.Multi in project smallrye-reactive-messaging by smallrye.

the class ReactiveKafkaConsumerTest method testGroupingRecordsByPartition.

@Test
// seems to fail on CI once in a while
@Tag(TestTags.FLAKY)
public void testGroupingRecordsByPartition() throws Exception {
    int count = 10000;
    String groupId = UUID.randomUUID().toString();
    MapBasedConfig config = createConsumerConfig(groupId).put("topic", topic);
    KafkaSource<Integer, String> source = createSource(config, groupId);
    Multi<IncomingKafkaRecord<Integer, String>> stream = source.getStream();
    CountDownLatch latch = new CountDownLatch(count);
    AtomicInteger concurrentPartitionExecutions = new AtomicInteger();
    Map<Integer, String> inProgressMap = new ConcurrentHashMap<>();
    int maxProcessingMs = 5;
    this.receiveTimeoutMillis = maxProcessingMs * count + 5000;
    stream.group().by(IncomingKafkaRecord::getPartition).subscribe().with(recFromPartition -> recFromPartition.emitOn(Infrastructure.getDefaultWorkerPool()).subscribe().with(record -> {
        int partition = record.getPartition();
        String current = Thread.currentThread().getName() + ":" + record.getOffset();
        String inProgress = inProgressMap.putIfAbsent(partition, current);
        if (inProgress != null) {
            concurrentPartitionExecutions.incrementAndGet();
        }
        onReceive(record);
        latch.countDown();
        record.ack();
        inProgressMap.remove(partition);
    }));
    waitForPartitionAssignment();
    sendMessages(0, count);
    waitForMessages(latch);
    assertThat(concurrentPartitionExecutions.get()).isEqualTo(0);
    checkConsumedMessages(0, count);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) java.util(java.util) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) AUTO_COMMIT_INTERVAL_MS_CONFIG(org.apache.kafka.clients.consumer.ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Multi(io.smallrye.mutiny.Multi) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Uni(io.smallrye.mutiny.Uni) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SingletonInstance(io.smallrye.reactive.messaging.kafka.base.SingletonInstance) Duration(java.time.Duration) CountKafkaCdiEvents(io.smallrye.reactive.messaging.kafka.CountKafkaCdiEvents) METADATA_MAX_AGE_CONFIG(org.apache.kafka.clients.consumer.ConsumerConfig.METADATA_MAX_AGE_CONFIG) TopicPartition(org.apache.kafka.common.TopicPartition) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) Awaitility.await(org.awaitility.Awaitility.await) TestTags(io.smallrye.reactive.messaging.kafka.TestTags) java.util.concurrent(java.util.concurrent) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) UnsatisfiedInstance(io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance) Collectors(java.util.stream.Collectors) KafkaConsumerRebalanceListener(io.smallrye.reactive.messaging.kafka.KafkaConsumerRebalanceListener) org.junit.jupiter.api(org.junit.jupiter.api) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) KafkaSource(io.smallrye.reactive.messaging.kafka.impl.KafkaSource) IncomingKafkaRecord(io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) IncomingKafkaRecord(io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord)

Example 57 with Multi

use of io.smallrye.mutiny.Multi in project smallrye-graphql by smallrye.

the class ReflectionInvoker method invoke.

public <T> T invoke(Object... arguments) throws Exception {
    if (this.injectContextAt > -1) {
        arguments = injectContext(arguments);
    }
    try {
        ManagedInstance<?> operationInstance = lookupService.getInstance(operationClass);
        Object operationInstance1 = operationInstance.get();
        eventEmitter.fireBeforeMethodInvoke(new InvokeInfo(operationInstance1, method, arguments));
        T result = (T) method.invoke(operationInstance1, arguments);
        if (result instanceof Uni) {
            return (T) ((Uni) result).onTermination().invoke(() -> {
                operationInstance.destroyIfNecessary();
            });
        } else if (result instanceof Multi) {
            return (T) ((Multi) result).onTermination().invoke(() -> {
                operationInstance.destroyIfNecessary();
            });
        } else {
            operationInstance.destroyIfNecessary();
            return result;
        }
    } catch (InvocationTargetException ex) {
        // Invoked method has thrown something, unwrap
        Throwable throwable = ex.getCause();
        if (throwable instanceof Error) {
            throw (Error) throwable;
        } else if (throwable instanceof GraphQLException) {
            throw (GraphQLException) throwable;
        } else if (throwable instanceof Exception) {
            throw (Exception) throwable;
        } else {
            throw msg.generalDataFetcherException(operationClass.getName() + ": " + method.getName(), throwable);
        }
    }
}
Also used : Uni(io.smallrye.mutiny.Uni) InvokeInfo(io.smallrye.graphql.execution.event.InvokeInfo) Multi(io.smallrye.mutiny.Multi) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PrivilegedActionException(java.security.PrivilegedActionException) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 58 with Multi

use of io.smallrye.mutiny.Multi in project smallrye-graphql by smallrye.

the class MultiDataFetcher method invokeAndTransform.

@Override
@SuppressWarnings("unchecked")
protected <O> O invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.Builder<Object> resultBuilder, Object[] transformedArguments) throws Exception {
    SmallRyeContext context = contextHelper.getSmallRyeContext(dfe);
    try {
        SmallRyeContext.setContext(context);
        Multi<?> multi = operationInvoker.invoke(transformedArguments);
        return (O) multi.onItem().transform((t) -> {
            try {
                Object resultFromTransform = fieldHelper.transformOrAdaptResponse(t, dfe);
                resultBuilder.data(resultFromTransform);
                return (O) resultBuilder.build();
            } catch (AbstractDataFetcherException abstractDataFetcherException) {
                // Arguments or result couldn't be transformed
                abstractDataFetcherException.appendDataFetcherResult(resultBuilder, dfe);
                eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), abstractDataFetcherException);
                return (O) resultBuilder.build();
            }
        }).onFailure().recoverWithItem(new Function<Throwable, O>() {

            public O apply(Throwable throwable) {
                eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
                if (throwable instanceof GraphQLException) {
                    GraphQLException graphQLException = (GraphQLException) throwable;
                    errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
                } else if (throwable instanceof Exception) {
                    DataFetcherException dataFetcherException = SmallRyeGraphQLServerMessages.msg.dataFetcherException(operation, throwable);
                    errorResultHelper.appendException(resultBuilder, dfe, dataFetcherException);
                } else if (throwable instanceof Error) {
                    errorResultHelper.appendException(resultBuilder, dfe, throwable);
                }
                return (O) resultBuilder.build();
            }
        });
    } finally {
        SmallRyeContext.remove();
    }
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) SmallRyeGraphQLServerMessages(io.smallrye.graphql.SmallRyeGraphQLServerMessages) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) Function(java.util.function.Function) Multi(io.smallrye.mutiny.Multi) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) BatchLoaderEnvironment(org.dataloader.BatchLoaderEnvironment) Operation(io.smallrye.graphql.schema.model.Operation) DataFetcherResult(graphql.execution.DataFetcherResult) Function(java.util.function.Function) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException)

Example 59 with Multi

use of io.smallrye.mutiny.Multi in project smallrye-graphql by smallrye.

the class PublisherDataFetcher method invokeAndTransform.

@Override
@SuppressWarnings("unchecked")
protected <O> O invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.Builder<Object> resultBuilder, Object[] transformedArguments) throws Exception {
    SmallRyeContext context = contextHelper.getSmallRyeContext(dfe);
    try {
        SmallRyeContext.setContext(context);
        Publisher<?> publisher = operationInvoker.invoke(transformedArguments);
        Multi<?> multi = Multi.createFrom().publisher(publisher);
        return (O) multi.onItem().transform((t) -> {
            try {
                Object resultFromTransform = fieldHelper.transformOrAdaptResponse(t, dfe);
                resultBuilder.data(resultFromTransform);
                return (O) resultBuilder.build();
            } catch (AbstractDataFetcherException abstractDataFetcherException) {
                // Arguments or result couldn't be transformed
                abstractDataFetcherException.appendDataFetcherResult(resultBuilder, dfe);
                eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), abstractDataFetcherException);
                return (O) resultBuilder.build();
            }
        }).onFailure().recoverWithItem(new Function<Throwable, O>() {

            @Override
            public O apply(Throwable throwable) {
                eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
                if (throwable instanceof GraphQLException) {
                    GraphQLException graphQLException = (GraphQLException) throwable;
                    errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
                } else if (throwable instanceof Exception) {
                    DataFetcherException dataFetcherException = SmallRyeGraphQLServerMessages.msg.dataFetcherException(operation, throwable);
                    errorResultHelper.appendException(resultBuilder, dfe, dataFetcherException);
                } else if (throwable instanceof Error) {
                    errorResultHelper.appendException(resultBuilder, dfe, throwable);
                }
                return (O) resultBuilder.build();
            }
        });
    } finally {
        SmallRyeContext.remove();
    }
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) SmallRyeGraphQLServerMessages(io.smallrye.graphql.SmallRyeGraphQLServerMessages) Publisher(org.reactivestreams.Publisher) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) Function(java.util.function.Function) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) BatchLoaderEnvironment(org.dataloader.BatchLoaderEnvironment) Operation(io.smallrye.graphql.schema.model.Operation) DataFetcherResult(graphql.execution.DataFetcherResult) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) Function(java.util.function.Function) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException)

Example 60 with Multi

use of io.smallrye.mutiny.Multi in project smallrye-mutiny-vertx-bindings by smallrye.

the class OtherWebClientTest method testPost.

@Test
public void testPost() {
    int times = 5;
    waitFor(times);
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
    server.requestStream().handler(req -> req.bodyHandler(buff -> {
        assertEquals("onetwothree", buff.toString());
        req.response().endAndForget();
    }));
    try {
        server.listenAndAwait();
        client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
        Multi<Buffer> stream = Multi.createFrom().items(Buffer.buffer("one"), Buffer.buffer("two"), Buffer.buffer("three"));
        Uni<HttpResponse<Buffer>> uni = client.post(8080, "localhost", "/the_uri").sendStream(stream);
        for (int i = 0; i < times; i++) {
            uni.subscribe().with(resp -> complete(), this::fail);
        }
        await();
    } finally {
        server.closeAndAwait();
    }
}
Also used : HttpResponse(io.vertx.mutiny.ext.web.client.HttpResponse) Test(org.junit.Test) VertxTestBase(io.vertx.test.core.VertxTestBase) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Buffer(io.vertx.mutiny.core.buffer.Buffer) HttpServer(io.vertx.mutiny.core.http.HttpServer) Vertx(io.vertx.mutiny.core.Vertx) WebClient(io.vertx.mutiny.ext.web.client.WebClient) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) HttpClientOptions(io.vertx.core.http.HttpClientOptions) BodyCodec(io.vertx.mutiny.ext.web.codec.BodyCodec) Buffer(io.vertx.mutiny.core.buffer.Buffer) HttpServer(io.vertx.mutiny.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpResponse(io.vertx.mutiny.ext.web.client.HttpResponse) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Aggregations

Multi (io.smallrye.mutiny.Multi)62 Uni (io.smallrye.mutiny.Uni)43 Test (org.junit.jupiter.api.Test)43 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)42 AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)41 List (java.util.List)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)34 IOException (java.io.IOException)33 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)31 Function (java.util.function.Function)30 Duration (java.time.Duration)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)25 Consumer (java.util.function.Consumer)25 InfrastructureResource (junit5.support.InfrastructureResource)22 ResourceAccessMode (org.junit.jupiter.api.parallel.ResourceAccessMode)22 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)22 Supplier (java.util.function.Supplier)20 CompositeException (io.smallrye.mutiny.CompositeException)19 ArrayList (java.util.ArrayList)18