Skip to content

Commit 6de187d

Browse files
committed
update: add downtime integration test for ssl tunnel mode
1 parent d0bbd9d commit 6de187d

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

src/test/java/io/r2dbc/postgresql/client/DowntimeIntegrationTests.java

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,65 @@
1818

1919
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
2020
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
21+
import io.r2dbc.postgresql.api.PostgresqlException;
2122
import org.junit.jupiter.api.Test;
2223
import reactor.netty.DisposableChannel;
2324
import reactor.netty.DisposableServer;
2425
import reactor.netty.tcp.TcpServer;
2526
import reactor.test.StepVerifier;
2627

28+
import java.nio.channels.ClosedChannelException;
29+
import java.util.function.Consumer;
30+
2731
import static org.assertj.core.api.Assertions.assertThat;
2832

2933
public class DowntimeIntegrationTests {
3034

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+
3159
@Test
3260
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+
}
5466

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+
});
5680
}
5781

5882
}

0 commit comments

Comments
 (0)