Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/AdaptSocket.java
38828 views
/*1* Copyright (c) 2001, 2012, 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 socket-channel adaptors25* @library ..26*/2728import java.io.*;29import java.net.*;30import java.nio.channels.*;313233public class AdaptSocket {3435static java.io.PrintStream out = System.out;3637static void test(TestServers.DayTimeServer dayTimeServer,38int timeout,39boolean shouldTimeout)40throws Exception41{42out.println();4344InetSocketAddress isa45= new InetSocketAddress(dayTimeServer.getAddress(),46dayTimeServer.getPort());47SocketChannel sc = SocketChannel.open();48Socket so = sc.socket();49out.println("opened: " + so);50out.println(" " + sc);5152//out.println("opts: " + sc.options());53so.setTcpNoDelay(true);54//so.setTrafficClass(SocketOpts.IP.TOS_THROUGHPUT);55so.setKeepAlive(true);56so.setSoLinger(true, 42);57so.setOOBInline(true);58so.setReceiveBufferSize(512);59so.setSendBufferSize(512);60//out.println(" " + sc.options());6162if (timeout == 0)63so.connect(isa);64else {65try {66so.connect(isa, timeout);67} catch (SocketTimeoutException x) {68if (shouldTimeout) {69out.println("Connection timed out, as expected");70return;71} else {72throw x;73}74}75}76out.println("connected: " + so);77out.println(" " + sc);78byte[] bb = new byte[100];79int n = so.getInputStream().read(bb);80String s = new String(bb, 0, n - 2, "US-ASCII");81out.println(isa + " says: \"" + s + "\"");82so.shutdownInput();83out.println("ishut: " + sc);84so.shutdownOutput();85out.println("oshut: " + sc);86so.close();87out.println("closed: " + so);88out.println(" " + sc);89}9091static String dataString = "foo\r\n";9293static void testRead(Socket so, boolean shouldTimeout)94throws Exception95{96String data = "foo\r\n";97so.getOutputStream().write(dataString.getBytes("US-ASCII"));98InputStream is = so.getInputStream();99try {100byte[] b = new byte[100];101int n = is.read(b);102if (n != 5)103throw new Exception("Incorrect number of bytes read: " + n);104if (!dataString.equals(new String(b, 0, n, "US-ASCII")))105throw new Exception("Incorrect data read: " + n);106} catch (SocketTimeoutException x) {107if (shouldTimeout) {108out.println("Read timed out, as expected");109return;110}111throw x;112}113}114115static void testRead(TestServers.EchoServer echoServer,116int timeout,117boolean shouldTimeout)118throws Exception119{120out.println();121122InetSocketAddress isa123= new InetSocketAddress(echoServer.getAddress(),124echoServer.getPort());125SocketChannel sc = SocketChannel.open();126sc.connect(isa);127Socket so = sc.socket();128out.println("connected: " + so);129out.println(" " + sc);130131if (timeout > 0)132so.setSoTimeout(timeout);133out.println("timeout: " + so.getSoTimeout());134135testRead(so, shouldTimeout);136for (int i = 0; i < 4; i++) {137testRead(so, shouldTimeout);138}139140sc.close();141}142143public static void main(String[] args) throws Exception {144145try (TestServers.DayTimeServer dayTimeServer146= TestServers.DayTimeServer.startNewServer()) {147test(dayTimeServer, 0, false);148test(dayTimeServer, 1000, false);149}150151try (TestServers.DayTimeServer lingerDayTimeServer152= TestServers.DayTimeServer.startNewServer(100)) {153// this test no longer really test the connection timeout154// since there is no way to prevent the server from eagerly155// accepting connection...156test(lingerDayTimeServer, 10, true);157}158159try (TestServers.EchoServer echoServer160= TestServers.EchoServer.startNewServer()) {161testRead(echoServer, 0, false);162testRead(echoServer, 8000, false);163}164165try (TestServers.EchoServer lingerEchoServer166= TestServers.EchoServer.startNewServer(100)) {167testRead(lingerEchoServer, 10, true);168}169}170}171172173