Skip to content

Commit 6dd837f

Browse files
committed
Move common handling to async cursor
1 parent a23da89 commit 6dd837f

File tree

12 files changed

+44
-40
lines changed

12 files changed

+44
-40
lines changed

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

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

21-
import java.util.concurrent.CompletableFuture;
2221
import java.util.concurrent.CompletionException;
2322
import java.util.concurrent.CompletionStage;
2423
import java.util.concurrent.atomic.AtomicBoolean;
@@ -83,10 +82,7 @@ public CompletionStage<ResultCursor> runAsync( Query query, TransactionConfig co
8382
buildResultCursorFactory( query, config ).thenCompose( ResultCursorFactory::asyncResult );
8483

8584
resultCursorStage = newResultCursorStage.exceptionally( error -> null );
86-
return newResultCursorStage.thenCompose(
87-
cursor -> cursor.runError()
88-
.map( Futures::<ResultCursor>failedFuture )
89-
.orElseGet( () -> CompletableFuture.completedFuture( cursor ) ) );
85+
return newResultCursorStage.thenCompose( AsyncResultCursor::mapSuccessfulRunCompletionAsync ).thenApply( cursor -> cursor );
9086
}
9187

9288
public CompletionStage<RxResultCursor> runRx(Query query, TransactionConfig config )

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.util.Arrays;
2222
import java.util.EnumSet;
23-
import java.util.concurrent.CompletableFuture;
2423
import java.util.concurrent.CompletionException;
2524
import java.util.concurrent.CompletionStage;
2625
import java.util.function.BiFunction;
@@ -212,10 +211,7 @@ public CompletionStage<ResultCursor> runAsync( Query query )
212211
CompletionStage<AsyncResultCursor> cursorStage =
213212
protocol.runInUnmanagedTransaction( connection, query, this, fetchSize ).asyncResult();
214213
resultCursors.add( cursorStage );
215-
return cursorStage.thenCompose(
216-
cursor -> cursor.runError()
217-
.map( Futures::<ResultCursor>failedFuture )
218-
.orElseGet( () -> CompletableFuture.completedFuture( cursor ) ) );
214+
return cursorStage.thenCompose( AsyncResultCursor::mapSuccessfulRunCompletionAsync ).thenApply( cursor -> cursor );
219215
}
220216

221217
public CompletionStage<RxResultCursor> runRx(Query query)

driver/src/main/java/org/neo4j/driver/internal/cursor/AsyncResultCursor.java

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

21-
import java.util.Optional;
21+
import java.util.concurrent.CompletableFuture;
2222

2323
import org.neo4j.driver.async.ResultCursor;
2424
import org.neo4j.driver.internal.FailableCursor;
2525

2626
public interface AsyncResultCursor extends ResultCursor, FailableCursor
2727
{
28-
Optional<Throwable> runError();
28+
CompletableFuture<AsyncResultCursor> mapSuccessfulRunCompletionAsync();
2929
}

driver/src/main/java/org/neo4j/driver/internal/cursor/AsyncResultCursorImpl.java

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

2121
import java.util.List;
22-
import java.util.Optional;
2322
import java.util.concurrent.CompletableFuture;
2423
import java.util.concurrent.CompletionStage;
2524
import java.util.function.Consumer;
@@ -161,8 +160,8 @@ else if ( record != null )
161160
}
162161

163162
@Override
164-
public Optional<Throwable> runError()
163+
public CompletableFuture<AsyncResultCursor> mapSuccessfulRunCompletionAsync()
165164
{
166-
return Optional.ofNullable( runError );
165+
return runError != null ? Futures.failedFuture( runError ) : CompletableFuture.completedFuture( this );
167166
}
168167
}

driver/src/main/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursor.java

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

2121
import java.util.List;
22-
import java.util.Optional;
2322
import java.util.concurrent.CompletableFuture;
2423
import java.util.concurrent.CompletionStage;
2524
import java.util.function.Consumer;
@@ -121,8 +120,8 @@ boolean isDisposed()
121120
}
122121

123122
@Override
124-
public Optional<Throwable> runError()
123+
public CompletableFuture<AsyncResultCursor> mapSuccessfulRunCompletionAsync()
125124
{
126-
return this.delegate.runError();
125+
return this.delegate.mapSuccessfulRunCompletionAsync().thenApply( ignored -> this.delegate );
127126
}
128127
}

driver/src/test/java/org/neo4j/driver/internal/cursor/AsyncResultCursorOnlyFactoryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
import static org.junit.Assert.assertThat;
3636
import static org.junit.jupiter.api.Assertions.assertSame;
3737
import static org.junit.jupiter.api.Assertions.assertThrows;
38-
import static org.junit.jupiter.api.Assertions.assertTrue;
3938
import static org.mockito.ArgumentMatchers.any;
4039
import static org.mockito.Mockito.mock;
4140
import static org.mockito.Mockito.verify;
4241
import static org.neo4j.driver.internal.util.Futures.getNow;
42+
import static org.neo4j.driver.util.TestUtil.await;
4343

