Skip to content

Commit 03a49ff

Browse files
authored
Rework Tracing tests (#661)
* fix racy tracing tests Signed-off-by: Billy Yuan <billy112487983@gmail.com> * rework tracing tests to make it in TCK now Signed-off-by: Billy Yuan <billy112487983@gmail.com>
1 parent e8faee8 commit 03a49ff

File tree

5 files changed

+221
-14
lines changed

5 files changed

+221
-14
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
12+
package io.vertx.db2client.tck;
13+
14+
import io.vertx.core.Vertx;
15+
import io.vertx.db2client.DB2Pool;
16+
import io.vertx.db2client.junit.DB2Resource;
17+
import io.vertx.ext.unit.junit.VertxUnitRunner;
18+
import io.vertx.sqlclient.Pool;
19+
import io.vertx.sqlclient.PoolOptions;
20+
import io.vertx.sqlclient.tck.TracingTestBase;
21+
import org.junit.ClassRule;
22+
import org.junit.runner.RunWith;
23+
24+
@RunWith(VertxUnitRunner.class)
25+
public class DB2TracingTest extends TracingTestBase {
26+
27+
@ClassRule
28+
public static DB2Resource rule = DB2Resource.SHARED_INSTANCE;
29+
30+
@Override
31+
protected Pool createPool(Vertx vertx) {
32+
return DB2Pool.pool(vertx, rule.options(), new PoolOptions());
33+
}
34+
35+
@Override
36+
protected String statement(String... parts) {
37+
return String.join("?", parts);
38+
}
39+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
12+
package io.vertx.mssqlclient.tck;
13+
14+
import io.vertx.core.Vertx;
15+
import io.vertx.ext.unit.TestContext;
16+
import io.vertx.ext.unit.junit.VertxUnitRunner;
17+
import io.vertx.mssqlclient.MSSQLPool;
18+
import io.vertx.mssqlclient.junit.MSSQLRule;
19+
import io.vertx.sqlclient.Pool;
20+
import io.vertx.sqlclient.PoolOptions;
21+
import io.vertx.sqlclient.tck.TracingTestBase;
22+
import org.junit.ClassRule;
23+
import org.junit.Ignore;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
@RunWith(VertxUnitRunner.class)
28+
public class MSSQLTracingTest extends TracingTestBase {
29+
30+
@ClassRule
31+
public static MSSQLRule rule = MSSQLRule.SHARED_INSTANCE;
32+
33+
@Override
34+
protected Pool createPool(Vertx vertx) {
35+
return MSSQLPool.pool(vertx, rule.options(), new PoolOptions());
36+
}
37+
38+
@Override
39+
protected String statement(String... parts) {
40+
StringBuilder sb = new StringBuilder();
41+
for (int i = 0; i < parts.length; i++) {
42+
if (i > 0) {
43+
sb.append("@p").append((i));
44+
}
45+
sb.append(parts[i]);
46+
}
47+
return sb.toString();
48+
}
49+
50+
@Ignore
51+
@Test
52+
@Override
53+
public void testTraceBatchQuery(TestContext ctx) {
54+
super.testTraceBatchQuery(ctx);
55+
}
56+
57+
@Ignore
58+
@Test
59+
@Override
60+
public void testTracingFailure(TestContext ctx) {
61+
super.testTracingFailure(ctx);
62+
}
63+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
12+
package io.vertx.mysqlclient.tck;
13+
14+
import io.vertx.core.Vertx;
15+
import io.vertx.ext.unit.junit.VertxUnitRunner;
16+
import io.vertx.mysqlclient.MySQLPool;
17+
import io.vertx.mysqlclient.junit.MySQLRule;
18+
import io.vertx.sqlclient.Pool;
19+
import io.vertx.sqlclient.PoolOptions;
20+
import io.vertx.sqlclient.tck.TracingTestBase;
21+
import org.junit.ClassRule;
22+
import org.junit.runner.RunWith;
23+
24+
@RunWith(VertxUnitRunner.class)
25+
public class MySQLTracingTest extends TracingTestBase {
26+
27+
@ClassRule
28+
public static MySQLRule rule = MySQLRule.SHARED_INSTANCE;
29+
30+
@Override
31+
protected Pool createPool(Vertx vertx) {
32+
return MySQLPool.pool(vertx, rule.options(), new PoolOptions());
33+
}
34+
35+
@Override
36+
protected String statement(String... parts) {
37+
return String.join("?", parts);
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
12+
package io.vertx.pgclient.tck;
13+
14+
import io.vertx.core.Vertx;
15+
import io.vertx.ext.unit.junit.VertxUnitRunner;
16+
import io.vertx.pgclient.PgPool;
17+
import io.vertx.pgclient.junit.ContainerPgRule;
18+
import io.vertx.sqlclient.Pool;
19+
import io.vertx.sqlclient.PoolOptions;
20+
import io.vertx.sqlclient.tck.TracingTestBase;
21+
import org.junit.ClassRule;
22+
import org.junit.runner.RunWith;
23+
24+
@RunWith(VertxUnitRunner.class)
25+
public class PgTracingTest extends TracingTestBase {
26+
@ClassRule
27+
public static ContainerPgRule rule = new ContainerPgRule();
28+
29+
@Override
30+
protected Pool createPool(Vertx vertx) {
31+
return PgPool.pool(vertx, rule.options(), new PoolOptions());
32+
}
33+
34+
@Override
35+
protected String statement(String... parts) {
36+
StringBuilder sb = new StringBuilder();
37+
for (int i = 0; i < parts.length; i++) {
38+
if (i > 0) {
39+
sb.append("$").append((i));
40+
}
41+
sb.append(parts[i]);
42+
}
43+
return sb.toString();
44+
}
45+
}

vertx-pg-client/src/test/java/io/vertx/pgclient/TracingTest.java renamed to vertx-sql-client/src/test/java/io/vertx/sqlclient/tck/TracingTestBase.java

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
package io.vertx.pgclient;
1+
/*
2+
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
12+
package io.vertx.sqlclient.tck;
213

314
import io.vertx.core.Context;
415
import io.vertx.core.Future;
@@ -10,10 +21,7 @@
1021
import io.vertx.core.tracing.TracingOptions;
1122
import io.vertx.ext.unit.Async;
1223
import io.vertx.ext.unit.TestContext;
13-
import io.vertx.sqlclient.PoolOptions;
14-
import io.vertx.sqlclient.RowSet;
15-
import io.vertx.sqlclient.SqlClient;
16-
import io.vertx.sqlclient.Tuple;
24+
import io.vertx.sqlclient.*;
1725
import io.vertx.sqlclient.impl.tracing.QueryRequest;
1826
import org.junit.After;
1927
import org.junit.Before;
@@ -29,15 +37,14 @@
2937
import java.util.function.BiConsumer;
3038
import java.util.function.Function;
3139

32-
public class TracingTest extends PgTestBase {
40+
public abstract class TracingTestBase {
3341

3442
Vertx vertx;
3543
VertxTracer tracer;
36-
PgPool pool;
44+
Pool pool;
3745

3846
@Before
3947
public void setup() throws Exception {
40-
super.setup();
4148
vertx = Vertx.vertx(new VertxOptions().setTracingOptions(
4249
new TracingOptions().setFactory(tracingOptions -> new VertxTracer() {
4350
@Override
@@ -51,30 +58,34 @@ public void receiveResponse(Context context, Object response, Object payload, Th
5158
}
5259
}))
5360
);
54-
pool = PgPool.pool(vertx, options, new PoolOptions());
61+
pool = createPool(vertx);
5562
}
5663

5764
@After
5865
public void teardown(TestContext ctx) {
5966
vertx.close(ctx.asyncAssertSuccess());
6067
}
6168

69+
protected abstract Pool createPool(Vertx vertx);
70+
71+
protected abstract String statement(String... parts);
72+
6273
@Test
6374
public void testTraceSimpleQuery(TestContext ctx) {
64-
String sql = "SELECT * FROM Fortune WHERE id=1";
75+
String sql = "SELECT * FROM immutable WHERE id=1";
6576
testTraceQuery(ctx, sql, Collections.emptyList(), conn -> conn.query(sql).execute());
6677
}
6778

6879
@Test
6980
public void testTracePreparedQuery(TestContext ctx) {
70-
String sql = "SELECT * FROM Fortune WHERE id=$1";
81+
String sql = statement("SELECT * FROM immutable WHERE id = ", "");
7182
Tuple tuple = Tuple.of(1);
7283
testTraceQuery(ctx, sql, Collections.singletonList(tuple), conn -> conn.preparedQuery(sql).execute(tuple));
7384
}
7485

7586
@Test
7687
public void testTraceBatchQuery(TestContext ctx) {
77-
String sql = "SELECT * FROM Fortune WHERE id=$1";
88+
String sql = statement("SELECT * FROM immutable WHERE id = ", "");
7889
List<Tuple> tuples = Arrays.asList(Tuple.of(1), Tuple.of(2));
7990
testTraceQuery(ctx, sql, tuples, conn -> conn.preparedQuery(sql).executeBatch(tuples));
8091
}
@@ -83,6 +94,7 @@ public void testTraceQuery(TestContext ctx, String expectedSql, List<Tuple> expe
8394
AtomicBoolean called = new AtomicBoolean();
8495
AtomicReference<Context> requestContext = new AtomicReference<>();
8596
AtomicReference<Context> responseContext = new AtomicReference<>();
97+
Async completed = ctx.async(2);
8698
Object expectedPayload = new Object();
8799
tracer = new VertxTracer<Object, Object>() {
88100
@Override
@@ -95,6 +107,7 @@ public <R> Object sendRequest(Context context, R request, String operation, BiCo
95107
ctx.assertEquals("sql", tags.get("db.type"));
96108
ctx.assertEquals(expectedSql, tags.get("db.statement"));
97109
requestContext.set(context);
110+
completed.countDown();
98111
return expectedPayload;
99112
}
100113
@Override
@@ -105,6 +118,7 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
105118
ctx.assertNull(failure);
106119
called.set(true);
107120
responseContext.set(context);
121+
completed.countDown();
108122
}
109123
};
110124
Async async = ctx.async();
@@ -114,6 +128,7 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
114128
fn.apply(conn).onComplete(ctx.asyncAssertSuccess(v2 -> {
115129
conn.close(ctx.asyncAssertSuccess(v3 -> {
116130
vertx.runOnContext(v4 -> {
131+
completed.await(2000);
117132
ctx.assertEquals(context, requestContext.get());
118133
ctx.assertEquals(context, responseContext.get());
119134
ctx.assertTrue(called.get());
@@ -128,6 +143,7 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
128143
@Test
129144
public void testTracingFailure(TestContext ctx) {
130145
AtomicBoolean called = new AtomicBoolean();
146+
Async completed = ctx.async();
131147
tracer = new VertxTracer<Object, Object>() {
132148
@Override
133149
public <R> Object sendRequest(Context context, R request, String operation, BiConsumer<String, String> headers, TagExtractor<R> tagExtractor) {
@@ -138,12 +154,14 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
138154
ctx.assertNull(response);
139155
ctx.assertNotNull(failure);
140156
called.set(true);
157+
completed.complete();
141158
}
142159
};
143160
pool.getConnection(ctx.asyncAssertSuccess(conn -> {
144161
conn
145-
.preparedQuery("SELECT 1 / $1")
162+
.preparedQuery(statement("SELECT * FROM undefined_table WHERE id = ", ""))
146163
.execute(Tuple.of(0), ctx.asyncAssertFailure(err -> {
164+
completed.await(2000);
147165
ctx.assertTrue(called.get());
148166
conn.close();
149167
}));
@@ -154,7 +172,8 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
154172
public void testMappingFailure(TestContext ctx) {
155173
RuntimeException failure = new RuntimeException();
156174
AtomicInteger called = new AtomicInteger();
157-
String sql = "SELECT * FROM Fortune WHERE id=$1";
175+
Async completed = ctx.async();
176+
String sql = statement("SELECT * FROM immutable WHERE id = ", "");
158177
tracer = new VertxTracer<Object, Object>() {
159178
@Override
160179
public <R> Object sendRequest(Context context, R request, String operation, BiConsumer<String, String> headers, TagExtractor<R> tagExtractor) {
@@ -163,6 +182,7 @@ public <R> Object sendRequest(Context context, R request, String operation, BiCo
163182
@Override
164183
public <R> void receiveResponse(Context context, R response, Object payload, Throwable failure, TagExtractor<R> tagExtractor) {
165184
ctx.assertEquals(1, called.incrementAndGet());
185+
completed.complete();
166186
}
167187
};
168188
Async async = ctx.async();
@@ -175,6 +195,7 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
175195
.execute(Tuple.of(1), ctx.asyncAssertFailure(err -> {
176196
conn.close(ctx.asyncAssertSuccess(v1 -> {
177197
vertx.runOnContext(v2 -> {
198+
completed.await(2000);
178199
ctx.assertEquals(1, called.get());
179200
async.complete();
180201
});

0 commit comments

Comments
 (0)