|
18 | 18 | */ |
19 | 19 | package org.neo4j.driver.internal.cluster; |
20 | 20 |
|
21 | | -import java.util.ArrayList; |
22 | | -import java.util.List; |
23 | | - |
24 | 21 | import org.hamcrest.Matcher; |
25 | 22 | import org.junit.Rule; |
26 | 23 | import org.junit.Test; |
27 | 24 | import org.junit.rules.TestRule; |
28 | 25 | import org.junit.runner.Description; |
29 | 26 | import org.junit.runners.model.Statement; |
30 | 27 |
|
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
31 | 31 | import org.neo4j.driver.internal.EventHandler; |
32 | 32 | import org.neo4j.driver.internal.net.BoltServerAddress; |
33 | 33 | import org.neo4j.driver.internal.spi.Connection; |
|
40 | 40 |
|
41 | 41 | import static org.hamcrest.Matchers.any; |
42 | 42 | import static org.hamcrest.Matchers.equalTo; |
| 43 | +import static org.hamcrest.Matchers.instanceOf; |
43 | 44 | import static org.hamcrest.Matchers.sameInstance; |
44 | 45 | import static org.junit.Assert.assertEquals; |
45 | 46 | import static org.junit.Assert.assertNotEquals; |
| 47 | +import static org.junit.Assert.assertThat; |
46 | 48 | import static org.junit.Assert.fail; |
47 | 49 | import static org.neo4j.driver.internal.cluster.ClusterTopology.Role.READ; |
48 | 50 | import static org.neo4j.driver.internal.cluster.ClusterTopology.Role.ROUTE; |
@@ -90,9 +92,15 @@ public void evaluate() throws Throwable |
90 | 92 | private final ClusterTopology cluster = new ClusterTopology( events, clock ); |
91 | 93 |
|
92 | 94 | private LoadBalancer seedLoadBalancer( String host, int port ) throws Exception |
| 95 | + { |
| 96 | + RoutingSettings defaultSettings = new RoutingSettings( MAX_ROUTING_FAILURES, RETRY_TIMEOUT_DELAY ); |
| 97 | + return seedLoadBalancer( host, port, defaultSettings ); |
| 98 | + } |
| 99 | + |
| 100 | + private LoadBalancer seedLoadBalancer( String host, int port, RoutingSettings settings ) throws Exception |
93 | 101 | { |
94 | 102 | return new LoadBalancer( |
95 | | - new RoutingSettings( MAX_ROUTING_FAILURES, RETRY_TIMEOUT_DELAY ), |
| 103 | + settings, |
96 | 104 | clock, |
97 | 105 | log, |
98 | 106 | connections, |
@@ -351,6 +359,38 @@ public void connectionFailure( BoltServerAddress address ) |
351 | 359 | any( ClusterComposition.class ) ) ) ); |
352 | 360 | } |
353 | 361 |
|
| 362 | + @Test |
| 363 | + public void shouldTryConfiguredMaxRoutingFailures() throws Exception |
| 364 | + { |
| 365 | + // given |
| 366 | + int port = 1337; |
| 367 | + int maxRoutingFailures = 7; |
| 368 | + RoutingSettings settings = new RoutingSettings( maxRoutingFailures, 10 ); |
| 369 | + |
| 370 | + coreClusterOn( 20, "one", port, "two" ); |
| 371 | + connections.up( "one", port ); |
| 372 | + |
| 373 | + LoadBalancer routing = seedLoadBalancer( "one", port, settings ); |
| 374 | + |
| 375 | + // when |
| 376 | + connections.down( "one", port ); |
| 377 | + clock.progress( 25_000 ); // will cause TTL timeout |
| 378 | + |
| 379 | + try |
| 380 | + { |
| 381 | + routing.acquireWriteConnection(); |
| 382 | + fail( "Exception expected" ); |
| 383 | + } |
| 384 | + catch ( Exception e ) |
| 385 | + { |
| 386 | + assertThat( e, instanceOf( ServiceUnavailableException.class ) ); |
| 387 | + } |
| 388 | + |
| 389 | + // then |
| 390 | + events.assertCount( connectionFailure( "one", port ), equalTo( maxRoutingFailures ) ); |
| 391 | + events.assertCount( connectionFailure( "two", port ), equalTo( maxRoutingFailures ) ); |
| 392 | + } |
| 393 | + |
354 | 394 | @Test |
355 | 395 | public void shouldFailIfEnoughConnectionAttemptsFail() throws Exception |
356 | 396 | { |
@@ -570,7 +610,6 @@ public void sleep( long timestamp, long millis ) |
570 | 610 |
|
571 | 611 | // then |
572 | 612 | assertEquals( new BoltServerAddress( "two", 1337 ), connection.boltServerAddress() ); |
573 | | - events.printEvents( System.out ); |
574 | 613 | } |
575 | 614 |
|
576 | 615 | private void coreClusterOn( int ttlSeconds, String leader, int port, String... others ) |
|
0 commit comments