4444
class AsyncResultCursorOnlyFactoryTest
4545
{
@@ -70,8 +70,8 @@ void shouldReturnAsyncResultWithRunErrorWhenRunFailed()
7070

7171
// Then
7272
AsyncResultCursor cursor = getNow( cursorFuture );
73-
assertTrue( cursor.runError().isPresent() );
74-
assertSame( error, cursor.runError().get() );
73+
Throwable actual = assertThrows( error.getClass(), () -> await( cursor.mapSuccessfulRunCompletionAsync() ) );
74+
assertSame( error, actual );
7575
}
7676

7777
@Test

driver/src/test/java/org/neo4j/driver/internal/cursor/ResultCursorFactoryImplTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
import static org.hamcrest.CoreMatchers.instanceOf;
3333
import static org.junit.Assert.assertThat;
3434
import static org.junit.jupiter.api.Assertions.assertSame;
35-
import static org.junit.jupiter.api.Assertions.assertTrue;
35+
import static org.junit.jupiter.api.Assertions.assertThrows;
3636
import static org.mockito.ArgumentMatchers.any;
3737
import static org.mockito.Mockito.mock;
3838
import static org.mockito.Mockito.verify;
3939
import static org.mockito.Mockito.verifyNoMoreInteractions;
4040
import static org.neo4j.driver.internal.util.Futures.getNow;
41+
import static org.neo4j.driver.util.TestUtil.await;
4142

4243
class ResultCursorFactoryImplTest
4344
{
@@ -68,8 +69,8 @@ void shouldReturnAsyncResultWithRunErrorWhenRunFailed()
6869

6970
// Then
7071
AsyncResultCursor cursor = getNow( cursorFuture );
71-
assertTrue( cursor.runError().isPresent() );
72-
assertSame( error, cursor.runError().get() );
72+
Throwable actual = assertThrows( error.getClass(), () -> await( cursor.mapSuccessfulRunCompletionAsync() ) );
73+
assertSame( error, actual );
7374
}
7475

7576
@Test

