Path: blob/master/test/jdk/java/nio/channels/unixdomain/IOExchanges.java
41153 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 824519426* @run testng/othervm IOExchanges27*/2829import java.io.IOException;30import java.net.*;31import java.nio.channels.*;32import java.nio.ByteBuffer;33import java.nio.file.Files;3435import org.testng.annotations.BeforeTest;36import org.testng.annotations.DataProvider;37import org.testng.annotations.Test;3839import static java.lang.System.out;40import static java.net.StandardProtocolFamily.*;41import static java.nio.channels.SelectionKey.OP_ACCEPT;42import static java.nio.channels.SelectionKey.OP_READ;43import static java.nio.channels.SelectionKey.OP_WRITE;44import static org.testng.Assert.assertEquals;45import static org.testng.Assert.assertTrue;4647public class IOExchanges {48static boolean unixDomainSupported = true;495051@BeforeTest()52public void setup() {53try {54SocketChannel.open(UNIX);55} catch (IOException | UnsupportedOperationException e) {56unixDomainSupported = false;57out.println("Unix domain channels not supported");58}59}6061static SocketChannel openSocketChannel(ProtocolFamily family)62throws IOException {63return family == UNIX ? SocketChannel.open(family)64: SocketChannel.open();65}6667static ServerSocketChannel openServerSocketChannel(ProtocolFamily family)68throws IOException {69return family == UNIX ? ServerSocketChannel.open(family)70: ServerSocketChannel.open();71}7273public static void deleteFile(SocketAddress addr) throws Exception {74if (addr instanceof UnixDomainSocketAddress) {75Files.deleteIfExists(((UnixDomainSocketAddress) addr).getPath());76}77}7879/*80The following, non-exhaustive set, of tests exercise different combinations81of blocking and non-blocking accept/connect calls along with I/O82operations, that exchange a single byte. The intent it to test a reasonable83set of blocking and non-blocking scenarios.8485The individual test method names follow their test scenario.86[BAccep|SELNBAccep|SPINNBAccep] - Accept either:87blocking, select-non-blocking, spinning-non-blocking88[BConn|NBConn] - blocking connect / non-blocking connect89[BIO|NBIO] - blocking / non-blocking I/O operations (read/write)90[WR|RW] - connecting thread write/accepting thread reads first, and vice-versa91[Id] - unique test Id9293BAccep_BConn_BIO_WR_194BAccep_BConn_BIO_RW_295SELNBAccep_BConn_BIO_WR_396SELNBAccep_BConn_BIO_RW_497SPINNBAccep_BConn_BIO_WR_598SPINNBAccep_BConn_BIO_RW_699BAccep_NBConn_BIO_WR_7100BAccep_NBConn_BIO_RW_8101SELNBAccep_NBConn_BIO_WR_9102SELNBAccep_NBConn_BIO_RW_10103SPINNBAccep_NBConn_BIO_WR_11104SPINNBAccep_NBConn_BIO_RW_12105106BAccep_BConn_NBIO_WR_1a // Non-Blocking I/O107BAccep_BConn_NBIO_RW_2a108SELNBAccep_BConn_NBIO_WR_3a109SELNBAccep_BConn_NBIO_RW_4a110SPINNBAccep_BConn_NBIO_WR_5a111SPINNBAccep_BConn_NBIO_RW_6a112BAccep_NBConn_NBIO_WR_7a113BAccep_NBConn_NBIO_RW_8a114SELNBAccep_NBConn_NBIO_WR_9a115SELNBAccep_NBConn_NBIO_RW_10a116SPINBAccep_NBConn_NBIO_WR_11a117SPINBAccep_NBConn_NBIO_RW_12a118*/119120@DataProvider(name = "family")121public Object[][] family() {122return unixDomainSupported ?123new Object[][] {124{ UNIX },125{ INET }}126: new Object[][] {127{ INET }128};129}130131@Test(dataProvider = "family")132public void BAccep_BConn_BIO_WR_1(ProtocolFamily family)133throws Throwable {134try (ServerSocketChannel ssc = openServerSocketChannel(family)) {135ssc.bind(null);136SocketAddress addr = ssc.getLocalAddress();137138TestThread t = TestThread.of("t1", () -> {139try (SocketChannel sc = openSocketChannel(family)) {140assertTrue(sc.connect(addr));141ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x01).flip();142assertEquals(sc.write(bb), 1);143out.printf("wrote: 0x%x%n", bb.get(0));144assertEquals(sc.read(bb.clear()), -1);145}146});147t.start();148149try (SocketChannel sc = ssc.accept()) {150ByteBuffer bb = ByteBuffer.allocate(10);151assertEquals(sc.read(bb), 1);152out.printf("read: 0x%x%n", bb.get(0));153assertEquals(bb.get(0), 0x01);154}155t.awaitCompletion();156deleteFile(addr);157}158}159160@Test(dataProvider = "family")161public void BAccep_BConn_BIO_RW_2(ProtocolFamily family)162throws Throwable {163try (ServerSocketChannel ssc = openServerSocketChannel(family)) {164ssc.bind(null);165SocketAddress addr = ssc.getLocalAddress();166167TestThread t = TestThread.of("t2", () -> {168try (SocketChannel sc = openSocketChannel(family)) {169assertTrue(sc.connect(addr));170ByteBuffer bb = ByteBuffer.allocate(10);171assertEquals(sc.read(bb), 1);172out.printf("read: 0x%x%n", bb.get(0));173assertEquals(bb.get(0), 0x02);174}175});176t.start();177178try (SocketChannel sc = ssc.accept()) {179ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x02).flip();180assertEquals(sc.write(bb), 1);181out.printf("wrote: 0x%x%n", bb.get(0));182assertEquals(sc.read(bb.clear()), -1);183}184t.awaitCompletion();185deleteFile(addr);186}187}188189@Test(dataProvider = "family")190public void SELNBAccep_BConn_BIO_WR_3(ProtocolFamily family)191throws Throwable {192try (ServerSocketChannel ssc = openServerSocketChannel(family);193Selector selector = Selector.open()) {194ssc.bind(null);195SocketAddress addr = ssc.getLocalAddress();196197TestThread t = TestThread.of("t3", () -> {198try (SocketChannel sc = openSocketChannel(family)) {199assertTrue(sc.connect(addr));200ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x03).flip();201assertEquals(sc.write(bb), 1);202out.printf("wrote: 0x%x%n", bb.get(0));203assertEquals(sc.read(bb.clear()), -1);204}205});206t.start();207208ssc.configureBlocking(false).register(selector, OP_ACCEPT);209assertEquals(selector.select(), 1);210211try (SocketChannel sc = ssc.accept()) {212ByteBuffer bb = ByteBuffer.allocate(10);213assertEquals(sc.read(bb), 1);214out.printf("read: 0x%x%n", bb.get(0));215assertEquals(bb.get(0), 0x03);216}217t.awaitCompletion();218deleteFile(addr);219}220}221222@Test(dataProvider = "family")223public void SELNBAccep_BConn_BIO_RW_4(ProtocolFamily family)224throws Throwable {225try (ServerSocketChannel ssc = openServerSocketChannel(family);226Selector selector = Selector.open()) {227ssc.bind(null);228SocketAddress addr = ssc.getLocalAddress();229230TestThread t = TestThread.of("t4", () -> {231try (SocketChannel sc = openSocketChannel(family)) {232assertTrue(sc.connect(addr));233ByteBuffer bb = ByteBuffer.allocate(10);234assertEquals(sc.read(bb), 1);235out.printf("read: 0x%x%n", bb.get(0));236assertEquals(bb.get(0), 0x04);237}238});239t.start();240241ssc.configureBlocking(false).register(selector, OP_ACCEPT);242assertEquals(selector.select(), 1);243244try (SocketChannel sc = ssc.accept()) {245ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x04).flip();246assertEquals(sc.write(bb), 1);247out.printf("wrote: 0x%x%n", bb.get(0));248assertEquals(sc.read(bb.clear()), -1);249250}251t.awaitCompletion();252deleteFile(addr);253}254}255256@Test(dataProvider = "family")257public void SPINNBAccep_BConn_BIO_WR_5(ProtocolFamily family)258throws Throwable {259try (ServerSocketChannel ssc = openServerSocketChannel(family)) {260ssc.bind(null);261SocketAddress addr = ssc.getLocalAddress();262263TestThread t = TestThread.of("t5", () -> {264try (SocketChannel sc = openSocketChannel(family)) {265assertTrue(sc.connect(addr));266ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x05).flip();267assertEquals(sc.write(bb), 1);268out.printf("wrote: 0x%x%n", bb.get(0));269assertEquals(sc.read(bb.clear()), -1);270}271});272t.start();273274SocketChannel accepted;275for (; ; ) {276accepted = ssc.accept();277if (accepted != null) {278out.println("accepted new connection");279break;280}281Thread.onSpinWait();282}283284try (SocketChannel sc = accepted) {285ByteBuffer bb = ByteBuffer.allocate(10);286assertEquals(sc.read(bb), 1);287out.printf("read: 0x%x%n", bb.get(0));288assertEquals(bb.get(0), 0x05);289}290t.awaitCompletion();291deleteFile(addr);292}293}294295@Test(dataProvider = "family")296public void SPINNBAccep_BConn_BIO_RW_6(ProtocolFamily family)297throws Throwable {298try (ServerSocketChannel ssc = openServerSocketChannel(family)) {299ssc.bind(null);300SocketAddress addr = ssc.getLocalAddress();301302TestThread t = TestThread.of("t6", () -> {303try (SocketChannel sc = openSocketChannel(family)) {304assertTrue(sc.connect(addr));305ByteBuffer bb = ByteBuffer.allocate(10);306assertEquals(sc.read(bb), 1);307out.printf("read: 0x%x%n", bb.get(0));308assertEquals(bb.get(0), 0x06);309}310});311t.start();312313SocketChannel accepted;314for (; ; ) {315accepted = ssc.accept();316if (accepted != null) {317out.println("accepted new connection");318break;319}320Thread.onSpinWait();321}322323try (SocketChannel sc = accepted) {324ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x06).flip();325assertEquals(sc.write(bb), 1);326out.printf("wrote: 0x%x%n", bb.get(0));327assertEquals(sc.read(bb.clear()), -1);328329}330t.awaitCompletion();331deleteFile(addr);332}333}334335// Similar to the previous six scenarios, but with same-thread336// non-blocking connect.337338@Test(dataProvider = "family")339public void BAccep_NBConn_BIO_WR_7(ProtocolFamily family)340throws Throwable {341try (ServerSocketChannel ssc = openServerSocketChannel(family)) {342ssc.bind(null);343SocketAddress addr = ssc.getLocalAddress();344345try (SocketChannel sc = openSocketChannel(family)) {346sc.configureBlocking(false);347sc.connect(addr);348349try (SocketChannel sc2 = ssc.accept()) {350assertTrue(sc.finishConnect());351sc.configureBlocking(true);352TestThread t = TestThread.of("t7", () -> {353ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x07).flip();354assertEquals(sc.write(bb), 1);355out.printf("wrote: 0x%x%n", bb.get(0));356assertEquals(sc.read(bb.clear()), -1);357});358t.start();359360ByteBuffer bb = ByteBuffer.allocate(10);361assertEquals(sc2.read(bb), 1);362out.printf("read: 0x%x%n", bb.get(0));363assertEquals(bb.get(0), 0x07);364sc2.shutdownOutput();365t.awaitCompletion();366}367}368deleteFile(addr);369}370}371372@Test(dataProvider = "family")373public void BAccep_NBConn_BIO_RW_8(ProtocolFamily family)374throws Throwable {375try (ServerSocketChannel ssc = openServerSocketChannel(family)) {376ssc.bind(null);377SocketAddress addr = ssc.getLocalAddress();378379try (SocketChannel sc = openSocketChannel(family)) {380sc.configureBlocking(false);381sc.connect(addr);382383try (SocketChannel sc2 = ssc.accept()) {384assertTrue(sc.finishConnect());385sc.configureBlocking(true);386TestThread t = TestThread.of("t8", () -> {387ByteBuffer bb = ByteBuffer.allocate(10);388assertEquals(sc.read(bb), 1);389out.printf("read: 0x%x%n", bb.get(0));390assertEquals(bb.get(0), 0x08);391sc.shutdownOutput();392});393t.start();394395ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x08).flip();396assertEquals(sc2.write(bb), 1);397out.printf("wrote: 0x%x%n", bb.get(0));398assertEquals(sc2.read(bb.clear()), -1);399t.awaitCompletion();400}401}402deleteFile(addr);403}404}405406@Test(dataProvider = "family")407public void SELNBAccep_NBConn_BIO_WR_9(ProtocolFamily family)408throws Throwable {409try (ServerSocketChannel ssc = openServerSocketChannel(family)) {410ssc.bind(null);411SocketAddress addr = ssc.getLocalAddress();412413try (SocketChannel sc = openSocketChannel(family);414Selector selector = Selector.open()) {415sc.configureBlocking(false);416sc.connect(addr);417418ssc.configureBlocking(false).register(selector, OP_ACCEPT);419assertEquals(selector.select(), 1);420421try (SocketChannel sc2 = ssc.accept()) {422assertTrue(sc.finishConnect());423sc.configureBlocking(true);424TestThread t = TestThread.of("t9", () -> {425ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x09).flip();426assertEquals(sc.write(bb), 1);427out.printf("wrote: 0x%x%n", bb.get(0));428assertEquals(sc.read(bb.clear()), -1);429});430t.start();431432ByteBuffer bb = ByteBuffer.allocate(10);433assertEquals(sc2.read(bb), 1);434out.printf("read: 0x%x%n", bb.get(0));435assertEquals(bb.get(0), 0x09);436sc2.shutdownOutput();437t.awaitCompletion();438}439}440deleteFile(addr);441}442}443444@Test(dataProvider = "family")445public void SELNBAccep_NBConn_BIO_RW_10(ProtocolFamily family)446throws Throwable {447try (ServerSocketChannel ssc = openServerSocketChannel(family)) {448ssc.bind(null);449SocketAddress addr = ssc.getLocalAddress();450451try (SocketChannel sc = openSocketChannel(family);452Selector selector = Selector.open()) {453sc.configureBlocking(false);454sc.connect(addr);455456ssc.configureBlocking(false).register(selector, OP_ACCEPT);457assertEquals(selector.select(), 1);458459try (SocketChannel sc2 = ssc.accept()) {460assertTrue(sc.finishConnect());461sc.configureBlocking(true);462TestThread t = TestThread.of("t10", () -> {463ByteBuffer bb = ByteBuffer.allocate(10);464assertEquals(sc.read(bb), 1);465out.printf("read: 0x%x%n", bb.get(0));466assertEquals(bb.get(0), 0x10);467sc.shutdownOutput();468});469t.start();470471ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x10).flip();472assertEquals(sc2.write(bb), 1);473out.printf("wrote: 0x%x%n", bb.get(0));474assertEquals(sc2.read(bb.clear()), -1);475t.awaitCompletion();476}477}478deleteFile(addr);479}480}481482@Test(dataProvider = "family")483public void SPINNBAccep_NBConn_BIO_WR_11(ProtocolFamily family)484throws Throwable {485try (ServerSocketChannel ssc = openServerSocketChannel(family)) {486ssc.bind(null);487SocketAddress addr = ssc.getLocalAddress();488489try (SocketChannel sc = openSocketChannel(family)) {490sc.configureBlocking(false);491sc.connect(addr);492493SocketChannel accepted;494for (; ; ) {495accepted = ssc.accept();496if (accepted != null) {497out.println("accepted new connection");498break;499}500Thread.onSpinWait();501}502503try (SocketChannel sc2 = accepted) {504assertTrue(sc.finishConnect());505sc.configureBlocking(true);506TestThread t = TestThread.of("t11", () -> {507ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x11).flip();508assertEquals(sc.write(bb), 1);509out.printf("wrote: 0x%x%n", bb.get(0));510assertEquals(sc.read(bb.clear()), -1);511});512t.start();513514ByteBuffer bb = ByteBuffer.allocate(10);515assertEquals(sc2.read(bb), 1);516out.printf("read: 0x%x%n", bb.get(0));517assertEquals(bb.get(0), 0x11);518sc2.shutdownOutput();519t.awaitCompletion();520}521}522deleteFile(addr);523}524}525526@Test(dataProvider = "family")527public void SPINNBAccep_NBConn_BIO_RW_12(ProtocolFamily family)528throws Throwable {529try (ServerSocketChannel ssc = openServerSocketChannel(family)) {530ssc.bind(null);531SocketAddress addr = ssc.getLocalAddress();532533try (SocketChannel sc = openSocketChannel(family)) {534sc.configureBlocking(false);535sc.connect(addr);536537SocketChannel accepted;538for (; ; ) {539accepted = ssc.accept();540if (accepted != null) {541out.println("accepted new connection");542break;543}544Thread.onSpinWait();545}546547try (SocketChannel sc2 = accepted) {548assertTrue(sc.finishConnect());549sc.configureBlocking(true);550TestThread t = TestThread.of("t12", () -> {551ByteBuffer bb = ByteBuffer.allocate(10);552assertEquals(sc.read(bb), 1);553out.printf("read: 0x%x%n", bb.get(0));554assertEquals(bb.get(0), 0x12);555sc.shutdownOutput();556});557t.start();558559ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x12).flip();560assertEquals(sc2.write(bb), 1);561out.printf("wrote: 0x%x%n", bb.get(0));562assertEquals(sc2.read(bb.clear()), -1);563t.awaitCompletion();564}565}566deleteFile(addr);567}568}569570// ---571// Similar to the previous twelve scenarios but with non-blocking IO572// ---573574@Test(dataProvider = "family")575public void BAccep_BConn_NBIO_WR_1a(ProtocolFamily family)576throws Throwable {577try (ServerSocketChannel ssc = openServerSocketChannel(family)) {578ssc.bind(null);579SocketAddress addr = ssc.getLocalAddress();580581TestThread t = TestThread.of("t1a", () -> {582try (SocketChannel sc = openSocketChannel(family);583Selector selector = Selector.open()) {584assertTrue(sc.connect(addr));585ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x1A).flip();586sc.configureBlocking(false);587SelectionKey k = sc.register(selector, OP_WRITE);588selector.select();589int c;590while ((c = sc.write(bb)) < 1) ;591assertEquals(c, 1);592out.printf("wrote: 0x%x%n", bb.get(0));593k.interestOps(OP_READ);594selector.select();595bb.clear();596while ((c = sc.read(bb)) == 0) ;597assertEquals(c, -1);598}599});600t.start();601602try (SocketChannel sc = ssc.accept();603Selector selector = Selector.open()) {604ByteBuffer bb = ByteBuffer.allocate(10);605sc.configureBlocking(false);606sc.register(selector, OP_READ);607selector.select();608int c;609while ((c = sc.read(bb)) == 0) ;610assertEquals(c, 1);611out.printf("read: 0x%x%n", bb.get(0));612assertEquals(bb.get(0), 0x1A);613}614t.awaitCompletion();615deleteFile(addr);616}617}618619@Test(dataProvider = "family")620public void BAccep_BConn_NBIO_RW_2a(ProtocolFamily family)621throws Throwable {622try (ServerSocketChannel ssc = openServerSocketChannel(family)) {623ssc.bind(null);624SocketAddress addr = ssc.getLocalAddress();625626TestThread t = TestThread.of("t2a", () -> {627try (SocketChannel sc = openSocketChannel(family);628Selector selector = Selector.open()) {629assertTrue(sc.connect(addr));630ByteBuffer bb = ByteBuffer.allocate(10);631sc.configureBlocking(false);632sc.register(selector, OP_READ);633selector.select();634int c;635while ((c = sc.read(bb)) == 0) ;636assertEquals(c, 1);637out.printf("read: 0x%x%n", bb.get(0));638assertEquals(bb.get(0), 0x2A);639}640});641t.start();642643try (SocketChannel sc = ssc.accept();644Selector selector = Selector.open()) {645ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x2A).flip();646sc.configureBlocking(false);647SelectionKey k = sc.register(selector, OP_WRITE);648selector.select();649int c;650while ((c = sc.write(bb)) < 1) ;651assertEquals(c, 1);652out.printf("wrote: 0x%x%n", bb.get(0));653k.interestOps(OP_READ);654selector.select();655bb.clear();656while ((c = sc.read(bb)) == 0) ;657assertEquals(c, -1);658}659t.awaitCompletion();660deleteFile(addr);661}662}663664@Test(dataProvider = "family")665public void SELNBAccep_BConn_NBIO_WR_3a(ProtocolFamily family)666throws Throwable {667try (ServerSocketChannel ssc = openServerSocketChannel(family);668Selector aselector = Selector.open()) {669ssc.bind(null);670SocketAddress addr = ssc.getLocalAddress();671672TestThread t = TestThread.of("t3a", () -> {673try (SocketChannel sc = openSocketChannel(family);674Selector selector = Selector.open()) {675assertTrue(sc.connect(addr));676ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x3A).flip();677sc.configureBlocking(false);678SelectionKey k = sc.register(selector, OP_WRITE);679selector.select();680int c;681while ((c = sc.write(bb)) < 1) ;682assertEquals(c, 1);683out.printf("wrote: 0x%x%n", bb.get(0));684k.interestOps(OP_READ);685selector.select();686bb.clear();687while ((c = sc.read(bb)) == 0) ;688assertEquals(c, -1);689}690});691t.start();692693ssc.configureBlocking(false).register(aselector, OP_ACCEPT);694assertEquals(aselector.select(), 1);695696try (SocketChannel sc = ssc.accept();697Selector selector = Selector.open()) {698ByteBuffer bb = ByteBuffer.allocate(10);699sc.configureBlocking(false);700sc.register(selector, OP_READ);701selector.select();702int c;703while ((c = sc.read(bb)) == 0) ;704assertEquals(c, 1);705out.printf("read: 0x%x%n", bb.get(0));706assertEquals(bb.get(0), 0x3A);707}708t.awaitCompletion();709deleteFile(addr);710}711}712713@Test(dataProvider = "family")714public void SELNBAccep_BConn_NBIO_RW_4a(ProtocolFamily family)715throws Throwable {716try (ServerSocketChannel ssc = openServerSocketChannel(family);717Selector aselector = Selector.open()) {718ssc.bind(null);719SocketAddress addr = ssc.getLocalAddress();720721TestThread t = TestThread.of("t4a", () -> {722try (SocketChannel sc = openSocketChannel(family);723Selector selector = Selector.open()) {724assertTrue(sc.connect(addr));725ByteBuffer bb = ByteBuffer.allocate(10);726sc.configureBlocking(false);727sc.register(selector, OP_READ);728selector.select();729int c;730while ((c = sc.read(bb)) == 0) ;731assertEquals(c, 1);732out.printf("read: 0x%x%n", bb.get(0));733assertEquals(bb.get(0), 0x4A);734}735});736t.start();737738ssc.configureBlocking(false).register(aselector, OP_ACCEPT);739assertEquals(aselector.select(), 1);740741try (SocketChannel sc = ssc.accept();742Selector selector = Selector.open()) {743ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x4A).flip();744sc.configureBlocking(false);745SelectionKey k = sc.register(selector, OP_WRITE);746selector.select();747int c;748while ((c = sc.write(bb)) < 1) ;749assertEquals(c, 1);750out.printf("wrote: 0x%x%n", bb.get(0));751k.interestOps(OP_READ);752selector.select();753bb.clear();754while ((c = sc.read(bb)) == 0) ;755assertEquals(c, -1);756}757t.awaitCompletion();758deleteFile(addr);759}760}761762@Test(dataProvider = "family")763public void SPINNBAccep_BConn_NBIO_WR_5a(ProtocolFamily family)764throws Throwable {765try (ServerSocketChannel ssc = openServerSocketChannel(family)) {766ssc.bind(null);767SocketAddress addr = ssc.getLocalAddress();768769TestThread t = TestThread.of("t5a", () -> {770try (SocketChannel sc = openSocketChannel(family);771Selector selector = Selector.open()) {772assertTrue(sc.connect(addr));773ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x5A).flip();774sc.configureBlocking(false);775SelectionKey k = sc.register(selector, OP_WRITE);776selector.select();777int c;778while ((c = sc.write(bb)) < 1) ;779assertEquals(c, 1);780out.printf("wrote: 0x%x%n", bb.get(0));781k.interestOps(OP_READ);782selector.select();783bb.clear();784while ((c = sc.read(bb)) == 0) ;785assertEquals(c, -1);786}787});788t.start();789790SocketChannel accepted;791for (; ; ) {792accepted = ssc.accept();793if (accepted != null) {794out.println("accepted new connection");795break;796}797Thread.onSpinWait();798}799800try (SocketChannel sc = accepted;801Selector selector = Selector.open()) {802ByteBuffer bb = ByteBuffer.allocate(10);803sc.configureBlocking(false);804sc.register(selector, OP_READ);805selector.select();806int c;807while ((c = sc.read(bb)) == 0) ;808assertEquals(c, 1);809out.printf("read: 0x%x%n", bb.get(0));810assertEquals(bb.get(0), 0x5A);811}812t.awaitCompletion();813deleteFile(addr);814}815}816817@Test(dataProvider = "family")818public void SPINNBAccep_BConn_NBIO_RW_6a(ProtocolFamily family)819throws Throwable {820try (ServerSocketChannel ssc = openServerSocketChannel(family)) {821ssc.bind(null);822SocketAddress addr = ssc.getLocalAddress();823824TestThread t = TestThread.of("t6a", () -> {825try (SocketChannel sc = openSocketChannel(family);826Selector selector = Selector.open()) {827assertTrue(sc.connect(addr));828ByteBuffer bb = ByteBuffer.allocate(10);829sc.configureBlocking(false);830sc.register(selector, OP_READ);831selector.select();832int c;833while ((c = sc.read(bb)) == 0) ;834assertEquals(c, 1);835out.printf("read: 0x%x%n", bb.get(0));836assertEquals(bb.get(0), 0x6A);837}838});839t.start();840841SocketChannel accepted;842for (; ; ) {843accepted = ssc.accept();844if (accepted != null) {845out.println("accepted new connection");846break;847}848Thread.onSpinWait();849}850851try (SocketChannel sc = accepted;852Selector selector = Selector.open()) {853ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x6A).flip();854sc.configureBlocking(false);855SelectionKey k = sc.register(selector, OP_WRITE);856selector.select();857int c;858while ((c = sc.write(bb)) < 1) ;859assertEquals(c, 1);860out.printf("wrote: 0x%x%n", bb.get(0));861k.interestOps(OP_READ);862selector.select();863bb.clear();864while ((c = sc.read(bb)) == 0) ;865assertEquals(c, -1);866867}868t.awaitCompletion();869deleteFile(addr);870}871}872873// Similar to the previous six scenarios but with same-thread874// non-blocking connect.875876@Test(dataProvider = "family")877public void BAccep_NBConn_NBIO_WR_7a(ProtocolFamily family)878throws Throwable {879try (ServerSocketChannel ssc = openServerSocketChannel(family)) {880ssc.bind(null);881SocketAddress addr = ssc.getLocalAddress();882883try (SocketChannel sc = openSocketChannel(family)) {884sc.configureBlocking(false);885sc.connect(addr);886887try (SocketChannel sc2 = ssc.accept()) {888assertTrue(sc.finishConnect());889TestThread t = TestThread.of("t7a", () -> {890try (Selector selector = Selector.open()) {891ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x7A).flip();892sc.configureBlocking(false);893SelectionKey k = sc.register(selector, OP_WRITE);894selector.select();895int c;896while ((c = sc.write(bb)) < 1) ;897assertEquals(c, 1);898out.printf("wrote: 0x%x%n", bb.get(0));899k.interestOps(OP_READ);900selector.select();901bb.clear();902while ((c = sc.read(bb)) == 0) ;903assertEquals(c, -1);904}905});906t.start();907908ByteBuffer bb = ByteBuffer.allocate(10);909sc2.configureBlocking(false);910try (Selector selector = Selector.open()) {911sc2.register(selector, OP_READ);912selector.select();913int c;914while ((c = sc2.read(bb)) == 0) ;915assertEquals(c, 1);916out.printf("read: 0x%x%n", bb.get(0));917assertEquals(bb.get(0), 0x7A);918sc2.shutdownOutput();919}920t.awaitCompletion();921}922}923deleteFile(addr);924}925}926927@Test(dataProvider = "family")928public void BAccep_NBConn_NBIO_RW_8a(ProtocolFamily family)929throws Throwable {930try (ServerSocketChannel ssc = openServerSocketChannel(family)) {931ssc.bind(null);932SocketAddress addr = ssc.getLocalAddress();933934try (SocketChannel sc = openSocketChannel(family)) {935sc.configureBlocking(false);936sc.connect(addr);937938try (SocketChannel sc2 = ssc.accept()) {939assertTrue(sc.finishConnect());940TestThread t = TestThread.of("t8a", () -> {941try (Selector selector = Selector.open()) {942ByteBuffer bb = ByteBuffer.allocate(10);943sc.register(selector, OP_READ);944selector.select();945int c;946while ((c = sc.read(bb)) == 0) ;947assertEquals(c, 1);948out.printf("read: 0x%x%n", bb.get(0));949assertEquals(bb.get(0), (byte) 0x8A);950sc.shutdownOutput();951}952});953t.start();954955ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x8A).flip();956sc2.configureBlocking(false);957try (Selector selector = Selector.open()) {958SelectionKey k = sc2.register(selector, OP_WRITE);959selector.select();960int c;961while ((c = sc2.write(bb)) < 1) ;962assertEquals(c, 1);963out.printf("wrote: 0x%x%n", bb.get(0));964k.interestOps(OP_READ);965selector.select();966bb.clear();967while ((c = sc2.read(bb)) == 0) ;968assertEquals(c, -1);969}970t.awaitCompletion();971}972}973deleteFile(addr);974}975}976977@Test(dataProvider = "family")978public void SELNBAccep_NBConn_NBIO_WR_9a(ProtocolFamily family)979throws Throwable {980try (ServerSocketChannel ssc = openServerSocketChannel(family)) {981ssc.bind(null);982SocketAddress addr = ssc.getLocalAddress();983984try (SocketChannel sc = openSocketChannel(family)) {985sc.configureBlocking(false);986sc.connect(addr);987988Selector aselector = Selector.open();989ssc.configureBlocking(false).register(aselector, OP_ACCEPT);990assertEquals(aselector.select(), 1);991992try (SocketChannel sc2 = ssc.accept()) {993assertTrue(sc.finishConnect());994TestThread t = TestThread.of("t9a", () -> {995try (Selector selector = Selector.open()) {996ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0x9A).flip();997sc.configureBlocking(false);998SelectionKey k = sc.register(selector, OP_WRITE);999selector.select();1000int c;1001while ((c = sc.write(bb)) < 1) ;1002assertEquals(c, 1);1003out.printf("wrote: 0x%x%n", bb.get(0));1004k.interestOps(OP_READ);1005selector.select();1006bb.clear();1007while ((c = sc.read(bb)) == 0) ;1008assertEquals(c, -1);1009}1010});1011t.start();10121013ByteBuffer bb = ByteBuffer.allocate(10);1014sc2.configureBlocking(false);1015try (Selector selector = Selector.open()) {1016sc2.register(selector, OP_READ);1017selector.select();1018int c;1019while ((c = sc2.read(bb)) == 0) ;1020assertEquals(c, 1);1021out.printf("read: 0x%x%n", bb.get(0));1022assertEquals(bb.get(0), (byte) 0x9A);1023sc2.shutdownOutput();1024}1025t.awaitCompletion();1026}1027}1028deleteFile(addr);1029}1030}10311032@Test(dataProvider = "family")1033public void SELNBAccep_NBConn_NBIO_RW_10a(ProtocolFamily family)1034throws Throwable {1035try (ServerSocketChannel ssc = openServerSocketChannel(family)) {1036ssc.bind(null);1037SocketAddress addr = ssc.getLocalAddress();10381039try (SocketChannel sc = openSocketChannel(family)) {1040sc.configureBlocking(false);1041sc.connect(addr);10421043Selector aselector = Selector.open();1044ssc.configureBlocking(false).register(aselector, OP_ACCEPT);1045assertEquals(aselector.select(), 1);10461047try (SocketChannel sc2 = ssc.accept()) {1048assertTrue(sc.finishConnect());1049TestThread t = TestThread.of("t10a", () -> {1050try (Selector selector = Selector.open()) {1051ByteBuffer bb = ByteBuffer.allocate(10);1052sc.register(selector, OP_READ);1053selector.select();1054int c;1055while ((c = sc.read(bb)) == 0) ;1056assertEquals(c, 1);1057out.printf("read: 0x%x%n", bb.get(0));1058assertEquals(bb.get(0), (byte) 0xAA);1059sc.shutdownOutput();1060}1061});1062t.start();10631064ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0xAA).flip();1065sc2.configureBlocking(false);1066try (Selector selector = Selector.open()) {1067SelectionKey k = sc2.register(selector, OP_WRITE);1068selector.select();1069int c;1070while ((c = sc2.write(bb)) < 1) ;1071assertEquals(c, 1);1072out.printf("wrote: 0x%x%n", bb.get(0));1073k.interestOps(OP_READ);1074selector.select();1075bb.clear();1076while ((c = sc2.read(bb)) == 0) ;1077assertEquals(c, -1);1078}1079t.awaitCompletion();1080}1081}1082deleteFile(addr);1083}1084}10851086@Test(dataProvider = "family")1087public void SPINBAccep_NBConn_NBIO_WR_11a(ProtocolFamily family)1088throws Throwable {1089try (ServerSocketChannel ssc = openServerSocketChannel(family)) {1090ssc.bind(null);1091SocketAddress addr = ssc.getLocalAddress();10921093try (SocketChannel sc = openSocketChannel(family)) {1094sc.configureBlocking(false);1095sc.connect(addr);10961097SocketChannel accepted;1098for (; ; ) {1099accepted = ssc.accept();1100if (accepted != null) {1101out.println("accepted new connection");1102break;1103}1104Thread.onSpinWait();1105}11061107try (SocketChannel sc2 = accepted) {1108assertTrue(sc.finishConnect());1109TestThread t = TestThread.of("t11a", () -> {1110try (Selector selector = Selector.open()) {1111ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0xBA).flip();1112sc.configureBlocking(false);1113SelectionKey k = sc.register(selector, OP_WRITE);1114selector.select();1115int c;1116while ((c = sc.write(bb)) < 1) ;1117assertEquals(c, 1);1118out.printf("wrote: 0x%x%n", bb.get(0));1119k.interestOps(OP_READ);1120selector.select();1121bb.clear();1122while ((c = sc.read(bb)) == 0) ;1123assertEquals(c, -1);1124}1125});1126t.start();11271128ByteBuffer bb = ByteBuffer.allocate(10);1129sc2.configureBlocking(false);1130try (Selector selector = Selector.open()) {1131sc2.register(selector, OP_READ);1132selector.select();1133int c;1134while ((c = sc2.read(bb)) == 0) ;1135assertEquals(c, 1);1136out.printf("read: 0x%x%n", bb.get(0));1137assertEquals(bb.get(0), (byte) 0xBA);1138sc2.shutdownOutput();1139}1140t.awaitCompletion();1141}1142}1143deleteFile(addr);1144}1145}11461147@Test(dataProvider = "family")1148public void SPINBAccep_NBConn_NBIO_RW_12a(ProtocolFamily family)1149throws Throwable {1150try (ServerSocketChannel ssc = openServerSocketChannel(family)) {1151ssc.bind(null);1152SocketAddress addr = ssc.getLocalAddress();11531154try (SocketChannel sc = openSocketChannel(family)) {1155sc.configureBlocking(false);1156sc.connect(addr);11571158SocketChannel accepted;1159for (; ; ) {1160accepted = ssc.accept();1161if (accepted != null) {1162out.println("accepted new connection");1163break;1164}1165Thread.onSpinWait();1166}11671168try (SocketChannel sc2 = accepted) {1169assertTrue(sc.finishConnect());1170TestThread t = TestThread.of("t10a", () -> {1171try (Selector selector = Selector.open()) {1172ByteBuffer bb = ByteBuffer.allocate(10);1173sc.register(selector, OP_READ);1174selector.select();1175int c;1176while ((c = sc.read(bb)) == 0) ;1177assertEquals(c, 1);1178out.printf("read: 0x%x%n", bb.get(0));1179assertEquals(bb.get(0), (byte) 0xCA);1180sc.shutdownOutput();1181}1182});1183t.start();11841185ByteBuffer bb = ByteBuffer.allocate(10).put((byte) 0xCA).flip();1186sc2.configureBlocking(false);1187try (Selector selector = Selector.open()) {1188SelectionKey k = sc2.register(selector, OP_WRITE);1189selector.select();1190int c;1191while ((c = sc2.write(bb)) < 1) ;1192assertEquals(c, 1);1193out.printf("wrote: 0x%x%n", bb.get(0));1194k.interestOps(OP_READ);1195selector.select();1196bb.clear();1197while ((c = sc2.read(bb)) == 0) ;1198assertEquals(c, -1);1199}1200t.awaitCompletion();1201}1202}1203deleteFile(addr);1204}1205}12061207// --12081209static class TestThread extends Thread {1210private final UncheckedRunnable runnable;1211private volatile Throwable throwable;12121213TestThread(UncheckedRunnable runnable, String name) {1214super(name);1215this.runnable = runnable;1216}12171218@Override1219public void run() {1220try {1221runnable.run();1222} catch (Throwable t) {1223out.printf("[%s] caught unexpected: %s%n", getName(), t);1224throwable = t;1225}1226}12271228interface UncheckedRunnable {1229void run() throws Throwable;1230}12311232static TestThread of(String name, UncheckedRunnable runnable) {1233return new TestThread(runnable, name);1234}12351236void awaitCompletion() throws Throwable {1237this.join();1238if (throwable != null)1239throw throwable;1240}1241}1242}124312441245