Merge pull request #11707 from hkhan/JAVA-9450-fix-integration-test

[JAVA-9450] Fix Timeout integration tests
This commit is contained in:
kwoyke 2022-01-16 11:59:12 +01:00 committed by GitHub
commit c62f1fbcb7
3 changed files with 42 additions and 17 deletions

View File

@ -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();
}

View File

@ -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<Book, String> {
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;
}
}

View File

@ -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();
}
}