BAEL-2552 - Add controller and unit test showing how to set timeouts
This commit is contained in:
parent
f2d45b8269
commit
f7a449978c
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.spring.serverconfig;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/timeout")
|
||||
public class TimeoutController {
|
||||
|
||||
@GetMapping("/{timeout}")
|
||||
private Mono<String> timeout(@PathVariable int timeout) {
|
||||
try {
|
||||
Thread.sleep(timeout * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
return Mono.just("OK");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package com.baeldung.spring.serverconfig;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
|
||||
import org.junit.Assert;
|
||||
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.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.handler.ssl.SslContext;
|
||||
import io.netty.handler.ssl.SslContextBuilder;
|
||||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
||||
import io.netty.handler.timeout.ReadTimeoutException;
|
||||
import io.netty.handler.timeout.ReadTimeoutHandler;
|
||||
import io.netty.handler.timeout.WriteTimeoutHandler;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
import reactor.netty.tcp.TcpClient;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
@DirtiesContext
|
||||
public class TimeoutLiveTest {
|
||||
|
||||
private static final String BASE_URL = "https://localhost:8443";
|
||||
private static final int TIMEOUT_MILLIS = 2000;
|
||||
|
||||
private WebTestClient webTestClient;
|
||||
|
||||
@Before
|
||||
public void setup() throws SSLException {
|
||||
webTestClient = WebTestClient.bindToServer(getConnector())
|
||||
.baseUrl(BASE_URL)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldTimeout() {
|
||||
try {
|
||||
webTestClient.get()
|
||||
.uri("/timeout/{timeout}", 3)
|
||||
.exchange();
|
||||
Assert.fail();
|
||||
} catch (ReadTimeoutException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotTimeout() {
|
||||
try {
|
||||
WebTestClient.ResponseSpec response = webTestClient.get()
|
||||
.uri("/timeout/{timeout}", 1)
|
||||
.exchange();
|
||||
response.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(String.class)
|
||||
.isEqualTo("OK");
|
||||
} catch (ReadTimeoutException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
}
|
||||
|
||||
private ReactorClientHttpConnector getConnector() throws SSLException {
|
||||
SslContext sslContext = SslContextBuilder
|
||||
.forClient()
|
||||
.trustManager(InsecureTrustManagerFactory.INSTANCE)
|
||||
.build();
|
||||
|
||||
TcpClient tcpClient = TcpClient
|
||||
.create()
|
||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, TIMEOUT_MILLIS)
|
||||
.doOnConnected(connection -> {
|
||||
connection.addHandlerLast(new ReadTimeoutHandler(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
|
||||
connection.addHandlerLast(new WriteTimeoutHandler(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
|
||||
});
|
||||
|
||||
HttpClient httpClient = HttpClient.from(tcpClient).secure(t -> t.sslContext(sslContext));
|
||||
return new ReactorClientHttpConnector(httpClient);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue