Skip to content

Commit 4c32d7f

Browse files
logachevlqiu96
authored andcommitted
chore(bigquery-jdbc): fix flaky testSetTimeout test (#13549)
1 parent e72ef06 commit 4c32d7f

3 files changed

Lines changed: 14 additions & 47 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333

3434
public class ITBase extends BigQueryJdbcBaseTest {
3535

36+
// This query takes 300 seconds to complete
37+
public static final String query300seconds =
38+
"DECLARE DELAY_TIME DATETIME; SET DELAY_TIME = DATETIME_ADD(CURRENT_DATETIME, INTERVAL 300"
39+
+ " SECOND); WHILE CURRENT_DATETIME < DELAY_TIME DO END WHILE;";
40+
3641
private static String sharedDataset;
3742
private static String sharedDataset2;
3843

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
import org.junit.jupiter.api.BeforeAll;
5757
import org.junit.jupiter.api.Test;
5858

59-
public class ITNightlyBigQueryTest {
59+
public class ITNightlyBigQueryTest extends ITBase {
6060
static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();
6161
static Connection bigQueryConnection;
6262
static Statement bigQueryStatement;
@@ -215,18 +215,13 @@ public void testQueryInterruptGracefullyStopsExplicitJob()
215215
DriverManager.getConnection(connection_uri + ";JobCreationMode=1", new Properties());
216216
Statement bigQueryStatement = bigQueryConnection.createStatement();
217217

218-
// This query takes 300 seconds to complete
219-
String query300Seconds =
220-
"DECLARE DELAY_TIME DATETIME; SET DELAY_TIME = DATETIME_ADD(CURRENT_DATETIME, INTERVAL 300"
221-
+ " SECOND); WHILE CURRENT_DATETIME < DELAY_TIME DO END WHILE;";
222-
223218
// Query will be started in the background thread & we will call cancel from current thread.
224219
Thread t =
225220
new Thread(
226221
() -> {
227222
SQLException e =
228223
assertThrows(
229-
SQLException.class, () -> bigQueryStatement.execute(query300Seconds));
224+
SQLException.class, () -> bigQueryStatement.execute(ITBase.query300seconds));
230225
assertTrue(e.getMessage().contains("User requested cancellation"));
231226
threadException.set(false);
232227
});
@@ -254,18 +249,13 @@ public void testQueryInterruptGracefullyStopsOptionalJob()
254249
DriverManager.getConnection(connection_uri + ";JobCreationMode=2", new Properties());
255250
Statement bigQueryStatement = bigQueryConnection.createStatement();
256251

257-
// This query takes 300 seconds to complete
258-
String query300Seconds =
259-
"DECLARE DELAY_TIME DATETIME; SET DELAY_TIME = DATETIME_ADD(CURRENT_DATETIME, INTERVAL 300"
260-
+ " SECOND); WHILE CURRENT_DATETIME < DELAY_TIME DO END WHILE;";
261-
262252
// Query will be started in the background thread & we will call cancel from current thread.
263253
Thread t =
264254
new Thread(
265255
() -> {
266256
SQLException e =
267257
assertThrows(
268-
SQLException.class, () -> bigQueryStatement.execute(query300Seconds));
258+
SQLException.class, () -> bigQueryStatement.execute(ITBase.query300seconds));
269259
assertTrue(e.getMessage().contains("Query was cancelled."));
270260
threadException.set(false);
271261
});
@@ -1684,12 +1674,4 @@ private String getSessionId() throws InterruptedException {
16841674
Job stubJob = bigQuery.getJob(job.getJobId());
16851675
return stubJob.getStatistics().getSessionInfo().getSessionId();
16861676
}
1687-
1688-
private int resultSetRowCount(ResultSet resultSet) throws SQLException {
1689-
int rowCount = 0;
1690-
while (resultSet.next()) {
1691-
rowCount++;
1692-
}
1693-
return rowCount;
1694-
}
16951677
}

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

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.bigquery.jdbc.it;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertFalse;
2122
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -48,7 +49,7 @@
4849
import org.junit.jupiter.api.Disabled;
4950
import org.junit.jupiter.api.Test;
5051

51-
public class ITStatementTest {
52+
public class ITStatementTest extends ITBase {
5253
private static final String DEFAULT_CATALOG = ServiceOptions.getDefaultProjectId();
5354
private static String DATASET;
5455
private static Random random = new Random();
@@ -122,7 +123,7 @@ public void testExecuteQuery() throws SQLException {
122123
// setMaxRows Test
123124
statement.setMaxRows(5);
124125
ResultSet maxRowsResultSet = statement.executeQuery(selectQuery);
125-
assertEquals(5, getSizeOfResultSet(maxRowsResultSet));
126+
assertEquals(5, resultSetRowCount(maxRowsResultSet));
126127

127128
try {
128129
statement.setMaxRows(0);
@@ -244,14 +245,6 @@ public void testScript() throws SQLException {
244245
connection.close();
245246
}
246247

247-
private int resultSetRowCount(ResultSet resultSet) throws SQLException {
248-
int rowCount = 0;
249-
while (resultSet.next()) {
250-
rowCount++;
251-
}
252-
return rowCount;
253-
}
254-
255248
@Test
256249
public void testStringColumnLength() throws SQLException {
257250
String TABLE_NAME = "StringColumnLengthTable";
@@ -323,18 +316,13 @@ public void testSetTimeout() throws SQLException {
323316
Connection connection = DriverManager.getConnection(ITBase.connectionUrl);
324317
Statement statement = connection.createStatement();
325318

326-
String selectQuery =
327-
"SELECT views FROM bigquery-public-data.wikipedia.pageviews_2020 WHERE datehour >="
328-
+ " '2020-01-01' LIMIT 9000000";
329-
330319
// statement.execute(selectQuery);
331320
assertEquals(0, statement.getQueryTimeout());
332321
statement.setQueryTimeout(1);
333322
assertEquals(1, statement.getQueryTimeout());
334-
SQLException e = assertThrows(SQLException.class, () -> statement.executeQuery(selectQuery));
335-
assertEquals(
336-
"BigQueryException during runQuery\nJob execution was cancelled: Job timed out",
337-
e.getMessage());
323+
SQLException e =
324+
assertThrows(SQLException.class, () -> statement.executeQuery(ITBase.query300seconds));
325+
assertThat(e.getMessage()).contains("Job execution was cancelled: Job timed out");
338326
statement.close();
339327
connection.close();
340328
}
@@ -388,14 +376,6 @@ public void testRangeSelectDataset() throws SQLException {
388376
connection.close();
389377
}
390378

391-
int getSizeOfResultSet(ResultSet resultSet) throws SQLException {
392-
int count = 0;
393-
while (resultSet.next()) {
394-
count++;
395-
}
396-
return count;
397-
}
398-
399379
@Test
400380
public void testTemporaryDatasetLocation() throws SQLException, InterruptedException {
401381
String projectId = DEFAULT_CATALOG;

0 commit comments

Comments
 (0)