Path: blob/master/test/jdk/java/nio/channels/SocketChannel/CloseDuringConnect.java
41154 views
/*1* Copyright (c) 2018, 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 819892825* @library /test/lib26* @build jdk.test.lib.Utils27* @run main CloseDuringConnect28* @summary Attempt to cause a deadlock by closing a SocketChannel in one thread29* where another thread is closing the channel after a connect fail30*/3132import java.io.IOException;33import java.io.UncheckedIOException;34import java.net.InetAddress;35import java.net.InetSocketAddress;36import java.net.SocketAddress;37import java.nio.channels.SocketChannel;38import java.util.concurrent.Executors;39import java.util.concurrent.Future;40import java.util.concurrent.ScheduledExecutorService;41import java.util.stream.IntStream;42import static java.util.concurrent.TimeUnit.MILLISECONDS;4344import jdk.test.lib.Utils;4546public class CloseDuringConnect {4748// number of test iterations, needs to be 5-10 at least49static final int ITERATIONS = 50;5051// maximum delay before closing SocketChannel, in milliseconds52static final int MAX_DELAY_BEFORE_CLOSE = 20;5354/**55* Invoked by a task in the thread pool to connect to a remote address.56* The connection should never be established.57*/58static Void connect(SocketChannel sc, SocketAddress remote) {59try {60if (!sc.connect(remote)) {61while (!sc.finishConnect()) {62Thread.yield();63}64}65throw new RuntimeException("Connected, should not happen");66} catch (IOException expected) { }67if (sc.isConnected())68throw new RuntimeException("isConnected return true, should not happen");69return null;70}7172/**73* Invoked by a task in the thread pool to close a socket channel.74*/75static Void close(SocketChannel sc) {76try {77sc.close();78} catch (IOException e) {79throw new UncheckedIOException("close failed", e);80}81return null;82}8384/**85* Test for deadlock by submitting a task to connect to the given address86* while another task closes the socket channel.87* @param pool the thread pool to submit or schedule tasks88* @param remote the remote address, does not accept connections89* @param blocking socket channel blocking mode90* @param delay the delay, in millis, before closing the channel91*/92static void test(ScheduledExecutorService pool,93SocketAddress remote,94boolean blocking,95long delay) {96try {97SocketChannel sc = SocketChannel.open();98sc.configureBlocking(blocking);99Future<Void> r1 = pool.submit(() -> connect(sc, remote));100Future<Void> r2 = pool.schedule(() -> close(sc), delay, MILLISECONDS);101r1.get();102r2.get();103} catch (Throwable t) {104throw new RuntimeException("Test failed", t);105}106}107108public static void main(String[] args) throws Exception {109SocketAddress refusing = Utils.refusingEndpoint();110ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);111try {112IntStream.range(0, ITERATIONS).forEach(i -> {113System.out.format("Iteration %d ...%n", (i + 1));114115// Execute the test for varying delays up to MAX_DELAY_BEFORE_CLOSE,116// for socket channels configured both blocking and non-blocking117IntStream.range(0, MAX_DELAY_BEFORE_CLOSE).forEach(delay -> {118test(pool, refusing, /*blocking mode*/true, delay);119test(pool, refusing, /*blocking mode*/false, delay);120});121});122} finally {123pool.shutdown();124}125}126}127128129