Path: blob/master/test/jdk/java/net/MulticastSocket/MultiDead.java
41149 views
/*1* Copyright (c) 2015, 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/**24* @test25* @bug 807246626* @summary Deadlock when initializing MulticastSocket and DatagramSocket27* @library /test/lib28* @run main/othervm MultiDead29*/3031import java.net.DatagramSocket;32import java.net.MulticastSocket;33import java.util.concurrent.atomic.AtomicBoolean;34import java.util.concurrent.atomic.AtomicReference;35import java.util.concurrent.CountDownLatch;36import static java.util.concurrent.TimeUnit.MILLISECONDS;37import jdk.test.lib.JDKToolLauncher;38import jdk.test.lib.Utils;3940public class MultiDead {41private static final int THREAD_PAIR_COUNT = 4;42private static final int CHILDREN_COUNT = 20;43// at least 2.5 seconds for a child to complete44private static final long CHILD_TIMEOUT = 2500;45private static final long TIMEOUT =46Utils.adjustTimeout(CHILDREN_COUNT * CHILD_TIMEOUT * 2);4748public static void main(String[] args) throws Throwable {49if (args.length == 0 || args[0].equals("parent")) {50parentProcess();51}5253if (args.length > 0 && args[0].equals("child")) {54childProcess();55}56}5758private static void parentProcess() throws Throwable {59JDKToolLauncher launcher = JDKToolLauncher60.createUsingTestJDK("java")61.addToolArg("MultiDead")62.addToolArg("child");63ProcessBuilder pb = new ProcessBuilder(launcher.getCommand());6465AtomicReference<Process> child = new AtomicReference<>();66AtomicBoolean stopFlag = new AtomicBoolean(false);6768Thread th = new Thread(() -> {69for (int i = 0; i < CHILDREN_COUNT; ++i) {70System.out.println("child #" + (i + 1) + " of " +71CHILDREN_COUNT);72long start = System.nanoTime();73try {74child.set(pb.start());75child.get().waitFor();76if (stopFlag.get()) {77break;78}79} catch (Exception e) {80throw new RuntimeException(e);81}82if (System.nanoTime() - start >83MILLISECONDS.toNanos(CHILD_TIMEOUT)) {84System.err.println("Machine is too slow, " +85"skipping the test...");86break;87}88}89});9091th.start();92th.join(TIMEOUT);9394stopFlag.set(true);95if (th.isAlive()) {96if (child.get() != null) {97child.get().destroyForcibly();98}99throw new RuntimeException("Failed to complete on time.");100}101}102103private static void childProcess() {104CountDownLatch latch = new CountDownLatch(1);105for (int i = 0; i < THREAD_PAIR_COUNT; ++i) {106new Thread(() -> {107try {108latch.await();109try (MulticastSocket a = new MulticastSocket(6000)) {110}111} catch (Exception ignore) {112}113}).start();114115new Thread(() -> {116try {117latch.await();118try (DatagramSocket b = new DatagramSocket(6000)) {119}120} catch (Exception ignore) {121}122}).start();123}124latch.countDown();125}126}127128129