Path: blob/master/test/jdk/java/nio/channels/ServerSocketChannel/AdaptServerSocket.java
41154 views
/*1* Copyright (c) 2001, 2016, 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/* @test24* @bug 4286936 814621325* @summary Unit test for server-socket-channel adaptors26*/2728import java.io.*;29import java.net.*;30import java.nio.*;31import java.nio.channels.*;32import java.nio.charset.*;333435public class AdaptServerSocket {3637static java.io.PrintStream out = System.out;38static volatile boolean clientStarted = false;39static volatile Exception clientException = null;40static volatile Thread client = null;4142static void startClient(final int port, final int dally)43throws Exception44{45Thread t = new Thread() {46public void run() {47try (Socket so = new Socket()) {48out.println("client: " + so);49clientStarted = true;50if (dally > 0)51Thread.sleep(dally);52so.connect(new InetSocketAddress(port));53if (Thread.interrupted()) {54out.println("client interrupted");55return;56}57out.println("client: " + so);58int a = so.getInputStream().read();59out.println("client: read " + a);60a += 1;61so.getOutputStream().write(a);62out.println("client: wrote " + a);63} catch (Exception x) {64if (x instanceof InterruptedException)65return;66clientException = x;67x.printStackTrace();68}69}70};71t.setDaemon(true);72t.start();73client = t;74}7576static void test(int clientDally, int timeout, boolean shouldTimeout)77throws Exception78{79boolean needClient = !shouldTimeout;80client = null;81clientException = null;82clientStarted = false;83out.println();8485try (ServerSocketChannel ssc = ServerSocketChannel.open();86ServerSocket sso = ssc.socket()) {87out.println("created: " + ssc);88out.println(" " + sso);89if (timeout != 0)90sso.setSoTimeout(timeout);91out.println("timeout: " + sso.getSoTimeout());92sso.bind(null);93out.println("bound: " + ssc);94out.println(" " + sso);95if (needClient) {96startClient(sso.getLocalPort(), clientDally);97while (!clientStarted) {98Thread.sleep(20);99}100}101Socket so = null;102try {103so = sso.accept();104} catch (SocketTimeoutException x) {105if (shouldTimeout)106out.println("Accept timed out, as expected");107else108throw x;109}110if (shouldTimeout && (so != null))111throw new Exception("Accept did not time out");112113if (so != null) {114int a = 42;115so.getOutputStream().write(a);116int b = so.getInputStream().read();117if (b != a + 1)118throw new Exception("Read incorrect data");119out.println("server: read " + b);120}121}122if (needClient) {123client.interrupt();124client.join();125if (clientException != null)126throw clientException;127}128}129130public static void main(String[] args) throws Exception {131test(0, 0, false);132test(50, 5000, false);133test(500, 50, true);134}135136}137138139