1
1
/**
2
2
* Copyright (c) 2002-2016 "Neo Technology,"
3
3
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4
- *
4
+ * <p>
5
5
* This file is part of Neo4j.
6
- *
6
+ * <p>
7
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
8
* you may not use this file except in compliance with the License.
9
9
* You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
10
+ * <p>
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ * <p>
13
13
* Unless required by applicable law or agreed to in writing, software
14
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46
46
import org .neo4j .driver .v1 .util .Function ;
47
47
import org .neo4j .driver .v1 .util .StubServer ;
48
48
49
+ import static org .hamcrest .Matchers .containsInAnyOrder ;
49
50
import static org .hamcrest .Matchers .hasItem ;
50
51
import static org .hamcrest .Matchers .hasSize ;
51
52
import static org .hamcrest .core .IsEqual .equalTo ;
52
53
import static org .hamcrest .core .IsNot .not ;
54
+ import static org .junit .Assert .assertFalse ;
53
55
import static org .junit .Assert .assertThat ;
54
56
import static org .junit .Assert .assertTrue ;
55
57
import static org .junit .Assert .fail ;
58
+
56
59
@ Ignore
57
60
public class ClusterDriverStubTest
58
61
{
@@ -73,10 +76,7 @@ public void shouldDiscoverServers() throws IOException, InterruptedException, St
73
76
{
74
77
// Then
75
78
Set <BoltServerAddress > addresses = driver .routingServers ();
76
- assertThat ( addresses , hasSize ( 3 ) );
77
- assertThat ( addresses , hasItem ( new BoltServerAddress ( "127.0.0.1" , 9001 ) ) );
78
- assertThat ( addresses , hasItem ( new BoltServerAddress ( "127.0.0.1" , 9002 ) ) );
79
- assertThat ( addresses , hasItem ( new BoltServerAddress ( "127.0.0.1" , 9003 ) ) );
79
+ assertThat ( addresses , containsInAnyOrder ( address (9001 ), address ( 9002 ), address ( 9003 ) ) );
80
80
}
81
81
82
82
// Finally
@@ -89,23 +89,21 @@ public void shouldDiscoverNewServers() throws IOException, InterruptedException,
89
89
// Given
90
90
StubServer server = StubServer .start ( resource ( "discover_new_servers.script" ), 9001 );
91
91
URI uri = URI .create ( "bolt+routing://127.0.0.1:9001" );
92
+ BoltServerAddress seed = address ( 9001 );
92
93
93
94
// When
94
95
try ( ClusterDriver driver = (ClusterDriver ) GraphDatabase .driver ( uri , config ) )
95
96
{
96
97
// Then
97
98
Set <BoltServerAddress > addresses = driver .routingServers ();
98
- assertThat ( addresses , hasSize ( 4 ) );
99
- assertThat ( addresses , hasItem ( new BoltServerAddress ( "127.0.0.1" , 9001 ) ) );
100
- assertThat ( addresses , hasItem ( new BoltServerAddress ( "127.0.0.1" , 9002 ) ) );
101
- assertThat ( addresses , hasItem ( new BoltServerAddress ( "127.0.0.1" , 9003 ) ) );
102
- assertThat ( addresses , hasItem ( new BoltServerAddress ( "127.0.0.1" , 9004 ) ) );
99
+ assertThat ( addresses , containsInAnyOrder ( address (9002 ), address ( 9003 ), address ( 9004 ) ) );
103
100
}
104
101
105
102
// Finally
106
103
assertThat ( server .exitStatus (), equalTo ( 0 ) );
107
104
}
108
105
106
+
109
107
@ Test
110
108
public void shouldHandleEmptyResponse () throws IOException , InterruptedException , StubServer .ForceKilled
111
109
{
@@ -115,8 +113,8 @@ public void shouldHandleEmptyResponse() throws IOException, InterruptedException
115
113
try ( ClusterDriver driver = (ClusterDriver ) GraphDatabase .driver ( uri , config ) )
116
114
{
117
115
Set <BoltServerAddress > servers = driver .routingServers ();
118
- assertThat ( servers , hasSize ( 1 ) );
119
- assertThat ( servers , hasItem ( new BoltServerAddress ( "127.0.0.1" , 9001 ) ) );
116
+ assertThat ( servers , hasSize ( 0 ) );
117
+ assertFalse ( driver . connectionPool (). hasAddress ( address ( 9001 ) ) );
120
118
}
121
119
122
120
// Finally
@@ -265,7 +263,7 @@ public void shouldRoundRobinWriteSessions() throws IOException, InterruptedExcep
265
263
{
266
264
for ( int i = 0 ; i < 2 ; i ++ )
267
265
{
268
- try ( Session session = driver .session () )
266
+ try ( Session session = driver .session () )
269
267
{
270
268
session .run ( "CREATE (n {name:'Bob'})" );
271
269
}
@@ -291,15 +289,9 @@ public void shouldRememberEndpoints() throws IOException, InterruptedException,
291
289
{
292
290
session .run ( "MATCH (n) RETURN n.name" ).consume ();
293
291
294
- assertThat ( driver .readServers (), hasSize ( 2 ));
295
- assertThat ( driver .readServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9005 ) ) );
296
- assertThat ( driver .readServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9006 ) ) );
297
- assertThat ( driver .writeServers (), hasSize ( 2 ));
298
- assertThat ( driver .writeServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9007 ) ) );
299
- assertThat ( driver .writeServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9008 ) ) );
300
- //Make sure we don't cache acquired servers as discovery servers
301
- assertThat ( driver .routingServers (), not (hasItem ( new BoltServerAddress ( "127.0.0.1" , 9005 ))));
302
- assertThat ( driver .routingServers (), not (hasItem ( new BoltServerAddress ( "127.0.0.1" , 9006 ))));
292
+ assertThat ( driver .readServers (), containsInAnyOrder ( address ( 9005 ), address ( 9006 ) ) );
293
+ assertThat ( driver .writeServers (), containsInAnyOrder ( address ( 9007 ), address ( 9008 ) ) );
294
+ assertThat ( driver .routingServers (), containsInAnyOrder ( address ( 9001 ), address ( 9002 ), address ( 9003 ) ) );
303
295
}
304
296
// Finally
305
297
assertThat ( server .exitStatus (), equalTo ( 0 ) );
@@ -316,29 +308,30 @@ public void shouldForgetEndpointsOnFailure() throws IOException, InterruptedExce
316
308
StubServer .start ( resource ( "dead_server.script" ), 9005 );
317
309
URI uri = URI .create ( "bolt+routing://127.0.0.1:9001" );
318
310
ClusterDriver driver = (ClusterDriver ) GraphDatabase .driver ( uri , config );
319
- boolean failed = false ;
320
311
try
321
312
{
322
313
Session session = driver .session ( AccessRole .READ );
323
314
session .run ( "MATCH (n) RETURN n.name" ).consume ();
324
315
session .close ();
316
+ fail ();
325
317
}
326
318
catch ( SessionExpiredException e )
327
319
{
328
- failed = true ;
320
+ //ignore
329
321
}
330
322
331
- assertTrue ( failed );
332
- assertThat ( driver .readServers (), not (hasItem ( new BoltServerAddress ( "127.0.0.1" , 9005 ) ) ));
323
+ assertThat ( driver .readServers (), not ( hasItem ( address ( 9005 ) ) ) );
333
324
assertThat ( driver .writeServers (), hasSize ( 2 ) );
325
+ assertFalse ( driver .connectionPool ().hasAddress ( address ( 9005 ) ) );
334
326
driver .close ();
335
327
336
328
// Finally
337
329
assertThat ( server .exitStatus (), equalTo ( 0 ) );
338
330
}
339
331
340
332
@ Test
341
- public void shouldRediscoverIfNecessaryOnSessionAcquisition () throws IOException , InterruptedException , StubServer .ForceKilled
333
+ public void shouldRediscoverIfNecessaryOnSessionAcquisition ()
334
+ throws IOException , InterruptedException , StubServer .ForceKilled
342
335
{
343
336
// Given
344
337
StubServer server = StubServer .start ( resource ( "rediscover.script" ), 9001 );
@@ -349,17 +342,15 @@ public void shouldRediscoverIfNecessaryOnSessionAcquisition() throws IOException
349
342
350
343
//On creation we only find ourselves
351
344
ClusterDriver driver = (ClusterDriver ) GraphDatabase .driver ( uri , config );
352
- assertThat ( driver .routingServers (), hasSize ( 1 ) );
353
- assertThat ( driver .routingServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9001 ) ));
345
+ assertThat ( driver .routingServers (), containsInAnyOrder ( address ( 9001 ) ) );
346
+ assertTrue ( driver .connectionPool (). hasAddress ( address ( 9001 ) ) );
354
347
355
- //since we know about less than three servers a rediscover should be triggered
348
+ //since we have no write nor read servers we must rediscover
356
349
Session session = driver .session ( AccessRole .READ );
357
- assertThat ( driver .routingServers (), hasSize ( 4 ) );
358
- assertThat ( driver .routingServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9001 ) ));
359
- assertThat ( driver .routingServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9002 ) ));
360
- assertThat ( driver .routingServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9003 ) ));
361
- assertThat ( driver .routingServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9004 ) ));
362
-
350
+ assertThat ( driver .routingServers (), containsInAnyOrder (address ( 9002 ),
351
+ address ( 9003 ), address ( 9004 ) ) );
352
+ //server told os to forget 9001
353
+ assertFalse ( driver .connectionPool ().hasAddress ( address ( 9001 ) ) );
363
354
session .close ();
364
355
driver .close ();
365
356
@@ -379,8 +370,7 @@ public void shouldOnlyGetServersOnce() throws IOException, InterruptedException,
379
370
380
371
//On creation we only find ourselves
381
372
final ClusterDriver driver = (ClusterDriver ) GraphDatabase .driver ( uri , config );
382
- assertThat ( driver .routingServers (), hasSize ( 1 ) );
383
- assertThat ( driver .routingServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9001 ) ));
373
+ assertThat ( driver .routingServers (), containsInAnyOrder ( address ( 9001 ) ) );
384
374
385
375
ExecutorService runner = Executors .newFixedThreadPool ( 10 );
386
376
for ( int i = 0 ; i < 10 ; i ++ )
@@ -391,7 +381,7 @@ public void shouldOnlyGetServersOnce() throws IOException, InterruptedException,
391
381
public void run ()
392
382
{
393
383
//noinspection EmptyTryBlock
394
- try ( Session ignore = driver .session ( AccessRole .READ ))
384
+ try ( Session ignore = driver .session ( AccessRole .READ ) )
395
385
{
396
386
//empty
397
387
}
@@ -401,11 +391,7 @@ public void run()
401
391
}
402
392
runner .awaitTermination ( 10 , TimeUnit .SECONDS );
403
393
//since we know about less than three servers a rediscover should be triggered
404
- assertThat ( driver .routingServers (), hasSize ( 4 ) );
405
- assertThat ( driver .routingServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9001 ) ));
406
- assertThat ( driver .routingServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9002 ) ));
407
- assertThat ( driver .routingServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9003 ) ));
408
- assertThat ( driver .routingServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9004 ) ));
394
+ assertThat ( driver .routingServers (), containsInAnyOrder ( address ( 9002 ), address ( 9003 ), address ( 9004 ) ) );
409
395
410
396
driver .close ();
411
397
@@ -450,19 +436,19 @@ public void shouldHandleLeaderSwitchWhenWriting()
450
436
boolean failed = false ;
451
437
try ( Session session = driver .session ( AccessRole .WRITE ) )
452
438
{
453
- assertThat (driver .writeServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9007 ) ));
454
- assertThat (driver .writeServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9008 ) ));
439
+ assertThat ( driver .writeServers (), hasItem (address ( 9007 ) ) );
440
+ assertThat ( driver .writeServers (), hasItem ( address ( 9008 ) ) );
455
441
session .run ( "CREATE ()" ).consume ();
456
442
}
457
- catch (SessionExpiredException e )
443
+ catch ( SessionExpiredException e )
458
444
{
459
445
failed = true ;
460
- assertThat (e .getMessage (), equalTo ( "Server at 127.0.0.1:9007 no longer accepts writes" ));
446
+ assertThat ( e .getMessage (), equalTo ( "Server at 127.0.0.1:9007 no longer accepts writes" ) );
461
447
}
462
448
assertTrue ( failed );
463
- assertThat ( driver .writeServers (), not ( hasItem ( new BoltServerAddress ( "127.0.0.1" , 9007 ) ) ) );
464
- assertThat ( driver .writeServers (), hasItem ( new BoltServerAddress ( "127.0.0.1" , 9008 ) ) );
465
- assertTrue ( driver .connectionPool ().hasAddress ( new BoltServerAddress ( "127.0.0.1" , 9007 ) ) );
449
+ assertThat ( driver .writeServers (), not ( hasItem ( address ( 9007 ) ) ) );
450
+ assertThat ( driver .writeServers (), hasItem ( address ( 9008 ) ) );
451
+ assertTrue ( driver .connectionPool ().hasAddress ( address ( 9007 ) ) );
466
452
467
453
driver .close ();
468
454
// Finally
@@ -478,4 +464,9 @@ String resource( String fileName )
478
464
}
479
465
return resource .getFile ();
480
466
}
467
+
468
+ private BoltServerAddress address ( int port )
469
+ {
470
+ return new BoltServerAddress ( "127.0.0.1" , port );
471
+ }
481
472
}
0 commit comments