Skip to content

Commit 8289679

Browse files
committed
job cancellation test
1 parent 40cd0a7 commit 8289679

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
import java.time.LocalTime;
5757
import java.util.Properties;
5858
import java.util.Random;
59+
import java.util.concurrent.ExecutorService;
60+
import java.util.concurrent.Executors;
61+
import java.util.concurrent.Future;
62+
import java.util.concurrent.TimeUnit;
5963
import java.util.function.BiFunction;
6064
import org.junit.jupiter.api.AfterAll;
6165
import org.junit.jupiter.api.Assertions;
@@ -406,6 +410,50 @@ public void testLocation() throws SQLException {
406410
connection2.close();
407411
}
408412

413+
@Test
414+
public void testCancelLocation() throws Exception {
415+
String connection_uri =
416+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;PROJECTID="
417+
+ PROJECT_ID
418+
+ ";OAUTHTYPE=3;LOCATION=EU";
419+
420+
Driver driver = BigQueryDriver.getRegisteredDriver();
421+
Connection connection = driver.connect(connection_uri, new Properties());
422+
Statement statement = connection.createStatement();
423+
424+
// Query a dataset in the EU with a heavy query so that we can cancel it while it runs.
425+
String query =
426+
"SELECT COUNT(*) FROM `bigquery-public-data.covid19_italy_eu.data_by_province` a "
427+
+ "CROSS JOIN `bigquery-public-data.covid19_italy_eu.data_by_province` b "
428+
+ "CROSS JOIN `bigquery-public-data.covid19_italy_eu.data_by_province` c";
429+
430+
// Run the query in a separate thread so we can cancel it from the main thread
431+
ExecutorService executor = Executors.newSingleThreadExecutor();
432+
Future<SQLException> future = executor.submit(() -> {
433+
try {
434+
statement.executeQuery(query);
435+
return null;
436+
} catch (SQLException e) {
437+
return e;
438+
}
439+
});
440+
441+
// Wait a short moment to let the query submit and start running
442+
Thread.sleep(1500);
443+
444+
// Cancel the query execution
445+
statement.cancel();
446+
447+
// Verify that the query threw a SQLException indicating it was cancelled
448+
SQLException exception = future.get(15, TimeUnit.SECONDS);
449+
assertNotNull(exception, "Expected SQLException to be thrown due to cancellation");
450+
assertTrue(exception.getMessage().contains("cancelled") || exception.getMessage().contains("Job was cancelled") || exception.getMessage().contains("Query was cancelled"),
451+
"Unexpected exception message: " + exception.getMessage());
452+
453+
connection.close();
454+
executor.shutdown();
455+
}
456+
409457
@Test
410458
public void testIncorrectLocation() throws SQLException {
411459
String connection_uri =

0 commit comments

Comments
 (0)