31
31
import org .neo4j .driver .internal .net .BoltServerAddress ;
32
32
import org .neo4j .driver .internal .spi .Connection ;
33
33
import org .neo4j .driver .internal .spi .ConnectionPool ;
34
+ import org .neo4j .driver .internal .util .Clock ;
34
35
import org .neo4j .driver .v1 .AccessRole ;
35
36
import org .neo4j .driver .v1 .Logger ;
36
37
import org .neo4j .driver .v1 .Logging ;
@@ -191,7 +192,63 @@ public void shouldForgetAboutServersOnRerouting()
191
192
verify ( pool ).purge ( boltAddress ( "localhost" , 1111 ) );
192
193
}
193
194
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
+
194
247
private ClusterDriver forSession ( final Session session )
248
+ {
249
+ return forSession ( session , Clock .SYSTEM );
250
+ }
251
+ private ClusterDriver forSession ( final Session session , Clock clock )
195
252
{
196
253
return new ClusterDriver ( SEED , pool , insecure (),
197
254
new BiFunction <Connection ,Logger ,Session >()
@@ -201,16 +258,23 @@ public Session apply( Connection connection, Logger ignore )
201
258
{
202
259
return session ;
203
260
}
204
- }, logging () );
261
+ }, clock , logging () );
205
262
}
206
263
207
264
private BoltServerAddress boltAddress ( String host , int port )
208
265
{
209
266
return new BoltServerAddress ( host , port );
210
267
}
211
268
269
+
212
270
StatementResult getServers ( final List <String > routers , final List <String > readers ,
213
271
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 )
214
278
{
215
279
return new StatementResult ()
216
280
{
@@ -233,7 +297,7 @@ public Record next()
233
297
{
234
298
return new InternalRecord ( asList ( "ttl" , "servers" ),
235
299
new Value []{
236
- value ( Long . MAX_VALUE ),
300
+ value ( ttl ),
237
301
value ( asList ( serverInfo ( "ROUTE" , routers ), serverInfo ( "WRITE" , writers ),
238
302
serverInfo ( "READ" , readers ) ) )
239
303
} );
0 commit comments