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.
24
24
import org .junit .rules .ExpectedException ;
25
25
26
26
import java .util .Collections ;
27
- import java .util .Iterator ;
27
+ import java .util .HashMap ;
28
28
import java .util .List ;
29
+ import java .util .Map ;
29
30
30
31
import org .neo4j .driver .internal .net .BoltServerAddress ;
31
32
import org .neo4j .driver .internal .spi .Connection ;
32
33
import org .neo4j .driver .internal .spi .ConnectionPool ;
33
- import org .neo4j .driver .internal .value .IntegerValue ;
34
- import org .neo4j .driver .internal .value .StringValue ;
35
34
import org .neo4j .driver .v1 .AccessMode ;
36
35
import org .neo4j .driver .v1 .Logger ;
37
36
import org .neo4j .driver .v1 .Logging ;
56
55
import static org .mockito .Mockito .mock ;
57
56
import static org .mockito .Mockito .when ;
58
57
import static org .neo4j .driver .internal .security .SecurityPlan .insecure ;
58
+ import static org .neo4j .driver .v1 .Values .value ;
59
59
60
60
public class ClusterDriverTest
61
61
{
@@ -64,17 +64,17 @@ public class ClusterDriverTest
64
64
65
65
private static final BoltServerAddress SEED = new BoltServerAddress ( "localhost" , 7687 );
66
66
private static final String GET_SERVERS = "CALL dbms.cluster.routing.getServers" ;
67
- private static final List <BoltServerAddress > NO_ADDRESSES = Collections .< BoltServerAddress > emptyList ();
67
+ private static final List <String > NO_ADDRESSES = Collections .emptyList ();
68
68
69
69
@ Test
70
70
public void shouldDoRoutingOnInitialization ()
71
71
{
72
72
// Given
73
73
final Session session = mock ( Session .class );
74
74
when ( session .run ( GET_SERVERS ) ).thenReturn (
75
- getServers ( singletonList ( boltAddress ( "localhost" , 1111 ) ),
76
- singletonList ( boltAddress ( "localhost" , 2222 ) ),
77
- singletonList ( boltAddress ( "localhost" , 3333 ) ) ) );
75
+ getServers ( singletonList ( "localhost: 1111" ),
76
+ singletonList ( "localhost: 2222" ),
77
+ singletonList ( "localhost: 3333" ) ) );
78
78
79
79
// When
80
80
ClusterDriver clusterDriver = forSession ( session );
@@ -96,11 +96,11 @@ public void shouldDoReRoutingOnSessionAcquisitionIfNecessary()
96
96
final Session session = mock ( Session .class );
97
97
when ( session .run ( GET_SERVERS ) )
98
98
.thenReturn (
99
- getServers ( singletonList ( boltAddress ( "localhost" , 1111 ) ), NO_ADDRESSES , NO_ADDRESSES ) )
99
+ getServers ( singletonList ( "localhost: 1111" ), NO_ADDRESSES , NO_ADDRESSES ) )
100
100
.thenReturn (
101
- getServers ( singletonList ( boltAddress ( "localhost" , 1112 ) ),
102
- singletonList ( boltAddress ( "localhost" , 2222 ) ),
103
- singletonList ( boltAddress ( "localhost" , 3333 ) ) ) );
101
+ getServers ( singletonList ( "localhost: 1112" ),
102
+ singletonList ( "localhost: 2222" ),
103
+ singletonList ( "localhost: 3333" ) ) );
104
104
105
105
ClusterDriver clusterDriver = forSession ( session );
106
106
@@ -129,12 +129,11 @@ public void shouldNotDoReRoutingOnSessionAcquisitionIfNotNecessary()
129
129
final Session session = mock ( Session .class );
130
130
when ( session .run ( GET_SERVERS ) )
131
131
.thenReturn (
132
- getServers ( asList ( boltAddress ( "localhost" , 1111 ), boltAddress ( "localhost" , 1112 ),
133
- boltAddress ( "localhost" , 1113 ) ),
134
- singletonList ( boltAddress ( "localhost" , 2222 ) ),
135
- singletonList ( boltAddress ( "localhost" , 3333 ) ) ) )
132
+ getServers ( asList ( "localhost:1111" , "localhost:1112" , "localhost:1113" ),
133
+ singletonList ( "localhost:2222" ),
134
+ singletonList ( "localhost:3333" ) ) )
136
135
.thenReturn (
137
- getServers ( singletonList ( boltAddress ( "localhost" , 5555 ) ), NO_ADDRESSES , NO_ADDRESSES ) );
136
+ getServers ( singletonList ( "localhost: 5555" ), NO_ADDRESSES , NO_ADDRESSES ) );
138
137
139
138
ClusterDriver clusterDriver = forSession ( session );
140
139
@@ -180,59 +179,34 @@ private BoltServerAddress boltAddress( String host, int port )
180
179
return new BoltServerAddress ( host , port );
181
180
}
182
181
183
- StatementResult getServers ( final List <BoltServerAddress > routers , final List <BoltServerAddress > readers ,
184
- final List <BoltServerAddress > writers )
182
+ StatementResult getServers ( final List <String > routers , final List <String > readers ,
183
+ final List <String > writers )
185
184
{
186
-
187
-
188
185
return new StatementResult ()
189
186
{
190
- private final int totalSize = routers .size () + readers .size () + writers .size ();
191
- private final Iterator <BoltServerAddress > routeIterator = routers .iterator ();
192
- private final Iterator <BoltServerAddress > readIterator = readers .iterator ();
193
- private final Iterator <BoltServerAddress > writeIterator = writers .iterator ();
194
187
private int counter = 0 ;
195
188
196
189
@ Override
197
190
public List <String > keys ()
198
191
{
199
- return asList ( "address " , "mode" , "expires " );
192
+ return asList ( "ttl " , "servers " );
200
193
}
201
194
202
195
@ Override
203
196
public boolean hasNext ()
204
197
{
205
- return counter ++ < totalSize ;
198
+ return counter ++ < 1 ;
206
199
}
207
200
208
201
@ Override
209
202
public Record next ()
210
203
{
211
- if ( routeIterator .hasNext () )
212
- {
213
- return new InternalRecord ( asList ( "address" , "mode" , "expires" ),
214
- new Value []{new StringValue ( routeIterator .next ().toString () ),
215
- new StringValue ( "ROUTE" ),
216
- new IntegerValue ( Long .MAX_VALUE )} );
217
- }
218
- else if ( readIterator .hasNext () )
219
- {
220
- return new InternalRecord ( asList ( "address" , "mode" , "expires" ),
221
- new Value []{new StringValue ( readIterator .next ().toString () ),
222
- new StringValue ( "READ" ),
223
- new IntegerValue ( Long .MAX_VALUE )} );
224
- }
225
- else if ( writeIterator .hasNext () )
226
- {
227
- return new InternalRecord ( asList ( "address" , "mode" , "expires" ),
228
- new Value []{new StringValue ( writeIterator .next ().toString () ),
229
- new StringValue ( "WRITE" ),
230
- new IntegerValue ( Long .MAX_VALUE )} );
231
- }
232
- else
233
- {
234
- return Collections .<Record >emptyIterator ().next ();
235
- }
204
+ return new InternalRecord ( asList ( "ttl" , "servers" ),
205
+ new Value []{
206
+ value ( Long .MAX_VALUE ),
207
+ value ( asList ( serverInfo ( "ROUTE" , routers ), serverInfo ( "WRITE" , writers ),
208
+ serverInfo ( "READ" , readers ) ) )
209
+ } );
236
210
}
237
211
238
212
@ Override
@@ -268,11 +242,20 @@ public ResultSummary consume()
268
242
@ Override
269
243
public void remove ()
270
244
{
271
- throw new UnsupportedOperationException ( );
245
+ throw new UnsupportedOperationException ();
272
246
}
273
247
};
274
248
}
275
249
250
+ private Map <String ,Object > serverInfo ( String role , List <String > addresses )
251
+ {
252
+ Map <String ,Object > map = new HashMap <>();
253
+ map .put ( "role" , role );
254
+ map .put ( "addresses" , addresses );
255
+
256
+ return map ;
257
+ }
258
+
276
259
private ConnectionPool pool ()
277
260
{
278
261
ConnectionPool pool = mock ( ConnectionPool .class );
0 commit comments