Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/ServerSocketChannel/AdaptServerSocket.java
38828 views
/*1* Copyright (c) 2001, 2013, 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* @summary Unit test for server-socket-channel adaptors25*/2627import java.io.*;28import java.net.*;29import java.nio.*;30import java.nio.channels.*;31import java.nio.charset.*;323334public class AdaptServerSocket {3536static java.io.PrintStream out = System.out;37static volatile boolean clientStarted = false;38static volatile Exception clientException = null;39static volatile Thread client = null;4041static void startClient(final int port, final int dally)42throws Exception43{44Thread t = new Thread() {45public void run() {46try (Socket so = new Socket()) {47out.println("client: " + so);48clientStarted = true;49if (dally > 0)50Thread.sleep(dally);51so.connect(new InetSocketAddress(port));52if (Thread.interrupted()) {53out.println("client interrupted");54return;55}56out.println("client: " + so);57int a = so.getInputStream().read();58out.println("client: read " + a);59a += 1;60so.getOutputStream().write(a);61out.println("client: wrote " + a);62} catch (Exception x) {63if (x instanceof InterruptedException)64return;65clientException = x;66x.printStackTrace();67}68}69};70t.setDaemon(true);71t.start();72client = t;73}7475static void test(int clientDally, int timeout, boolean shouldTimeout)76throws Exception77{78clientStarted = false;79out.println();8081try (ServerSocketChannel ssc = ServerSocketChannel.open();82ServerSocket sso = ssc.socket()) {83out.println("created: " + ssc);84out.println(" " + sso);85if (timeout != 0)86sso.setSoTimeout(timeout);87out.println("timeout: " + sso.getSoTimeout());88sso.bind(null);89out.println("bound: " + ssc);90out.println(" " + sso);91startClient(sso.getLocalPort(), clientDally);92while (!clientStarted) {93Thread.sleep(20);94}95Socket so = null;96try {97so = sso.accept();98} catch (SocketTimeoutException x) {99if (shouldTimeout)100out.println("Accept timed out, as expected");101else102throw x;103}104if (shouldTimeout && (so != null))105throw new Exception("Accept did not time out");106107if (so != null) {108int a = 42;109so.getOutputStream().write(a);110int b = so.getInputStream().read();111if (b != a + 1)112throw new Exception("Read incorrect data");113out.println("server: read " + b);114}115}116client.interrupt();117client.join();118if (clientException != null)119throw clientException;120}121122public static void main(String[] args) throws Exception {123test(0, 0, false);124test(50, 5000, false);125test(500, 50, true);126}127128}129130131