[JAVA-9450] Fix Timeout integration tests
This commit is contained in:
parent
bd34e3b56e
commit
06ba4f7852
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,40 +1,53 @@
|
|||
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")
|
||||
|
@ -43,4 +56,5 @@ public class RequestTimeoutIntegrationTest {
|
|||
.bodyToMono(String.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue