|
18 | 18 |
|
19 | 19 | import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
|
20 | 20 | import io.r2dbc.postgresql.PostgresqlConnectionFactory;
|
| 21 | +import io.r2dbc.postgresql.api.PostgresqlException; |
21 | 22 | import org.junit.jupiter.api.Test;
|
22 | 23 | import reactor.netty.DisposableChannel;
|
23 | 24 | import reactor.netty.DisposableServer;
|
24 | 25 | import reactor.netty.tcp.TcpServer;
|
25 | 26 | import reactor.test.StepVerifier;
|
26 | 27 |
|
| 28 | +import java.nio.channels.ClosedChannelException; |
| 29 | +import java.util.function.Consumer; |
| 30 | + |
27 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
28 | 32 |
|
29 | 33 | public class DowntimeIntegrationTests {
|
30 | 34 |
|
| 35 | + // Simulate server downtime, where connections are accepted and then closed immediately |
| 36 | + static DisposableServer newServer() { |
| 37 | + return TcpServer.create() |
| 38 | + .doOnConnection(DisposableChannel::dispose) |
| 39 | + .bindNow(); |
| 40 | + } |
| 41 | + |
| 42 | + static PostgresqlConnectionFactory newConnectionFactory(DisposableServer server, SSLMode sslMode) { |
| 43 | + return new PostgresqlConnectionFactory( |
| 44 | + PostgresqlConnectionConfiguration.builder() |
| 45 | + .host(server.host()) |
| 46 | + .port(server.port()) |
| 47 | + .username("test") |
| 48 | + .sslMode(sslMode) |
| 49 | + .build()); |
| 50 | + } |
| 51 | + |
| 52 | + static void verifyError(SSLMode sslMode, Consumer<Throwable> assertions) { |
| 53 | + DisposableServer server = newServer(); |
| 54 | + PostgresqlConnectionFactory connectionFactory = newConnectionFactory(server, sslMode); |
| 55 | + connectionFactory.create().as(StepVerifier::create).verifyErrorSatisfies(assertions); |
| 56 | + server.disposeNow(); |
| 57 | + } |
| 58 | + |
31 | 59 | @Test
|
32 | 60 | void failSslHandshakeIfInboundClosed() {
|
33 |
| - // Simulate server downtime, where connections are accepted and then closed immediately |
34 |
| - DisposableServer server = |
35 |
| - TcpServer.create() |
36 |
| - .doOnConnection(DisposableChannel::dispose) |
37 |
| - .bindNow(); |
38 |
| - |
39 |
| - PostgresqlConnectionFactory connectionFactory = |
40 |
| - new PostgresqlConnectionFactory( |
41 |
| - PostgresqlConnectionConfiguration.builder() |
42 |
| - .host(server.host()) |
43 |
| - .port(server.port()) |
44 |
| - .username("test") |
45 |
| - .sslMode(SSLMode.REQUIRE) |
46 |
| - .build()); |
47 |
| - |
48 |
| - connectionFactory.create() |
49 |
| - .as(StepVerifier::create) |
50 |
| - .verifyErrorSatisfies(error -> |
51 |
| - assertThat(error) |
52 |
| - .isInstanceOf(AbstractPostgresSSLHandlerAdapter.PostgresqlSslException.class) |
53 |
| - .hasMessage("Connection closed during SSL negotiation")); |
| 61 | + verifyError(SSLMode.REQUIRE, error -> |
| 62 | + assertThat(error) |
| 63 | + .isInstanceOf(AbstractPostgresSSLHandlerAdapter.PostgresqlSslException.class) |
| 64 | + .hasMessage("Connection closed during SSL negotiation")); |
| 65 | + } |
54 | 66 |
|
55 |
| - server.disposeNow(); |
| 67 | + @Test |
| 68 | + void failSslTunnelIfInboundClosed() { |
| 69 | + verifyError(SSLMode.TUNNEL, error -> { |
| 70 | + assertThat(error) |
| 71 | + .isInstanceOf(PostgresqlException.class) |
| 72 | + .cause() |
| 73 | + .isInstanceOf(ClosedChannelException.class); |
| 74 | + |
| 75 | + assertThat(error.getCause().getSuppressed().length).isOne(); |
| 76 | + |
| 77 | + assertThat(error.getCause().getSuppressed()[0]) |
| 78 | + .hasMessage("Connection closed while SSL/TLS handshake was in progress"); |
| 79 | + }); |
56 | 80 | }
|
57 | 81 |
|
58 | 82 | }
|
0 commit comments