Skip to content

Commit

Permalink
RR client: run client on same context
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Sep 9, 2021
1 parent 5ddaf8d commit 8dd0af4
Showing 8 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -45,4 +45,9 @@ void shouldHaveApplicationScopeByDefault() {
Bean<?> resolvedBean = beanManager.resolve(beans);
assertThat(resolvedBean.getScope()).isEqualTo(ApplicationScoped.class);
}

@Test
void shouldInvokeClientResponseOnSameContext() {
assertThat(testBean.bug18977()).isEqualTo("Hello");
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package io.quarkus.rest.client.reactive;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import io.smallrye.mutiny.Uni;

@RegisterRestClient(configKey = "hello2")
public interface HelloClient2 {
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Path("/")
String echo(String name);

@GET
String bug18977();

@GET
@Path("delay")
Uni<String> delay();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package io.quarkus.rest.client.reactive;

import java.time.Duration;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;

import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Assertions;

import io.smallrye.mutiny.Uni;

@Path("/hello")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
@@ -16,4 +24,24 @@ public class HelloResource {
public String echo(String name, @Context Request request) {
return "hello, " + name;
}

@RestClient
HelloClient2 client2;

@GET
public Uni<String> something() {
Thread thread = Thread.currentThread();
return client2.delay()
.map(foo -> {
Assertions.assertSame(thread, Thread.currentThread());
return foo;
});
}

@Path("delay")
@GET
public Uni<String> delay() {
return Uni.createFrom().item("Hello")
.onItem().delayIt().by(Duration.ofMillis(500));
}
}
Original file line number Diff line number Diff line change
@@ -27,4 +27,8 @@ String helloViaBuiltClient(String name) {
.build(HelloClient.class);
return helloClient.echo(name);
}

String bug18977() {
return client2.bug18977();
}
}
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.rest.client.reactive.HelloClient2;
import io.quarkus.rest.client.reactive.HelloResource;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
@@ -24,9 +25,11 @@ public class ProviderDisabledAutodiscoveryTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(HelloResource.class, HelloClient.class, GlobalRequestFilter.class, GlobalResponseFilter.class)
.addClasses(HelloResource.class, HelloClient2.class, HelloClient.class, GlobalRequestFilter.class,
GlobalResponseFilter.class)
.addAsResource(
new StringAsset(setUrlForClass(HelloClient.class)
+ setUrlForClass(HelloClient2.class)
+ "quarkus.rest-client-reactive.provider-autodiscovery=false"),
"application.properties"));

Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.rest.client.reactive.HelloClient2;
import io.quarkus.rest.client.reactive.HelloResource;
import io.quarkus.test.QuarkusUnitTest;

@@ -23,12 +24,14 @@ public class ProviderPriorityTest {
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(HelloResource.class,
HelloClient.class,
HelloClient2.class,
HelloClientWithFilter.class,
ResponseFilterLowestPrio.class,
GlobalResponseFilter.class,
GlobalResponseFilterLowPrio.class)
.addAsResource(
new StringAsset(setUrlForClass(HelloClient.class)
+ setUrlForClass(HelloClient2.class)
+ setUrlForClass(HelloClientWithFilter.class)),
"application.properties"));

Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.rest.client.reactive.HelloClient2;
import io.quarkus.rest.client.reactive.HelloResource;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
@@ -25,11 +26,12 @@ public class ProviderTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(HelloResource.class, HelloClient.class, GlobalRequestFilter.class,
.addClasses(HelloResource.class, HelloClient2.class, HelloClient.class, GlobalRequestFilter.class,
GlobalResponseFilter.class, GlobalRequestFilterConstrainedToServer.class,
GlobalFeature.class)
.addAsResource(
new StringAsset(setUrlForClass(HelloClient.class)),
new StringAsset(setUrlForClass(HelloClient.class)
+ setUrlForClass(HelloClient2.class)),
"application.properties"));

@RestClient
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jboss.resteasy.reactive.client.impl;

import io.vertx.core.Context;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;
@@ -219,7 +220,14 @@ public HttpClientResponse getVertxClientResponse() {
@Override
protected Executor getEventLoop() {
if (httpClientRequest == null) {
return restClient.getVertx().nettyEventLoopGroup().next();
// make sure we execute the client callbacks on the same context as the current thread
Context context = restClient.getVertx().getOrCreateContext();
return new Executor() {
@Override
public void execute(Runnable command) {
context.runOnContext(v -> command.run());
}
};
} else {
return new Executor() {
@Override

0 comments on commit 8dd0af4

Please sign in to comment.