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 ;
2
13
3
14
import io .vertx .core .Context ;
4
15
import io .vertx .core .Future ;
10
21
import io .vertx .core .tracing .TracingOptions ;
11
22
import io .vertx .ext .unit .Async ;
12
23
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 .*;
17
25
import io .vertx .sqlclient .impl .tracing .QueryRequest ;
18
26
import org .junit .After ;
19
27
import org .junit .Before ;
29
37
import java .util .function .BiConsumer ;
30
38
import java .util .function .Function ;
31
39
32
- public class TracingTest extends PgTestBase {
40
+ public abstract class TracingTestBase {
33
41
34
42
Vertx vertx ;
35
43
VertxTracer tracer ;
36
- PgPool pool ;
44
+ Pool pool ;
37
45
38
46
@ Before
39
47
public void setup () throws Exception {
40
- super .setup ();
41
48
vertx = Vertx .vertx (new VertxOptions ().setTracingOptions (
42
49
new TracingOptions ().setFactory (tracingOptions -> new VertxTracer () {
43
50
@ Override
@@ -51,30 +58,34 @@ public void receiveResponse(Context context, Object response, Object payload, Th
51
58
}
52
59
}))
53
60
);
54
- pool = PgPool . pool (vertx , options , new PoolOptions () );
61
+ pool = createPool (vertx );
55
62
}
56
63
57
64
@ After
58
65
public void teardown (TestContext ctx ) {
59
66
vertx .close (ctx .asyncAssertSuccess ());
60
67
}
61
68
69
+ protected abstract Pool createPool (Vertx vertx );
70
+
71
+ protected abstract String statement (String ... parts );
72
+
62
73
@ Test
63
74
public void testTraceSimpleQuery (TestContext ctx ) {
64
- String sql = "SELECT * FROM Fortune WHERE id=1" ;
75
+ String sql = "SELECT * FROM immutable WHERE id=1" ;
65
76
testTraceQuery (ctx , sql , Collections .emptyList (), conn -> conn .query (sql ).execute ());
66
77
}
67
78
68
79
@ Test
69
80
public void testTracePreparedQuery (TestContext ctx ) {
70
- String sql = "SELECT * FROM Fortune WHERE id=$1" ;
81
+ String sql = statement ( "SELECT * FROM immutable WHERE id = " , "" ) ;
71
82
Tuple tuple = Tuple .of (1 );
72
83
testTraceQuery (ctx , sql , Collections .singletonList (tuple ), conn -> conn .preparedQuery (sql ).execute (tuple ));
73
84
}
74
85
75
86
@ Test
76
87
public void testTraceBatchQuery (TestContext ctx ) {
77
- String sql = "SELECT * FROM Fortune WHERE id=$1" ;
88
+ String sql = statement ( "SELECT * FROM immutable WHERE id = " , "" ) ;
78
89
List <Tuple > tuples = Arrays .asList (Tuple .of (1 ), Tuple .of (2 ));
79
90
testTraceQuery (ctx , sql , tuples , conn -> conn .preparedQuery (sql ).executeBatch (tuples ));
80
91
}
@@ -83,6 +94,7 @@ public void testTraceQuery(TestContext ctx, String expectedSql, List<Tuple> expe
83
94
AtomicBoolean called = new AtomicBoolean ();
84
95
AtomicReference <Context > requestContext = new AtomicReference <>();
85
96
AtomicReference <Context > responseContext = new AtomicReference <>();
97
+ Async completed = ctx .async (2 );
86
98
Object expectedPayload = new Object ();
87
99
tracer = new VertxTracer <Object , Object >() {
88
100
@ Override
@@ -95,6 +107,7 @@ public <R> Object sendRequest(Context context, R request, String operation, BiCo
95
107
ctx .assertEquals ("sql" , tags .get ("db.type" ));
96
108
ctx .assertEquals (expectedSql , tags .get ("db.statement" ));
97
109
requestContext .set (context );
110
+ completed .countDown ();
98
111
return expectedPayload ;
99
112
}
100
113
@ Override
@@ -105,6 +118,7 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
105
118
ctx .assertNull (failure );
106
119
called .set (true );
107
120
responseContext .set (context );
121
+ completed .countDown ();
108
122
}
109
123
};
110
124
Async async = ctx .async ();
@@ -114,6 +128,7 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
114
128
fn .apply (conn ).onComplete (ctx .asyncAssertSuccess (v2 -> {
115
129
conn .close (ctx .asyncAssertSuccess (v3 -> {
116
130
vertx .runOnContext (v4 -> {
131
+ completed .await (2000 );
117
132
ctx .assertEquals (context , requestContext .get ());
118
133
ctx .assertEquals (context , responseContext .get ());
119
134
ctx .assertTrue (called .get ());
@@ -128,6 +143,7 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
128
143
@ Test
129
144
public void testTracingFailure (TestContext ctx ) {
130
145
AtomicBoolean called = new AtomicBoolean ();
146
+ Async completed = ctx .async ();
131
147
tracer = new VertxTracer <Object , Object >() {
132
148
@ Override
133
149
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
138
154
ctx .assertNull (response );
139
155
ctx .assertNotNull (failure );
140
156
called .set (true );
157
+ completed .complete ();
141
158
}
142
159
};
143
160
pool .getConnection (ctx .asyncAssertSuccess (conn -> {
144
161
conn
145
- .preparedQuery ("SELECT 1 / $1" )
162
+ .preparedQuery (statement ( "SELECT * FROM undefined_table WHERE id = " , "" ) )
146
163
.execute (Tuple .of (0 ), ctx .asyncAssertFailure (err -> {
164
+ completed .await (2000 );
147
165
ctx .assertTrue (called .get ());
148
166
conn .close ();
149
167
}));
@@ -154,7 +172,8 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
154
172
public void testMappingFailure (TestContext ctx ) {
155
173
RuntimeException failure = new RuntimeException ();
156
174
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 = " , "" );
158
177
tracer = new VertxTracer <Object , Object >() {
159
178
@ Override
160
179
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
163
182
@ Override
164
183
public <R > void receiveResponse (Context context , R response , Object payload , Throwable failure , TagExtractor <R > tagExtractor ) {
165
184
ctx .assertEquals (1 , called .incrementAndGet ());
185
+ completed .complete ();
166
186
}
167
187
};
168
188
Async async = ctx .async ();
@@ -175,6 +195,7 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
175
195
.execute (Tuple .of (1 ), ctx .asyncAssertFailure (err -> {
176
196
conn .close (ctx .asyncAssertSuccess (v1 -> {
177
197
vertx .runOnContext (v2 -> {
198
+ completed .await (2000 );
178
199
ctx .assertEquals (1 , called .get ());
179
200
async .complete ();
180
201
});
0 commit comments