From b4a0b318cf947e175c2c5c842fa5fabc3ffe6594 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 5 Aug 2021 11:16:16 +0300 Subject: [PATCH] Add test for rest-client reactive timeout Follow up on: #19223 --- ...tTest.java => BuilderReadTimeoutTest.java} | 42 +++++++++- .../rest/client/reactive/timeout/Client.java | 19 ----- .../timeout/RegisterReadTimeoutTest.java | 82 +++++++++++++++++++ .../client/reactive/timeout/Resource.java | 36 -------- 4 files changed, 123 insertions(+), 56 deletions(-) rename extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/{ReadTimeoutTest.java => BuilderReadTimeoutTest.java} (65%) delete mode 100644 extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/Client.java create mode 100644 extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/RegisterReadTimeoutTest.java delete mode 100644 extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/Resource.java diff --git a/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/ReadTimeoutTest.java b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/BuilderReadTimeoutTest.java similarity index 65% rename from extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/ReadTimeoutTest.java rename to extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/BuilderReadTimeoutTest.java index 33d1a86d32612..c236c8928460e 100644 --- a/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/ReadTimeoutTest.java +++ b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/BuilderReadTimeoutTest.java @@ -4,9 +4,17 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.net.URI; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + import org.eclipse.microprofile.rest.client.RestClientBuilder; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; @@ -16,7 +24,7 @@ import io.quarkus.test.QuarkusUnitTest; import io.quarkus.test.common.http.TestHTTPResource; -public class ReadTimeoutTest { +public class BuilderReadTimeoutTest { @TestHTTPResource URI uri; @@ -55,4 +63,36 @@ void shouldNotTimeoutOnDefaultTimeout() { assertThat(client.slow()).isEqualTo("slow-response"); } + + @Produces(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN) + public static interface Client { + @GET + @Path("/slow") + String slow(); + + @GET + @Path("/fast") + String fast(); + } + + @Path("/") + @Produces(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN) + public static class Resource { + + @Path("/slow") + @GET + public String slow() throws InterruptedException { + Thread.sleep(5000L); + return "slow-response"; + } + + @Path("/fast") + @GET + public CompletionStage fast() { + return CompletableFuture.completedFuture("fast-response"); + } + + } } diff --git a/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/Client.java b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/Client.java deleted file mode 100644 index de2f2ffeea031..0000000000000 --- a/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/Client.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.quarkus.rest.client.reactive.timeout; - -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -@Produces(MediaType.TEXT_PLAIN) -@Consumes(MediaType.TEXT_PLAIN) -public interface Client { - @GET - @Path("/slow") - String slow(); - - @GET - @Path("/fast") - String fast(); -} diff --git a/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/RegisterReadTimeoutTest.java b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/RegisterReadTimeoutTest.java new file mode 100644 index 0000000000000..4e9df6788b66f --- /dev/null +++ b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/RegisterReadTimeoutTest.java @@ -0,0 +1,82 @@ +package io.quarkus.rest.client.reactive.timeout; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.TimeoutException; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; +import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; + +public class RegisterReadTimeoutTest { + + @RestClient + Client client; + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) + .addClasses(Client.class, Resource.class) + .addAsResource(new StringAsset( + "client/mp-rest/readTimeout=1000\nclient/mp-rest/url=http://${quarkus.http.host}:${quarkus.http.test-port}"), + "application.properties")); + + @Test + void shouldTimeoutIfReadTimeoutSetShort() { + RuntimeException exception = assertThrows(RuntimeException.class, client::slow); + assertThat(exception).hasCauseInstanceOf(TimeoutException.class); + } + + @Test + void shouldNotTimeoutOnFastResponse() { + assertThat(client.fast()).isEqualTo("fast-response"); + } + + @Produces(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN) + @RegisterRestClient(configKey = "client") + public interface Client { + @GET + @Path("/slow") + String slow(); + + @GET + @Path("/fast") + String fast(); + } + + @Path("/") + @Produces(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN) + public static class Resource { + + @Path("/slow") + @GET + public String slow() throws InterruptedException { + Thread.sleep(5000L); + return "slow-response"; + } + + @Path("/fast") + @GET + public CompletionStage fast() { + return CompletableFuture.completedFuture("fast-response"); + } + + } +} diff --git a/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/Resource.java b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/Resource.java deleted file mode 100644 index 968c1b849ad80..0000000000000 --- a/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/timeout/Resource.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.quarkus.rest.client.reactive.timeout; - -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; - -import javax.inject.Inject; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -import io.vertx.core.Vertx; - -@Path("/") -@Produces(MediaType.TEXT_PLAIN) -@Consumes(MediaType.TEXT_PLAIN) -public class Resource { - - @Inject - Vertx vertx; - - @Path("/slow") - @GET - public String slow() throws InterruptedException { - Thread.sleep(5000L); - return "slow-response"; - } - - @Path("/fast") - @GET - public CompletionStage fast() { - return CompletableFuture.completedFuture("fast-response"); - } - -}