diff --git a/driver/src/test/java/org/neo4j/driver/internal/DirectDriverBoltKitTest.java b/driver/src/test/java/org/neo4j/driver/internal/DirectDriverBoltKitTest.java index f017f225d2..118511726a 100644 --- a/driver/src/test/java/org/neo4j/driver/internal/DirectDriverBoltKitTest.java +++ b/driver/src/test/java/org/neo4j/driver/internal/DirectDriverBoltKitTest.java @@ -19,6 +19,8 @@ package org.neo4j.driver.internal; import io.netty.channel.Channel; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import reactor.core.publisher.Flux; @@ -53,6 +55,7 @@ import org.neo4j.driver.reactive.RxResult; import org.neo4j.driver.reactive.RxSession; import org.neo4j.driver.util.StubServer; +import org.neo4j.driver.util.StubServerController; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; @@ -75,15 +78,30 @@ import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING; import static org.neo4j.driver.util.StubServer.INSECURE_CONFIG; import static org.neo4j.driver.util.StubServer.insecureBuilder; +import static org.neo4j.driver.util.StubServer.start; import static org.neo4j.driver.util.TestUtil.asOrderedSet; import static org.neo4j.driver.util.TestUtil.await; class DirectDriverBoltKitTest { + private static StubServerController stubController; + + @BeforeAll + public static void setup() + { + stubController = new StubServerController(); + } + + @AfterEach + public void killServers() + { + stubController.reset(); + } + @Test void shouldBeAbleRunCypher() throws Exception { - StubServer server = StubServer.start( "return_x.script", 9001 ); + StubServer server = stubController.startStub( "return_x.script", 9001 ); URI uri = URI.create( "bolt://127.0.0.1:9001" ); int x; @@ -103,7 +121,7 @@ void shouldBeAbleRunCypher() throws Exception @Test void shouldSendMultipleBookmarks() throws Exception { - StubServer server = StubServer.start( "multiple_bookmarks.script", 9001 ); + StubServer server = stubController.startStub( "multiple_bookmarks.script", 9001 ); Bookmark bookmarks = InternalBookmark.parse( asOrderedSet( "neo4j:bookmark:v1:tx5", "neo4j:bookmark:v1:tx29", "neo4j:bookmark:v1:tx94", "neo4j:bookmark:v1:tx56", "neo4j:bookmark:v1:tx16", "neo4j:bookmark:v1:tx68" ) ); @@ -128,7 +146,7 @@ void shouldSendMultipleBookmarks() throws Exception @Test void shouldLogConnectionIdInDebugMode() throws Exception { - StubServer server = StubServer.start( "hello_run_exit.script", 9001 ); + StubServer server = stubController.startStub( "hello_run_exit.script", 9001 ); Logger logger = mock( Logger.class ); when( logger.isDebugEnabled() ).thenReturn( true ); @@ -164,7 +182,7 @@ void shouldLogConnectionIdInDebugMode() throws Exception @Test void shouldSendReadAccessModeInQueryMetadata() throws Exception { - StubServer server = StubServer.start( "hello_run_exit_read.script", 9001 ); + StubServer server = stubController.startStub( "hello_run_exit_read.script", 9001 ); try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ); @@ -182,7 +200,7 @@ void shouldSendReadAccessModeInQueryMetadata() throws Exception @Test void shouldNotSendWriteAccessModeInQueryMetadata() throws Exception { - StubServer server = StubServer.start( "hello_run_exit.script", 9001 ); + StubServer server = stubController.startStub( "hello_run_exit.script", 9001 ); try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ); Session session = driver.session( builder().withDefaultAccessMode( AccessMode.WRITE ).build() ) ) @@ -199,7 +217,7 @@ void shouldNotSendWriteAccessModeInQueryMetadata() throws Exception @Test void shouldCloseChannelWhenResetFails() throws Exception { - StubServer server = StubServer.start( "reset_error.script", 9001 ); + StubServer server = stubController.startStub( "reset_error.script", 9001 ); try { URI uri = URI.create( "bolt://localhost:9001" ); @@ -230,7 +248,7 @@ void shouldCloseChannelWhenResetFails() throws Exception @Test void shouldPropagateTransactionRollbackErrorWhenSessionClosed() throws Exception { - StubServer server = StubServer.start( "rollback_error.script", 9001 ); + StubServer server = stubController.startStub( "rollback_error.script", 9001 ); try { try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) ) @@ -255,7 +273,7 @@ void shouldPropagateTransactionRollbackErrorWhenSessionClosed() throws Exception @Test void shouldStreamingRecordsInBatchesRx() throws Exception { - StubServer server = StubServer.start( "streaming_records_v4_rx.script", 9001 ); + StubServer server = stubController.startStub( "streaming_records_v4_rx.script", 9001 ); try { try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) ) @@ -275,7 +293,7 @@ void shouldStreamingRecordsInBatchesRx() throws Exception @Test void shouldStreamingRecordsInBatches() throws Exception { - StubServer server = StubServer.start( "streaming_records_v4.script", 9001 ); + StubServer server = stubController.startStub( "streaming_records_v4.script", 9001 ); try { try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", insecureBuilder().withFetchSize( 2 ).build() ) ) @@ -295,7 +313,7 @@ void shouldStreamingRecordsInBatches() throws Exception @Test void shouldChangeFetchSize() throws Exception { - StubServer server = StubServer.start( "streaming_records_v4.script", 9001 ); + StubServer server = stubController.startStub( "streaming_records_v4.script", 9001 ); try { try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) ) @@ -315,7 +333,7 @@ void shouldChangeFetchSize() throws Exception @Test void shouldOnlyPullRecordsWhenNeededSimpleSession() throws Exception { - StubServer server = StubServer.start( "streaming_records_v4_buffering.script", 9001 ); + StubServer server = stubController.startStub( "streaming_records_v4_buffering.script", 9001 ); try { try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) ) @@ -337,7 +355,7 @@ void shouldOnlyPullRecordsWhenNeededSimpleSession() throws Exception @Test void shouldOnlyPullRecordsWhenNeededAsyncSession() throws Exception { - StubServer server = StubServer.start( "streaming_records_v4_buffering.script", 9001 ); + StubServer server = stubController.startStub( "streaming_records_v4_buffering.script", 9001 ); try { try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) ) @@ -362,7 +380,7 @@ void shouldOnlyPullRecordsWhenNeededAsyncSession() throws Exception @Test void shouldPullAllRecordsOnListAsyncWhenOverWatermark() throws Exception { - StubServer server = StubServer.start( "streaming_records_v4_list_async.script", 9001 ); + StubServer server = stubController.startStub( "streaming_records_v4_list_async.script", 9001 ); try { try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) ) @@ -384,7 +402,7 @@ void shouldPullAllRecordsOnListAsyncWhenOverWatermark() throws Exception @Test void shouldAllowPullAll() throws Exception { - StubServer server = StubServer.start( "streaming_records_v4_all.script", 9001 ); + StubServer server = stubController.startStub( "streaming_records_v4_all.script", 9001 ); try { try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", insecureBuilder().withFetchSize( -1 ).build() ) ) @@ -423,7 +441,7 @@ void shouldThrowRollbackErrorWhenTransactionClose() throws Exception @Test void shouldThrowCorrectErrorOnRunFailure() throws Throwable { - StubServer server = StubServer.start( "database_shutdown.script", 9001 ); + StubServer server = stubController.startStub( "database_shutdown.script", 9001 ); Bookmark bookmark = InternalBookmark.parse( "neo4j:bookmark:v1:tx0" ); try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ); @@ -446,7 +464,7 @@ void shouldThrowCorrectErrorOnRunFailure() throws Throwable @Test void shouldThrowCorrectErrorOnCommitFailure() throws Throwable { - StubServer server = StubServer.start( "database_shutdown_at_commit.script", 9001 ); + StubServer server = stubController.startStub( "database_shutdown_at_commit.script", 9001 ); try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ); Session session = driver.session() ) @@ -467,7 +485,7 @@ void shouldThrowCorrectErrorOnCommitFailure() throws Throwable @Test void shouldAllowDatabaseNameInSessionRun() throws Throwable { - StubServer server = StubServer.start( "read_server_v4_read.script", 9001 ); + StubServer server = stubController.startStub( "read_server_v4_read.script", 9001 ); try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ); Session session = driver.session( builder().withDatabase( "mydatabase" ).withDefaultAccessMode( AccessMode.READ ).build() ) ) @@ -484,7 +502,7 @@ void shouldAllowDatabaseNameInSessionRun() throws Throwable @Test void shouldAllowDatabaseNameInBeginTransaction() throws Throwable { - StubServer server = StubServer.start( "read_server_v4_read_tx.script", 9001 ); + StubServer server = stubController.startStub( "read_server_v4_read_tx.script", 9001 ); try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ); Session session = driver.session( forDatabase( "mydatabase" ) ) ) @@ -500,7 +518,7 @@ void shouldAllowDatabaseNameInBeginTransaction() throws Throwable @Test void shouldDiscardIfPullNotFinished() throws Throwable { - StubServer server = StubServer.start( "read_tx_v4_discard.script", 9001 ); + StubServer server = stubController.startStub( "read_tx_v4_discard.script", 9001 ); try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) ) { @@ -519,7 +537,7 @@ void shouldDiscardIfPullNotFinished() throws Throwable @Test void shouldServerWithBoltV4SupportMultiDb() throws Throwable { - StubServer server = StubServer.start( "support_multidb_v4.script", 9001 ); + StubServer server = stubController.startStub( "support_multidb_v4.script", 9001 ); try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) ) { assertTrue( driver.supportsMultiDb() ); @@ -533,7 +551,7 @@ void shouldServerWithBoltV4SupportMultiDb() throws Throwable @Test void shouldServerWithBoltV3NotSupportMultiDb() throws Throwable { - StubServer server = StubServer.start( "support_multidb_v3.script", 9001 ); + StubServer server = stubController.startStub( "support_multidb_v3.script", 9001 ); try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) ) { assertFalse( driver.supportsMultiDb() ); @@ -547,7 +565,7 @@ void shouldServerWithBoltV3NotSupportMultiDb() throws Throwable @Test void shouldBeAbleHandleNOOPsDuringRunCypher() throws Exception { - StubServer server = StubServer.start( "noop.script", 9001 ); + StubServer server = stubController.startStub( "noop.script", 9001 ); URI uri = URI.create( "bolt://127.0.0.1:9001" ); try ( Driver driver = GraphDatabase.driver( uri, INSECURE_CONFIG ) ) @@ -565,7 +583,7 @@ void shouldBeAbleHandleNOOPsDuringRunCypher() throws Exception private static void testTxCloseErrorPropagation( String script, Consumer txAction, String expectedErrorMessage ) throws Exception { - StubServer server = StubServer.start( script, 9001 ); + StubServer server = stubController.startStub( script, 9001 ); try { try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ); diff --git a/driver/src/test/java/org/neo4j/driver/util/Neo4jRunner.java b/driver/src/test/java/org/neo4j/driver/util/Neo4jRunner.java index 1ec3dc6e00..a36b931eda 100644 --- a/driver/src/test/java/org/neo4j/driver/util/Neo4jRunner.java +++ b/driver/src/test/java/org/neo4j/driver/util/Neo4jRunner.java @@ -366,5 +366,10 @@ public static void debug( String text, Object... args ) { System.out.println( String.format( text, args ) ); } + + public static void debug( String text ) + { + System.out.println( text ); + } } diff --git a/driver/src/test/java/org/neo4j/driver/util/StubServer.java b/driver/src/test/java/org/neo4j/driver/util/StubServer.java index c0ea59a131..59d55175bd 100644 --- a/driver/src/test/java/org/neo4j/driver/util/StubServer.java +++ b/driver/src/test/java/org/neo4j/driver/util/StubServer.java @@ -33,7 +33,6 @@ import static java.lang.Thread.sleep; import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static java.util.concurrent.Executors.newCachedThreadPool; import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assumptions.assumeTrue; diff --git a/driver/src/test/resources/commit_error.script b/driver/src/test/resources/commit_error.script index 28d9f40e69..493e37b10b 100644 --- a/driver/src/test/resources/commit_error.script +++ b/driver/src/test/resources/commit_error.script @@ -1,5 +1,4 @@ !: BOLT 3 -!: AUTO RESET !: AUTO HELLO !: AUTO GOODBYE @@ -14,3 +13,5 @@ C: COMMIT S: FAILURE {"code": "Neo.TransientError.General.DatabaseUnavailable", "message": "Unable to commit"} C: RESET S: SUCCESS {} +C: RESET +S: SUCCESS {} diff --git a/driver/src/test/resources/rollback_error.script b/driver/src/test/resources/rollback_error.script index 5d889f60f2..ef0ff1260c 100644 --- a/driver/src/test/resources/rollback_error.script +++ b/driver/src/test/resources/rollback_error.script @@ -1,5 +1,4 @@ !: BOLT 3 -!: AUTO RESET !: AUTO HELLO !: AUTO GOODBYE @@ -14,3 +13,5 @@ C: ROLLBACK S: FAILURE {"code": "Neo.TransientError.General.DatabaseUnavailable", "message": "Unable to rollback"} C: RESET S: SUCCESS {} +C: RESET +S: SUCCESS {}