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
314import io .vertx .core .Context ;
415import io .vertx .core .Future ;
1021import io .vertx .core .tracing .TracingOptions ;
1122import io .vertx .ext .unit .Async ;
1223import 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 .*;
1725import io .vertx .sqlclient .impl .tracing .QueryRequest ;
1826import org .junit .After ;
1927import org .junit .Before ;
2937import java .util .function .BiConsumer ;
3038import 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