Skip to content

Commit 239d9de

Browse files
authored
4.0 adapt for bolkit 4.1 (#722)
* Upgrade Boltkit to 4.1. Adding BoltKit Test for NOOP message. (#703) * Adapt bolkit tests for boltkit 4.1 (#721) Refactor `DirectDriverBoltKitTest` to ensure boltstub is restarted freshly for each run. Fix scripts that are broken with boltkit 4.1. * Remove NOOP test, not needed for 4.0 branch
1 parent a845c70 commit 239d9de

10 files changed

+246
-146
lines changed

driver/src/test/java/org/neo4j/driver/integration/RoutingDriverBoltKitTest.java

Lines changed: 129 additions & 107 deletions
Large diffs are not rendered by default.

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

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.neo4j.driver.internal;
2020

2121
import io.netty.channel.Channel;
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeAll;
2224
import org.junit.jupiter.api.Test;
2325
import org.mockito.ArgumentCaptor;
2426
import reactor.core.publisher.Flux;
@@ -53,6 +55,7 @@
5355
import org.neo4j.driver.reactive.RxResult;
5456
import org.neo4j.driver.reactive.RxSession;
5557
import org.neo4j.driver.util.StubServer;
58+
import org.neo4j.driver.util.StubServerController;
5659

5760
import static java.util.Arrays.asList;
5861
import static java.util.Collections.singletonList;
@@ -80,10 +83,24 @@
8083

8184
class DirectDriverBoltKitTest
8285
{
86+
private static StubServerController stubController;
87+
88+
@BeforeAll
89+
public static void setup()
90+
{
91+
stubController = new StubServerController();
92+
}
93+
94+
@AfterEach
95+
public void killServers()
96+
{
97+
stubController.reset();
98+
}
99+
83100
@Test
84101
void shouldBeAbleRunCypher() throws Exception
85102
{
86-
StubServer server = StubServer.start( "return_x.script", 9001 );
103+
StubServer server = stubController.startStub( "return_x.script", 9001 );
87104
URI uri = URI.create( "bolt://127.0.0.1:9001" );
88105
int x;
89106

@@ -103,7 +120,7 @@ void shouldBeAbleRunCypher() throws Exception
103120
@Test
104121
void shouldSendMultipleBookmarks() throws Exception
105122
{
106-
StubServer server = StubServer.start( "multiple_bookmarks.script", 9001 );
123+
StubServer server = stubController.startStub( "multiple_bookmarks.script", 9001 );
107124

108125
Bookmark bookmarks = InternalBookmark.parse( asOrderedSet( "neo4j:bookmark:v1:tx5", "neo4j:bookmark:v1:tx29",
109126
"neo4j:bookmark:v1:tx94", "neo4j:bookmark:v1:tx56", "neo4j:bookmark:v1:tx16", "neo4j:bookmark:v1:tx68" ) );
@@ -128,7 +145,7 @@ void shouldSendMultipleBookmarks() throws Exception
128145
@Test
129146
void shouldLogConnectionIdInDebugMode() throws Exception
130147
{
131-
StubServer server = StubServer.start( "hello_run_exit.script", 9001 );
148+
StubServer server = stubController.startStub( "hello_run_exit.script", 9001 );
132149

133150
Logger logger = mock( Logger.class );
134151
when( logger.isDebugEnabled() ).thenReturn( true );
@@ -164,7 +181,7 @@ void shouldLogConnectionIdInDebugMode() throws Exception
164181
@Test
165182
void shouldSendReadAccessModeInQueryMetadata() throws Exception
166183
{
167-
StubServer server = StubServer.start( "hello_run_exit_read.script", 9001 );
184+
StubServer server = stubController.startStub( "hello_run_exit_read.script", 9001 );
168185

169186

170187
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
@@ -182,7 +199,7 @@ void shouldSendReadAccessModeInQueryMetadata() throws Exception
182199
@Test
183200
void shouldNotSendWriteAccessModeInQueryMetadata() throws Exception
184201
{
185-
StubServer server = StubServer.start( "hello_run_exit.script", 9001 );
202+
StubServer server = stubController.startStub( "hello_run_exit.script", 9001 );
186203

187204
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
188205
Session session = driver.session( builder().withDefaultAccessMode( AccessMode.WRITE ).build() ) )
@@ -199,7 +216,7 @@ void shouldNotSendWriteAccessModeInQueryMetadata() throws Exception
199216
@Test
200217
void shouldCloseChannelWhenResetFails() throws Exception
201218
{
202-
StubServer server = StubServer.start( "reset_error.script", 9001 );
219+
StubServer server = stubController.startStub( "reset_error.script", 9001 );
203220
try
204221
{
205222
URI uri = URI.create( "bolt://localhost:9001" );
@@ -230,7 +247,7 @@ void shouldCloseChannelWhenResetFails() throws Exception
230247
@Test
231248
void shouldPropagateTransactionRollbackErrorWhenSessionClosed() throws Exception
232249
{
233-
StubServer server = StubServer.start( "rollback_error.script", 9001 );
250+
StubServer server = stubController.startStub( "rollback_error.script", 9001 );
234251
try
235252
{
236253
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
@@ -255,7 +272,7 @@ void shouldPropagateTransactionRollbackErrorWhenSessionClosed() throws Exception
255272
@Test
256273
void shouldStreamingRecordsInBatchesRx() throws Exception
257274
{
258-
StubServer server = StubServer.start( "streaming_records_v4_rx.script", 9001 );
275+
StubServer server = stubController.startStub( "streaming_records_v4_rx.script", 9001 );
259276
try
260277
{
261278
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
@@ -275,7 +292,7 @@ void shouldStreamingRecordsInBatchesRx() throws Exception
275292
@Test
276293
void shouldStreamingRecordsInBatches() throws Exception
277294
{
278-
StubServer server = StubServer.start( "streaming_records_v4.script", 9001 );
295+
StubServer server = stubController.startStub( "streaming_records_v4.script", 9001 );
279296
try
280297
{
281298
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", insecureBuilder().withFetchSize( 2 ).build() ) )
@@ -295,7 +312,7 @@ void shouldStreamingRecordsInBatches() throws Exception
295312
@Test
296313
void shouldChangeFetchSize() throws Exception
297314
{
298-
StubServer server = StubServer.start( "streaming_records_v4.script", 9001 );
315+
StubServer server = stubController.startStub( "streaming_records_v4.script", 9001 );
299316
try
300317
{
301318
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
@@ -315,7 +332,7 @@ void shouldChangeFetchSize() throws Exception
315332
@Test
316333
void shouldOnlyPullRecordsWhenNeededSimpleSession() throws Exception
317334
{
318-
StubServer server = StubServer.start( "streaming_records_v4_buffering.script", 9001 );
335+
StubServer server = stubController.startStub( "streaming_records_v4_buffering.script", 9001 );
319336
try
320337
{
321338
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
@@ -337,7 +354,7 @@ void shouldOnlyPullRecordsWhenNeededSimpleSession() throws Exception
337354
@Test
338355
void shouldOnlyPullRecordsWhenNeededAsyncSession() throws Exception
339356
{
340-
StubServer server = StubServer.start( "streaming_records_v4_buffering.script", 9001 );
357+
StubServer server = stubController.startStub( "streaming_records_v4_buffering.script", 9001 );
341358
try
342359
{
343360
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
@@ -362,7 +379,7 @@ void shouldOnlyPullRecordsWhenNeededAsyncSession() throws Exception
362379
@Test
363380
void shouldPullAllRecordsOnListAsyncWhenOverWatermark() throws Exception
364381
{
365-
StubServer server = StubServer.start( "streaming_records_v4_list_async.script", 9001 );
382+
StubServer server = stubController.startStub( "streaming_records_v4_list_async.script", 9001 );
366383
try
367384
{
368385
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
@@ -384,7 +401,7 @@ void shouldPullAllRecordsOnListAsyncWhenOverWatermark() throws Exception
384401
@Test
385402
void shouldAllowPullAll() throws Exception
386403
{
387-
StubServer server = StubServer.start( "streaming_records_v4_all.script", 9001 );
404+
StubServer server = stubController.startStub( "streaming_records_v4_all.script", 9001 );
388405
try
389406
{
390407
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", insecureBuilder().withFetchSize( -1 ).build() ) )
@@ -423,7 +440,7 @@ void shouldThrowRollbackErrorWhenTransactionClose() throws Exception
423440
@Test
424441
void shouldThrowCorrectErrorOnRunFailure() throws Throwable
425442
{
426-
StubServer server = StubServer.start( "database_shutdown.script", 9001 );
443+
StubServer server = stubController.startStub( "database_shutdown.script", 9001 );
427444

428445
Bookmark bookmark = InternalBookmark.parse( "neo4j:bookmark:v1:tx0" );
429446
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
@@ -446,7 +463,7 @@ void shouldThrowCorrectErrorOnRunFailure() throws Throwable
446463
@Test
447464
void shouldThrowCorrectErrorOnCommitFailure() throws Throwable
448465
{
449-
StubServer server = StubServer.start( "database_shutdown_at_commit.script", 9001 );
466+
StubServer server = stubController.startStub( "database_shutdown_at_commit.script", 9001 );
450467

451468
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
452469
Session session = driver.session() )
@@ -467,7 +484,7 @@ void shouldThrowCorrectErrorOnCommitFailure() throws Throwable
467484
@Test
468485
void shouldAllowDatabaseNameInSessionRun() throws Throwable
469486
{
470-
StubServer server = StubServer.start( "read_server_v4_read.script", 9001 );
487+
StubServer server = stubController.startStub( "read_server_v4_read.script", 9001 );
471488

472489
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
473490
Session session = driver.session( builder().withDatabase( "mydatabase" ).withDefaultAccessMode( AccessMode.READ ).build() ) )
@@ -484,7 +501,7 @@ void shouldAllowDatabaseNameInSessionRun() throws Throwable
484501
@Test
485502
void shouldAllowDatabaseNameInBeginTransaction() throws Throwable
486503
{
487-
StubServer server = StubServer.start( "read_server_v4_read_tx.script", 9001 );
504+
StubServer server = stubController.startStub( "read_server_v4_read_tx.script", 9001 );
488505

489506
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
490507
Session session = driver.session( forDatabase( "mydatabase" ) ) )
@@ -500,7 +517,7 @@ void shouldAllowDatabaseNameInBeginTransaction() throws Throwable
500517
@Test
501518
void shouldDiscardIfPullNotFinished() throws Throwable
502519
{
503-
StubServer server = StubServer.start( "read_tx_v4_discard.script", 9001 );
520+
StubServer server = stubController.startStub( "read_tx_v4_discard.script", 9001 );
504521

505522
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
506523
{
@@ -519,7 +536,7 @@ void shouldDiscardIfPullNotFinished() throws Throwable
519536
@Test
520537
void shouldServerWithBoltV4SupportMultiDb() throws Throwable
521538
{
522-
StubServer server = StubServer.start( "support_multidb_v4.script", 9001 );
539+
StubServer server = stubController.startStub( "support_multidb_v4.script", 9001 );
523540
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
524541
{
525542
assertTrue( driver.supportsMultiDb() );
@@ -533,7 +550,7 @@ void shouldServerWithBoltV4SupportMultiDb() throws Throwable
533550
@Test
534551
void shouldServerWithBoltV3NotSupportMultiDb() throws Throwable
535552
{
536-
StubServer server = StubServer.start( "support_multidb_v3.script", 9001 );
553+
StubServer server = stubController.startStub( "support_multidb_v3.script", 9001 );
537554
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
538555
{
539556
assertFalse( driver.supportsMultiDb() );
@@ -547,7 +564,7 @@ void shouldServerWithBoltV3NotSupportMultiDb() throws Throwable
547564
private static void testTxCloseErrorPropagation( String script, Consumer<Transaction> txAction, String expectedErrorMessage )
548565
throws Exception
549566
{
550-
StubServer server = StubServer.start( script, 9001 );
567+
StubServer server = stubController.startStub( script, 9001 );
551568
try
552569
{
553570
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );

driver/src/test/java/org/neo4j/driver/util/Neo4jRunner.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,5 +366,10 @@ public static void debug( String text, Object... args )
366366
{
367367
System.out.println( String.format( text, args ) );
368368
}
369+
370+
public static void debug( String text )
371+
{
372+
System.out.println( text );
373+
}
369374
}
370375

driver/src/test/java/org/neo4j/driver/util/StubServer.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
import static java.lang.Thread.sleep;
3535
import static java.util.Arrays.asList;
36-
import static java.util.Collections.singletonList;
3736
import static java.util.concurrent.Executors.newCachedThreadPool;
3837
import static org.junit.jupiter.api.Assertions.fail;
3938
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@@ -51,15 +50,16 @@ public class StubServer
5150
// This may be thrown if the driver has not been closed properly
5251
public static class ForceKilled extends Exception {}
5352

54-
private static final String BOLT_STUB_COMMAND = "boltstub";
53+
private static final String BOLT_COMMAND = "bolt";
54+
private static final String BOLT_STUB_COMMAND = "stub";
5555

5656
private Process process;
5757

5858
private StubServer( String script, int port ) throws IOException, InterruptedException
5959
{
6060
List<String> command = new ArrayList<>();
61-
command.addAll( singletonList( BOLT_STUB_COMMAND ) );
62-
command.addAll( asList( Integer.toString( port ), script ) );
61+
command.addAll( asList( BOLT_COMMAND, BOLT_STUB_COMMAND ) );
62+
command.addAll( asList( "-l", "localhost:" + port, script ) );
6363
ProcessBuilder server = new ProcessBuilder().command( command );
6464
process = server.start();
6565
startReadingOutput( process );
@@ -72,7 +72,7 @@ public static StubServer start( String resource, int port ) throws IOException,
7272
return new StubServer( resource(resource), port );
7373
}
7474

75-
public int exitStatus() throws InterruptedException, ForceKilled
75+
public int exitStatus() throws InterruptedException
7676
{
7777
sleep( 500 ); // wait for a moment to allow disconnection to occur
7878
try
@@ -83,19 +83,26 @@ public int exitStatus() throws InterruptedException, ForceKilled
8383
{
8484
// not exited yet
8585
exit();
86-
throw new ForceKilled();
8786
}
87+
return -1;
8888
}
8989

9090
public static Config.ConfigBuilder insecureBuilder()
9191
{
9292
return Config.builder().withoutEncryption().withLogging( none() );
9393
}
9494

95-
private void exit() throws InterruptedException
95+
private void exit()
9696
{
9797
process.destroy();
98-
process.waitFor();
98+
try
99+
{
100+
process.waitFor();
101+
}
102+
catch ( InterruptedException ex )
103+
{
104+
throw new RuntimeException( "Interrupted whilst waiting for forced stub shutdown", ex);
105+
}
99106
}
100107

101108
private static String resource( String fileName )
@@ -113,7 +120,7 @@ private static boolean boltKitAvailable()
113120
try
114121
{
115122
// run 'help' command to see if boltstub is available
116-
Process process = new ProcessBuilder( BOLT_STUB_COMMAND, "-h" ).start();
123+
Process process = new ProcessBuilder( "bolt" ).start();
117124
int exitCode = process.waitFor();
118125
return exitCode == 0;
119126
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2002-2020 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.util;
20+
21+
import java.io.IOException;
22+
import java.util.List;
23+
import java.util.concurrent.CopyOnWriteArrayList;
24+
25+
public class StubServerController
26+
{
27+
private final List<StubServer> currentStubServers = new CopyOnWriteArrayList<>();
28+
29+
public StubServer startStub( String script, int port) throws IOException, InterruptedException
30+
{
31+
StubServer server = StubServer.start( script, port );
32+
currentStubServers.add( server );
33+
return server;
34+
}
35+
36+
public void reset()
37+
{
38+
for ( StubServer server : currentStubServers )
39+
{
40+
try
41+
{
42+
server.exitStatus();
43+
}
44+
catch ( InterruptedException e )
45+
{
46+
throw new RuntimeException( "Interrupted whilst waiting for stub shutdown", e);
47+
}
48+
currentStubServers.remove( server );
49+
}
50+
51+
}
52+
}

driver/src/test/resources/acquire_endpoints_v3_point_to_empty_router_and_exit.script

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,3 @@ C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"addre
88
S: SUCCESS {"fields": ["ttl", "servers"]}
99
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"}]]
1010
SUCCESS {}
11-
C: RESET
12-
S: SUCCESS {}
13-
<EXIT>

driver/src/test/resources/acquire_endpoints_v3_three_servers_and_exit.script

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
!: AUTO RESET
33
!: AUTO HELLO
44
!: AUTO GOODBYE
5+
!: AUTO BEGIN
56

67
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"address": "my.virtual.host:8080"}} {}
78
PULL_ALL
89
S: SUCCESS {"fields": ["ttl", "servers"]}
910
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"}]]
1011
SUCCESS {}
11-
C: RESET
12-
S: SUCCESS {}
1312
<EXIT>

0 commit comments

Comments
 (0)