From a427085e162da1de58a516e495d17aa1707f5208 Mon Sep 17 00:00:00 2001 From: Dmitriy Tverdiakov Date: Wed, 29 Mar 2023 12:46:46 +0100 Subject: [PATCH] [Internal] Allow interrupt for already interrupted transaction --- .../internal/async/UnmanagedTransaction.java | 2 +- .../async/UnmanagedTransactionTest.java | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/driver/src/main/java/org/neo4j/driver/internal/async/UnmanagedTransaction.java b/driver/src/main/java/org/neo4j/driver/internal/async/UnmanagedTransaction.java index 69786ece57..494f845bfa 100644 --- a/driver/src/main/java/org/neo4j/driver/internal/async/UnmanagedTransaction.java +++ b/driver/src/main/java/org/neo4j/driver/internal/async/UnmanagedTransaction.java @@ -179,7 +179,7 @@ public boolean isOpen() { public void markTerminated(Throwable cause) { executeWithLock(lock, () -> { if (state == State.TERMINATED) { - if (causeOfTermination != null) { + if (causeOfTermination != null && cause != null) { addSuppressedWhenNotCaptured(causeOfTermination, cause); } } else { diff --git a/driver/src/test/java/org/neo4j/driver/internal/async/UnmanagedTransactionTest.java b/driver/src/test/java/org/neo4j/driver/internal/async/UnmanagedTransactionTest.java index 06bcfcc393..658755e1d3 100644 --- a/driver/src/test/java/org/neo4j/driver/internal/async/UnmanagedTransactionTest.java +++ b/driver/src/test/java/org/neo4j/driver/internal/async/UnmanagedTransactionTest.java @@ -43,6 +43,7 @@ import static org.neo4j.driver.testutil.TestUtil.await; import static org.neo4j.driver.testutil.TestUtil.beginMessage; import static org.neo4j.driver.testutil.TestUtil.connectionMock; +import static org.neo4j.driver.testutil.TestUtil.setupFailingRun; import static org.neo4j.driver.testutil.TestUtil.setupSuccessfulRunAndPull; import static org.neo4j.driver.testutil.TestUtil.setupSuccessfulRunRx; import static org.neo4j.driver.testutil.TestUtil.verifyBeginTx; @@ -69,6 +70,7 @@ import org.neo4j.driver.exceptions.AuthorizationExpiredException; import org.neo4j.driver.exceptions.ClientException; import org.neo4j.driver.exceptions.ConnectionReadTimeoutException; +import org.neo4j.driver.exceptions.Neo4jException; import org.neo4j.driver.internal.FailableCursor; import org.neo4j.driver.internal.InternalBookmark; import org.neo4j.driver.internal.messaging.BoltProtocol; @@ -456,6 +458,27 @@ void shouldServeTheSameStageOnInterruptAsync() { assertEquals(stage0, stage1); } + @Test + void shouldHandleInterruptionWhenAlreadyInterrupted() throws ExecutionException, InterruptedException { + // Given + var connection = connectionMock(BoltProtocolV4.INSTANCE); + var exception = new Neo4jException("message"); + setupFailingRun(connection, exception); + var tx = beginTx(connection); + Throwable actualException = null; + + // When + try { + tx.runAsync(new Query("RETURN 1")).toCompletableFuture().get(); + } catch (ExecutionException e) { + actualException = e.getCause(); + } + tx.interruptAsync().toCompletableFuture().get(); + + // Then + assertEquals(exception, actualException); + } + private static UnmanagedTransaction beginTx(Connection connection) { return beginTx(connection, Collections.emptySet()); }