Skip to content

Commit f3ae1d4

Browse files
committed
Tests for commit and rollback error propagation
Commit adds couple stub-server tests to assert that commit and rollback errors are correctly propagated.
1 parent b35d44c commit f3ae1d4

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@
2828
import org.neo4j.driver.internal.cluster.RoutingSettings;
2929
import org.neo4j.driver.internal.util.ChannelTrackingDriverFactory;
3030
import org.neo4j.driver.internal.util.Clock;
31+
import org.neo4j.driver.v1.AuthTokens;
3132
import org.neo4j.driver.v1.Config;
3233
import org.neo4j.driver.v1.Driver;
34+
import org.neo4j.driver.v1.GraphDatabase;
3335
import org.neo4j.driver.v1.Record;
3436
import org.neo4j.driver.v1.Session;
3537
import org.neo4j.driver.v1.StatementResult;
3638
import org.neo4j.driver.v1.Transaction;
3739
import org.neo4j.driver.v1.Value;
3840
import org.neo4j.driver.v1.exceptions.ClientException;
3941
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
42+
import org.neo4j.driver.v1.exceptions.TransientException;
43+
import org.neo4j.driver.v1.util.StubServer;
4044
import org.neo4j.driver.v1.util.TestNeo4jSession;
4145
import org.neo4j.driver.v1.util.TestUtil;
4246

@@ -385,6 +389,18 @@ public void shouldThrowWhenConnectionKilledDuringTransactionMarkedForSuccess()
385389
testFailWhenConnectionKilledDuringTransaction( true );
386390
}
387391

392+
@Test
393+
public void shouldThrowCommitError() throws Exception
394+
{
395+
testTxCloseErrorPropagation( "commit_error.script", true, "Unable to commit" );
396+
}
397+
398+
@Test
399+
public void shouldThrowRollbackError() throws Exception
400+
{
401+
testTxCloseErrorPropagation( "rollback_error.script", false, "Unable to rollback" );
402+
}
403+
388404
private void testFailWhenConnectionKilledDuringTransaction( boolean markForSuccess )
389405
{
390406
ChannelTrackingDriverFactory factory = new ChannelTrackingDriverFactory( 1, Clock.SYSTEM );
@@ -420,4 +436,45 @@ private void testFailWhenConnectionKilledDuringTransaction( boolean markForSucce
420436

421437
assertEquals( 0, session.run( "MATCH (n:MyNode {id: 1}) RETURN count(n)" ).single().get( 0 ).asInt() );
422438
}
439+
440+
private static void testTxCloseErrorPropagation( String script, boolean commit, String expectedErrorMessage )
441+
throws Exception
442+
{
443+
StubServer server = StubServer.start( script, 9001 );
444+
try
445+
{
446+
Config config = Config.build().withLogging( DEV_NULL_LOGGING ).withoutEncryption().toConfig();
447+
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", AuthTokens.none(), config );
448+
Session session = driver.session() )
449+
{
450+
Transaction tx = session.beginTransaction();
451+
StatementResult result = tx.run( "CREATE (n {name:'Alice'}) RETURN n.name AS name" );
452+
assertEquals( "Alice", result.single().get( "name" ).asString() );
453+
454+
if ( commit )
455+
{
456+
tx.success();
457+
}
458+
else
459+
{
460+
tx.failure();
461+
}
462+
463+
try
464+
{
465+
tx.close();
466+
fail( "Exception expected" );
467+
}
468+
catch ( TransientException e )
469+
{
470+
assertEquals( "Neo.TransientError.General.DatabaseUnavailable", e.code() );
471+
assertEquals( expectedErrorMessage, e.getMessage() );
472+
}
473+
}
474+
}
475+
finally
476+
{
477+
assertEquals( 0, server.exitStatus() );
478+
}
479+
}
423480
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
4+
C: RUN "BEGIN" {}
5+
PULL_ALL
6+
S: SUCCESS {}
7+
SUCCESS {}
8+
C: RUN "CREATE (n {name:'Alice'}) RETURN n.name AS name" {}
9+
PULL_ALL
10+
S: SUCCESS {"fields": ["name"]}
11+
RECORD ["Alice"]
12+
SUCCESS {}
13+
C: RUN "COMMIT" {}
14+
PULL_ALL
15+
S: FAILURE {"code": "Neo.TransientError.General.DatabaseUnavailable", "message": "Unable to commit"}
16+
IGNORED
17+
C: ACK_FAILURE
18+
S: SUCCESS {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
4+
C: RUN "BEGIN" {}
5+
PULL_ALL
6+
S: SUCCESS {}
7+
SUCCESS {}
8+
C: RUN "CREATE (n {name:'Alice'}) RETURN n.name AS name" {}
9+
PULL_ALL
10+
S: SUCCESS {"fields": ["name"]}
11+
RECORD ["Alice"]
12+
SUCCESS {}
13+
C: RUN "ROLLBACK" {}
14+
PULL_ALL
15+
S: FAILURE {"code": "Neo.TransientError.General.DatabaseUnavailable", "message": "Unable to rollback"}
16+
IGNORED
17+
C: ACK_FAILURE
18+
S: SUCCESS {}

0 commit comments

Comments
 (0)