-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathPreparedStatementRepreparePipeliningTest.java
More file actions
150 lines (127 loc) · 4.81 KB
/
PreparedStatementRepreparePipeliningTest.java
File metadata and controls
150 lines (127 loc) · 4.81 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
148
149
150
package io.vertx.tests.pgclient;
import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgConnection;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.Tuple;
import io.vertx.tests.sqlclient.ProxyServer;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntConsumer;
public class PreparedStatementRepreparePipeliningTest extends PreparedStatementTestBase {
@Override
protected PgConnectOptions options() {
return new PgConnectOptions(options)
.setPipeliningLimit(1);
}
@Test
public void testReprepareDoesNotBypassPipeliningLimitWithEnabledCache(TestContext ctx) {
testReprepareDoesNotBypassPipeliningLimit(ctx, true);
}
@Test
public void testReprepareDoesNotBypassPipeliningLimitWithDisabledCache(TestContext ctx) {
testReprepareDoesNotBypassPipeliningLimit(ctx, false);
}
private void testReprepareDoesNotBypassPipeliningLimit(TestContext ctx, boolean cachePreparedStatements) {
Async async = ctx.async();
PgConnectOptions backend = options().setCachePreparedStatements(cachePreparedStatements);
ProxyServer proxy = ProxyServer.create(vertx, backend.getPort(), backend.getHost());
AtomicBoolean observe = new AtomicBoolean();
AtomicInteger readyForQueryCount = new AtomicInteger();
AtomicBoolean sawSecondQuery = new AtomicBoolean();
AtomicBoolean secondQueryTooEarly = new AtomicBoolean();
TaggedMessageScanner frontendScanner = new TaggedMessageScanner();
TaggedMessageScanner backendScanner = new TaggedMessageScanner();
proxy.proxyHandler(conn -> {
conn.clientHandler(buff -> {
if (observe.get()) {
frontendScanner.handle(buff, tag -> {
if (tag == 'Q') {
sawSecondQuery.set(true);
if (readyForQueryCount.get() < 3) {
secondQueryTooEarly.set(true);
}
}
});
}
conn.serverSocket().write(buff);
});
conn.serverHandler(buff -> {
if (observe.get()) {
backendScanner.handle(buff, tag -> {
if (tag == 'Z') {
readyForQueryCount.incrementAndGet();
}
});
}
conn.clientSocket().write(buff);
});
conn.connect();
});
proxy.listen(8080, "localhost", ctx.asyncAssertSuccess(v -> {
PgConnectOptions proxied = new PgConnectOptions(backend)
.setHost("localhost")
.setPort(8080);
PgConnection.connect(vertx, proxied).onComplete(ctx.asyncAssertSuccess(conn -> {
observe.set(true);
Future
.all(
conn.preparedQuery("WITH s AS (SELECT pg_sleep(1)) SELECT CONCAT('HELLO ', $1) FROM s")
.execute(Tuple.of("WORLD")),
conn.query("SELECT 1").execute()
)
.eventually(() -> conn.close())
.onComplete(ctx.asyncAssertSuccess(ar -> {
RowSet<Row> first = ar.result().resultAt(0);
RowSet<Row> second = ar.result().resultAt(1);
ctx.assertEquals(1, first.size());
ctx.assertEquals("HELLO WORLD", first.iterator().next().getString(0));
ctx.assertEquals(1, second.size());
ctx.assertEquals(1, second.iterator().next().getInteger(0).intValue());
ctx.assertTrue(
sawSecondQuery.get(),
"Test setup invalid: did not observe frontend simple-query ('Q') message for the second command"
);
ctx.assertFalse(
secondQueryTooEarly.get(),
"Second command was written too early before reprepare flow finished " +
"(cachePreparedStatements=" + cachePreparedStatements +
", readyForQueryCount=" + readyForQueryCount.get() + ")"
);
async.complete();
}));
}));
}));
}
private static final class TaggedMessageScanner {
private Buffer pending = Buffer.buffer();
void handle(Buffer incoming, IntConsumer tagHandler) {
pending.appendBuffer(incoming);
while (true) {
if (pending.length() < 5) {
return;
}
int len = pending.getInt(1);
if (len < 4) {
throw new IllegalStateException("Invalid PostgreSQL message length: " + len);
}
int frameLen = 1 + len;
if (pending.length() < frameLen) {
return;
}
int tag = pending.getByte(0) & 0xFF;
tagHandler.accept(tag);
if (pending.length() == frameLen) {
pending = Buffer.buffer();
} else {
pending = pending.getBuffer(frameLen, pending.length());
}
}
}
}
}