|
56 | 56 | import java.time.LocalTime; |
57 | 57 | import java.util.Properties; |
58 | 58 | 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; |
59 | 63 | import java.util.function.BiFunction; |
60 | 64 | import org.junit.jupiter.api.AfterAll; |
61 | 65 | import org.junit.jupiter.api.Assertions; |
@@ -406,6 +410,50 @@ public void testLocation() throws SQLException { |
406 | 410 | connection2.close(); |
407 | 411 | } |
408 | 412 |
|
| 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 | + |
409 | 457 | @Test |
410 | 458 | public void testIncorrectLocation() throws SQLException { |
411 | 459 | String connection_uri = |
|
0 commit comments