Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/SocksConnectTimeout.java
38812 views
/*1* Copyright (c) 2010, 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 622363526* @summary Code hangs at connect call even when Timeout is specified27*/2829import java.net.InetAddress;30import java.net.InetSocketAddress;31import java.net.Proxy;32import java.net.Socket;33import java.net.ServerSocket;34import java.net.SocketTimeoutException;35import java.io.IOException;36import java.io.Closeable;37import java.util.concurrent.Phaser;38import java.util.concurrent.TimeUnit;3940public class SocksConnectTimeout {41static ServerSocket serverSocket;42static final boolean debug = true;43static final Phaser startPhaser = new Phaser(2);44static final Phaser finishPhaser = new Phaser(2);45static int failed, passed;4647public static void main(String[] args) {48try {49serverSocket = new ServerSocket(0);5051(new Thread() {52@Override53public void run() { serve(); }54}).start();5556Proxy socksProxy = new Proxy(Proxy.Type.SOCKS,57new InetSocketAddress(InetAddress.getLocalHost(), serverSocket.getLocalPort()));5859test(socksProxy);60} catch (IOException e) {61unexpected(e);62} finally {63close(serverSocket);6465if (failed > 0)66throw new RuntimeException("Test Failed: passed:" + passed + ", failed:" + failed);67}68}6970static void test(Proxy proxy) {71startPhaser.arriveAndAwaitAdvance();72Socket socket = null;73try {74socket = new Socket(proxy);75connectWithTimeout(socket);76failed("connected successfully!");77} catch (SocketTimeoutException socketTimeout) {78debug("Passed: Received: " + socketTimeout);79passed();80} catch (Exception exception) {81failed("Connect timeout test failed", exception);82} finally {83finishPhaser.arriveAndAwaitAdvance();84close(socket);85}86}8788static void connectWithTimeout(Socket socket) throws IOException {89socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1234), 500);90}9192static void serve() {93Socket client = null;94try {95startPhaser.arriveAndAwaitAdvance();96client = serverSocket.accept();97finishPhaser.awaitAdvanceInterruptibly(finishPhaser.arrive(), 5, TimeUnit.SECONDS);98} catch (Exception e) {99unexpected(e);100} finally {101close(client);102}103}104105static void debug(String message) {106if (debug)107System.out.println(message);108}109110static void unexpected(Exception e ) {111System.out.println("Unexcepted Exception: " + e);112}113114static void close(Closeable closeable) {115if (closeable != null) try { closeable.close(); } catch (IOException e) {unexpected(e);}116}117118static void failed(String message) {119System.out.println(message);120failed++;121}122123static void failed(String message, Exception e) {124System.out.println(message);125System.out.println(e);126failed++;127}128129static void passed() { passed++; };130131}132133134