Skip to content

Commit 6efcacc

Browse files
committed
Fixed couple flaky tests
Couple tests in `SessionAsyncIT` were flaky because they tried to run queries after killing the database and only retrieval from result cursor was expected to throw. However, `#runAsync()` itself can also throw because connection can't be acquired. This commit makes code treat exception from `#runAsync()` as expected. Stress test queries can handle leader switches but still tried to assert on number of created nodes even after failure. This commit makes them skip assertion after a handled failure.
1 parent f55cbcd commit 6efcacc

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed

driver/src/test/java/org/neo4j/driver/v1/integration/SessionAsyncIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,10 @@ public void shouldRunAfterRunFailureToAcquireConnection()
659659
{
660660
neo4j.killDb();
661661

662-
StatementResultCursor cursor1 = getBlocking( session.runAsync( "RETURN 42" ) );
663662
try
664663
{
665-
getBlocking( cursor1.nextAsync() );
664+
StatementResultCursor cursor = getBlocking( session.runAsync( "RETURN 42" ) );
665+
getBlocking( cursor.nextAsync() );
666666
fail( "Exception expected" );
667667
}
668668
catch ( ServiceUnavailableException e )
@@ -705,10 +705,10 @@ public void shouldBeginTxAfterRunFailureToAcquireConnection()
705705
{
706706
neo4j.killDb();
707707

708-
StatementResultCursor cursor1 = await( session.runAsync( "RETURN 42" ) );
709708
try
710709
{
711-
getBlocking( cursor1.consumeAsync() );
710+
StatementResultCursor cursor = await( session.runAsync( "RETURN 42" ) );
711+
getBlocking( cursor.consumeAsync() );
712712
fail( "Exception expected" );
713713
}
714714
catch ( ServiceUnavailableException e )

driver/src/test/java/org/neo4j/driver/v1/stress/AsyncWriteQuery.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,25 @@ public CompletionStage<Void> execute( C context )
4949
{
5050
session.closeAsync();
5151

52-
handleError( Futures.completionErrorCause( error ), context );
53-
assertEquals( 1, summary.counters().nodesCreated() );
54-
context.nodeCreated();
52+
if ( error != null )
53+
{
54+
handleError( Futures.completionErrorCause( error ), context );
55+
}
56+
else
57+
{
58+
assertEquals( 1, summary.counters().nodesCreated() );
59+
context.nodeCreated();
60+
}
61+
5562
return null;
5663
} );
5764
}
5865

5966
private void handleError( Throwable error, C context )
6067
{
61-
if ( error != null )
68+
if ( !stressTest.handleWriteFailure( error, context ) )
6269
{
63-
if ( !stressTest.handleWriteFailure( error, context ) )
64-
{
65-
throw new RuntimeException( error );
66-
}
70+
throw new RuntimeException( error );
6771
}
6872
}
6973
}

driver/src/test/java/org/neo4j/driver/v1/stress/AsyncWriteQueryInTx.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,25 @@ public CompletionStage<Void> execute( C context )
5252
{
5353
session.closeAsync();
5454

55-
handleError( Futures.completionErrorCause( error ), context );
56-
assertEquals( 1, summary.counters().nodesCreated() );
57-
context.nodeCreated();
55+
if ( error != null )
56+
{
57+
handleError( Futures.completionErrorCause( error ), context );
58+
}
59+
else
60+
{
61+
assertEquals( 1, summary.counters().nodesCreated() );
62+
context.nodeCreated();
63+
}
64+
5865
return null;
5966
} );
6067
}
6168

6269
private void handleError( Throwable error, C context )
6370
{
64-
if ( error != null )
71+
if ( !stressTest.handleWriteFailure( error, context ) )
6572
{
66-
if ( !stressTest.handleWriteFailure( error, context ) )
67-
{
68-
throw new RuntimeException( error );
69-
}
73+
throw new RuntimeException( error );
7074
}
7175
}
7276
}

driver/src/test/java/org/neo4j/driver/v1/stress/BlockingWriteQuery.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,22 @@ public BlockingWriteQuery( AbstractStressTestBase<C> stressTest, Driver driver,
3939
public void execute( C context )
4040
{
4141
StatementResult result = null;
42+
Throwable queryError = null;
4243

4344
try ( Session session = newSession( AccessMode.WRITE, context ) )
4445
{
4546
result = session.run( "CREATE ()" );
4647
}
4748
catch ( Throwable error )
4849
{
50+
queryError = error;
4951
if ( !stressTest.handleWriteFailure( error, context ) )
5052
{
5153
throw error;
5254
}
5355
}
5456

55-
if ( result != null )
57+
if ( queryError == null && result != null )
5658
{
5759
assertEquals( 1, result.summary().counters().nodesCreated() );
5860
context.nodeCreated();

driver/src/test/java/org/neo4j/driver/v1/stress/BlockingWriteQueryInTx.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public BlockingWriteQueryInTx( AbstractStressTestBase<C> stressTest, Driver driv
4040
public void execute( C context )
4141
{
4242
StatementResult result = null;
43+
Throwable txError = null;
4344

4445
try ( Session session = newSession( AccessMode.WRITE, context ) )
4546
{
@@ -53,13 +54,14 @@ public void execute( C context )
5354
}
5455
catch ( Throwable error )
5556
{
57+
txError = error;
5658
if ( !stressTest.handleWriteFailure( error, context ) )
5759
{
5860
throw error;
5961
}
6062
}
6163

62-
if ( result != null )
64+
if ( txError == null && result != null )
6365
{
6466
assertEquals( 1, result.summary().counters().nodesCreated() );
6567
context.nodeCreated();

0 commit comments

Comments
 (0)