Skip to content

Commit f13a4e2

Browse files
committed
Expose transaction open status in AsyncTransaction and RxTransaction
This update adds the following methods: - `AsyncTransaction.isOpenAsync` - `RxTransaction.isOpen` They can be used to verify if transaction is open.
1 parent 350435c commit f13a4e2

File tree

7 files changed

+77
-0
lines changed

7 files changed

+77
-0
lines changed

driver/clirr-ignored-differences.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,16 @@
122122
<method>org.reactivestreams.Publisher isOpen()</method>
123123
</difference>
124124

125+
<difference>
126+
<className>org/neo4j/driver/async/AsyncTransaction</className>
127+
<differenceType>7012</differenceType>
128+
<method>java.util.concurrent.CompletionStage isOpenAsync()</method>
129+
</difference>
130+
131+
<difference>
132+
<className>org/neo4j/driver/reactive/RxTransaction</className>
133+
<differenceType>7012</differenceType>
134+
<method>org.reactivestreams.Publisher isOpen()</method>
135+
</difference>
136+
125137
</differences>

driver/src/main/java/org/neo4j/driver/async/AsyncTransaction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,11 @@ public interface AsyncTransaction extends AsyncQueryRunner
9898
* @return new {@link CompletionStage} that gets completed with {@code null} when close is successful, otherwise it gets completed exceptionally.
9999
*/
100100
CompletionStage<Void> closeAsync();
101+
102+
/**
103+
* Determine if transaction is open.
104+
*
105+
* @return a {@link CompletionStage} completed with {@code true} if transaction is open and {@code false} otherwise.
106+
*/
107+
CompletionStage<Boolean> isOpenAsync();
101108
}

driver/src/main/java/org/neo4j/driver/internal/async/InternalAsyncTransaction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.neo4j.driver.internal.async;
2020

21+
import java.util.concurrent.CompletableFuture;
2122
import java.util.concurrent.CompletionStage;
2223

2324
import org.neo4j.driver.Query;
@@ -50,6 +51,12 @@ public CompletionStage<Void> closeAsync()
5051
return tx.closeAsync();
5152
}
5253

54+
@Override
55+
public CompletionStage<Boolean> isOpenAsync()
56+
{
57+
return CompletableFuture.completedFuture( isOpen() );
58+
}
59+
5360
@Override
5461
public CompletionStage<ResultCursor> runAsync( Query query )
5562
{

driver/src/main/java/org/neo4j/driver/internal/reactive/InternalRxTransaction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.neo4j.driver.internal.reactive;
2020

2121
import org.reactivestreams.Publisher;
22+
import reactor.core.publisher.Mono;
2223

2324
import java.util.concurrent.CompletableFuture;
2425

@@ -81,6 +82,12 @@ public Publisher<Void> close()
8182
return close( false );
8283
}
8384

85+
@Override
86+
public Publisher<Boolean> isOpen()
87+
{
88+
return Mono.just( tx.isOpen() );
89+
}
90+
8491
Publisher<Void> close( boolean commit )
8592
{
8693
return createEmptyPublisher( () -> tx.closeAsync( commit ) );

driver/src/main/java/org/neo4j/driver/reactive/RxTransaction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,11 @@ public interface RxTransaction extends RxQueryRunner
5656
* @return new {@link Publisher} that gets completed when close is successful, otherwise an error is signalled.
5757
*/
5858
Publisher<Void> close();
59+
60+
/**
61+
* Determine if transaction is open.
62+
*
63+
* @return a publisher emitting {@code true} if transaction is open and {@code false} otherwise.
64+
*/
65+
Publisher<Boolean> isOpen();
5966
}

driver/src/test/java/org/neo4j/driver/internal/async/InternalAsyncTransactionTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.jupiter.params.provider.MethodSource;
2525

2626
import java.util.concurrent.CompletionStage;
27+
import java.util.concurrent.ExecutionException;
2728
import java.util.function.Function;
2829
import java.util.stream.Stream;
2930

@@ -42,8 +43,11 @@
4243
import static java.util.Collections.singletonMap;
4344
import static java.util.concurrent.CompletableFuture.completedFuture;
4445
import static org.junit.Assert.assertFalse;
46+
import static org.junit.jupiter.api.Assertions.assertEquals;
4547
import static org.junit.jupiter.api.Assertions.assertThrows;
4648
import static org.mockito.ArgumentMatchers.any;
49+
import static org.mockito.BDDMockito.given;
50+
import static org.mockito.BDDMockito.then;
4751
import static org.mockito.Mockito.mock;
4852
import static org.mockito.Mockito.verify;
4953
import static org.mockito.Mockito.when;
@@ -139,4 +143,21 @@ void shouldReleaseConnectionWhenFailedToRollback()
139143
verify( connection ).release();
140144
assertFalse( tx.isOpen() );
141145
}
146+
147+
@Test
148+
void shouldDelegateIsOpenAsync() throws ExecutionException, InterruptedException
149+
{
150+
// GIVEN
151+
UnmanagedTransaction utx = mock( UnmanagedTransaction.class );
152+
boolean expected = false;
153+
given( utx.isOpen() ).willReturn( expected );
154+
tx = new InternalAsyncTransaction( utx );
155+
156+
// WHEN
157+
boolean actual = tx.isOpenAsync().toCompletableFuture().get();
158+
159+
// THEN
160+
assertEquals( expected, actual );
161+
then( utx ).should().isOpen();
162+
}
142163
}

driver/src/test/java/org/neo4j/driver/internal/reactive/InternalRxTransactionTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import static org.junit.Assert.assertThat;
4848
import static org.junit.jupiter.api.Assertions.assertThrows;
4949
import static org.mockito.ArgumentMatchers.any;
50+
import static org.mockito.BDDMockito.given;
51+
import static org.mockito.BDDMockito.then;
5052
import static org.mockito.Mockito.mock;
5153
import static org.mockito.Mockito.verify;
5254
import static org.mockito.Mockito.when;
@@ -163,4 +165,18 @@ void shouldDelegateClose()
163165

164166
verify( tx ).closeAsync( false );
165167
}
168+
169+
@Test
170+
void shouldDelegateIsOpenAsync()
171+
{
172+
// GIVEN
173+
UnmanagedTransaction utx = mock( UnmanagedTransaction.class );
174+
boolean expected = false;
175+
given( utx.isOpen() ).willReturn( expected );
176+
RxTransaction tx = new InternalRxTransaction( utx );
177+
178+
// WHEN & THEN
179+
StepVerifier.create( tx.isOpen() ).expectNext( expected ).expectComplete().verify();
180+
then( utx ).should().isOpen();
181+
}
166182
}

0 commit comments

Comments
 (0)