test/java/nio/channels/SocketChannel/AdaptSocket.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 33,55 **** public class AdaptSocket { static java.io.PrintStream out = System.out; ! static final int ECHO_PORT = 7; ! static final int DAYTIME_PORT = 13; ! static final String REMOTE_HOST = TestUtil.HOST; ! static final String VERY_REMOTE_HOST = TestUtil.FAR_HOST; ! ! static void test(String hn, int timeout, boolean shouldTimeout) throws Exception { out.println(); ! InetSocketAddress isa ! = new InetSocketAddress(InetAddress.getByName(hn), ! DAYTIME_PORT); SocketChannel sc = SocketChannel.open(); Socket so = sc.socket(); out.println("opened: " + so); out.println(" " + sc); --- 33,49 ---- public class AdaptSocket { static java.io.PrintStream out = System.out; ! static void test(TestUtil.DayTimeServer dayTimeServer,int timeout, boolean shouldTimeout) throws Exception { out.println(); ! InetSocketAddress isa = new InetSocketAddress(dayTimeServer.getAddress(), ! dayTimeServer.getPort()); SocketChannel sc = SocketChannel.open(); Socket so = sc.socket(); out.println("opened: " + so); out.println(" " + sc);
*** 114,130 **** } throw x; } } ! static void testRead(String hn, int timeout, boolean shouldTimeout) throws Exception { out.println(); ! InetSocketAddress isa ! = new InetSocketAddress(InetAddress.getByName(hn), ECHO_PORT); SocketChannel sc = SocketChannel.open(); sc.connect(isa); Socket so = sc.socket(); out.println("connected: " + so); out.println(" " + sc); --- 108,124 ---- } throw x; } } ! static void testRead(TestUtil.EchoServer echoServer, int timeout, boolean shouldTimeout) throws Exception { out.println(); ! InetSocketAddress isa = new InetSocketAddress(echoServer.getAddress(), ! echoServer.getPort()); SocketChannel sc = SocketChannel.open(); sc.connect(isa); Socket so = sc.socket(); out.println("connected: " + so); out.println(" " + sc);
*** 132,155 **** if (timeout > 0) so.setSoTimeout(timeout); out.println("timeout: " + so.getSoTimeout()); testRead(so, shouldTimeout); ! for (int i = 0; i < 4; i++) testRead(so, shouldTimeout); sc.close(); } public static void main(String[] args) throws Exception { ! test(REMOTE_HOST, 0, false); ! test(REMOTE_HOST, 1000, false); ! test(VERY_REMOTE_HOST, 10, true); ! ! testRead(REMOTE_HOST, 0, false); ! testRead(REMOTE_HOST, 8000, false); ! testRead(VERY_REMOTE_HOST, 10, true); } } --- 126,165 ---- if (timeout > 0) so.setSoTimeout(timeout); out.println("timeout: " + so.getSoTimeout()); testRead(so, shouldTimeout); ! for (int i = 0; i < 4; i++) { testRead(so, shouldTimeout); + } sc.close(); } public static void main(String[] args) throws Exception { ! try (TestUtil.DayTimeServer dayTimeServer = ! TestUtil.DayTimeServer.startNewServer()) { ! test(dayTimeServer, 0, false); ! test(dayTimeServer, 1000, false); ! } + try (TestUtil.DayTimeServer lingerDayTimeServer = + TestUtil.DayTimeServer.startNewServer(100)) { + // this test no longer really test the connection timeout + // since there is no way to prevent the server from eagerly + // accepting connection... + test(lingerDayTimeServer, 10, true); } + try (TestUtil.EchoServer echoServer = + TestUtil.EchoServer.startNewServer()) { + testRead(echoServer, 0, false); + testRead(echoServer, 8000, false); + } + + try (TestUtil.EchoServer lingerEchoServer = + TestUtil.EchoServer.startNewServer(100)) { + testRead(lingerEchoServer, 10, true); + } + } }