Skip to content

4.0 adapt for bolkit 4.1 #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -80,10 +83,24 @@

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;

Expand All @@ -103,7 +120,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" ) );
Expand All @@ -128,7 +145,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 );
Expand Down Expand Up @@ -164,7 +181,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 );
Expand All @@ -182,7 +199,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() ) )
Expand All @@ -199,7 +216,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" );
Expand Down Expand Up @@ -230,7 +247,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 ) )
Expand All @@ -255,7 +272,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 ) )
Expand All @@ -275,7 +292,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() ) )
Expand All @@ -295,7 +312,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 ) )
Expand All @@ -315,7 +332,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 ) )
Expand All @@ -337,7 +354,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 ) )
Expand All @@ -362,7 +379,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 ) )
Expand All @@ -384,7 +401,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() ) )
Expand Down Expand Up @@ -423,7 +440,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 );
Expand All @@ -446,7 +463,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() )
Expand All @@ -467,7 +484,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() ) )
Expand All @@ -484,7 +501,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" ) ) )
Expand All @@ -500,7 +517,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 ) )
{
Expand All @@ -519,7 +536,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() );
Expand All @@ -533,7 +550,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() );
Expand All @@ -547,7 +564,7 @@ void shouldServerWithBoltV3NotSupportMultiDb() throws Throwable
private static void testTxCloseErrorPropagation( String script, Consumer<Transaction> 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 );
Expand Down
5 changes: 5 additions & 0 deletions driver/src/test/java/org/neo4j/driver/util/Neo4jRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

25 changes: 16 additions & 9 deletions driver/src/test/java/org/neo4j/driver/util/StubServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,15 +50,16 @@ public class StubServer
// This may be thrown if the driver has not been closed properly
public static class ForceKilled extends Exception {}

private static final String BOLT_STUB_COMMAND = "boltstub";
private static final String BOLT_COMMAND = "bolt";
private static final String BOLT_STUB_COMMAND = "stub";

private Process process;

private StubServer( String script, int port ) throws IOException, InterruptedException
{
List<String> command = new ArrayList<>();
command.addAll( singletonList( BOLT_STUB_COMMAND ) );
command.addAll( asList( Integer.toString( port ), script ) );
command.addAll( asList( BOLT_COMMAND, BOLT_STUB_COMMAND ) );
command.addAll( asList( "-l", "localhost:" + port, script ) );
ProcessBuilder server = new ProcessBuilder().command( command );
process = server.start();
startReadingOutput( process );
Expand All @@ -72,7 +72,7 @@ public static StubServer start( String resource, int port ) throws IOException,
return new StubServer( resource(resource), port );
}

public int exitStatus() throws InterruptedException, ForceKilled
public int exitStatus() throws InterruptedException
{
sleep( 500 ); // wait for a moment to allow disconnection to occur
try
Expand All @@ -83,19 +83,26 @@ public int exitStatus() throws InterruptedException, ForceKilled
{
// not exited yet
exit();
throw new ForceKilled();
}
return -1;
}

public static Config.ConfigBuilder insecureBuilder()
{
return Config.builder().withoutEncryption().withLogging( none() );
}

private void exit() throws InterruptedException
private void exit()
{
process.destroy();
process.waitFor();
try
{
process.waitFor();
}
catch ( InterruptedException ex )
{
throw new RuntimeException( "Interrupted whilst waiting for forced stub shutdown", ex);
}
}

private static String resource( String fileName )
Expand All @@ -113,7 +120,7 @@ private static boolean boltKitAvailable()
try
{
// run 'help' command to see if boltstub is available
Process process = new ProcessBuilder( BOLT_STUB_COMMAND, "-h" ).start();
Process process = new ProcessBuilder( "bolt" ).start();
int exitCode = process.waitFor();
return exitCode == 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2002-2020 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.util;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class StubServerController
{
private final List<StubServer> currentStubServers = new CopyOnWriteArrayList<>();

public StubServer startStub( String script, int port) throws IOException, InterruptedException
{
StubServer server = StubServer.start( script, port );
currentStubServers.add( server );
return server;
}

public void reset()
{
for ( StubServer server : currentStubServers )
{
try
{
server.exitStatus();
}
catch ( InterruptedException e )
{
throw new RuntimeException( "Interrupted whilst waiting for stub shutdown", e);
}
currentStubServers.remove( server );
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"addre
S: SUCCESS {"fields": ["ttl", "servers"]}
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9010"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9011"], "role": "READ"},{"addresses": ["127.0.0.1:9004"], "role": "ROUTE"}]]
SUCCESS {}
C: RESET
S: SUCCESS {}
<EXIT>
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
!: AUTO RESET
!: AUTO HELLO
!: AUTO GOODBYE
!: AUTO BEGIN

C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"address": "my.virtual.host:8080"}} {}
PULL_ALL
S: SUCCESS {"fields": ["ttl", "servers"]}
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9001"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9002","127.0.0.1:9003"], "role": "READ"},{"addresses": ["127.0.0.1:9001","127.0.0.1:9002","127.0.0.1:9003"], "role": "ROUTE"}]]
SUCCESS {}
C: RESET
S: SUCCESS {}
<EXIT>
Loading