3131import org .neo4j .driver .internal .net .BoltServerAddress ;
3232import org .neo4j .driver .internal .spi .Connection ;
3333import org .neo4j .driver .internal .spi .ConnectionPool ;
34+ import org .neo4j .driver .internal .util .Clock ;
3435import org .neo4j .driver .v1 .AccessRole ;
3536import org .neo4j .driver .v1 .Logger ;
3637import org .neo4j .driver .v1 .Logging ;
@@ -191,7 +192,63 @@ public void shouldForgetAboutServersOnRerouting()
191192 verify ( pool ).purge ( boltAddress ( "localhost" , 1111 ) );
192193 }
193194
195+ @ Test
196+ public void shouldRediscoverOnTimeout ()
197+ {
198+ // Given
199+ final Session session = mock ( Session .class );
200+ Clock clock = mock ( Clock .class );
201+ when (clock .millis ()).thenReturn ( 0L , 11000L , 22000L );
202+ when ( session .run ( GET_SERVERS ) )
203+ .thenReturn (
204+ getServers ( asList ( "localhost:1111" , "localhost:1112" , "localhost:1113" ),
205+ singletonList ( "localhost:2222" ),
206+ singletonList ( "localhost:3333" ), 10L /*seconds*/ ) )
207+ .thenReturn (
208+ getServers ( singletonList ( "localhost:5555" ), singletonList ( "localhost:5555" ), singletonList ( "localhost:5555" ) ) );
209+
210+ ClusterDriver clusterDriver = forSession ( session , clock );
211+
212+ // When
213+ clusterDriver .session ( AccessRole .WRITE );
214+
215+ // Then
216+ assertThat ( clusterDriver .routingServers (), containsInAnyOrder ( boltAddress ( "localhost" , 5555 ) ) );
217+ assertThat ( clusterDriver .readServers (), containsInAnyOrder ( boltAddress ( "localhost" , 5555 ) ) );
218+ assertThat ( clusterDriver .writeServers (), containsInAnyOrder ( boltAddress ( "localhost" , 5555 ) ) );
219+ }
220+
221+ @ Test
222+ public void shouldNotRediscoverWheNoTimeout ()
223+ {
224+ // Given
225+ final Session session = mock ( Session .class );
226+ Clock clock = mock ( Clock .class );
227+ when (clock .millis ()).thenReturn ( 0L , 9900L , 18800L );
228+ when ( session .run ( GET_SERVERS ) )
229+ .thenReturn (
230+ getServers ( asList ( "localhost:1111" , "localhost:1112" , "localhost:1113" ),
231+ singletonList ( "localhost:2222" ),
232+ singletonList ( "localhost:3333" ), 10L /*seconds*/ ) )
233+ .thenReturn (
234+ getServers ( singletonList ( "localhost:5555" ), singletonList ( "localhost:5555" ), singletonList ( "localhost:5555" ) ) );
235+
236+ ClusterDriver clusterDriver = forSession ( session , clock );
237+
238+ // When
239+ clusterDriver .session ( AccessRole .WRITE );
240+
241+ // Then
242+ assertThat ( clusterDriver .routingServers (), containsInAnyOrder ( boltAddress ( "localhost" , 1111 ), boltAddress ( "localhost" , 1112 ), boltAddress ( "localhost" , 1113 ) ) );
243+ assertThat ( clusterDriver .readServers (), containsInAnyOrder ( boltAddress ( "localhost" , 2222 ) ) );
244+ assertThat ( clusterDriver .writeServers (), containsInAnyOrder ( boltAddress ( "localhost" , 3333 ) ) );
245+ }
246+
194247 private ClusterDriver forSession ( final Session session )
248+ {
249+ return forSession ( session , Clock .SYSTEM );
250+ }
251+ private ClusterDriver forSession ( final Session session , Clock clock )
195252 {
196253 return new ClusterDriver ( SEED , pool , insecure (),
197254 new BiFunction <Connection ,Logger ,Session >()
@@ -201,16 +258,23 @@ public Session apply( Connection connection, Logger ignore )
201258 {
202259 return session ;
203260 }
204- }, logging () );
261+ }, clock , logging () );
205262 }
206263
207264 private BoltServerAddress boltAddress ( String host , int port )
208265 {
209266 return new BoltServerAddress ( host , port );
210267 }
211268
269+
212270 StatementResult getServers ( final List <String > routers , final List <String > readers ,
213271 final List <String > writers )
272+ {
273+ return getServers ( routers ,readers , writers , Long .MAX_VALUE );
274+ }
275+
276+ StatementResult getServers ( final List <String > routers , final List <String > readers ,
277+ final List <String > writers , final long ttl )
214278 {
215279 return new StatementResult ()
216280 {
@@ -233,7 +297,7 @@ public Record next()
233297 {
234298 return new InternalRecord ( asList ( "ttl" , "servers" ),
235299 new Value []{
236- value ( Long . MAX_VALUE ),
300+ value ( ttl ),
237301 value ( asList ( serverInfo ( "ROUTE" , routers ), serverInfo ( "WRITE" , writers ),
238302 serverInfo ( "READ" , readers ) ) )
239303 } );
0 commit comments