Skip to content

Commit 8c24767

Browse files
committed
chore: move extended test suit to monorepo
1 parent 463831b commit 8c24767

File tree

9 files changed

+4532
-1890
lines changed

9 files changed

+4532
-1890
lines changed

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBase.java

Lines changed: 654 additions & 0 deletions
Large diffs are not rendered by default.

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBigQueryJDBCTest.java

Lines changed: 530 additions & 1890 deletions
Large diffs are not rendered by default.

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITCallableStatementTest.java

Lines changed: 665 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigquery.jdbc.it;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
25+
import com.google.cloud.ServiceOptions;
26+
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
27+
import com.google.cloud.bigquery.jdbc.PooledConnectionDataSource;
28+
import com.google.cloud.bigquery.jdbc.PooledConnectionListener;
29+
import com.google.cloud.bigquery.jdbc.utils.TestUtilities.TestConnectionListener;
30+
import java.sql.Connection;
31+
import java.sql.ResultSet;
32+
import java.sql.SQLException;
33+
import java.sql.Statement;
34+
import java.util.concurrent.ExecutorService;
35+
import java.util.concurrent.Executors;
36+
import javax.sql.PooledConnection;
37+
import org.junit.jupiter.api.Test;
38+
39+
public class ITConnectionPoolingTest extends ITBase {
40+
static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();
41+
private static final Long DEFAULT_CONN_POOL_SIZE = 10L;
42+
private static final Long CUSTOM_CONN_POOL_SIZE = 5L;
43+
44+
@Test
45+
public void testPooledConnectionDataSourceSuccess() throws SQLException {
46+
String connectionUrl =
47+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
48+
49+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
50+
pooledDataSource.setURL(connectionUrl);
51+
52+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
53+
assertNotNull(pooledConnection);
54+
}
55+
56+
@Test
57+
public void testPooledConnectionDataSourceFailNoConnectionURl() {
58+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
59+
60+
assertThrows(BigQueryJdbcException.class, () -> pooledDataSource.getPooledConnection());
61+
}
62+
63+
@Test
64+
public void testPooledConnectionDataSourceFailInvalidConnectionURl() {
65+
String connectionUrl =
66+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;"
67+
+ "ListenerPoolSize=invalid";
68+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
69+
pooledDataSource.setURL(connectionUrl);
70+
71+
assertThrows(NumberFormatException.class, () -> pooledDataSource.getPooledConnection());
72+
}
73+
74+
@Test
75+
public void testPooledConnectionAddConnectionListener() throws SQLException {
76+
String connectionUrl =
77+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
78+
79+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
80+
pooledDataSource.setURL(connectionUrl);
81+
82+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
83+
assertNotNull(pooledConnection);
84+
TestConnectionListener listener = new TestConnectionListener();
85+
pooledConnection.addConnectionEventListener(listener);
86+
assertEquals(0, listener.getConnectionClosedCount());
87+
assertEquals(0, listener.getConnectionErrorCount());
88+
}
89+
90+
@Test
91+
public void testPooledConnectionRemoveConnectionListener() throws SQLException {
92+
String connectionUrl =
93+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
94+
95+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
96+
pooledDataSource.setURL(connectionUrl);
97+
98+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
99+
assertNotNull(pooledConnection);
100+
TestConnectionListener listener = new TestConnectionListener();
101+
pooledConnection.removeConnectionEventListener(listener);
102+
assertEquals(0, listener.getConnectionClosedCount());
103+
assertEquals(0, listener.getConnectionErrorCount());
104+
}
105+
106+
@Test
107+
public void testPooledConnectionConnectionClosed() throws SQLException {
108+
String connectionUrl =
109+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
110+
111+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
112+
pooledDataSource.setURL(connectionUrl);
113+
114+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
115+
assertNotNull(pooledConnection);
116+
TestConnectionListener listener = new TestConnectionListener();
117+
pooledConnection.addConnectionEventListener(listener);
118+
assertEquals(0, listener.getConnectionClosedCount());
119+
assertEquals(0, listener.getConnectionErrorCount());
120+
121+
Connection connection = pooledConnection.getConnection();
122+
assertNotNull(connection);
123+
assertFalse(connection.isClosed());
124+
125+
connection.close();
126+
assertEquals(1, listener.getConnectionClosedCount());
127+
assertEquals(0, listener.getConnectionErrorCount());
128+
}
129+
130+
@Test
131+
public void testPooledConnectionClose() throws SQLException {
132+
String connectionUrl =
133+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
134+
135+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
136+
pooledDataSource.setURL(connectionUrl);
137+
138+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
139+
assertNotNull(pooledConnection);
140+
TestConnectionListener listener = new TestConnectionListener();
141+
pooledConnection.addConnectionEventListener(listener);
142+
assertEquals(0, listener.getConnectionClosedCount());
143+
assertEquals(0, listener.getConnectionErrorCount());
144+
145+
pooledConnection.close();
146+
assertEquals(1, listener.getConnectionClosedCount());
147+
assertEquals(0, listener.getConnectionErrorCount());
148+
}
149+
150+
@Test
151+
public void testPooledConnectionConnectionError() throws SQLException {
152+
String connectionUrl =
153+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
154+
155+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
156+
pooledDataSource.setURL(connectionUrl);
157+
158+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
159+
assertNotNull(pooledConnection);
160+
TestConnectionListener listener = new TestConnectionListener();
161+
pooledConnection.addConnectionEventListener(listener);
162+
assertEquals(0, listener.getConnectionClosedCount());
163+
assertEquals(0, listener.getConnectionErrorCount());
164+
165+
Connection connection = pooledConnection.getConnection();
166+
assertNotNull(connection);
167+
assertFalse(connection.isClosed());
168+
169+
ExecutorService executor = Executors.newFixedThreadPool(3);
170+
connection.abort(executor);
171+
assertEquals(0, listener.getConnectionClosedCount());
172+
assertEquals(1, listener.getConnectionErrorCount());
173+
174+
executor.shutdown();
175+
connection.close();
176+
pooledConnection.close();
177+
}
178+
179+
@Test
180+
public void testPooledConnectionListenerAddListener() throws SQLException {
181+
String connectionUrl =
182+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
183+
184+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
185+
pooledDataSource.setURL(connectionUrl);
186+
187+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
188+
assertNotNull(pooledConnection);
189+
PooledConnectionListener listener = new PooledConnectionListener(DEFAULT_CONN_POOL_SIZE);
190+
pooledConnection.addConnectionEventListener(listener);
191+
assertTrue(listener.isConnectionPoolEmpty());
192+
pooledConnection.close();
193+
}
194+
195+
@Test
196+
public void testPooledConnectionListenerRemoveListener() throws SQLException {
197+
String connectionUrl =
198+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
199+
200+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
201+
pooledDataSource.setURL(connectionUrl);
202+
203+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
204+
assertNotNull(pooledConnection);
205+
PooledConnectionListener listener = new PooledConnectionListener(DEFAULT_CONN_POOL_SIZE);
206+
pooledConnection.addConnectionEventListener(listener);
207+
assertTrue(listener.isConnectionPoolEmpty());
208+
209+
pooledConnection.removeConnectionEventListener(listener);
210+
assertTrue(listener.isConnectionPoolEmpty());
211+
pooledConnection.close();
212+
}
213+
214+
@Test
215+
public void testPooledConnectionListenerCloseConnection() throws SQLException {
216+
String connectionUrl =
217+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
218+
219+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
220+
pooledDataSource.setURL(connectionUrl);
221+
222+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
223+
assertNotNull(pooledConnection);
224+
PooledConnectionListener listener = new PooledConnectionListener(DEFAULT_CONN_POOL_SIZE);
225+
pooledConnection.addConnectionEventListener(listener);
226+
assertTrue(listener.isConnectionPoolEmpty());
227+
228+
Connection connection = pooledConnection.getConnection();
229+
assertNotNull(connection);
230+
assertFalse(connection.isClosed());
231+
232+
connection.close();
233+
assertFalse(listener.isConnectionPoolEmpty());
234+
pooledConnection.close();
235+
}
236+
237+
@Test
238+
public void testPooledConnectionListenerClosePooledConnection() throws SQLException {
239+
String connectionUrl =
240+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
241+
242+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
243+
pooledDataSource.setURL(connectionUrl);
244+
245+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
246+
assertNotNull(pooledConnection);
247+
PooledConnectionListener listener = new PooledConnectionListener(DEFAULT_CONN_POOL_SIZE);
248+
pooledConnection.addConnectionEventListener(listener);
249+
assertTrue(listener.isConnectionPoolEmpty());
250+
251+
pooledConnection.close();
252+
assertFalse(listener.isConnectionPoolEmpty());
253+
}
254+
255+
@Test
256+
public void testPooledConnectionListenerConnectionError() throws SQLException {
257+
String connectionUrl =
258+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;ConnectionPoolSize=20;ListenerPoolSize=20;";
259+
260+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
261+
pooledDataSource.setURL(connectionUrl);
262+
263+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
264+
assertNotNull(pooledConnection);
265+
PooledConnectionListener listener = new PooledConnectionListener(DEFAULT_CONN_POOL_SIZE);
266+
pooledConnection.addConnectionEventListener(listener);
267+
assertTrue(listener.isConnectionPoolEmpty());
268+
269+
Connection connection = pooledConnection.getConnection();
270+
assertNotNull(connection);
271+
assertFalse(connection.isClosed());
272+
273+
ExecutorService executor = Executors.newFixedThreadPool(3);
274+
connection.abort(executor);
275+
assertTrue(listener.isConnectionPoolEmpty());
276+
277+
executor.shutdown();
278+
connection.close();
279+
pooledConnection.close();
280+
}
281+
282+
@Test
283+
public void testExecuteQueryWithConnectionPoolingEnabledDefaultPoolSize() throws SQLException {
284+
String connectionURL =
285+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
286+
+ "OAuthType=3;ProjectId="
287+
+ PROJECT_ID
288+
+ ";";
289+
assertConnectionPoolingResults(connectionURL, DEFAULT_CONN_POOL_SIZE);
290+
}
291+
292+
@Test
293+
public void testExecuteQueryWithConnectionPoolingEnabledCustomPoolSize() throws SQLException {
294+
String connectionURL =
295+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
296+
+ "OAuthType=3;ProjectId="
297+
+ PROJECT_ID
298+
+ ";"
299+
+ "ConnectionPoolSize="
300+
+ CUSTOM_CONN_POOL_SIZE
301+
+ ";";
302+
assertConnectionPoolingResults(connectionURL, CUSTOM_CONN_POOL_SIZE);
303+
}
304+
305+
private void assertConnectionPoolingResults(String connectionURL, Long connectionPoolSize)
306+
throws SQLException {
307+
// Create Pooled Connection Datasource
308+
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
309+
pooledDataSource.setURL(connectionURL);
310+
311+
// Get pooled connection and ensure listner was added with default connection pool size.
312+
PooledConnection pooledConnection = pooledDataSource.getPooledConnection();
313+
assertNotNull(pooledConnection);
314+
PooledConnectionListener listener = pooledDataSource.getConnectionPoolManager();
315+
assertNotNull(listener);
316+
assertTrue(listener.isConnectionPoolEmpty());
317+
318+
// Get Underlying physical connection
319+
Connection connection = pooledConnection.getConnection();
320+
assertNotNull(connection);
321+
assertFalse(connection.isClosed());
322+
323+
// Execute query with physical connection
324+
String query =
325+
"SELECT DISTINCT repository_name FROM `bigquery-public-data.samples.github_timeline` LIMIT"
326+
+ " 850";
327+
Statement statement = connection.createStatement();
328+
ResultSet jsonResultSet = statement.executeQuery(query);
329+
assertTrue(jsonResultSet.getClass().getName().contains("BigQueryJsonResultSet"));
330+
331+
// Close physical connection
332+
connection.close();
333+
assertFalse(listener.isConnectionPoolEmpty());
334+
assertEquals(1, listener.getConnectionPoolCurrentCapacity());
335+
assertEquals(connectionPoolSize, listener.getConnectionPoolSize());
336+
337+
// Reuse same physical connection.
338+
connection = pooledConnection.getConnection();
339+
assertNotNull(connection);
340+
assertFalse(connection.isClosed());
341+
assertFalse(listener.isConnectionPoolEmpty());
342+
assertEquals(1, listener.getConnectionPoolCurrentCapacity());
343+
assertEquals(connectionPoolSize, listener.getConnectionPoolSize());
344+
345+
// Execute query with reusable physical connection
346+
jsonResultSet = statement.executeQuery(query);
347+
assertTrue(jsonResultSet.getClass().getName().contains("BigQueryJsonResultSet"));
348+
349+
// Return connection back to the pool.
350+
connection.close();
351+
assertFalse(listener.isConnectionPoolEmpty());
352+
assertEquals(1, listener.getConnectionPoolCurrentCapacity());
353+
assertEquals(connectionPoolSize, listener.getConnectionPoolSize());
354+
pooledConnection.close();
355+
}
356+
}

0 commit comments

Comments
 (0)