82
82
import java .util .concurrent .Future ;
83
83
import java .util .concurrent .LinkedBlockingQueue ;
84
84
import java .util .concurrent .ThreadLocalRandom ;
85
- import java .util .concurrent .TimeUnit ;
86
85
import java .util .concurrent .atomic .AtomicBoolean ;
87
86
import java .util .concurrent .atomic .AtomicReference ;
88
87
89
- import scala .concurrent .duration .Deadline ;
90
- import scala .concurrent .duration .FiniteDuration ;
91
-
92
88
import static org .junit .Assert .assertArrayEquals ;
93
89
import static org .junit .Assert .assertEquals ;
94
90
import static org .junit .Assert .assertNotNull ;
@@ -100,8 +96,6 @@ public class ClientTest extends TestLogger {
100
96
101
97
private static final Logger LOG = LoggerFactory .getLogger (ClientTest .class );
102
98
103
- private static final FiniteDuration TEST_TIMEOUT = new FiniteDuration (20L , TimeUnit .SECONDS );
104
-
105
99
// Thread pool for client bootstrap (shared between tests)
106
100
private NioEventLoopGroup nioGroup ;
107
101
@@ -114,14 +108,13 @@ public void setUp() throws Exception {
114
108
public void tearDown () throws Exception {
115
109
if (nioGroup != null ) {
116
110
// note: no "quiet period" to not trigger Netty#4357
117
- nioGroup .shutdownGracefully (0 , 10 , TimeUnit . SECONDS );
111
+ nioGroup .shutdownGracefully ();
118
112
}
119
113
}
120
114
121
115
/** Tests simple queries, of which half succeed and half fail. */
122
116
@ Test
123
117
public void testSimpleRequests () throws Exception {
124
- Deadline deadline = TEST_TIMEOUT .fromNow ();
125
118
AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats ();
126
119
127
120
MessageSerializer <KvStateInternalRequest , KvStateResponse > serializer =
@@ -173,7 +166,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
173
166
Exception testException = new RuntimeException ("Expected test Exception" );
174
167
175
168
for (long i = 0L ; i < numQueries ; i ++) {
176
- ByteBuf buf = received .poll ( deadline . timeLeft (). toMillis (), TimeUnit . MILLISECONDS );
169
+ ByteBuf buf = received .take ( );
177
170
assertNotNull ("Receive timed out" , buf );
178
171
179
172
Channel ch = channel .get ();
@@ -205,14 +198,11 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
205
198
for (long i = 0L ; i < numQueries ; i ++) {
206
199
207
200
if (i % 2L == 0L ) {
208
- KvStateResponse serializedResult =
209
- futures .get ((int ) i )
210
- .get (deadline .timeLeft ().toMillis (), TimeUnit .MILLISECONDS );
201
+ KvStateResponse serializedResult = futures .get ((int ) i ).get ();
211
202
assertArrayEquals (expected , serializedResult .getContent ());
212
203
} else {
213
204
try {
214
- futures .get ((int ) i )
215
- .get (deadline .timeLeft ().toMillis (), TimeUnit .MILLISECONDS );
205
+ futures .get ((int ) i ).get ();
216
206
fail ("Did not throw expected Exception" );
217
207
} catch (ExecutionException e ) {
218
208
@@ -228,9 +218,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
228
218
long expectedRequests = numQueries / 2L ;
229
219
230
220
// Counts can take some time to propagate
231
- while (deadline .hasTimeLeft ()
232
- && (stats .getNumSuccessful () != expectedRequests
233
- || stats .getNumFailed () != expectedRequests )) {
221
+ while (stats .getNumSuccessful () != expectedRequests
222
+ || stats .getNumFailed () != expectedRequests ) {
234
223
Thread .sleep (100L );
235
224
}
236
225
@@ -246,7 +235,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
246
235
// this is why we now simply wait a bit so that everything is
247
236
// shut down and then we check
248
237
249
- client .shutdown ().get (10L , TimeUnit . SECONDS );
238
+ client .shutdown ().get ();
250
239
} catch (Exception e ) {
251
240
exc = e ;
252
241
LOG .error ("An exception occurred while shutting down netty." , e );
@@ -267,7 +256,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
267
256
/** Tests that a request to an unavailable host is failed with ConnectException. */
268
257
@ Test
269
258
public void testRequestUnavailableHost () throws Exception {
270
- Deadline deadline = TEST_TIMEOUT .fromNow ();
271
259
AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats ();
272
260
273
261
MessageSerializer <KvStateInternalRequest , KvStateResponse > serializer =
@@ -290,7 +278,7 @@ public void testRequestUnavailableHost() throws Exception {
290
278
CompletableFuture <KvStateResponse > future = client .sendRequest (serverAddress , request );
291
279
292
280
try {
293
- future .get (deadline . timeLeft (). toMillis (), TimeUnit . MILLISECONDS );
281
+ future .get ();
294
282
fail ("Did not throw expected ConnectException" );
295
283
} catch (ExecutionException e ) {
296
284
if (!(e .getCause () instanceof ConnectException )) {
@@ -301,7 +289,7 @@ public void testRequestUnavailableHost() throws Exception {
301
289
} finally {
302
290
if (client != null ) {
303
291
try {
304
- client .shutdown ().get (10L , TimeUnit . SECONDS );
292
+ client .shutdown ().get ();
305
293
} catch (Exception e ) {
306
294
e .printStackTrace ();
307
295
}
@@ -315,7 +303,6 @@ public void testRequestUnavailableHost() throws Exception {
315
303
/** Multiple threads concurrently fire queries. */
316
304
@ Test
317
305
public void testConcurrentQueries () throws Exception {
318
- Deadline deadline = TEST_TIMEOUT .fromNow ();
319
306
AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats ();
320
307
321
308
final MessageSerializer <KvStateInternalRequest , KvStateResponse > serializer =
@@ -389,19 +376,17 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
389
376
390
377
// Verify results
391
378
for (Future <List <CompletableFuture <KvStateResponse >>> future : futures ) {
392
- List <CompletableFuture <KvStateResponse >> results =
393
- future .get (deadline .timeLeft ().toMillis (), TimeUnit .MILLISECONDS );
379
+ List <CompletableFuture <KvStateResponse >> results = future .get ();
394
380
for (CompletableFuture <KvStateResponse > result : results ) {
395
- KvStateResponse actual =
396
- result .get (deadline .timeLeft ().toMillis (), TimeUnit .MILLISECONDS );
381
+ KvStateResponse actual = result .get ();
397
382
assertArrayEquals (serializedResult , actual .getContent ());
398
383
}
399
384
}
400
385
401
386
int totalQueries = numQueryTasks * numQueriesPerTask ;
402
387
403
388
// Counts can take some time to propagate
404
- while (deadline . hasTimeLeft () && stats .getNumSuccessful () != totalQueries ) {
389
+ while (stats .getNumSuccessful () != totalQueries ) {
405
390
Thread .sleep (100L );
406
391
}
407
392
@@ -418,7 +403,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
418
403
419
404
if (client != null ) {
420
405
try {
421
- client .shutdown ().get (10L , TimeUnit . SECONDS );
406
+ client .shutdown ().get ();
422
407
} catch (Exception e ) {
423
408
e .printStackTrace ();
424
409
}
@@ -435,7 +420,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
435
420
*/
436
421
@ Test
437
422
public void testFailureClosesChannel () throws Exception {
438
- Deadline deadline = TEST_TIMEOUT .fromNow ();
439
423
AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats ();
440
424
441
425
final MessageSerializer <KvStateInternalRequest , KvStateResponse > serializer =
@@ -478,11 +462,11 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
478
462
futures .add (client .sendRequest (serverAddress , request ));
479
463
futures .add (client .sendRequest (serverAddress , request ));
480
464
481
- ByteBuf buf = received .poll ( deadline . timeLeft (). toMillis (), TimeUnit . MILLISECONDS );
465
+ ByteBuf buf = received .take ( );
482
466
assertNotNull ("Receive timed out" , buf );
483
467
buf .release ();
484
468
485
- buf = received .poll ( deadline . timeLeft (). toMillis (), TimeUnit . MILLISECONDS );
469
+ buf = received .take ( );
486
470
assertNotNull ("Receive timed out" , buf );
487
471
buf .release ();
488
472
@@ -498,7 +482,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
498
482
new RuntimeException ("Expected test server failure" )));
499
483
500
484
try {
501
- futures .remove (0 ).get (deadline . timeLeft (). toMillis (), TimeUnit . MILLISECONDS );
485
+ futures .remove (0 ).get ();
502
486
fail ("Did not throw expected server failure" );
503
487
} catch (ExecutionException e ) {
504
488
@@ -509,7 +493,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
509
493
}
510
494
511
495
try {
512
- futures .remove (0 ).get (deadline . timeLeft (). toMillis (), TimeUnit . MILLISECONDS );
496
+ futures .remove (0 ).get ();
513
497
fail ("Did not throw expected server failure" );
514
498
} catch (ExecutionException e ) {
515
499
@@ -522,8 +506,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
522
506
assertEquals (0L , stats .getNumConnections ());
523
507
524
508
// Counts can take some time to propagate
525
- while (deadline .hasTimeLeft ()
526
- && (stats .getNumSuccessful () != 0L || stats .getNumFailed () != 2L )) {
509
+ while (stats .getNumSuccessful () != 0L || stats .getNumFailed () != 2L ) {
527
510
Thread .sleep (100L );
528
511
}
529
512
@@ -533,7 +516,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
533
516
} finally {
534
517
if (client != null ) {
535
518
try {
536
- client .shutdown ().get (10L , TimeUnit . SECONDS );
519
+ client .shutdown ().get ();
537
520
} catch (Exception e ) {
538
521
e .printStackTrace ();
539
522
}
@@ -554,7 +537,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
554
537
*/
555
538
@ Test
556
539
public void testServerClosesChannel () throws Exception {
557
- Deadline deadline = TEST_TIMEOUT .fromNow ();
558
540
AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats ();
559
541
560
542
final MessageSerializer <KvStateInternalRequest , KvStateResponse > serializer =
@@ -594,17 +576,17 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
594
576
new KvStateInternalRequest (new KvStateID (), new byte [0 ]);
595
577
Future <KvStateResponse > future = client .sendRequest (serverAddress , request );
596
578
597
- while (!received .get () && deadline . hasTimeLeft () ) {
579
+ while (!received .get ()) {
598
580
Thread .sleep (50L );
599
581
}
600
582
assertTrue ("Receive timed out" , received .get ());
601
583
602
584
assertEquals (1 , stats .getNumConnections ());
603
585
604
- channel .get ().close ().await (deadline . timeLeft (). toMillis (), TimeUnit . MILLISECONDS );
586
+ channel .get ().close ().await ();
605
587
606
588
try {
607
- future .get (deadline . timeLeft (). toMillis (), TimeUnit . MILLISECONDS );
589
+ future .get ();
608
590
fail ("Did not throw expected server failure" );
609
591
} catch (ExecutionException e ) {
610
592
if (!(e .getCause () instanceof ClosedChannelException )) {
@@ -616,8 +598,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
616
598
assertEquals (0L , stats .getNumConnections ());
617
599
618
600
// Counts can take some time to propagate
619
- while (deadline .hasTimeLeft ()
620
- && (stats .getNumSuccessful () != 0L || stats .getNumFailed () != 1L )) {
601
+ while (stats .getNumSuccessful () != 0L || stats .getNumFailed () != 1L ) {
621
602
Thread .sleep (100L );
622
603
}
623
604
@@ -627,7 +608,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
627
608
} finally {
628
609
if (client != null ) {
629
610
try {
630
- client .shutdown ().get (10L , TimeUnit . SECONDS );
611
+ client .shutdown ().get ();
631
612
} catch (Exception e ) {
632
613
e .printStackTrace ();
633
614
}
@@ -679,8 +660,6 @@ public void testClientServerIntegration() throws Throwable {
679
660
Collections .emptyList (),
680
661
new CloseableRegistry ());
681
662
682
- final FiniteDuration timeout = new FiniteDuration (10 , TimeUnit .SECONDS );
683
-
684
663
AtomicKvStateRequestStats clientStats = new AtomicKvStateRequestStats ();
685
664
686
665
final MessageSerializer <KvStateInternalRequest , KvStateResponse > serializer =
@@ -787,9 +766,7 @@ public void testClientServerIntegration() throws Throwable {
787
766
int targetServer = random .get (j ) % numServers ;
788
767
789
768
Future <KvStateResponse > future = futures .get (j );
790
- byte [] buf =
791
- future .get (timeout .toMillis (), TimeUnit .MILLISECONDS )
792
- .getContent ();
769
+ byte [] buf = future .get ().getContent ();
793
770
int value =
794
771
KvStateSerializer .deserializeValue (
795
772
buf , IntSerializer .INSTANCE );
@@ -811,7 +788,7 @@ public void testClientServerIntegration() throws Throwable {
811
788
}
812
789
813
790
try {
814
- client .shutdown ().get (10L , TimeUnit . SECONDS );
791
+ client .shutdown ().get ();
815
792
} catch (Exception e ) {
816
793
e .printStackTrace ();
817
794
}
@@ -855,7 +832,7 @@ public void testClientServerIntegration() throws Throwable {
855
832
} finally {
856
833
if (client != null ) {
857
834
try {
858
- client .shutdown ().get (10L , TimeUnit . SECONDS );
835
+ client .shutdown ().get ();
859
836
} catch (Exception e ) {
860
837
e .printStackTrace ();
861
838
}
0 commit comments