1
1
package tech .ydb .core .impl .pool ;
2
2
3
3
import java .io .IOException ;
4
- import java .net .ServerSocket ;
4
+ import java .net .Socket ;
5
+ import java .net .SocketAddress ;
5
6
import java .util .Arrays ;
6
7
import java .util .List ;
7
8
import java .util .concurrent .ThreadLocalRandom ;
8
9
9
- import javax .net .ServerSocketFactory ;
10
+ import javax .net .SocketFactory ;
10
11
11
12
import com .google .common .base .Ticker ;
12
13
import org .junit .After ;
20
21
import tech .ydb .core .grpc .BalancingSettings ;
21
22
import tech .ydb .core .timer .TestTicker ;
22
23
23
- import static org .mockito .Mockito .mockStatic ;
24
- import static org .mockito .Mockito .times ;
25
- import static org .mockito .Mockito .verify ;
26
- import static org .mockito .Mockito .when ;
27
-
28
24
/**
29
25
* @author Aleksandr Gorshenin
30
26
* @author Kirill Kurdyukov
31
27
*/
32
28
public class EndpointPoolTest {
33
29
private AutoCloseable mocks ;
34
- private final MockedStatic <ThreadLocalRandom > threadLocalStaticMock = mockStatic (ThreadLocalRandom .class );
35
- private final MockedStatic <Ticker > tickerStaticMock = mockStatic (Ticker .class );
30
+ private final MockedStatic <ThreadLocalRandom > threadLocalStaticMock = Mockito .mockStatic (ThreadLocalRandom .class );
31
+ private final MockedStatic <Ticker > tickerStaticMock = Mockito .mockStatic (Ticker .class );
32
+ private final MockedStatic <SocketFactory > socketFactoryStaticMock = Mockito .mockStatic (SocketFactory .class );
33
+
34
+ private final Socket socket = Mockito .mock (Socket .class );
35
+ private final SocketFactory socketFactory = Mockito .mock (SocketFactory .class );
36
36
private final ThreadLocalRandom random = Mockito .mock (ThreadLocalRandom .class );
37
37
38
38
@ Before
39
- public void setUp () {
39
+ public void setUp () throws IOException {
40
40
mocks = MockitoAnnotations .openMocks (this );
41
41
threadLocalStaticMock .when (ThreadLocalRandom ::current ).thenReturn (random );
42
+ socketFactoryStaticMock .when (SocketFactory ::getDefault ).thenReturn (socketFactory );
43
+ Mockito .when (socketFactory .createSocket ()).thenReturn (socket );
44
+ Mockito .doNothing ().when (socket ).connect (Mockito .any (SocketAddress .class ));
42
45
}
43
46
44
47
@ After
45
48
public void tearDown () throws Exception {
49
+ socketFactoryStaticMock .close ();
46
50
tickerStaticMock .close ();
47
51
threadLocalStaticMock .close ();
48
52
mocks .close ();
@@ -77,7 +81,7 @@ public void useAllNodesTest() {
77
81
78
82
check (pool ).records (3 ).knownNodes (3 ).needToReDiscovery (false ).bestEndpointsCount (3 );
79
83
80
- when (random .nextInt (3 )).thenReturn (2 , 0 , 2 , 1 );
84
+ Mockito . when (random .nextInt (3 )).thenReturn (2 , 0 , 2 , 1 );
81
85
82
86
check (pool .getEndpoint (null )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12345 ); // random choice
83
87
check (pool .getEndpoint (0 )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12345 ); // random choose
@@ -87,7 +91,7 @@ public void useAllNodesTest() {
87
91
check (pool .getEndpoint (4 )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12345 ); // random choose
88
92
check (pool .getEndpoint (5 )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12345 ); // random choose
89
93
90
- verify (random , times (4 )).nextInt (3 );
94
+ Mockito . verify (random , Mockito . times (4 )).nextInt (3 );
91
95
}
92
96
93
97
@ Test
@@ -103,7 +107,7 @@ public void localDcTest() {
103
107
104
108
check (pool ).records (3 ).knownNodes (3 ).needToReDiscovery (false ).bestEndpointsCount (1 );
105
109
106
- when (random .nextInt (1 )).thenReturn (0 , 0 , 0 );
110
+ Mockito . when (random .nextInt (1 )).thenReturn (0 , 0 , 0 );
107
111
108
112
check (pool .getEndpoint (null )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12345 ); // random from local DC
109
113
check (pool .getEndpoint (0 )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12345 ); // random from local DC
@@ -112,7 +116,7 @@ public void localDcTest() {
112
116
check (pool .getEndpoint (3 )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12345 ); // preferred
113
117
check (pool .getEndpoint (4 )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12345 ); // random from local DC
114
118
115
- verify (random , times (3 )).nextInt (1 );
119
+ Mockito . verify (random , Mockito . times (3 )).nextInt (1 );
116
120
}
117
121
118
122
@ Test
@@ -128,7 +132,7 @@ public void preferredDcTest() {
128
132
129
133
check (pool ).records (3 ).knownNodes (3 ).needToReDiscovery (false ).bestEndpointsCount (1 );
130
134
131
- when (random .nextInt (1 )).thenReturn (0 , 0 , 0 );
135
+ Mockito . when (random .nextInt (1 )).thenReturn (0 , 0 , 0 );
132
136
133
137
check (pool .getEndpoint (null )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12345 ); // random from DC1
134
138
check (pool .getEndpoint (0 )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12345 ); // random from DC1
@@ -137,7 +141,7 @@ public void preferredDcTest() {
137
141
check (pool .getEndpoint (3 )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12345 ); // preferred
138
142
check (pool .getEndpoint (4 )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12345 ); // random from DC1
139
143
140
- verify (random , times (3 )).nextInt (1 );
144
+ Mockito . verify (random , Mockito . times (3 )).nextInt (1 );
141
145
}
142
146
143
147
@ Test
@@ -153,7 +157,7 @@ public void preferredEndpointsTest() {
153
157
154
158
check (pool ).records (3 ).knownNodes (3 ).needToReDiscovery (false ).bestEndpointsCount (3 );
155
159
156
- when (random .nextInt (3 )).thenReturn (2 , 0 , 2 , 1 );
160
+ Mockito . when (random .nextInt (3 )).thenReturn (2 , 0 , 2 , 1 );
157
161
158
162
// If node is known
159
163
check (pool .getEndpoint (1 )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 );
@@ -167,7 +171,7 @@ public void preferredEndpointsTest() {
167
171
check (pool .getEndpoint (6 )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 );
168
172
check (pool .getEndpoint (7 )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 );
169
173
170
- verify (random , times (4 )).nextInt (3 );
174
+ Mockito . verify (random , Mockito . times (4 )).nextInt (3 );
171
175
}
172
176
173
177
@ Test
@@ -185,24 +189,24 @@ public void nodePessimizationTest() {
185
189
186
190
check (pool ).records (5 ).knownNodes (5 ).needToReDiscovery (false ).bestEndpointsCount (5 );
187
191
188
- when (random .nextInt (5 )).thenReturn (0 , 1 , 3 , 2 , 4 );
192
+ Mockito . when (random .nextInt (5 )).thenReturn (0 , 1 , 3 , 2 , 4 );
189
193
check (pool .getEndpoint (null )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 );
190
194
check (pool .getEndpoint (null )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 );
191
195
check (pool .getEndpoint (null )).hostname ("n4.ydb.tech" ).nodeID (4 ).port (12344 );
192
196
check (pool .getEndpoint (null )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 );
193
197
check (pool .getEndpoint (null )).hostname ("n5.ydb.tech" ).nodeID (5 ).port (12345 );
194
- verify (random , times (5 )).nextInt (5 );
198
+ Mockito . verify (random , Mockito . times (5 )).nextInt (5 );
195
199
196
200
// Pessimize one node - four left in use
197
201
pool .pessimizeEndpoint (pool .getEndpoint (2 ));
198
202
check (pool ).records (5 ).knownNodes (5 ).needToReDiscovery (false ).bestEndpointsCount (4 );
199
203
200
- when (random .nextInt (4 )).thenReturn (0 , 2 , 1 , 3 );
204
+ Mockito . when (random .nextInt (4 )).thenReturn (0 , 2 , 1 , 3 );
201
205
check (pool .getEndpoint (null )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 );
202
206
check (pool .getEndpoint (null )).hostname ("n4.ydb.tech" ).nodeID (4 ).port (12344 );
203
207
check (pool .getEndpoint (null )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 );
204
208
check (pool .getEndpoint (null )).hostname ("n5.ydb.tech" ).nodeID (5 ).port (12345 );
205
- verify (random , times (4 )).nextInt (4 );
209
+ Mockito . verify (random , Mockito . times (4 )).nextInt (4 );
206
210
207
211
// but we can use pessimized node if specify it as preferred
208
212
check (pool .getEndpoint (2 )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 );
@@ -217,25 +221,25 @@ public void nodePessimizationTest() {
217
221
pool .pessimizeEndpoint (pool .getEndpoint (2 ));
218
222
check (pool ).records (5 ).knownNodes (5 ).needToReDiscovery (false ).bestEndpointsCount (4 );
219
223
220
- when (random .nextInt (4 )).thenReturn (3 , 1 , 2 , 0 );
224
+ Mockito . when (random .nextInt (4 )).thenReturn (3 , 1 , 2 , 0 );
221
225
check (pool .getEndpoint (null )).hostname ("n5.ydb.tech" ).nodeID (5 ).port (12345 );
222
226
check (pool .getEndpoint (null )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 );
223
227
check (pool .getEndpoint (null )).hostname ("n4.ydb.tech" ).nodeID (4 ).port (12344 );
224
228
check (pool .getEndpoint (null )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 );
225
- verify (random , times (8 )).nextInt (4 ); // Mockito counts also previous 4
229
+ Mockito . verify (random , Mockito . times (8 )).nextInt (4 ); // Mockito counts also previous 4
226
230
227
231
// Pessimize two nodes - then we need to discovery
228
232
pool .pessimizeEndpoint (pool .getEndpoint (3 ));
229
233
check (pool ).records (5 ).knownNodes (5 ).needToReDiscovery (false ).bestEndpointsCount (3 );
230
234
pool .pessimizeEndpoint (pool .getEndpoint (5 ));
231
235
check (pool ).records (5 ).knownNodes (5 ).needToReDiscovery (true ).bestEndpointsCount (2 );
232
236
233
- when (random .nextInt (2 )).thenReturn (1 , 1 , 0 , 0 );
237
+ Mockito . when (random .nextInt (2 )).thenReturn (1 , 1 , 0 , 0 );
234
238
check (pool .getEndpoint (null )).hostname ("n4.ydb.tech" ).nodeID (4 ).port (12344 );
235
239
check (pool .getEndpoint (null )).hostname ("n4.ydb.tech" ).nodeID (4 ).port (12344 );
236
240
check (pool .getEndpoint (null )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 );
237
241
check (pool .getEndpoint (null )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 );
238
- verify (random , times (4 )).nextInt (2 );
242
+ Mockito . verify (random , Mockito . times (4 )).nextInt (2 );
239
243
}
240
244
241
245
@ Test
@@ -253,39 +257,39 @@ public void nodePessimizationFallbackTest() {
253
257
check (pool ).records (4 ).knownNodes (4 ).needToReDiscovery (false ).bestEndpointsCount (2 );
254
258
255
259
// Only local nodes are used
256
- when (random .nextInt (2 )).thenReturn (0 , 1 );
260
+ Mockito . when (random .nextInt (2 )).thenReturn (0 , 1 );
257
261
check (pool .getEndpoint (null )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 );
258
262
check (pool .getEndpoint (null )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 );
259
- verify (random , times (2 )).nextInt (2 );
263
+ Mockito . verify (random , Mockito . times (2 )).nextInt (2 );
260
264
261
265
// Pessimize first local node - use second
262
266
pool .pessimizeEndpoint (pool .getEndpoint (1 ));
263
267
check (pool ).records (4 ).knownNodes (4 ).needToReDiscovery (false ).bestEndpointsCount (1 );
264
268
265
- when (random .nextInt (1 )).thenReturn (0 );
269
+ Mockito . when (random .nextInt (1 )).thenReturn (0 );
266
270
check (pool .getEndpoint (null )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 );
267
- verify (random , times (1 )).nextInt (1 );
271
+ Mockito . verify (random , Mockito . times (1 )).nextInt (1 );
268
272
269
273
// Pessimize second local node - use unlocal nodes
270
274
pool .pessimizeEndpoint (pool .getEndpoint (2 ));
271
275
check (pool ).records (4 ).knownNodes (4 ).needToReDiscovery (false ).bestEndpointsCount (2 );
272
276
273
- when (random .nextInt (2 )).thenReturn (1 , 0 );
277
+ Mockito . when (random .nextInt (2 )).thenReturn (1 , 0 );
274
278
check (pool .getEndpoint (null )).hostname ("n4.ydb.tech" ).nodeID (4 ).port (12344 );
275
279
check (pool .getEndpoint (null )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 );
276
- verify (random , times (4 )).nextInt (2 );
280
+ Mockito . verify (random , Mockito . times (4 )).nextInt (2 );
277
281
278
282
// Pessimize all - fallback to use all nodes
279
283
pool .pessimizeEndpoint (pool .getEndpoint (3 ));
280
284
pool .pessimizeEndpoint (pool .getEndpoint (4 ));
281
285
check (pool ).records (4 ).knownNodes (4 ).needToReDiscovery (true ).bestEndpointsCount (4 );
282
286
283
- when (random .nextInt (4 )).thenReturn (3 , 2 , 1 , 0 );
287
+ Mockito . when (random .nextInt (4 )).thenReturn (3 , 2 , 1 , 0 );
284
288
check (pool .getEndpoint (null )).hostname ("n4.ydb.tech" ).nodeID (4 ).port (12344 );
285
289
check (pool .getEndpoint (null )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 );
286
290
check (pool .getEndpoint (null )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 );
287
291
check (pool .getEndpoint (null )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 );
288
- verify (random , times (4 )).nextInt (4 );
292
+ Mockito . verify (random , Mockito . times (4 )).nextInt (4 );
289
293
290
294
// setNewState reset all
291
295
pool .setNewState ("DC3" , list (
@@ -318,7 +322,7 @@ public void duplicateEndpointsTest() {
318
322
check (pool ).record (2 ).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 );
319
323
check (pool ).record (3 ).hostname ("n3.ydb.tech" ).nodeID (6 ).port (12344 );
320
324
321
- when (random .nextInt (4 )).thenReturn (2 , 0 , 3 , 1 );
325
+ Mockito . when (random .nextInt (4 )).thenReturn (2 , 0 , 3 , 1 );
322
326
323
327
check (pool .getEndpoint (null )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 ); // random
324
328
check (pool .getEndpoint (0 )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 ); // random
@@ -329,7 +333,7 @@ public void duplicateEndpointsTest() {
329
333
check (pool .getEndpoint (5 )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 ); // random
330
334
check (pool .getEndpoint (6 )).hostname ("n3.ydb.tech" ).nodeID (6 ).port (12344 );
331
335
332
- verify (random , times (4 )).nextInt (4 );
336
+ Mockito . verify (random , Mockito . times (4 )).nextInt (4 );
333
337
}
334
338
335
339
@ Test
@@ -349,15 +353,15 @@ public void duplicateNodesTest() {
349
353
check (pool ).record (1 ).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 );
350
354
check (pool ).record (2 ).hostname ("n3.ydb.tech" ).nodeID (2 ).port (12343 );
351
355
352
- when (random .nextInt (3 )).thenReturn (1 , 0 , 2 );
356
+ Mockito . when (random .nextInt (3 )).thenReturn (1 , 0 , 2 );
353
357
354
358
check (pool .getEndpoint (null )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 ); // random
355
359
check (pool .getEndpoint (0 )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 ); // random
356
360
check (pool .getEndpoint (1 )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 );
357
361
check (pool .getEndpoint (2 )).hostname ("n3.ydb.tech" ).nodeID (2 ).port (12343 );
358
362
check (pool .getEndpoint (3 )).hostname ("n3.ydb.tech" ).nodeID (2 ).port (12343 ); // random
359
363
360
- verify (random , times (3 )).nextInt (3 );
364
+ Mockito . verify (random , Mockito . times (3 )).nextInt (3 );
361
365
}
362
366
363
367
@ Test
@@ -377,7 +381,7 @@ public void removeEndpointsTest() {
377
381
check (pool ).record (1 ).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 );
378
382
check (pool ).record (2 ).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 );
379
383
380
- when (random .nextInt (3 )).thenReturn (1 , 0 , 2 );
384
+ Mockito . when (random .nextInt (3 )).thenReturn (1 , 0 , 2 );
381
385
382
386
check (pool .getEndpoint (null )).hostname ("n2.ydb.tech" ).nodeID (2 ).port (12342 ); // random
383
387
check (pool .getEndpoint (0 )).hostname ("n1.ydb.tech" ).nodeID (1 ).port (12341 ); // random
@@ -386,7 +390,7 @@ public void removeEndpointsTest() {
386
390
check (pool .getEndpoint (3 )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 );
387
391
check (pool .getEndpoint (4 )).hostname ("n3.ydb.tech" ).nodeID (3 ).port (12343 ); // random
388
392
389
- verify (random , times (3 )).nextInt (3 );
393
+ Mockito . verify (random , Mockito . times (3 )).nextInt (3 );
390
394
391
395
pool .setNewState ("DC" , list (
392
396
endpoint (2 , "n2.ydb.tech" , 12342 , "DC" ),
@@ -402,7 +406,7 @@ public void removeEndpointsTest() {
402
406
check (pool ).record (2 ).hostname ("n5.ydb.tech" ).nodeID (5 ).port (12345 );
403
407
check (pool ).record (3 ).hostname ("n6.ydb.tech" ).nodeID (6 ).port (12346 );
404
408
405
- when (random .nextInt (4 )).thenReturn (3 , 1 , 2 , 0 );
409
+ Mockito . when (random .nextInt (4 )).thenReturn (3 , 1 , 2 , 0 );
406
410
407
411
check (pool .getEndpoint (null )).hostname ("n6.ydb.tech" ).nodeID (6 ).port (12346 ); // random
408
412
check (pool .getEndpoint (0 )).hostname ("n4.ydb.tech" ).nodeID (4 ).port (12344 ); // random
@@ -413,7 +417,7 @@ public void removeEndpointsTest() {
413
417
check (pool .getEndpoint (5 )).hostname ("n5.ydb.tech" ).nodeID (5 ).port (12345 );
414
418
check (pool .getEndpoint (6 )).hostname ("n6.ydb.tech" ).nodeID (6 ).port (12346 );
415
419
416
- verify (random , times (4 )).nextInt (4 );
420
+ Mockito . verify (random , Mockito . times (4 )).nextInt (4 );
417
421
}
418
422
419
423
@@ -427,42 +431,35 @@ public void detectLocalDCTest() throws IOException {
427
431
428
432
tickerStaticMock .when (Ticker ::systemTicker ).thenReturn (testTicker );
429
433
430
- try (
431
- ServerSocket s1 = ServerSocketFactory .getDefault ().createServerSocket (0 );
432
- ServerSocket s2 = ServerSocketFactory .getDefault ().createServerSocket (0 );
433
- ServerSocket s3 = ServerSocketFactory .getDefault ().createServerSocket (0 );
434
- ) {
435
-
436
- EndpointPool pool = new EndpointPool (detectLocalDC ());
437
- check (pool ).records (0 ).knownNodes (0 ).needToReDiscovery (false );
438
-
439
- int p1 = s1 .getLocalPort ();
440
- int p2 = s2 .getLocalPort ();
441
- int p3 = s3 .getLocalPort ();
442
-
443
- pool .setNewState ("DC" , list (
444
- endpoint (1 , "127.0.0.1" , p1 , "DC1" ),
445
- endpoint (2 , "127.0.0.2" , p2 , "DC2" ),
446
- endpoint (3 , "127.0.0.3" , p3 , "DC3" )
447
- ));
448
-
449
- check (pool ).records (3 ).knownNodes (3 ).needToReDiscovery (false ).bestEndpointsCount (1 );
450
-
451
- check (pool .getEndpoint (null )).hostname ("127.0.0.2" ).nodeID (2 ).port (p2 ); // detect local dc
452
- check (pool .getEndpoint (0 )).hostname ("127.0.0.2" ).nodeID (2 ).port (p2 ); // random from local dc
453
- check (pool .getEndpoint (1 )).hostname ("127.0.0.1" ).nodeID (1 ).port (p1 );
454
- check (pool .getEndpoint (2 )).hostname ("127.0.0.2" ).nodeID (2 ).port (p2 ); // local dc
455
- check (pool .getEndpoint (3 )).hostname ("127.0.0.3" ).nodeID (3 ).port (p3 );
456
- check (pool .getEndpoint (4 )).hostname ("127.0.0.2" ).nodeID (2 ).port (p2 ); // random from local dc
457
-
458
- pool .pessimizeEndpoint (pool .getEndpoint (2 ));
459
- check (pool .getEndpoint (null )).hostname ("127.0.0.1" ).nodeID (1 ).port (p1 ); // new local dc
460
- check (pool .getEndpoint (0 )).hostname ("127.0.0.1" ).nodeID (1 ).port (p1 ); // random from local dc
461
- check (pool .getEndpoint (1 )).hostname ("127.0.0.1" ).nodeID (1 ).port (p1 );
462
- check (pool .getEndpoint (2 )).hostname ("127.0.0.2" ).nodeID (2 ).port (p2 ); // local dc
463
- check (pool .getEndpoint (3 )).hostname ("127.0.0.3" ).nodeID (3 ).port (p3 );
464
- check (pool .getEndpoint (4 )).hostname ("127.0.0.1" ).nodeID (1 ).port (p1 ); // random from local dc
465
- }
434
+ EndpointPool pool = new EndpointPool (detectLocalDC ());
435
+ check (pool ).records (0 ).knownNodes (0 ).needToReDiscovery (false );
436
+
437
+ int p1 = 1234 ;
438
+ int p2 = 1235 ;
439
+ int p3 = 1236 ;
440
+
441
+ pool .setNewState ("DC" , list (
442
+ endpoint (1 , "127.0.0.1" , p1 , "DC1" ),
443
+ endpoint (2 , "127.0.0.2" , p2 , "DC2" ),
444
+ endpoint (3 , "127.0.0.3" , p3 , "DC3" )
445
+ ));
446
+
447
+ check (pool ).records (3 ).knownNodes (3 ).needToReDiscovery (false ).bestEndpointsCount (1 );
448
+
449
+ check (pool .getEndpoint (null )).hostname ("127.0.0.2" ).nodeID (2 ).port (p2 ); // detect local dc
450
+ check (pool .getEndpoint (0 )).hostname ("127.0.0.2" ).nodeID (2 ).port (p2 ); // random from local dc
451
+ check (pool .getEndpoint (1 )).hostname ("127.0.0.1" ).nodeID (1 ).port (p1 );
452
+ check (pool .getEndpoint (2 )).hostname ("127.0.0.2" ).nodeID (2 ).port (p2 ); // local dc
453
+ check (pool .getEndpoint (3 )).hostname ("127.0.0.3" ).nodeID (3 ).port (p3 );
454
+ check (pool .getEndpoint (4 )).hostname ("127.0.0.2" ).nodeID (2 ).port (p2 ); // random from local dc
455
+
456
+ pool .pessimizeEndpoint (pool .getEndpoint (2 ));
457
+ check (pool .getEndpoint (null )).hostname ("127.0.0.1" ).nodeID (1 ).port (p1 ); // new local dc
458
+ check (pool .getEndpoint (0 )).hostname ("127.0.0.1" ).nodeID (1 ).port (p1 ); // random from local dc
459
+ check (pool .getEndpoint (1 )).hostname ("127.0.0.1" ).nodeID (1 ).port (p1 );
460
+ check (pool .getEndpoint (2 )).hostname ("127.0.0.2" ).nodeID (2 ).port (p2 ); // local dc
461
+ check (pool .getEndpoint (3 )).hostname ("127.0.0.3" ).nodeID (3 ).port (p3 );
462
+ check (pool .getEndpoint (4 )).hostname ("127.0.0.1" ).nodeID (1 ).port (p1 ); // random from local dc
466
463
}
467
464
468
465
private static class PoolChecker {
0 commit comments