Skip to content

Expose transaction open status in AsyncTransaction and RxTransaction #1199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions driver/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,16 @@
<method>org.reactivestreams.Publisher executeWriteWithoutResult(java.util.function.Consumer, org.neo4j.driver.TransactionConfig)</method>
</difference>

<difference>
<className>org/neo4j/driver/async/AsyncTransaction</className>
<differenceType>7012</differenceType>
<method>java.util.concurrent.CompletionStage isOpenAsync()</method>
</difference>

<difference>
<className>org/neo4j/driver/reactive/RxTransaction</className>
<differenceType>7012</differenceType>
<method>org.reactivestreams.Publisher isOpen()</method>
</difference>

</differences>
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,11 @@ public interface AsyncTransaction extends AsyncQueryRunner
* @return new {@link CompletionStage} that gets completed with {@code null} when close is successful, otherwise it gets completed exceptionally.
*/
CompletionStage<Void> closeAsync();

/**
* Determine if transaction is open.
*
* @return a {@link CompletionStage} completed with {@code true} if transaction is open and {@code false} otherwise.
*/
CompletionStage<Boolean> isOpenAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.neo4j.driver.internal.async;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import org.neo4j.driver.Query;
Expand Down Expand Up @@ -50,6 +51,12 @@ public CompletionStage<Void> closeAsync()
return tx.closeAsync();
}

@Override
public CompletionStage<Boolean> isOpenAsync()
{
return CompletableFuture.completedFuture( isOpen() );
}

@Override
public CompletionStage<ResultCursor> runAsync( Query query )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.neo4j.driver.internal.reactive;

import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;

import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -81,6 +82,12 @@ public Publisher<Void> close()
return close( false );
}

@Override
public Publisher<Boolean> isOpen()
{
return Mono.just( tx.isOpen() );
}

Publisher<Void> close( boolean commit )
{
return createEmptyPublisher( () -> tx.closeAsync( commit ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ public interface RxTransaction extends RxQueryRunner
* @return new {@link Publisher} that gets completed when close is successful, otherwise an error is signalled.
*/
Publisher<Void> close();

/**
* Determine if transaction is open.
*
* @return a publisher emitting {@code true} if transaction is open and {@code false} otherwise.
*/
Publisher<Boolean> isOpen();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.jupiter.params.provider.MethodSource;

import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.stream.Stream;

Expand All @@ -42,8 +43,11 @@
import static java.util.Collections.singletonMap;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -139,4 +143,21 @@ void shouldReleaseConnectionWhenFailedToRollback()
verify( connection ).release();
assertFalse( tx.isOpen() );
}

@Test
void shouldDelegateIsOpenAsync() throws ExecutionException, InterruptedException
{
// GIVEN
UnmanagedTransaction utx = mock( UnmanagedTransaction.class );
boolean expected = false;
given( utx.isOpen() ).willReturn( expected );
tx = new InternalAsyncTransaction( utx );

// WHEN
boolean actual = tx.isOpenAsync().toCompletableFuture().get();

// THEN
assertEquals( expected, actual );
then( utx ).should().isOpen();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -163,4 +165,18 @@ void shouldDelegateClose()

verify( tx ).closeAsync( false );
}

@Test
void shouldDelegateIsOpenAsync()
{
// GIVEN
UnmanagedTransaction utx = mock( UnmanagedTransaction.class );
boolean expected = false;
given( utx.isOpen() ).willReturn( expected );
RxTransaction tx = new InternalRxTransaction( utx );

// WHEN & THEN
StepVerifier.create( tx.isOpen() ).expectNext( expected ).expectComplete().verify();
then( utx ).should().isOpen();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public class StartTest implements TestkitRequest
COMMON_SKIP_PATTERN_TO_REASON.put( "^.*\\.test_partial_summary_contains_system_updates$", "Does not contain updates because value is zero" );
COMMON_SKIP_PATTERN_TO_REASON.put( "^.*\\.test_partial_summary_contains_updates$", "Does not contain updates because value is zero" );
COMMON_SKIP_PATTERN_TO_REASON.put( "^.*\\.test_supports_multi_db$", "Database is None" );
COMMON_SKIP_PATTERN_TO_REASON.put( "^.*\\.test_managed_tx_raises_tx_managed_exec$",
"Driver (still) allows explicit managing of managed transaction" );
String skipMessage = "This test expects hostname verification to be turned off when all certificates are trusted";
COMMON_SKIP_PATTERN_TO_REASON.put( "^.*\\.TestTrustAllCertsConfig\\.test_trusted_ca_wrong_hostname$", skipMessage );
COMMON_SKIP_PATTERN_TO_REASON.put( "^.*\\.TestTrustAllCertsConfig\\.test_untrusted_ca_wrong_hostname$", skipMessage );
Expand Down