Skip to content

Commit

Permalink
Merge pull request #19244 from geoand/#19223-follow-up
Browse files Browse the repository at this point in the history
Add test for rest-client reactive timeout
  • Loading branch information
geoand authored Aug 19, 2021
2 parents 8017b6e + b4a0b31 commit 1458b75
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> fast() {
return CompletableFuture.completedFuture("fast-response");
}

}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<String> fast() {
return CompletableFuture.completedFuture("fast-response");
}

}
}

This file was deleted.

0 comments on commit 1458b75

Please sign in to comment.