Merge pull request #8814 from markathomas/BAEL-2552

BAEL-2552 - spatialguru.net@gmail.com
This commit is contained in:
rpvilao 2020-03-10 21:37:45 +01:00 committed by GitHub
commit 30ffb3e3cf
2 changed files with 108 additions and 0 deletions

View File

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

View File

@ -0,0 +1,85 @@
package com.baeldung.spring.serverconfig;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
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;
@Rule
public ExpectedException exception = ExpectedException.none();
@Before
public void setup() throws SSLException {
webTestClient = WebTestClient.bindToServer(getConnector())
.baseUrl(BASE_URL)
.build();
}
@Test
public void shouldTimeout() {
exception.expect(ReadTimeoutException.class);
webTestClient.get()
.uri("/timeout/{timeout}", 3)
.exchange();
}
@Test
public void shouldNotTimeout() {
WebTestClient.ResponseSpec response = webTestClient.get()
.uri("/timeout/{timeout}", 1)
.exchange();
response.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("OK");
}
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);
}
}