Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/unixdomain/IOExchanges.java
41153 views
1
/*
2
* Copyright (c) 2018, 2020, 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
/**
25
* @test
26
* @bug 8245194
27
* @run testng/othervm IOExchanges
28
*/
29
30
import java.io.IOException;
31
import java.net.*;
32
import java.nio.channels.*;
33
import java.nio.ByteBuffer;
34
import java.nio.file.Files;
35
36
import org.testng.annotations.BeforeTest;
37
import org.testng.annotations.DataProvider;
38
import org.testng.annotations.Test;
39
40
import static java.lang.System.out;
41
import static java.net.StandardProtocolFamily.*;
42
import static java.nio.channels.SelectionKey.OP_ACCEPT;
43
import static java.nio.channels.SelectionKey.OP_READ;
44
import static java.nio.channels.SelectionKey.OP_WRITE;
45
import static org.testng.Assert.assertEquals;
46
import static org.testng.Assert.assertTrue;
47
48
public class IOExchanges {
49
static boolean unixDomainSupported = true;
50
51
52
@BeforeTest()
53
public void setup() {
54
try {
55
SocketChannel.open(UNIX);
56
} catch (IOException | UnsupportedOperationException e) {
57
unixDomainSupported = false;
58
out.println("Unix domain channels not supported");
59
}
60
}
61
62
static SocketChannel openSocketChannel(ProtocolFamily family)
63
throws IOException {
64
return family == UNIX ? SocketChannel.open(family)
65
: SocketChannel.open();
66
}
67
68
static ServerSocketChannel openServerSocketChannel(ProtocolFamily family)
69
throws IOException {
70
return family == UNIX ? ServerSocketChannel.open(family)
71
: ServerSocketChannel.open();
72
}
73
74
public static void deleteFile(SocketAddress addr) throws Exception {
75
if (addr instanceof UnixDomainSocketAddress) {
76
Files.deleteIfExists(((UnixDomainSocketAddress) addr).getPath());
77
}
78
}
79
80
/*
81
The following, non-exhaustive set, of tests exercise different combinations
82
of blocking and non-blocking accept/connect calls along with I/O
83
operations, that exchange a single byte. The intent it to test a reasonable
84
set of blocking and non-blocking scenarios.
85
86
The individual test method names follow their test scenario.
87
[BAccep|SELNBAccep|SPINNBAccep] - Accept either:
88
blocking, select-non-blocking, spinning-non-blocking
89
[BConn|NBConn] - blocking connect / non-blocking connect
90
[BIO|NBIO] - blocking / non-blocking I/O operations (read/write)
91
[WR|RW] - connecting thread write/accepting thread reads first, and vice-versa
92
[Id] - unique test Id
93
94
BAccep_BConn_BIO_WR_1
95
BAccep_BConn_BIO_RW_2
96
SELNBAccep_BConn_BIO_WR_3
97
SELNBAccep_BConn_BIO_RW_4
98
SPINNBAccep_BConn_BIO_WR_5
99
SPINNBAccep_BConn_BIO_RW_6
100
BAccep_NBConn_BIO_WR_7
101
BAccep_NBConn_BIO_RW_8
102
SELNBAccep_NBConn_BIO_WR_9
103
SELNBAccep_NBConn_BIO_RW_10
104
SPINNBAccep_NBConn_BIO_WR_11
105
SPINNBAccep_NBConn_BIO_RW_12
106
107
BAccep_BConn_NBIO_WR_1a // Non-Blocking I/O
108
BAccep_BConn_NBIO_RW_2a
109
SELNBAccep_BConn_NBIO_WR_3a
110
SELNBAccep_BConn_NBIO_RW_4a
111
SPINNBAccep_BConn_NBIO_WR_5a
112
SPINNBAccep_BConn_NBIO_RW_6a
113
BAccep_NBConn_NBIO_WR_7a
114
BAccep_NBConn_NBIO_RW_8a
115
SELNBAccep_NBConn_NBIO_WR_9a
116
SELNBAccep_NBConn_NBIO_RW_10a
117
SPINBAccep_NBConn_NBIO_WR_11a
118
SPINBAccep_NBConn_NBIO_RW_12a
119
*/
120
121
@DataProvider(name = "family")
122
public Object[][] family() {
123
return unixDomainSupported ?
124
new Object[][] {
125
{ UNIX },
126
{ INET }}
127
: new Object[][] {
128
{ INET }
129
};
130
}
131
132
@Test(dataProvider = "family")
133
public void BAccep_BConn_BIO_WR_1(ProtocolFamily family)
134
throws Throwable {
135
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
136
ssc.bind(null);
137
SocketAddress addr = ssc.getLocalAddress();
138
139
TestThread t = TestThread.of("t1", () -> {
140
try (SocketChannel sc = openSocketChannel(family)) {
141
assertTrue(sc.connect(addr));
142
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x01).flip();
143
assertEquals(sc.write(bb), 1);
144
out.printf("wrote: 0x%x%n", bb.get(0));
145
assertEquals(sc.read(bb.clear()), -1);
146
}
147
});
148
t.start();
149
150
try (SocketChannel sc = ssc.accept()) {
151
ByteBuffer bb = ByteBuffer.allocate(10);
152
assertEquals(sc.read(bb), 1);
153
out.printf("read: 0x%x%n", bb.get(0));
154
assertEquals(bb.get(0), 0x01);
155
}
156
t.awaitCompletion();
157
deleteFile(addr);
158
}
159
}
160
161
@Test(dataProvider = "family")
162
public void BAccep_BConn_BIO_RW_2(ProtocolFamily family)
163
throws Throwable {
164
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
165
ssc.bind(null);
166
SocketAddress addr = ssc.getLocalAddress();
167
168
TestThread t = TestThread.of("t2", () -> {
169
try (SocketChannel sc = openSocketChannel(family)) {
170
assertTrue(sc.connect(addr));
171
ByteBuffer bb = ByteBuffer.allocate(10);
172
assertEquals(sc.read(bb), 1);
173
out.printf("read: 0x%x%n", bb.get(0));
174
assertEquals(bb.get(0), 0x02);
175
}
176
});
177
t.start();
178
179
try (SocketChannel sc = ssc.accept()) {
180
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x02).flip();
181
assertEquals(sc.write(bb), 1);
182
out.printf("wrote: 0x%x%n", bb.get(0));
183
assertEquals(sc.read(bb.clear()), -1);
184
}
185
t.awaitCompletion();
186
deleteFile(addr);
187
}
188
}
189
190
@Test(dataProvider = "family")
191
public void SELNBAccep_BConn_BIO_WR_3(ProtocolFamily family)
192
throws Throwable {
193
try (ServerSocketChannel ssc = openServerSocketChannel(family);
194
Selector selector = Selector.open()) {
195
ssc.bind(null);
196
SocketAddress addr = ssc.getLocalAddress();
197
198
TestThread t = TestThread.of("t3", () -> {
199
try (SocketChannel sc = openSocketChannel(family)) {
200
assertTrue(sc.connect(addr));
201
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x03).flip();
202
assertEquals(sc.write(bb), 1);
203
out.printf("wrote: 0x%x%n", bb.get(0));
204
assertEquals(sc.read(bb.clear()), -1);
205
}
206
});
207
t.start();
208
209
ssc.configureBlocking(false).register(selector, OP_ACCEPT);
210
assertEquals(selector.select(), 1);
211
212
try (SocketChannel sc = ssc.accept()) {
213
ByteBuffer bb = ByteBuffer.allocate(10);
214
assertEquals(sc.read(bb), 1);
215
out.printf("read: 0x%x%n", bb.get(0));
216
assertEquals(bb.get(0), 0x03);
217
}
218
t.awaitCompletion();
219
deleteFile(addr);
220
}
221
}
222
223
@Test(dataProvider = "family")
224
public void SELNBAccep_BConn_BIO_RW_4(ProtocolFamily family)
225
throws Throwable {
226
try (ServerSocketChannel ssc = openServerSocketChannel(family);
227
Selector selector = Selector.open()) {
228
ssc.bind(null);
229
SocketAddress addr = ssc.getLocalAddress();
230
231
TestThread t = TestThread.of("t4", () -> {
232
try (SocketChannel sc = openSocketChannel(family)) {
233
assertTrue(sc.connect(addr));
234
ByteBuffer bb = ByteBuffer.allocate(10);
235
assertEquals(sc.read(bb), 1);
236
out.printf("read: 0x%x%n", bb.get(0));
237
assertEquals(bb.get(0), 0x04);
238
}
239
});
240
t.start();
241
242
ssc.configureBlocking(false).register(selector, OP_ACCEPT);
243
assertEquals(selector.select(), 1);
244
245
try (SocketChannel sc = ssc.accept()) {
246
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x04).flip();
247
assertEquals(sc.write(bb), 1);
248
out.printf("wrote: 0x%x%n", bb.get(0));
249
assertEquals(sc.read(bb.clear()), -1);
250
251
}
252
t.awaitCompletion();
253
deleteFile(addr);
254
}
255
}
256
257
@Test(dataProvider = "family")
258
public void SPINNBAccep_BConn_BIO_WR_5(ProtocolFamily family)
259
throws Throwable {
260
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
261
ssc.bind(null);
262
SocketAddress addr = ssc.getLocalAddress();
263
264
TestThread t = TestThread.of("t5", () -> {
265
try (SocketChannel sc = openSocketChannel(family)) {
266
assertTrue(sc.connect(addr));
267
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x05).flip();
268
assertEquals(sc.write(bb), 1);
269
out.printf("wrote: 0x%x%n", bb.get(0));
270
assertEquals(sc.read(bb.clear()), -1);
271
}
272
});
273
t.start();
274
275
SocketChannel accepted;
276
for (; ; ) {
277
accepted = ssc.accept();
278
if (accepted != null) {
279
out.println("accepted new connection");
280
break;
281
}
282
Thread.onSpinWait();
283
}
284
285
try (SocketChannel sc = accepted) {
286
ByteBuffer bb = ByteBuffer.allocate(10);
287
assertEquals(sc.read(bb), 1);
288
out.printf("read: 0x%x%n", bb.get(0));
289
assertEquals(bb.get(0), 0x05);
290
}
291
t.awaitCompletion();
292
deleteFile(addr);
293
}
294
}
295
296
@Test(dataProvider = "family")
297
public void SPINNBAccep_BConn_BIO_RW_6(ProtocolFamily family)
298
throws Throwable {
299
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
300
ssc.bind(null);
301
SocketAddress addr = ssc.getLocalAddress();
302
303
TestThread t = TestThread.of("t6", () -> {
304
try (SocketChannel sc = openSocketChannel(family)) {
305
assertTrue(sc.connect(addr));
306
ByteBuffer bb = ByteBuffer.allocate(10);
307
assertEquals(sc.read(bb), 1);
308
out.printf("read: 0x%x%n", bb.get(0));
309
assertEquals(bb.get(0), 0x06);
310
}
311
});
312
t.start();
313
314
SocketChannel accepted;
315
for (; ; ) {
316
accepted = ssc.accept();
317
if (accepted != null) {
318
out.println("accepted new connection");
319
break;
320
}
321
Thread.onSpinWait();
322
}
323
324
try (SocketChannel sc = accepted) {
325
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x06).flip();
326
assertEquals(sc.write(bb), 1);
327
out.printf("wrote: 0x%x%n", bb.get(0));
328
assertEquals(sc.read(bb.clear()), -1);
329
330
}
331
t.awaitCompletion();
332
deleteFile(addr);
333
}
334
}
335
336
// Similar to the previous six scenarios, but with same-thread
337
// non-blocking connect.
338
339
@Test(dataProvider = "family")
340
public void BAccep_NBConn_BIO_WR_7(ProtocolFamily family)
341
throws Throwable {
342
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
343
ssc.bind(null);
344
SocketAddress addr = ssc.getLocalAddress();
345
346
try (SocketChannel sc = openSocketChannel(family)) {
347
sc.configureBlocking(false);
348
sc.connect(addr);
349
350
try (SocketChannel sc2 = ssc.accept()) {
351
assertTrue(sc.finishConnect());
352
sc.configureBlocking(true);
353
TestThread t = TestThread.of("t7", () -> {
354
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x07).flip();
355
assertEquals(sc.write(bb), 1);
356
out.printf("wrote: 0x%x%n", bb.get(0));
357
assertEquals(sc.read(bb.clear()), -1);
358
});
359
t.start();
360
361
ByteBuffer bb = ByteBuffer.allocate(10);
362
assertEquals(sc2.read(bb), 1);
363
out.printf("read: 0x%x%n", bb.get(0));
364
assertEquals(bb.get(0), 0x07);
365
sc2.shutdownOutput();
366
t.awaitCompletion();
367
}
368
}
369
deleteFile(addr);
370
}
371
}
372
373
@Test(dataProvider = "family")
374
public void BAccep_NBConn_BIO_RW_8(ProtocolFamily family)
375
throws Throwable {
376
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
377
ssc.bind(null);
378
SocketAddress addr = ssc.getLocalAddress();
379
380
try (SocketChannel sc = openSocketChannel(family)) {
381
sc.configureBlocking(false);
382
sc.connect(addr);
383
384
try (SocketChannel sc2 = ssc.accept()) {
385
assertTrue(sc.finishConnect());
386
sc.configureBlocking(true);
387
TestThread t = TestThread.of("t8", () -> {
388
ByteBuffer bb = ByteBuffer.allocate(10);
389
assertEquals(sc.read(bb), 1);
390
out.printf("read: 0x%x%n", bb.get(0));
391
assertEquals(bb.get(0), 0x08);
392
sc.shutdownOutput();
393
});
394
t.start();
395
396
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x08).flip();
397
assertEquals(sc2.write(bb), 1);
398
out.printf("wrote: 0x%x%n", bb.get(0));
399
assertEquals(sc2.read(bb.clear()), -1);
400
t.awaitCompletion();
401
}
402
}
403
deleteFile(addr);
404
}
405
}
406
407
@Test(dataProvider = "family")
408
public void SELNBAccep_NBConn_BIO_WR_9(ProtocolFamily family)
409
throws Throwable {
410
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
411
ssc.bind(null);
412
SocketAddress addr = ssc.getLocalAddress();
413
414
try (SocketChannel sc = openSocketChannel(family);
415
Selector selector = Selector.open()) {
416
sc.configureBlocking(false);
417
sc.connect(addr);
418
419
ssc.configureBlocking(false).register(selector, OP_ACCEPT);
420
assertEquals(selector.select(), 1);
421
422
try (SocketChannel sc2 = ssc.accept()) {
423
assertTrue(sc.finishConnect());
424
sc.configureBlocking(true);
425
TestThread t = TestThread.of("t9", () -> {
426
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x09).flip();
427
assertEquals(sc.write(bb), 1);
428
out.printf("wrote: 0x%x%n", bb.get(0));
429
assertEquals(sc.read(bb.clear()), -1);
430
});
431
t.start();
432
433
ByteBuffer bb = ByteBuffer.allocate(10);
434
assertEquals(sc2.read(bb), 1);
435
out.printf("read: 0x%x%n", bb.get(0));
436
assertEquals(bb.get(0), 0x09);
437
sc2.shutdownOutput();
438
t.awaitCompletion();
439
}
440
}
441
deleteFile(addr);
442
}
443
}
444
445
@Test(dataProvider = "family")
446
public void SELNBAccep_NBConn_BIO_RW_10(ProtocolFamily family)
447
throws Throwable {
448
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
449
ssc.bind(null);
450
SocketAddress addr = ssc.getLocalAddress();
451
452
try (SocketChannel sc = openSocketChannel(family);
453
Selector selector = Selector.open()) {
454
sc.configureBlocking(false);
455
sc.connect(addr);
456
457
ssc.configureBlocking(false).register(selector, OP_ACCEPT);
458
assertEquals(selector.select(), 1);
459
460
try (SocketChannel sc2 = ssc.accept()) {
461
assertTrue(sc.finishConnect());
462
sc.configureBlocking(true);
463
TestThread t = TestThread.of("t10", () -> {
464
ByteBuffer bb = ByteBuffer.allocate(10);
465
assertEquals(sc.read(bb), 1);
466
out.printf("read: 0x%x%n", bb.get(0));
467
assertEquals(bb.get(0), 0x10);
468
sc.shutdownOutput();
469
});
470
t.start();
471
472
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x10).flip();
473
assertEquals(sc2.write(bb), 1);
474
out.printf("wrote: 0x%x%n", bb.get(0));
475
assertEquals(sc2.read(bb.clear()), -1);
476
t.awaitCompletion();
477
}
478
}
479
deleteFile(addr);
480
}
481
}
482
483
@Test(dataProvider = "family")
484
public void SPINNBAccep_NBConn_BIO_WR_11(ProtocolFamily family)
485
throws Throwable {
486
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
487
ssc.bind(null);
488
SocketAddress addr = ssc.getLocalAddress();
489
490
try (SocketChannel sc = openSocketChannel(family)) {
491
sc.configureBlocking(false);
492
sc.connect(addr);
493
494
SocketChannel accepted;
495
for (; ; ) {
496
accepted = ssc.accept();
497
if (accepted != null) {
498
out.println("accepted new connection");
499
break;
500
}
501
Thread.onSpinWait();
502
}
503
504
try (SocketChannel sc2 = accepted) {
505
assertTrue(sc.finishConnect());
506
sc.configureBlocking(true);
507
TestThread t = TestThread.of("t11", () -> {
508
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x11).flip();
509
assertEquals(sc.write(bb), 1);
510
out.printf("wrote: 0x%x%n", bb.get(0));
511
assertEquals(sc.read(bb.clear()), -1);
512
});
513
t.start();
514
515
ByteBuffer bb = ByteBuffer.allocate(10);
516
assertEquals(sc2.read(bb), 1);
517
out.printf("read: 0x%x%n", bb.get(0));
518
assertEquals(bb.get(0), 0x11);
519
sc2.shutdownOutput();
520
t.awaitCompletion();
521
}
522
}
523
deleteFile(addr);
524
}
525
}
526
527
@Test(dataProvider = "family")
528
public void SPINNBAccep_NBConn_BIO_RW_12(ProtocolFamily family)
529
throws Throwable {
530
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
531
ssc.bind(null);
532
SocketAddress addr = ssc.getLocalAddress();
533
534
try (SocketChannel sc = openSocketChannel(family)) {
535
sc.configureBlocking(false);
536
sc.connect(addr);
537
538
SocketChannel accepted;
539
for (; ; ) {
540
accepted = ssc.accept();
541
if (accepted != null) {
542
out.println("accepted new connection");
543
break;
544
}
545
Thread.onSpinWait();
546
}
547
548
try (SocketChannel sc2 = accepted) {
549
assertTrue(sc.finishConnect());
550
sc.configureBlocking(true);
551
TestThread t = TestThread.of("t12", () -> {
552
ByteBuffer bb = ByteBuffer.allocate(10);
553
assertEquals(sc.read(bb), 1);
554
out.printf("read: 0x%x%n", bb.get(0));
555
assertEquals(bb.get(0), 0x12);
556
sc.shutdownOutput();
557
});
558
t.start();
559
560
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x12).flip();
561
assertEquals(sc2.write(bb), 1);
562
out.printf("wrote: 0x%x%n", bb.get(0));
563
assertEquals(sc2.read(bb.clear()), -1);
564
t.awaitCompletion();
565
}
566
}
567
deleteFile(addr);
568
}
569
}
570
571
// ---
572
// Similar to the previous twelve scenarios but with non-blocking IO
573
// ---
574
575
@Test(dataProvider = "family")
576
public void BAccep_BConn_NBIO_WR_1a(ProtocolFamily family)
577
throws Throwable {
578
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
579
ssc.bind(null);
580
SocketAddress addr = ssc.getLocalAddress();
581
582
TestThread t = TestThread.of("t1a", () -> {
583
try (SocketChannel sc = openSocketChannel(family);
584
Selector selector = Selector.open()) {
585
assertTrue(sc.connect(addr));
586
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x1A).flip();
587
sc.configureBlocking(false);
588
SelectionKey k = sc.register(selector, OP_WRITE);
589
selector.select();
590
int c;
591
while ((c = sc.write(bb)) < 1) ;
592
assertEquals(c, 1);
593
out.printf("wrote: 0x%x%n", bb.get(0));
594
k.interestOps(OP_READ);
595
selector.select();
596
bb.clear();
597
while ((c = sc.read(bb)) == 0) ;
598
assertEquals(c, -1);
599
}
600
});
601
t.start();
602
603
try (SocketChannel sc = ssc.accept();
604
Selector selector = Selector.open()) {
605
ByteBuffer bb = ByteBuffer.allocate(10);
606
sc.configureBlocking(false);
607
sc.register(selector, OP_READ);
608
selector.select();
609
int c;
610
while ((c = sc.read(bb)) == 0) ;
611
assertEquals(c, 1);
612
out.printf("read: 0x%x%n", bb.get(0));
613
assertEquals(bb.get(0), 0x1A);
614
}
615
t.awaitCompletion();
616
deleteFile(addr);
617
}
618
}
619
620
@Test(dataProvider = "family")
621
public void BAccep_BConn_NBIO_RW_2a(ProtocolFamily family)
622
throws Throwable {
623
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
624
ssc.bind(null);
625
SocketAddress addr = ssc.getLocalAddress();
626
627
TestThread t = TestThread.of("t2a", () -> {
628
try (SocketChannel sc = openSocketChannel(family);
629
Selector selector = Selector.open()) {
630
assertTrue(sc.connect(addr));
631
ByteBuffer bb = ByteBuffer.allocate(10);
632
sc.configureBlocking(false);
633
sc.register(selector, OP_READ);
634
selector.select();
635
int c;
636
while ((c = sc.read(bb)) == 0) ;
637
assertEquals(c, 1);
638
out.printf("read: 0x%x%n", bb.get(0));
639
assertEquals(bb.get(0), 0x2A);
640
}
641
});
642
t.start();
643
644
try (SocketChannel sc = ssc.accept();
645
Selector selector = Selector.open()) {
646
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x2A).flip();
647
sc.configureBlocking(false);
648
SelectionKey k = sc.register(selector, OP_WRITE);
649
selector.select();
650
int c;
651
while ((c = sc.write(bb)) < 1) ;
652
assertEquals(c, 1);
653
out.printf("wrote: 0x%x%n", bb.get(0));
654
k.interestOps(OP_READ);
655
selector.select();
656
bb.clear();
657
while ((c = sc.read(bb)) == 0) ;
658
assertEquals(c, -1);
659
}
660
t.awaitCompletion();
661
deleteFile(addr);
662
}
663
}
664
665
@Test(dataProvider = "family")
666
public void SELNBAccep_BConn_NBIO_WR_3a(ProtocolFamily family)
667
throws Throwable {
668
try (ServerSocketChannel ssc = openServerSocketChannel(family);
669
Selector aselector = Selector.open()) {
670
ssc.bind(null);
671
SocketAddress addr = ssc.getLocalAddress();
672
673
TestThread t = TestThread.of("t3a", () -> {
674
try (SocketChannel sc = openSocketChannel(family);
675
Selector selector = Selector.open()) {
676
assertTrue(sc.connect(addr));
677
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x3A).flip();
678
sc.configureBlocking(false);
679
SelectionKey k = sc.register(selector, OP_WRITE);
680
selector.select();
681
int c;
682
while ((c = sc.write(bb)) < 1) ;
683
assertEquals(c, 1);
684
out.printf("wrote: 0x%x%n", bb.get(0));
685
k.interestOps(OP_READ);
686
selector.select();
687
bb.clear();
688
while ((c = sc.read(bb)) == 0) ;
689
assertEquals(c, -1);
690
}
691
});
692
t.start();
693
694
ssc.configureBlocking(false).register(aselector, OP_ACCEPT);
695
assertEquals(aselector.select(), 1);
696
697
try (SocketChannel sc = ssc.accept();
698
Selector selector = Selector.open()) {
699
ByteBuffer bb = ByteBuffer.allocate(10);
700
sc.configureBlocking(false);
701
sc.register(selector, OP_READ);
702
selector.select();
703
int c;
704
while ((c = sc.read(bb)) == 0) ;
705
assertEquals(c, 1);
706
out.printf("read: 0x%x%n", bb.get(0));
707
assertEquals(bb.get(0), 0x3A);
708
}
709
t.awaitCompletion();
710
deleteFile(addr);
711
}
712
}
713
714
@Test(dataProvider = "family")
715
public void SELNBAccep_BConn_NBIO_RW_4a(ProtocolFamily family)
716
throws Throwable {
717
try (ServerSocketChannel ssc = openServerSocketChannel(family);
718
Selector aselector = Selector.open()) {
719
ssc.bind(null);
720
SocketAddress addr = ssc.getLocalAddress();
721
722
TestThread t = TestThread.of("t4a", () -> {
723
try (SocketChannel sc = openSocketChannel(family);
724
Selector selector = Selector.open()) {
725
assertTrue(sc.connect(addr));
726
ByteBuffer bb = ByteBuffer.allocate(10);
727
sc.configureBlocking(false);
728
sc.register(selector, OP_READ);
729
selector.select();
730
int c;
731
while ((c = sc.read(bb)) == 0) ;
732
assertEquals(c, 1);
733
out.printf("read: 0x%x%n", bb.get(0));
734
assertEquals(bb.get(0), 0x4A);
735
}
736
});
737
t.start();
738
739
ssc.configureBlocking(false).register(aselector, OP_ACCEPT);
740
assertEquals(aselector.select(), 1);
741
742
try (SocketChannel sc = ssc.accept();
743
Selector selector = Selector.open()) {
744
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x4A).flip();
745
sc.configureBlocking(false);
746
SelectionKey k = sc.register(selector, OP_WRITE);
747
selector.select();
748
int c;
749
while ((c = sc.write(bb)) < 1) ;
750
assertEquals(c, 1);
751
out.printf("wrote: 0x%x%n", bb.get(0));
752
k.interestOps(OP_READ);
753
selector.select();
754
bb.clear();
755
while ((c = sc.read(bb)) == 0) ;
756
assertEquals(c, -1);
757
}
758
t.awaitCompletion();
759
deleteFile(addr);
760
}
761
}
762
763
@Test(dataProvider = "family")
764
public void SPINNBAccep_BConn_NBIO_WR_5a(ProtocolFamily family)
765
throws Throwable {
766
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
767
ssc.bind(null);
768
SocketAddress addr = ssc.getLocalAddress();
769
770
TestThread t = TestThread.of("t5a", () -> {
771
try (SocketChannel sc = openSocketChannel(family);
772
Selector selector = Selector.open()) {
773
assertTrue(sc.connect(addr));
774
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x5A).flip();
775
sc.configureBlocking(false);
776
SelectionKey k = sc.register(selector, OP_WRITE);
777
selector.select();
778
int c;
779
while ((c = sc.write(bb)) < 1) ;
780
assertEquals(c, 1);
781
out.printf("wrote: 0x%x%n", bb.get(0));
782
k.interestOps(OP_READ);
783
selector.select();
784
bb.clear();
785
while ((c = sc.read(bb)) == 0) ;
786
assertEquals(c, -1);
787
}
788
});
789
t.start();
790
791
SocketChannel accepted;
792
for (; ; ) {
793
accepted = ssc.accept();
794
if (accepted != null) {
795
out.println("accepted new connection");
796
break;
797
}
798
Thread.onSpinWait();
799
}
800
801
try (SocketChannel sc = accepted;
802
Selector selector = Selector.open()) {
803
ByteBuffer bb = ByteBuffer.allocate(10);
804
sc.configureBlocking(false);
805
sc.register(selector, OP_READ);
806
selector.select();
807
int c;
808
while ((c = sc.read(bb)) == 0) ;
809
assertEquals(c, 1);
810
out.printf("read: 0x%x%n", bb.get(0));
811
assertEquals(bb.get(0), 0x5A);
812
}
813
t.awaitCompletion();
814
deleteFile(addr);
815
}
816
}
817
818
@Test(dataProvider = "family")
819
public void SPINNBAccep_BConn_NBIO_RW_6a(ProtocolFamily family)
820
throws Throwable {
821
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
822
ssc.bind(null);
823
SocketAddress addr = ssc.getLocalAddress();
824
825
TestThread t = TestThread.of("t6a", () -> {
826
try (SocketChannel sc = openSocketChannel(family);
827
Selector selector = Selector.open()) {
828
assertTrue(sc.connect(addr));
829
ByteBuffer bb = ByteBuffer.allocate(10);
830
sc.configureBlocking(false);
831
sc.register(selector, OP_READ);
832
selector.select();
833
int c;
834
while ((c = sc.read(bb)) == 0) ;
835
assertEquals(c, 1);
836
out.printf("read: 0x%x%n", bb.get(0));
837
assertEquals(bb.get(0), 0x6A);
838
}
839
});
840
t.start();
841
842
SocketChannel accepted;
843
for (; ; ) {
844
accepted = ssc.accept();
845
if (accepted != null) {
846
out.println("accepted new connection");
847
break;
848
}
849
Thread.onSpinWait();
850
}
851
852
try (SocketChannel sc = accepted;
853
Selector selector = Selector.open()) {
854
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x6A).flip();
855
sc.configureBlocking(false);
856
SelectionKey k = sc.register(selector, OP_WRITE);
857
selector.select();
858
int c;
859
while ((c = sc.write(bb)) < 1) ;
860
assertEquals(c, 1);
861
out.printf("wrote: 0x%x%n", bb.get(0));
862
k.interestOps(OP_READ);
863
selector.select();
864
bb.clear();
865
while ((c = sc.read(bb)) == 0) ;
866
assertEquals(c, -1);
867
868
}
869
t.awaitCompletion();
870
deleteFile(addr);
871
}
872
}
873
874
// Similar to the previous six scenarios but with same-thread
875
// non-blocking connect.
876
877
@Test(dataProvider = "family")
878
public void BAccep_NBConn_NBIO_WR_7a(ProtocolFamily family)
879
throws Throwable {
880
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
881
ssc.bind(null);
882
SocketAddress addr = ssc.getLocalAddress();
883
884
try (SocketChannel sc = openSocketChannel(family)) {
885
sc.configureBlocking(false);
886
sc.connect(addr);
887
888
try (SocketChannel sc2 = ssc.accept()) {
889
assertTrue(sc.finishConnect());
890
TestThread t = TestThread.of("t7a", () -> {
891
try (Selector selector = Selector.open()) {
892
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x7A).flip();
893
sc.configureBlocking(false);
894
SelectionKey k = sc.register(selector, OP_WRITE);
895
selector.select();
896
int c;
897
while ((c = sc.write(bb)) < 1) ;
898
assertEquals(c, 1);
899
out.printf("wrote: 0x%x%n", bb.get(0));
900
k.interestOps(OP_READ);
901
selector.select();
902
bb.clear();
903
while ((c = sc.read(bb)) == 0) ;
904
assertEquals(c, -1);
905
}
906
});
907
t.start();
908
909
ByteBuffer bb = ByteBuffer.allocate(10);
910
sc2.configureBlocking(false);
911
try (Selector selector = Selector.open()) {
912
sc2.register(selector, OP_READ);
913
selector.select();
914
int c;
915
while ((c = sc2.read(bb)) == 0) ;
916
assertEquals(c, 1);
917
out.printf("read: 0x%x%n", bb.get(0));
918
assertEquals(bb.get(0), 0x7A);
919
sc2.shutdownOutput();
920
}
921
t.awaitCompletion();
922
}
923
}
924
deleteFile(addr);
925
}
926
}
927
928
@Test(dataProvider = "family")
929
public void BAccep_NBConn_NBIO_RW_8a(ProtocolFamily family)
930
throws Throwable {
931
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
932
ssc.bind(null);
933
SocketAddress addr = ssc.getLocalAddress();
934
935
try (SocketChannel sc = openSocketChannel(family)) {
936
sc.configureBlocking(false);
937
sc.connect(addr);
938
939
try (SocketChannel sc2 = ssc.accept()) {
940
assertTrue(sc.finishConnect());
941
TestThread t = TestThread.of("t8a", () -> {
942
try (Selector selector = Selector.open()) {
943
ByteBuffer bb = ByteBuffer.allocate(10);
944
sc.register(selector, OP_READ);
945
selector.select();
946
int c;
947
while ((c = sc.read(bb)) == 0) ;
948
assertEquals(c, 1);
949
out.printf("read: 0x%x%n", bb.get(0));
950
assertEquals(bb.get(0), (byte) 0x8A);
951
sc.shutdownOutput();
952
}
953
});
954
t.start();
955
956
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x8A).flip();
957
sc2.configureBlocking(false);
958
try (Selector selector = Selector.open()) {
959
SelectionKey k = sc2.register(selector, OP_WRITE);
960
selector.select();
961
int c;
962
while ((c = sc2.write(bb)) < 1) ;
963
assertEquals(c, 1);
964
out.printf("wrote: 0x%x%n", bb.get(0));
965
k.interestOps(OP_READ);
966
selector.select();
967
bb.clear();
968
while ((c = sc2.read(bb)) == 0) ;
969
assertEquals(c, -1);
970
}
971
t.awaitCompletion();
972
}
973
}
974
deleteFile(addr);
975
}
976
}
977
978
@Test(dataProvider = "family")
979
public void SELNBAccep_NBConn_NBIO_WR_9a(ProtocolFamily family)
980
throws Throwable {
981
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
982
ssc.bind(null);
983
SocketAddress addr = ssc.getLocalAddress();
984
985
try (SocketChannel sc = openSocketChannel(family)) {
986
sc.configureBlocking(false);
987
sc.connect(addr);
988
989
Selector aselector = Selector.open();
990
ssc.configureBlocking(false).register(aselector, OP_ACCEPT);
991
assertEquals(aselector.select(), 1);
992
993
try (SocketChannel sc2 = ssc.accept()) {
994
assertTrue(sc.finishConnect());
995
TestThread t = TestThread.of("t9a", () -> {
996
try (Selector selector = Selector.open()) {
997
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x9A).flip();
998
sc.configureBlocking(false);
999
SelectionKey k = sc.register(selector, OP_WRITE);
1000
selector.select();
1001
int c;
1002
while ((c = sc.write(bb)) < 1) ;
1003
assertEquals(c, 1);
1004
out.printf("wrote: 0x%x%n", bb.get(0));
1005
k.interestOps(OP_READ);
1006
selector.select();
1007
bb.clear();
1008
while ((c = sc.read(bb)) == 0) ;
1009
assertEquals(c, -1);
1010
}
1011
});
1012
t.start();
1013
1014
ByteBuffer bb = ByteBuffer.allocate(10);
1015
sc2.configureBlocking(false);
1016
try (Selector selector = Selector.open()) {
1017
sc2.register(selector, OP_READ);
1018
selector.select();
1019
int c;
1020
while ((c = sc2.read(bb)) == 0) ;
1021
assertEquals(c, 1);
1022
out.printf("read: 0x%x%n", bb.get(0));
1023
assertEquals(bb.get(0), (byte) 0x9A);
1024
sc2.shutdownOutput();
1025
}
1026
t.awaitCompletion();
1027
}
1028
}
1029
deleteFile(addr);
1030
}
1031
}
1032
1033
@Test(dataProvider = "family")
1034
public void SELNBAccep_NBConn_NBIO_RW_10a(ProtocolFamily family)
1035
throws Throwable {
1036
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
1037
ssc.bind(null);
1038
SocketAddress addr = ssc.getLocalAddress();
1039
1040
try (SocketChannel sc = openSocketChannel(family)) {
1041
sc.configureBlocking(false);
1042
sc.connect(addr);
1043
1044
Selector aselector = Selector.open();
1045
ssc.configureBlocking(false).register(aselector, OP_ACCEPT);
1046
assertEquals(aselector.select(), 1);
1047
1048
try (SocketChannel sc2 = ssc.accept()) {
1049
assertTrue(sc.finishConnect());
1050
TestThread t = TestThread.of("t10a", () -> {
1051
try (Selector selector = Selector.open()) {
1052
ByteBuffer bb = ByteBuffer.allocate(10);
1053
sc.register(selector, OP_READ);
1054
selector.select();
1055
int c;
1056
while ((c = sc.read(bb)) == 0) ;
1057
assertEquals(c, 1);
1058
out.printf("read: 0x%x%n", bb.get(0));
1059
assertEquals(bb.get(0), (byte) 0xAA);
1060
sc.shutdownOutput();
1061
}
1062
});
1063
t.start();
1064
1065
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0xAA).flip();
1066
sc2.configureBlocking(false);
1067
try (Selector selector = Selector.open()) {
1068
SelectionKey k = sc2.register(selector, OP_WRITE);
1069
selector.select();
1070
int c;
1071
while ((c = sc2.write(bb)) < 1) ;
1072
assertEquals(c, 1);
1073
out.printf("wrote: 0x%x%n", bb.get(0));
1074
k.interestOps(OP_READ);
1075
selector.select();
1076
bb.clear();
1077
while ((c = sc2.read(bb)) == 0) ;
1078
assertEquals(c, -1);
1079
}
1080
t.awaitCompletion();
1081
}
1082
}
1083
deleteFile(addr);
1084
}
1085
}
1086
1087
@Test(dataProvider = "family")
1088
public void SPINBAccep_NBConn_NBIO_WR_11a(ProtocolFamily family)
1089
throws Throwable {
1090
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
1091
ssc.bind(null);
1092
SocketAddress addr = ssc.getLocalAddress();
1093
1094
try (SocketChannel sc = openSocketChannel(family)) {
1095
sc.configureBlocking(false);
1096
sc.connect(addr);
1097
1098
SocketChannel accepted;
1099
for (; ; ) {
1100
accepted = ssc.accept();
1101
if (accepted != null) {
1102
out.println("accepted new connection");
1103
break;
1104
}
1105
Thread.onSpinWait();
1106
}
1107
1108
try (SocketChannel sc2 = accepted) {
1109
assertTrue(sc.finishConnect());
1110
TestThread t = TestThread.of("t11a", () -> {
1111
try (Selector selector = Selector.open()) {
1112
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0xBA).flip();
1113
sc.configureBlocking(false);
1114
SelectionKey k = sc.register(selector, OP_WRITE);
1115
selector.select();
1116
int c;
1117
while ((c = sc.write(bb)) < 1) ;
1118
assertEquals(c, 1);
1119
out.printf("wrote: 0x%x%n", bb.get(0));
1120
k.interestOps(OP_READ);
1121
selector.select();
1122
bb.clear();
1123
while ((c = sc.read(bb)) == 0) ;
1124
assertEquals(c, -1);
1125
}
1126
});
1127
t.start();
1128
1129
ByteBuffer bb = ByteBuffer.allocate(10);
1130
sc2.configureBlocking(false);
1131
try (Selector selector = Selector.open()) {
1132
sc2.register(selector, OP_READ);
1133
selector.select();
1134
int c;
1135
while ((c = sc2.read(bb)) == 0) ;
1136
assertEquals(c, 1);
1137
out.printf("read: 0x%x%n", bb.get(0));
1138
assertEquals(bb.get(0), (byte) 0xBA);
1139
sc2.shutdownOutput();
1140
}
1141
t.awaitCompletion();
1142
}
1143
}
1144
deleteFile(addr);
1145
}
1146
}
1147
1148
@Test(dataProvider = "family")
1149
public void SPINBAccep_NBConn_NBIO_RW_12a(ProtocolFamily family)
1150
throws Throwable {
1151
try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
1152
ssc.bind(null);
1153
SocketAddress addr = ssc.getLocalAddress();
1154
1155
try (SocketChannel sc = openSocketChannel(family)) {
1156
sc.configureBlocking(false);
1157
sc.connect(addr);
1158
1159
SocketChannel accepted;
1160
for (; ; ) {
1161
accepted = ssc.accept();
1162
if (accepted != null) {
1163
out.println("accepted new connection");
1164
break;
1165
}
1166
Thread.onSpinWait();
1167
}
1168
1169
try (SocketChannel sc2 = accepted) {
1170
assertTrue(sc.finishConnect());
1171
TestThread t = TestThread.of("t10a", () -> {
1172
try (Selector selector = Selector.open()) {
1173
ByteBuffer bb = ByteBuffer.allocate(10);
1174
sc.register(selector, OP_READ);
1175
selector.select();
1176
int c;
1177
while ((c = sc.read(bb)) == 0) ;
1178
assertEquals(c, 1);
1179
out.printf("read: 0x%x%n", bb.get(0));
1180
assertEquals(bb.get(0), (byte) 0xCA);
1181
sc.shutdownOutput();
1182
}
1183
});
1184
t.start();
1185
1186
ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0xCA).flip();
1187
sc2.configureBlocking(false);
1188
try (Selector selector = Selector.open()) {
1189
SelectionKey k = sc2.register(selector, OP_WRITE);
1190
selector.select();
1191
int c;
1192
while ((c = sc2.write(bb)) < 1) ;
1193
assertEquals(c, 1);
1194
out.printf("wrote: 0x%x%n", bb.get(0));
1195
k.interestOps(OP_READ);
1196
selector.select();
1197
bb.clear();
1198
while ((c = sc2.read(bb)) == 0) ;
1199
assertEquals(c, -1);
1200
}
1201
t.awaitCompletion();
1202
}
1203
}
1204
deleteFile(addr);
1205
}
1206
}
1207
1208
// --
1209
1210
static class TestThread extends Thread {
1211
private final UncheckedRunnable runnable;
1212
private volatile Throwable throwable;
1213
1214
TestThread(UncheckedRunnable runnable, String name) {
1215
super(name);
1216
this.runnable = runnable;
1217
}
1218
1219
@Override
1220
public void run() {
1221
try {
1222
runnable.run();
1223
} catch (Throwable t) {
1224
out.printf("[%s] caught unexpected: %s%n", getName(), t);
1225
throwable = t;
1226
}
1227
}
1228
1229
interface UncheckedRunnable {
1230
void run() throws Throwable;
1231
}
1232
1233
static TestThread of(String name, UncheckedRunnable runnable) {
1234
return new TestThread(runnable, name);
1235
}
1236
1237
void awaitCompletion() throws Throwable {
1238
this.join();
1239
if (throwable != null)
1240
throw throwable;
1241
}
1242
}
1243
}
1244
1245