diff --git a/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/requesttimeout/configuration/WebClientConfiguration.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/requesttimeout/configuration/WebClientConfiguration.java index 52b3573411..709812db98 100644 --- a/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/requesttimeout/configuration/WebClientConfiguration.java +++ b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/requesttimeout/configuration/WebClientConfiguration.java @@ -1,5 +1,6 @@ package com.baeldung.requesttimeout.configuration; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.reactive.ReactorClientHttpConnector; @@ -11,10 +12,13 @@ import java.time.Duration; @Configuration public class WebClientConfiguration { + @Value("${server.port}") + private int serverPort; + @Bean public WebClient webClient() { return WebClient.builder() - .baseUrl("http://localhost:8080") + .baseUrl("http://localhost:" + serverPort) .clientConnector(new ReactorClientHttpConnector(HttpClient.create().responseTimeout(Duration.ofMillis(250)))) .build(); } diff --git a/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/requesttimeout/domain/BookRepository.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/requesttimeout/domain/BookRepository.java index 8ecab0f1d2..5eb4bc7268 100644 --- a/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/requesttimeout/domain/BookRepository.java +++ b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/requesttimeout/domain/BookRepository.java @@ -1,14 +1,21 @@ package com.baeldung.requesttimeout.domain; +import com.google.common.base.Stopwatch; import org.springframework.data.jpa.repository.JpaRepository; +import static java.util.concurrent.TimeUnit.SECONDS; + public interface BookRepository extends JpaRepository { - default int wasteTime() { - int i = Integer.MIN_VALUE; - while(i < Integer.MAX_VALUE) { - i++; + default void wasteTime() { + Stopwatch watch = Stopwatch.createStarted(); + + // delay for 2 seconds + while (watch.elapsed(SECONDS) < 2) { + int i = Integer.MIN_VALUE; + while (i < Integer.MAX_VALUE) { + i++; + } } - return i; } } diff --git a/spring-web-modules/spring-rest-http-2/src/test/java/com/baeldung/requesttimeout/RequestTimeoutIntegrationTest.java b/spring-web-modules/spring-rest-http-2/src/test/java/com/baeldung/requesttimeout/RequestTimeoutIntegrationTest.java index 4775602e8a..d30dc351fa 100644 --- a/spring-web-modules/spring-rest-http-2/src/test/java/com/baeldung/requesttimeout/RequestTimeoutIntegrationTest.java +++ b/spring-web-modules/spring-rest-http-2/src/test/java/com/baeldung/requesttimeout/RequestTimeoutIntegrationTest.java @@ -1,46 +1,60 @@ package com.baeldung.requesttimeout; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.reactive.function.client.WebClient; -import org.springframework.web.reactive.function.client.WebClientRequestException; +import org.springframework.web.reactive.function.client.WebClientResponseException; -@SpringBootTest +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @RunWith(SpringRunner.class) public class RequestTimeoutIntegrationTest { - private static final WebClient WEB_CLIENT = WebClient.builder().baseUrl("http://localhost:8080").build(); + @LocalServerPort + private int port; - @Test(expected = WebClientRequestException.class) + private WebClient webClient; + + @Before + public void setUp() { + webClient = WebClient.builder() + .baseUrl("http://localhost:" + port) + .build(); + } + + @Test(expected = WebClientResponseException.InternalServerError.class) public void givenTransactionTimeout_whenTimeExpires_thenReceiveException() { getAuthor("transactional"); } - @Test(expected = WebClientRequestException.class) + @Test(expected = WebClientResponseException.InternalServerError.class) public void givenResilience4jTimeLimiter_whenTimeExpires_thenReceiveException() { getAuthor("resilience4j"); } - @Test(expected = WebClientRequestException.class) + @Test(expected = WebClientResponseException.ServiceUnavailable.class) public void givenMvcRequestTimeout_whenTimeExpires_thenReceiveException() { getAuthor("mvc-request-timeout"); } - @Test(expected = WebClientRequestException.class) + @Test(expected = WebClientResponseException.InternalServerError.class) public void givenWebClientTimeout_whenTimeExpires_thenReceiveException() { getAuthor("webclient"); } private void getAuthor(String authorPath) { - WEB_CLIENT.get() + webClient.get() .uri(uriBuilder -> uriBuilder - .path("/author/" + authorPath) - .queryParam("title", "title") - .build()) + .path("/author/" + authorPath) + .queryParam("title", "title") + .build()) .retrieve() .bodyToMono(String.class) .block(); } + }