Repository navigation
Expand file tree
/
Copy pathCloseConnectionTest.java
More file actions
147 lines (134 loc) · 5 KB
/
Copy pathCloseConnectionTest.java
File metadata and controls
147 lines (134 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package io.vertx.tests.pgclient;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelOption;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.internal.net.NetSocketInternal;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.pgclient.PgBuilder;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgConnection;
import io.vertx.pgclient.SslMode;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.tests.sqlclient.ProxyServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
public class CloseConnectionTest extends PgTestBase {
protected Vertx vertx;
@Before
public void setup() throws Exception {
super.setup();
vertx = Vertx.vertx();
}
@After
public void tearDown(TestContext ctx) {
vertx.close().onComplete(ctx.asyncAssertSuccess());
}
@Test
public void testCloseConnection(TestContext ctx) {
testCloseConnection(ctx, () -> {
PgConnection.connect(vertx, options).onComplete(ctx.asyncAssertSuccess(conn -> {
conn.close().onComplete(ctx.asyncAssertSuccess());
}));
});
}
@Test
public void testClosePooledConnection(TestContext ctx) {
testCloseConnection(ctx, () -> {
Pool pool = PgBuilder.pool().connectingTo(options).with(new PoolOptions().setMaxSize(1)).using(vertx).build();
pool.getConnection().onComplete(ctx.asyncAssertSuccess(conn -> {
conn.close().onComplete(ctx.asyncAssertSuccess(v -> {
pool.close().onComplete(ctx.asyncAssertSuccess());
}));
}));
});
}
private void testCloseConnection(TestContext ctx, Runnable test) {
Async async = ctx.async();
ProxyServer proxy = ProxyServer.create(vertx, options.getPort(), options.getHost());
proxy.proxyHandler(conn -> {
Buffer data = Buffer.buffer();
conn.clientHandler(buff -> {
data.appendBuffer(buff);
conn.serverSocket().write(buff);
});
conn.serverCloseHandler(v -> {
ctx.assertTrue(data.length() > 5);
String hex = ByteBufUtil.hexDump(data.slice(data.length() - 5, data.length()).getBytes());
// Connection close packet
ctx.assertEquals("5800000004", hex);
async.complete();
});
conn.connect();
});
proxy.listen(8080, "localhost", ctx.asyncAssertSuccess(v1 -> {
options.setPort(8080).setHost("localhost");
test.run();
}));
}
@Test
public void testReadIdleTimeoutClosesConnection(TestContext ctx) {
Async async = ctx.async();
vertx.createNetServer().connectHandler(socket -> {
// Enable half-close so the server keeps the connection open after receiving FIN,
// otherwise Vert.x auto-closes and sends FIN back, masking the bug
((NetSocketInternal) socket).channelHandlerContext().channel().config()
.setOption(ChannelOption.ALLOW_HALF_CLOSURE, true);
socket.handler(data -> {
// Received startup message, send PG handshake response then stay silent
Buffer response = Buffer.buffer()
.appendByte((byte) 'R').appendInt(8).appendInt(0)
.appendByte((byte) 'S').appendInt(25)
.appendBytes("client_encoding\0UTF8\0".getBytes(StandardCharsets.UTF_8))
.appendByte((byte) 'K').appendInt(12).appendInt(1).appendInt(0)
.appendByte((byte) 'Z').appendInt(5).appendByte((byte) 'I');
socket.write(response);
socket.handler(null);
});
}).listen(0, "localhost").onComplete(ctx.asyncAssertSuccess(server -> {
PgConnectOptions connectOptions = new PgConnectOptions()
.setHost("localhost")
.setPort(server.actualPort())
.setUser("test")
.setPassword("test")
.setDatabase("test")
.setSslMode(SslMode.DISABLE);
Pool pool = PgBuilder.pool()
.connectingTo(connectOptions)
.with(new PoolOptions().setMaxSize(1))
.with(new io.vertx.core.net.NetClientOptions()
.setReadIdleTimeout(200)
.setIdleTimeoutUnit(TimeUnit.MILLISECONDS))
.using(vertx)
.build();
pool.getConnection().onComplete(ctx.asyncAssertSuccess(conn ->
conn.closeHandler(v -> {
pool.close();
server.close();
async.complete();
})
));
}));
}
@Test
public void testTransactionInProgressShouldFail(TestContext ctx) {
ProxyServer proxy = ProxyServer.create(vertx, options.getPort(), options.getHost());
proxy.proxyHandler(conn -> {
conn.connect();
vertx.setTimer(1_000, l -> conn.close());
});
proxy.listen(8080, "localhost", ctx.asyncAssertSuccess(v1 -> {
options.setPort(8080).setHost("localhost");
Pool pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(1));
pool.withTransaction(conn -> conn.query("select pg_sleep(60)").execute())
.onComplete(ctx.asyncAssertFailure(t -> {
t.printStackTrace();
}));
}));
}
}