Skip to content

Commit 889e048

Browse files
author
Zhen Li
committed
Update boltkit script tests use BoltV3
1 parent 0847c09 commit 889e048

File tree

57 files changed

+512
-675
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+512
-675
lines changed

driver/src/main/java/org/neo4j/driver/internal/messaging/request/BeginMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public BeginMessage( Bookmarks bookmarks, TransactionConfig config, AccessMode m
3838

3939
public BeginMessage( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
4040
{
41-
super( bookmarks, txTimeout, txMetadata, mode );
41+
super( buildMetadata( bookmarks, txTimeout, txMetadata, mode ) );
4242
}
4343

4444
@Override

driver/src/main/java/org/neo4j/driver/internal/messaging/request/RunWithMetadataMessage.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,41 @@
2424

2525
import org.neo4j.driver.internal.Bookmarks;
2626
import org.neo4j.driver.v1.AccessMode;
27+
import org.neo4j.driver.v1.Statement;
2728
import org.neo4j.driver.v1.TransactionConfig;
2829
import org.neo4j.driver.v1.Value;
2930

31+
import static java.util.Collections.emptyMap;
32+
import static org.neo4j.driver.v1.Values.ofValue;
33+
3034
public class RunWithMetadataMessage extends TransactionStartingMessage
3135
{
3236
public final static byte SIGNATURE = 0x10;
3337

3438
private final String statement;
3539
private final Map<String,Value> parameters;
3640

37-
public RunWithMetadataMessage( String statement, Map<String,Value> parameters, Bookmarks bookmarks, TransactionConfig config, AccessMode mode )
41+
public static RunWithMetadataMessage autoCommitTxRunMessage( Statement statement, TransactionConfig config, AccessMode mode,
42+
Bookmarks bookmark )
43+
{
44+
return autoCommitTxRunMessage( statement.text(), statement.parameters().asMap( ofValue() ), config.timeout(), config.metadata(), mode, bookmark );
45+
}
46+
47+
public static RunWithMetadataMessage autoCommitTxRunMessage( String statement, Map<String,Value> parameters, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode,
48+
Bookmarks bookmark )
49+
{
50+
Map<String,Value> metadata = buildMetadata( bookmark, txTimeout, txMetadata, mode );
51+
return new RunWithMetadataMessage( statement, parameters, metadata );
52+
}
53+
54+
public static RunWithMetadataMessage explicitTxRunMessage( Statement statement )
3855
{
39-
this( statement, parameters, bookmarks, config.timeout(), config.metadata(), mode );
56+
return new RunWithMetadataMessage( statement.text(), statement.parameters().asMap( ofValue() ), emptyMap() );
4057
}
4158

42-
public RunWithMetadataMessage( String statement, Map<String,Value> parameters, Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata,
43-
AccessMode mode )
59+
public RunWithMetadataMessage( String statement, Map<String,Value> parameters, Map<String,Value> metadata )
4460
{
45-
super( bookmarks, txTimeout, txMetadata, mode );
61+
super( metadata );
4662
this.statement = statement;
4763
this.parameters = parameters;
4864
}

driver/src/main/java/org/neo4j/driver/internal/messaging/request/TransactionStartingMessage.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.neo4j.driver.v1.Value;
2929

3030
import static java.util.Collections.emptyMap;
31+
import static java.util.Objects.requireNonNull;
3132
import static org.neo4j.driver.v1.Values.value;
3233

3334
abstract class TransactionStartingMessage implements Message
@@ -40,17 +41,17 @@ abstract class TransactionStartingMessage implements Message
4041

4142
final Map<String,Value> metadata;
4243

43-
TransactionStartingMessage( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
44+
TransactionStartingMessage( Map<String,Value> metadata )
4445
{
45-
this.metadata = buildMetadata( bookmarks, txTimeout, txMetadata, mode );
46+
this.metadata = requireNonNull( metadata );
4647
}
4748

4849
public final Map<String,Value> metadata()
4950
{
5051
return metadata;
5152
}
5253

53-
private static Map<String,Value> buildMetadata( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
54+
static Map<String,Value> buildMetadata( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
5455
{
5556
boolean bookmarksPresent = bookmarks != null && !bookmarks.isEmpty();
5657
boolean txTimeoutPresent = txTimeout != null;

driver/src/main/java/org/neo4j/driver/internal/messaging/v3/BoltProtocolV3.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.neo4j.driver.internal.messaging.request.BeginMessage;
4545
import org.neo4j.driver.internal.messaging.request.GoodbyeMessage;
4646
import org.neo4j.driver.internal.messaging.request.HelloMessage;
47-
import org.neo4j.driver.internal.messaging.request.RunWithMetadataMessage;
4847
import org.neo4j.driver.internal.spi.Connection;
4948
import org.neo4j.driver.internal.util.Futures;
5049
import org.neo4j.driver.internal.util.MetadataExtractor;
@@ -57,6 +56,8 @@
5756
import static org.neo4j.driver.internal.messaging.request.CommitMessage.COMMIT;
5857
import static org.neo4j.driver.internal.messaging.request.PullAllMessage.PULL_ALL;
5958
import static org.neo4j.driver.internal.messaging.request.RollbackMessage.ROLLBACK;
59+
import static org.neo4j.driver.internal.messaging.request.RunWithMetadataMessage.autoCommitTxRunMessage;
60+
import static org.neo4j.driver.internal.messaging.request.RunWithMetadataMessage.explicitTxRunMessage;
6061
import static org.neo4j.driver.v1.Values.ofValue;
6162

6263
public class BoltProtocolV3 implements BoltProtocol
@@ -131,24 +132,25 @@ public CompletionStage<Void> rollbackTransaction( Connection connection )
131132
public CompletionStage<InternalStatementResultCursor> runInAutoCommitTransaction( Connection connection, Statement statement,
132133
BookmarksHolder bookmarksHolder, TransactionConfig config, boolean waitForRunResponse )
133134
{
134-
return runStatement( connection, statement, bookmarksHolder, null, config, waitForRunResponse );
135+
Message runMessage = autoCommitTxRunMessage( statement, config, connection.mode(), bookmarksHolder.getBookmarks() );
136+
return runStatement( connection, statement, bookmarksHolder, null, waitForRunResponse, runMessage );
135137
}
136138

137139
@Override
138140
public CompletionStage<InternalStatementResultCursor> runInExplicitTransaction( Connection connection, Statement statement, ExplicitTransaction tx,
139141
boolean waitForRunResponse )
140142
{
141-
return runStatement( connection, statement, BookmarksHolder.NO_OP, tx, TransactionConfig.empty(), waitForRunResponse );
143+
Message runMessage = explicitTxRunMessage( statement );
144+
return runStatement( connection, statement, BookmarksHolder.NO_OP, tx, waitForRunResponse, runMessage );
142145
}
143146

144-
private static CompletionStage<InternalStatementResultCursor> runStatement( Connection connection, Statement statement,
145-
BookmarksHolder bookmarksHolder, ExplicitTransaction tx, TransactionConfig config, boolean waitForRunResponse )
147+
private static CompletionStage<InternalStatementResultCursor> runStatement( Connection connection, Statement statement, BookmarksHolder bookmarksHolder,
148+
ExplicitTransaction tx, boolean waitForRunResponse, Message runMessage )
146149
{
147150
String query = statement.text();
148151
Map<String,Value> params = statement.parameters().asMap( ofValue() );
149152

150153
CompletableFuture<Void> runCompletedFuture = new CompletableFuture<>();
151-
Message runMessage = new RunWithMetadataMessage( query, params, bookmarksHolder.getBookmarks(), config, connection.mode() );
152154
RunResponseHandler runHandler = new RunResponseHandler( runCompletedFuture, METADATA_EXTRACTOR );
153155
PullAllResponseHandler pullAllHandler = newPullAllHandler( statement, runHandler, connection, bookmarksHolder, tx );
154156

driver/src/test/java/org/neo4j/driver/internal/DirectDriverBoltKitTest.java

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.net.URI;
2626
import java.util.List;
2727
import java.util.Optional;
28+
import java.util.function.Consumer;
2829

2930
import org.neo4j.driver.internal.cluster.RoutingSettings;
3031
import org.neo4j.driver.internal.retry.RetrySettings;
@@ -150,7 +151,7 @@ void shouldSendReadAccessModeInStatementMetadata() throws Exception
150151
StubServer server = StubServer.start( "hello_run_exit_read.script", 9001 );
151152

152153
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
153-
Session session = driver.session( AccessMode.READ ) )
154+
Session session = driver.session( AccessMode.READ ) )
154155
{
155156
List<String> names = session.run( "MATCH (n) RETURN n.name" ).list( record -> record.get( 0 ).asString() );
156157
assertEquals( asList( "Foo", "Bar" ), names );
@@ -167,7 +168,7 @@ void shouldNotSendWriteAccessModeInStatementMetadata() throws Exception
167168
StubServer server = StubServer.start( "hello_run_exit.script", 9001 );
168169

169170
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
170-
Session session = driver.session( AccessMode.WRITE ) )
171+
Session session = driver.session( AccessMode.WRITE ) )
171172
{
172173
List<String> names = session.run( "MATCH (n) RETURN n.name" ).list( record -> record.get( 0 ).asString() );
173174
assertEquals( asList( "Foo", "Bar" ), names );
@@ -209,36 +210,62 @@ void shouldCloseChannelWhenResetFails() throws Exception
209210
}
210211

211212
@Test
212-
void shouldPropagateTransactionCommitErrorWhenSessionClosed() throws Exception
213+
void shouldPropagateTransactionRollbackErrorWhenSessionClosed() throws Exception
213214
{
214-
testTransactionCloseErrorPropagationWhenSessionClosed( "commit_error.script", true, "Unable to commit" );
215+
StubServer server = StubServer.start( "rollback_error.script", 9001 );
216+
try
217+
{
218+
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
219+
{
220+
Session session = driver.session();
221+
222+
Transaction tx = session.beginTransaction();
223+
StatementResult result = tx.run( "CREATE (n {name:'Alice'}) RETURN n.name AS name" );
224+
assertEquals( "Alice", result.single().get( "name" ).asString() );
225+
226+
TransientException e = assertThrows( TransientException.class, session::close );
227+
assertEquals( "Neo.TransientError.General.DatabaseUnavailable", e.code() );
228+
assertEquals( "Unable to rollback", e.getMessage() );
229+
}
230+
}
231+
finally
232+
{
233+
assertEquals( 0, server.exitStatus() );
234+
}
215235
}
216236

217237
@Test
218-
void shouldPropagateTransactionRollbackErrorWhenSessionClosed() throws Exception
238+
void shouldThrowCommitErrorWhenTransactionCommit() throws Exception
219239
{
220-
testTransactionCloseErrorPropagationWhenSessionClosed( "rollback_error.script", false, "Unable to rollback" );
240+
testTxCloseErrorPropagation( "commit_error.script", tx -> {
241+
tx.success();
242+
tx.close();
243+
}, "Unable to commit" );
221244
}
222245

223246
@Test
224-
void shouldThrowCommitErrorWhenTransactionClosed() throws Exception
247+
void shouldThrowRollbackErrorWhenTransactionRollback() throws Exception
225248
{
226-
testTxCloseErrorPropagation( "commit_error.script", true, "Unable to commit" );
249+
testTxCloseErrorPropagation( "rollback_error.script", tx -> {
250+
tx.failure();
251+
tx.close();
252+
}, "Unable to rollback" );
227253
}
228254

229255
@Test
230-
void shouldThrowRollbackErrorWhenTransactionClosed() throws Exception
256+
void shouldThrowRollbackErrorWhenTransactionClose() throws Exception
231257
{
232-
testTxCloseErrorPropagation( "rollback_error.script", false, "Unable to rollback" );
258+
testTxCloseErrorPropagation( "rollback_error.script", Transaction::close, "Unable to rollback" );
233259
}
234260

261+
235262
@Test
236263
void shouldThrowCorrectErrorOnRunFailure() throws Throwable
237264
{
238265
StubServer server = StubServer.start( "database_shutdown.script", 9001 );
239266

240267
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
241-
Session session = driver.session( "neo4j:bookmark:v1:tx0" );
268+
Session session = driver.session( "neo4j:bookmark:v1:tx0" );
242269
// has to enforce to flush BEGIN to have tx started.
243270
Transaction transaction = session.beginTransaction() )
244271
{
@@ -260,7 +287,7 @@ void shouldThrowCorrectErrorOnCommitFailure() throws Throwable
260287
StubServer server = StubServer.start( "database_shutdown_at_commit.script", 9001 );
261288

262289
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
263-
Session session = driver.session() )
290+
Session session = driver.session() )
264291
{
265292
Transaction transaction = session.beginTransaction();
266293
StatementResult result = transaction.run( "CREATE (n {name:'Bob'})" );
@@ -276,41 +303,7 @@ void shouldThrowCorrectErrorOnCommitFailure() throws Throwable
276303
}
277304
}
278305

279-
private static void testTransactionCloseErrorPropagationWhenSessionClosed( String script, boolean commit,
280-
String expectedErrorMessage ) throws Exception
281-
{
282-
StubServer server = StubServer.start( script, 9001 );
283-
try
284-
{
285-
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
286-
{
287-
Session session = driver.session();
288-
289-
Transaction tx = session.beginTransaction();
290-
StatementResult result = tx.run( "CREATE (n {name:'Alice'}) RETURN n.name AS name" );
291-
assertEquals( "Alice", result.single().get( "name" ).asString() );
292-
293-
if ( commit )
294-
{
295-
tx.success();
296-
}
297-
else
298-
{
299-
tx.failure();
300-
}
301-
302-
TransientException e = assertThrows( TransientException.class, session::close );
303-
assertEquals( "Neo.TransientError.General.DatabaseUnavailable", e.code() );
304-
assertEquals( expectedErrorMessage, e.getMessage() );
305-
}
306-
}
307-
finally
308-
{
309-
assertEquals( 0, server.exitStatus() );
310-
}
311-
}
312-
313-
private static void testTxCloseErrorPropagation( String script, boolean commit, String expectedErrorMessage )
306+
private static void testTxCloseErrorPropagation( String script, Consumer<Transaction> txAction, String expectedErrorMessage )
314307
throws Exception
315308
{
316309
StubServer server = StubServer.start( script, 9001 );
@@ -323,16 +316,8 @@ private static void testTxCloseErrorPropagation( String script, boolean commit,
323316
StatementResult result = tx.run( "CREATE (n {name:'Alice'}) RETURN n.name AS name" );
324317
assertEquals( "Alice", result.single().get( "name" ).asString() );
325318

326-
if ( commit )
327-
{
328-
tx.success();
329-
}
330-
else
331-
{
332-
tx.failure();
333-
}
319+
TransientException e = assertThrows( TransientException.class, () -> txAction.accept( tx ) );
334320

335-
TransientException e = assertThrows( TransientException.class, tx::close );
336321
assertEquals( "Neo.TransientError.General.DatabaseUnavailable", e.code() );
337322
assertEquals( expectedErrorMessage, e.getMessage() );
338323
}

0 commit comments

Comments
 (0)