Skip to content

Commit

Permalink
Replace sleep with scheduled execution
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Mar 13, 2024
1 parent fd804f6 commit 70c0536
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions src/test/org/firebirdsql/jdbc/FBPreparedStatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
import java.math.BigInteger;
import java.sql.*;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

Expand Down Expand Up @@ -688,34 +691,24 @@ void testInsertReturning() throws Exception {
void testCancelStatement() throws Exception {
assumeTrue(getDefaultSupportInfo().supportsCancelOperation(), "Test requires fb_cancel_operations support");
assumeTrue(getDefaultSupportInfo().supportsExecuteBlock(), "Test requires EXECUTE BLOCK support");
final var cancelFailed = new AtomicBoolean(false);
try (var stmt = con.createStatement()) {
var cancelThread = new Thread(() -> {
final var cancelFailed = new AtomicBoolean(true);
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(() -> {
try {
Thread.sleep(5);
stmt.cancel();
} catch (SQLException ex) {
cancelFailed.set(true);
} catch (InterruptedException ex) {
// empty
cancelFailed.set(false);
} catch (SQLException ignored) {
}
}, "cancel-thread");

cancelThread.start();

try {
long start = System.currentTimeMillis();
SQLException exception = assertThrows(SQLException.class, () -> stmt.execute(LONG_RUNNING_STATEMENT),
"Statement should raise a cancel exception");
long end = System.currentTimeMillis();
System.out.println("testCancelStatement: statement cancelled after " + (end - start) + " milliseconds");
assertThat("Unexpected exception for cancellation", exception, allOf(
message(startsWith(getFbMessage(ISCConstants.isc_cancelled))),
errorCode(equalTo(ISCConstants.isc_cancelled)),
sqlState(equalTo("HY008"))));
} finally {
cancelThread.join();
}
}, 10, TimeUnit.MILLISECONDS);
executor.shutdown();

SQLException exception = assertThrows(SQLException.class, () -> stmt.execute(LONG_RUNNING_STATEMENT),
"Statement should raise a cancel exception");
assertThat("Unexpected exception for cancellation", exception, allOf(
message(startsWith(getFbMessage(ISCConstants.isc_cancelled))),
errorCode(equalTo(ISCConstants.isc_cancelled)),
sqlState(equalTo("HY008"))));
assertFalse(cancelFailed.get(), "Issuing statement cancel failed");
}
}
Expand Down

0 comments on commit 70c0536

Please sign in to comment.