[JAVA-9450] Fix Timeout integration tests
This commit is contained in:
parent
bd34e3b56e
commit
06ba4f7852
|
@ -1,5 +1,6 @@
|
||||||
package com.baeldung.requesttimeout.configuration;
|
package com.baeldung.requesttimeout.configuration;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||||
|
@ -11,10 +12,13 @@ import java.time.Duration;
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebClientConfiguration {
|
public class WebClientConfiguration {
|
||||||
|
|
||||||
|
@Value("${server.port}")
|
||||||
|
private int serverPort;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public WebClient webClient() {
|
public WebClient webClient() {
|
||||||
return WebClient.builder()
|
return WebClient.builder()
|
||||||
.baseUrl("http://localhost:8080")
|
.baseUrl("http://localhost:" + serverPort)
|
||||||
.clientConnector(new ReactorClientHttpConnector(HttpClient.create().responseTimeout(Duration.ofMillis(250))))
|
.clientConnector(new ReactorClientHttpConnector(HttpClient.create().responseTimeout(Duration.ofMillis(250))))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,21 @@
|
||||||
package com.baeldung.requesttimeout.domain;
|
package com.baeldung.requesttimeout.domain;
|
||||||
|
|
||||||
|
import com.google.common.base.Stopwatch;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||||
|
|
||||||
public interface BookRepository extends JpaRepository<Book, String> {
|
public interface BookRepository extends JpaRepository<Book, String> {
|
||||||
|
|
||||||
default int wasteTime() {
|
default void wasteTime() {
|
||||||
int i = Integer.MIN_VALUE;
|
Stopwatch watch = Stopwatch.createStarted();
|
||||||
while(i < Integer.MAX_VALUE) {
|
|
||||||
i++;
|
// delay for 2 seconds
|
||||||
|
while (watch.elapsed(SECONDS) < 2) {
|
||||||
|
int i = Integer.MIN_VALUE;
|
||||||
|
while (i < Integer.MAX_VALUE) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,46 +1,60 @@
|
||||||
package com.baeldung.requesttimeout;
|
package com.baeldung.requesttimeout;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
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.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
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)
|
@RunWith(SpringRunner.class)
|
||||||
public class RequestTimeoutIntegrationTest {
|
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() {
|
public void givenTransactionTimeout_whenTimeExpires_thenReceiveException() {
|
||||||
getAuthor("transactional");
|
getAuthor("transactional");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = WebClientRequestException.class)
|
@Test(expected = WebClientResponseException.InternalServerError.class)
|
||||||
public void givenResilience4jTimeLimiter_whenTimeExpires_thenReceiveException() {
|
public void givenResilience4jTimeLimiter_whenTimeExpires_thenReceiveException() {
|
||||||
getAuthor("resilience4j");
|
getAuthor("resilience4j");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = WebClientRequestException.class)
|
@Test(expected = WebClientResponseException.ServiceUnavailable.class)
|
||||||
public void givenMvcRequestTimeout_whenTimeExpires_thenReceiveException() {
|
public void givenMvcRequestTimeout_whenTimeExpires_thenReceiveException() {
|
||||||
getAuthor("mvc-request-timeout");
|
getAuthor("mvc-request-timeout");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = WebClientRequestException.class)
|
@Test(expected = WebClientResponseException.InternalServerError.class)
|
||||||
public void givenWebClientTimeout_whenTimeExpires_thenReceiveException() {
|
public void givenWebClientTimeout_whenTimeExpires_thenReceiveException() {
|
||||||
getAuthor("webclient");
|
getAuthor("webclient");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getAuthor(String authorPath) {
|
private void getAuthor(String authorPath) {
|
||||||
WEB_CLIENT.get()
|
webClient.get()
|
||||||
.uri(uriBuilder -> uriBuilder
|
.uri(uriBuilder -> uriBuilder
|
||||||
.path("/author/" + authorPath)
|
.path("/author/" + authorPath)
|
||||||
.queryParam("title", "title")
|
.queryParam("title", "title")
|
||||||
.build())
|
.build())
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
.block();
|
.block();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue