Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/Selector/SelectWithConsumer.java
41153 views
1
/*
2
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
* @summary Unit test for Selector.select/selectNow(Consumer)
26
* @bug 8199433 8208780
27
* @run testng SelectWithConsumer
28
*/
29
30
/* @test
31
* @requires (os.family == "windows")
32
* @run testng/othervm -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.WindowsSelectorProvider SelectWithConsumer
33
*/
34
35
import java.io.Closeable;
36
import java.io.IOException;
37
import java.net.InetSocketAddress;
38
import java.nio.ByteBuffer;
39
import java.nio.channels.ClosedSelectorException;
40
import java.nio.channels.Pipe;
41
import java.nio.channels.SelectionKey;
42
import java.nio.channels.Selector;
43
import java.nio.channels.ServerSocketChannel;
44
import java.nio.channels.SocketChannel;
45
import java.nio.channels.WritableByteChannel;
46
import java.util.concurrent.Executors;
47
import java.util.concurrent.ScheduledExecutorService;
48
import java.util.concurrent.TimeUnit;
49
import java.util.concurrent.atomic.AtomicInteger;
50
import static java.util.concurrent.TimeUnit.*;
51
52
import org.testng.annotations.AfterTest;
53
import org.testng.annotations.Test;
54
import static org.testng.Assert.*;
55
56
@Test
57
public class SelectWithConsumer {
58
59
/**
60
* Invoke the select methods that take an action and check that the
61
* accumulated ready ops notified to the action matches the expected ops.
62
*/
63
void testActionInvoked(SelectionKey key, int expectedOps) throws Exception {
64
var callerThread = Thread.currentThread();
65
var sel = key.selector();
66
var interestOps = key.interestOps();
67
var notifiedOps = new AtomicInteger();
68
69
if (expectedOps == 0) {
70
// ensure select(Consumer) does not block indefinitely
71
sel.wakeup();
72
} else {
73
// ensure that the channel is ready for all expected operations
74
sel.select();
75
while ((key.readyOps() & interestOps) != expectedOps) {
76
Thread.sleep(100);
77
sel.select();
78
}
79
}
80
81
// select(Consumer)
82
notifiedOps.set(0);
83
int n = sel.select(k -> {
84
assertTrue(Thread.currentThread() == callerThread);
85
assertTrue(k == key);
86
int readyOps = key.readyOps();
87
assertTrue((readyOps & interestOps) != 0);
88
assertTrue((readyOps & notifiedOps.get()) == 0);
89
notifiedOps.set(notifiedOps.get() | readyOps);
90
});
91
assertTrue((n == 1) ^ (expectedOps == 0));
92
assertTrue(notifiedOps.get() == expectedOps);
93
94
// select(Consumer, timeout)
95
notifiedOps.set(0);
96
n = sel.select(k -> {
97
assertTrue(Thread.currentThread() == callerThread);
98
assertTrue(k == key);
99
int readyOps = key.readyOps();
100
assertTrue((readyOps & interestOps) != 0);
101
assertTrue((readyOps & notifiedOps.get()) == 0);
102
notifiedOps.set(notifiedOps.get() | readyOps);
103
}, 1000);
104
assertTrue((n == 1) ^ (expectedOps == 0));
105
assertTrue(notifiedOps.get() == expectedOps);
106
107
// selectNow(Consumer)
108
notifiedOps.set(0);
109
n = sel.selectNow(k -> {
110
assertTrue(Thread.currentThread() == callerThread);
111
assertTrue(k == key);
112
int readyOps = key.readyOps();
113
assertTrue((readyOps & interestOps) != 0);
114
assertTrue((readyOps & notifiedOps.get()) == 0);
115
notifiedOps.set(notifiedOps.get() | readyOps);
116
});
117
assertTrue((n == 1) ^ (expectedOps == 0));
118
assertTrue(notifiedOps.get() == expectedOps);
119
}
120
121
/**
122
* Test that an action is performed when a channel is ready for reading.
123
*/
124
public void testReadable() throws Exception {
125
Pipe p = Pipe.open();
126
try (Selector sel = Selector.open()) {
127
Pipe.SinkChannel sink = p.sink();
128
Pipe.SourceChannel source = p.source();
129
source.configureBlocking(false);
130
SelectionKey key = source.register(sel, SelectionKey.OP_READ);
131
132
// write to sink to ensure source is readable
133
scheduleWrite(sink, messageBuffer(), 100, MILLISECONDS);
134
135
// test that action is invoked
136
testActionInvoked(key, SelectionKey.OP_READ);
137
} finally {
138
closePipe(p);
139
}
140
}
141
142
/**
143
* Test that an action is performed when a channel is ready for writing.
144
*/
145
public void testWritable() throws Exception {
146
Pipe p = Pipe.open();
147
try (Selector sel = Selector.open()) {
148
Pipe.SourceChannel source = p.source();
149
Pipe.SinkChannel sink = p.sink();
150
sink.configureBlocking(false);
151
SelectionKey key = sink.register(sel, SelectionKey.OP_WRITE);
152
153
// test that action is invoked
154
testActionInvoked(key, SelectionKey.OP_WRITE);
155
} finally {
156
closePipe(p);
157
}
158
}
159
160
/**
161
* Test that an action is performed when a channel is ready for both
162
* reading and writing.
163
*/
164
public void testReadableAndWriteable() throws Exception {
165
ServerSocketChannel ssc = null;
166
SocketChannel sc = null;
167
SocketChannel peer = null;
168
try (Selector sel = Selector.open()) {
169
ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
170
sc = SocketChannel.open(ssc.getLocalAddress());
171
sc.configureBlocking(false);
172
SelectionKey key = sc.register(sel, (SelectionKey.OP_READ |
173
SelectionKey.OP_WRITE));
174
175
// accept connection and write data so the source is readable
176
peer = ssc.accept();
177
peer.write(messageBuffer());
178
179
// test that action is invoked
180
testActionInvoked(key, (SelectionKey.OP_READ | SelectionKey.OP_WRITE));
181
} finally {
182
if (ssc != null) ssc.close();
183
if (sc != null) sc.close();
184
if (peer != null) peer.close();
185
}
186
}
187
188
/**
189
* Test that the action is called for two selected channels
190
*/
191
public void testTwoChannels() throws Exception {
192
Pipe p = Pipe.open();
193
try (Selector sel = Selector.open()) {
194
Pipe.SourceChannel source = p.source();
195
Pipe.SinkChannel sink = p.sink();
196
source.configureBlocking(false);
197
sink.configureBlocking(false);
198
SelectionKey key1 = source.register(sel, SelectionKey.OP_READ);
199
SelectionKey key2 = sink.register(sel, SelectionKey.OP_WRITE);
200
201
// write to sink to ensure that the source is readable
202
sink.write(messageBuffer());
203
204
// wait for key1 to be readable
205
sel.select();
206
assertTrue(key2.isWritable());
207
while (!key1.isReadable()) {
208
Thread.sleep(20);
209
sel.select();
210
}
211
212
var counter = new AtomicInteger();
213
214
// select(Consumer)
215
counter.set(0);
216
int n = sel.select(k -> {
217
assertTrue(k == key1 || k == key2);
218
counter.incrementAndGet();
219
});
220
assertTrue(n == 2);
221
assertTrue(counter.get() == 2);
222
223
// select(Consumer, timeout)
224
counter.set(0);
225
n = sel.select(k -> {
226
assertTrue(k == key1 || k == key2);
227
counter.incrementAndGet();
228
}, 1000);
229
assertTrue(n == 2);
230
assertTrue(counter.get() == 2);
231
232
// selectNow(Consumer)
233
counter.set(0);
234
n = sel.selectNow(k -> {
235
assertTrue(k == key1 || k == key2);
236
counter.incrementAndGet();
237
});
238
assertTrue(n == 2);
239
assertTrue(counter.get() == 2);
240
} finally {
241
closePipe(p);
242
}
243
}
244
245
/**
246
* Test calling select twice, the action should be invoked each time
247
*/
248
public void testRepeatedSelect1() throws Exception {
249
Pipe p = Pipe.open();
250
try (Selector sel = Selector.open()) {
251
Pipe.SourceChannel source = p.source();
252
Pipe.SinkChannel sink = p.sink();
253
source.configureBlocking(false);
254
SelectionKey key = source.register(sel, SelectionKey.OP_READ);
255
256
// write to sink to ensure that the source is readable
257
sink.write(messageBuffer());
258
259
// test that action is invoked
260
testActionInvoked(key, SelectionKey.OP_READ);
261
testActionInvoked(key, SelectionKey.OP_READ);
262
263
} finally {
264
closePipe(p);
265
}
266
}
267
268
/**
269
* Test calling select twice. An I/O operation is performed after the
270
* first select so the channel will not be selected by the second select.
271
*/
272
public void testRepeatedSelect2() throws Exception {
273
Pipe p = Pipe.open();
274
try (Selector sel = Selector.open()) {
275
Pipe.SourceChannel source = p.source();
276
Pipe.SinkChannel sink = p.sink();
277
source.configureBlocking(false);
278
SelectionKey key = source.register(sel, SelectionKey.OP_READ);
279
280
// write to sink to ensure that the source is readable
281
sink.write(messageBuffer());
282
283
// test that action is invoked
284
testActionInvoked(key, SelectionKey.OP_READ);
285
286
// read all bytes
287
int n;
288
ByteBuffer bb = ByteBuffer.allocate(100);
289
do {
290
n = source.read(bb);
291
bb.clear();
292
} while (n > 0);
293
294
// test that action is not invoked
295
testActionInvoked(key, 0);
296
} finally {
297
closePipe(p);
298
}
299
}
300
301
/**
302
* Test timeout
303
*/
304
public void testTimeout() throws Exception {
305
Pipe p = Pipe.open();
306
try (Selector sel = Selector.open()) {
307
Pipe.SourceChannel source = p.source();
308
Pipe.SinkChannel sink = p.sink();
309
source.configureBlocking(false);
310
source.register(sel, SelectionKey.OP_READ);
311
long start = System.currentTimeMillis();
312
int n = sel.select(k -> assertTrue(false), 1000L);
313
long duration = System.currentTimeMillis() - start;
314
assertTrue(n == 0);
315
assertTrue(duration > 500, "select took " + duration + " ms");
316
} finally {
317
closePipe(p);
318
}
319
}
320
321
/**
322
* Test wakeup prior to select
323
*/
324
public void testWakeupBeforeSelect() throws Exception {
325
// select(Consumer)
326
try (Selector sel = Selector.open()) {
327
sel.wakeup();
328
int n = sel.select(k -> assertTrue(false));
329
assertTrue(n == 0);
330
}
331
332
// select(Consumer, timeout)
333
try (Selector sel = Selector.open()) {
334
sel.wakeup();
335
long start = System.currentTimeMillis();
336
int n = sel.select(k -> assertTrue(false), 60*1000);
337
long duration = System.currentTimeMillis() - start;
338
assertTrue(n == 0);
339
assertTrue(duration < 5000, "select took " + duration + " ms");
340
}
341
}
342
343
/**
344
* Test wakeup during select
345
*/
346
public void testWakeupDuringSelect() throws Exception {
347
// select(Consumer)
348
try (Selector sel = Selector.open()) {
349
scheduleWakeup(sel, 1, SECONDS);
350
int n = sel.select(k -> assertTrue(false));
351
assertTrue(n == 0);
352
}
353
354
// select(Consumer, timeout)
355
try (Selector sel = Selector.open()) {
356
scheduleWakeup(sel, 1, SECONDS);
357
long start = System.currentTimeMillis();
358
int n = sel.select(k -> assertTrue(false), 60*1000);
359
long duration = System.currentTimeMillis() - start;
360
assertTrue(n == 0);
361
assertTrue(duration > 500 && duration < 10*1000,
362
"select took " + duration + " ms");
363
}
364
}
365
366
/**
367
* Test invoking select with interrupt status set
368
*/
369
public void testInterruptBeforeSelect() throws Exception {
370
// select(Consumer)
371
try (Selector sel = Selector.open()) {
372
Thread.currentThread().interrupt();
373
int n = sel.select(k -> assertTrue(false));
374
assertTrue(n == 0);
375
assertTrue(Thread.currentThread().isInterrupted());
376
assertTrue(sel.isOpen());
377
} finally {
378
Thread.currentThread().interrupted(); // clear interrupt status
379
}
380
381
// select(Consumer, timeout)
382
try (Selector sel = Selector.open()) {
383
Thread.currentThread().interrupt();
384
long start = System.currentTimeMillis();
385
int n = sel.select(k -> assertTrue(false), 60*1000);
386
long duration = System.currentTimeMillis() - start;
387
assertTrue(n == 0);
388
assertTrue(duration < 5000, "select took " + duration + " ms");
389
assertTrue(Thread.currentThread().isInterrupted());
390
assertTrue(sel.isOpen());
391
} finally {
392
Thread.currentThread().interrupted(); // clear interrupt status
393
}
394
}
395
396
/**
397
* Test interrupt thread during select
398
*/
399
public void testInterruptDuringSelect() throws Exception {
400
// select(Consumer)
401
try (Selector sel = Selector.open()) {
402
scheduleInterrupt(Thread.currentThread(), 1, SECONDS);
403
int n = sel.select(k -> assertTrue(false));
404
assertTrue(n == 0);
405
assertTrue(Thread.currentThread().isInterrupted());
406
assertTrue(sel.isOpen());
407
} finally {
408
Thread.currentThread().interrupted(); // clear interrupt status
409
}
410
411
// select(Consumer, timeout)
412
try (Selector sel = Selector.open()) {
413
scheduleInterrupt(Thread.currentThread(), 1, SECONDS);
414
long start = System.currentTimeMillis();
415
int n = sel.select(k -> assertTrue(false), 60*1000);
416
long duration = System.currentTimeMillis() - start;
417
assertTrue(n == 0);
418
assertTrue(Thread.currentThread().isInterrupted());
419
assertTrue(sel.isOpen());
420
} finally {
421
Thread.currentThread().interrupted(); // clear interrupt status
422
}
423
}
424
425
/**
426
* Test invoking select on a closed selector
427
*/
428
@Test(expectedExceptions = ClosedSelectorException.class)
429
public void testClosedSelector1() throws Exception {
430
Selector sel = Selector.open();
431
sel.close();
432
sel.select(k -> assertTrue(false));
433
}
434
@Test(expectedExceptions = ClosedSelectorException.class)
435
public void testClosedSelector2() throws Exception {
436
Selector sel = Selector.open();
437
sel.close();
438
sel.select(k -> assertTrue(false), 1000);
439
}
440
@Test(expectedExceptions = ClosedSelectorException.class)
441
public void testClosedSelector3() throws Exception {
442
Selector sel = Selector.open();
443
sel.close();
444
sel.selectNow(k -> assertTrue(false));
445
}
446
447
/**
448
* Test closing selector while in a selection operation
449
*/
450
public void testCloseDuringSelect() throws Exception {
451
// select(Consumer)
452
try (Selector sel = Selector.open()) {
453
scheduleClose(sel, 3, SECONDS);
454
int n = sel.select(k -> assertTrue(false));
455
assertTrue(n == 0);
456
assertFalse(sel.isOpen());
457
}
458
459
// select(Consumer, timeout)
460
try (Selector sel = Selector.open()) {
461
long before = System.nanoTime();
462
scheduleClose(sel, 3, SECONDS);
463
long start = System.nanoTime();
464
int n = sel.select(k -> assertTrue(false), 60*1000);
465
long after = System.nanoTime();
466
long selectDuration = (after - start) / 1000000;
467
long scheduleDuration = (start - before) / 1000000;
468
assertTrue(n == 0);
469
assertTrue(selectDuration > 2000 && selectDuration < 10*1000,
470
"select took " + selectDuration + " ms schedule took " +
471
scheduleDuration + " ms");
472
assertFalse(sel.isOpen());
473
}
474
}
475
476
/**
477
* Test action closing selector
478
*/
479
@Test(expectedExceptions = ClosedSelectorException.class)
480
public void testActionClosingSelector() throws Exception {
481
Pipe p = Pipe.open();
482
try (Selector sel = Selector.open()) {
483
Pipe.SourceChannel source = p.source();
484
Pipe.SinkChannel sink = p.sink();
485
source.configureBlocking(false);
486
SelectionKey key = source.register(sel, SelectionKey.OP_READ);
487
488
// write to sink to ensure that the source is readable
489
sink.write(messageBuffer());
490
491
// should relay ClosedSelectorException
492
sel.select(k -> {
493
assertTrue(k == key);
494
try {
495
sel.close();
496
} catch (IOException ioe) { }
497
});
498
} finally {
499
closePipe(p);
500
}
501
}
502
503
/**
504
* Test that the action is invoked while synchronized on the selector and
505
* its selected-key set.
506
*/
507
public void testLocks() throws Exception {
508
Pipe p = Pipe.open();
509
try (Selector sel = Selector.open()) {
510
Pipe.SourceChannel source = p.source();
511
Pipe.SinkChannel sink = p.sink();
512
source.configureBlocking(false);
513
SelectionKey key = source.register(sel, SelectionKey.OP_READ);
514
515
// write to sink to ensure that the source is readable
516
sink.write(messageBuffer());
517
518
// select(Consumer)
519
sel.select(k -> {
520
assertTrue(k == key);
521
assertTrue(Thread.holdsLock(sel));
522
assertFalse(Thread.holdsLock(sel.keys()));
523
assertTrue(Thread.holdsLock(sel.selectedKeys()));
524
});
525
526
// select(Consumer, timeout)
527
sel.select(k -> {
528
assertTrue(k == key);
529
assertTrue(Thread.holdsLock(sel));
530
assertFalse(Thread.holdsLock(sel.keys()));
531
assertTrue(Thread.holdsLock(sel.selectedKeys()));
532
}, 1000L);
533
534
// selectNow(Consumer)
535
sel.selectNow(k -> {
536
assertTrue(k == key);
537
assertTrue(Thread.holdsLock(sel));
538
assertFalse(Thread.holdsLock(sel.keys()));
539
assertTrue(Thread.holdsLock(sel.selectedKeys()));
540
});
541
} finally {
542
closePipe(p);
543
}
544
}
545
546
/**
547
* Test that selection operations remove cancelled keys from the selector's
548
* key and selected-key sets.
549
*/
550
public void testCancel() throws Exception {
551
Pipe p = Pipe.open();
552
try (Selector sel = Selector.open()) {
553
Pipe.SinkChannel sink = p.sink();
554
Pipe.SourceChannel source = p.source();
555
556
// write to sink to ensure that the source is readable
557
sink.write(messageBuffer());
558
559
source.configureBlocking(false);
560
SelectionKey key1 = source.register(sel, SelectionKey.OP_READ);
561
// make sure pipe source is readable before we do following checks.
562
// this is sometime necessary on windows where pipe is implemented
563
// as a pair of connected socket, so there is no guarantee that written
564
// bytes on sink side is immediately available on source side.
565
sel.select();
566
567
sink.configureBlocking(false);
568
SelectionKey key2 = sink.register(sel, SelectionKey.OP_WRITE);
569
sel.selectNow();
570
571
assertTrue(sel.keys().contains(key1));
572
assertTrue(sel.keys().contains(key2));
573
assertTrue(sel.selectedKeys().contains(key1));
574
assertTrue(sel.selectedKeys().contains(key2));
575
576
// cancel key1
577
key1.cancel();
578
int n = sel.selectNow(k -> assertTrue(k == key2));
579
assertTrue(n == 1);
580
assertFalse(sel.keys().contains(key1));
581
assertTrue(sel.keys().contains(key2));
582
assertFalse(sel.selectedKeys().contains(key1));
583
assertTrue(sel.selectedKeys().contains(key2));
584
585
// cancel key2
586
key2.cancel();
587
n = sel.selectNow(k -> assertTrue(false));
588
assertTrue(n == 0);
589
assertFalse(sel.keys().contains(key1));
590
assertFalse(sel.keys().contains(key2));
591
assertFalse(sel.selectedKeys().contains(key1));
592
assertFalse(sel.selectedKeys().contains(key2));
593
} finally {
594
closePipe(p);
595
}
596
}
597
598
/**
599
* Test an action invoking select()
600
*/
601
public void testReentrantSelect1() throws Exception {
602
Pipe p = Pipe.open();
603
try (Selector sel = Selector.open()) {
604
Pipe.SinkChannel sink = p.sink();
605
Pipe.SourceChannel source = p.source();
606
source.configureBlocking(false);
607
source.register(sel, SelectionKey.OP_READ);
608
609
// write to sink to ensure that the source is readable
610
scheduleWrite(sink, messageBuffer(), 100, MILLISECONDS);
611
612
int n = sel.select(k -> {
613
try {
614
sel.select();
615
assertTrue(false);
616
} catch (IOException ioe) {
617
throw new RuntimeException(ioe);
618
} catch (IllegalStateException expected) {
619
}
620
});
621
assertTrue(n == 1);
622
} finally {
623
closePipe(p);
624
}
625
}
626
627
/**
628
* Test an action invoking selectNow()
629
*/
630
public void testReentrantSelect2() throws Exception {
631
Pipe p = Pipe.open();
632
try (Selector sel = Selector.open()) {
633
Pipe.SinkChannel sink = p.sink();
634
Pipe.SourceChannel source = p.source();
635
636
// write to sink to ensure that the source is readable
637
scheduleWrite(sink, messageBuffer(), 100, MILLISECONDS);
638
639
source.configureBlocking(false);
640
source.register(sel, SelectionKey.OP_READ);
641
int n = sel.select(k -> {
642
try {
643
sel.selectNow();
644
assertTrue(false);
645
} catch (IOException ioe) {
646
throw new RuntimeException(ioe);
647
} catch (IllegalStateException expected) {
648
}
649
});
650
assertTrue(n == 1);
651
} finally {
652
closePipe(p);
653
}
654
}
655
656
/**
657
* Test an action invoking select(Consumer)
658
*/
659
public void testReentrantSelect3() throws Exception {
660
Pipe p = Pipe.open();
661
try (Selector sel = Selector.open()) {
662
Pipe.SinkChannel sink = p.sink();
663
Pipe.SourceChannel source = p.source();
664
665
// write to sink to ensure that the source is readable
666
scheduleWrite(sink, messageBuffer(), 100, MILLISECONDS);
667
668
source.configureBlocking(false);
669
source.register(sel, SelectionKey.OP_READ);
670
int n = sel.select(k -> {
671
try {
672
sel.select(x -> assertTrue(false));
673
assertTrue(false);
674
} catch (IOException ioe) {
675
throw new RuntimeException(ioe);
676
} catch (IllegalStateException expected) {
677
}
678
});
679
assertTrue(n == 1);
680
} finally {
681
closePipe(p);
682
}
683
}
684
685
/**
686
* Negative timeout
687
*/
688
@Test(expectedExceptions = IllegalArgumentException.class)
689
public void testNegativeTimeout() throws Exception {
690
try (Selector sel = Selector.open()) {
691
sel.select(k -> { }, -1L);
692
}
693
}
694
695
/**
696
* Null action
697
*/
698
@Test(expectedExceptions = NullPointerException.class)
699
public void testNull1() throws Exception {
700
try (Selector sel = Selector.open()) {
701
sel.select(null);
702
}
703
}
704
@Test(expectedExceptions = NullPointerException.class)
705
public void testNull2() throws Exception {
706
try (Selector sel = Selector.open()) {
707
sel.select(null, 1000);
708
}
709
}
710
@Test(expectedExceptions = NullPointerException.class)
711
public void testNull3() throws Exception {
712
try (Selector sel = Selector.open()) {
713
sel.selectNow(null);
714
}
715
}
716
717
718
// -- support methods ---
719
720
private final ScheduledExecutorService POOL = Executors.newScheduledThreadPool(1);
721
722
@AfterTest
723
void shutdownThreadPool() {
724
POOL.shutdown();
725
}
726
727
void scheduleWakeup(Selector sel, long delay, TimeUnit unit) {
728
POOL.schedule(() -> sel.wakeup(), delay, unit);
729
}
730
731
void scheduleInterrupt(Thread t, long delay, TimeUnit unit) {
732
POOL.schedule(() -> t.interrupt(), delay, unit);
733
}
734
735
void scheduleClose(Closeable c, long delay, TimeUnit unit) {
736
POOL.schedule(() -> {
737
try {
738
c.close();
739
} catch (IOException ioe) {
740
ioe.printStackTrace();
741
}
742
}, delay, unit);
743
}
744
745
void scheduleWrite(WritableByteChannel sink, ByteBuffer buf, long delay, TimeUnit unit) {
746
POOL.schedule(() -> {
747
try {
748
sink.write(buf);
749
} catch (IOException ioe) {
750
ioe.printStackTrace();
751
}
752
}, delay, unit);
753
}
754
755
static void closePipe(Pipe p) {
756
try { p.sink().close(); } catch (IOException ignore) { }
757
try { p.source().close(); } catch (IOException ignore) { }
758
}
759
760
static ByteBuffer messageBuffer() {
761
try {
762
return ByteBuffer.wrap("message".getBytes("UTF-8"));
763
} catch (Exception e) {
764
throw new RuntimeException(e);
765
}
766
}
767
}
768
769