driver/src/test/java/org/neo4j/driver/internal/messaging/v3/BoltProtocolV3Test.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,12 @@ protected void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean succe
406406
assertTrue( cursorFuture.isDone() );
407407
if ( success )
408408
{
409-
assertFalse( cursorFuture.get().runError().isPresent() );
409+
assertNotNull( await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
410410
}
411411
else
412412
{
413-
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
413+
Throwable actual = assertThrows( error.getClass(), () -> await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
414+
assertSame( error, actual );
414415
}
415416
}
416417

@@ -481,7 +482,8 @@ protected void testFailedRunInAutoCommitTxWithWaitingForResponse( Bookmark bookm
481482
assertEquals( bookmark, bookmarkHolder.getBookmark() );
482483

483484
assertTrue( cursorFuture.isDone() );
484-
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
485+
Throwable actual = assertThrows( error.getClass(), () -> await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
486+
assertSame( error, actual );
485487
}
486488

487489
private static InternalAuthToken dummyAuthToken()

driver/src/test/java/org/neo4j/driver/internal/messaging/v4/BoltProtocolV4Test.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import static org.junit.jupiter.api.Assertions.assertNotNull;
8080
import static org.junit.jupiter.api.Assertions.assertNull;
8181
import static org.junit.jupiter.api.Assertions.assertSame;
82+
import static org.junit.jupiter.api.Assertions.assertThrows;
8283
import static org.junit.jupiter.api.Assertions.assertTrue;
8384
import static org.mockito.ArgumentMatchers.any;
8485
import static org.mockito.ArgumentMatchers.eq;
@@ -387,7 +388,8 @@ protected void testFailedRunInAutoCommitTxWithWaitingForResponse( Bookmark bookm
387388
// Then
388389
assertEquals( bookmark, bookmarkHolder.getBookmark() );
389390
assertTrue( cursorFuture.isDone() );
390-
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
391+
Throwable actual = assertThrows( error.getClass(), () -> await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
392+
assertSame( error, actual );
391393
}
392394

393395
protected void testSuccessfulRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmark, TransactionConfig config, AccessMode mode ) throws Exception
@@ -441,11 +443,12 @@ protected void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean succe
441443
assertTrue( cursorFuture.isDone() );
442444
if ( success )
443445
{
444-
assertFalse( cursorFuture.get().runError().isPresent() );
446+
assertNotNull( await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
445447
}
446448
else
447449
{
448-
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
450+
Throwable actual = assertThrows( error.getClass(), () -> await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
451+
assertSame( error, actual );
449452
}
450453
}
451454

driver/src/test/java/org/neo4j/driver/internal/messaging/v41/BoltProtocolV41Test.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import static org.junit.jupiter.api.Assertions.assertNotNull;
8181
import static org.junit.jupiter.api.Assertions.assertNull;
8282
import static org.junit.jupiter.api.Assertions.assertSame;
83+
import static org.junit.jupiter.api.Assertions.assertThrows;
8384
import static org.junit.jupiter.api.Assertions.assertTrue;
8485
import static org.mockito.ArgumentMatchers.any;
8586
import static org.mockito.ArgumentMatchers.eq;
@@ -382,7 +383,8 @@ private void testFailedRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmar
382383
// Then
383384
assertEquals( bookmark, bookmarkHolder.getBookmark() );
384385
assertTrue( cursorFuture.isDone() );
385-
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
386+
Throwable actual = assertThrows( error.getClass(), () -> await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
387+
assertSame( error, actual );
386388
}
387389

388390
private void testSuccessfulRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmark, TransactionConfig config, AccessMode mode ) throws Exception
@@ -436,11 +438,12 @@ private void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean success
436438
assertTrue( cursorFuture.isDone() );
437439
if ( success )
438440
{
439-
assertFalse( cursorFuture.get().runError().isPresent() );
441+
assertNotNull( await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
440442
}
441443
else
442444
{
443-
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
445+
Throwable actual = assertThrows( error.getClass(), () -> await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
446+
assertSame( error, actual );
444447
}
445448
}
446449

driver/src/test/java/org/neo4j/driver/internal/messaging/v42/BoltProtocolV42Test.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import static org.junit.jupiter.api.Assertions.assertNotNull;
8181
import static org.junit.jupiter.api.Assertions.assertNull;
8282
import static org.junit.jupiter.api.Assertions.assertSame;
83+
import static org.junit.jupiter.api.Assertions.assertThrows;
8384
import static org.junit.jupiter.api.Assertions.assertTrue;
8485
import static org.mockito.ArgumentMatchers.any;
8586
import static org.mockito.ArgumentMatchers.eq;
@@ -381,8 +382,8 @@ private void testFailedRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmar
381382

382383
// Then
383384
assertEquals( bookmark, bookmarkHolder.getBookmark() );
384-
assertTrue( cursorFuture.isDone() );
385-
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
385+
Throwable actual = assertThrows( error.getClass(), () -> await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
386+
assertSame( error, actual );
386387
}
387388

388389
private void testSuccessfulRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmark, TransactionConfig config, AccessMode mode ) throws Exception
@@ -436,11 +437,12 @@ private void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean success
436437
assertTrue( cursorFuture.isDone() );
437438
if ( success )
438439
{
439-
assertFalse( cursorFuture.get().runError().isPresent() );
440+
assertNotNull( await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
440441
}
441442
else
442443
{
443-
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
444+
Throwable actual = assertThrows( error.getClass(), () -> await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
445+
assertSame( error, actual );
444446
}
445447
}
446448

driver/src/test/java/org/neo4j/driver/internal/messaging/v43/BoltProtocolV43Test.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import static org.junit.jupiter.api.Assertions.assertNotNull;
8080
import static org.junit.jupiter.api.Assertions.assertNull;
8181
import static org.junit.jupiter.api.Assertions.assertSame;
82+
import static org.junit.jupiter.api.Assertions.assertThrows;
8283
import static org.junit.jupiter.api.Assertions.assertTrue;
8384
import static org.mockito.ArgumentMatchers.any;
8485
import static org.mockito.ArgumentMatchers.eq;
@@ -381,7 +382,8 @@ private void testFailedRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmar
381382
// Then
382383
assertEquals( bookmark, bookmarkHolder.getBookmark() );
383384
assertTrue( cursorFuture.isDone() );
384-
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
385+
Throwable actual = assertThrows( error.getClass(), () -> await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
386+
assertSame( error, actual );
385387
}
386388

387389
private void testSuccessfulRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmark, TransactionConfig config, AccessMode mode ) throws Exception
@@ -435,11 +437,12 @@ private void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean success
435437
assertTrue( cursorFuture.isDone() );
436438
if ( success )
437439
{
438-
assertFalse( cursorFuture.get().runError().isPresent() );
440+
assertNotNull( await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
439441
}
440442
else
441443
{
442-
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
444+
Throwable actual = assertThrows( error.getClass(), () -> await( cursorFuture.get().mapSuccessfulRunCompletionAsync() ) );
445+
assertSame( error, actual );
443446
}
444447
}
445448

0 commit comments

Comments
 (0)