Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/MulticastSocket/MultiDead.java
38812 views
/*1* Copyright (c) 2015, 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 /lib/testlibrary28* @build jdk.testlibrary.*29* @run main/othervm MultiDead30*/3132import java.net.DatagramSocket;33import java.net.MulticastSocket;34import java.util.concurrent.atomic.AtomicBoolean;35import java.util.concurrent.atomic.AtomicReference;36import java.util.concurrent.CountDownLatch;37import static java.util.concurrent.TimeUnit.MILLISECONDS;38import jdk.testlibrary.JDKToolLauncher;39import jdk.testlibrary.Utils;4041public class MultiDead {42private static final int THREAD_PAIR_COUNT = 4;43private static final int CHILDREN_COUNT = 20;44// at least 2.5 seconds for a child to complete45private static final long CHILD_TIMEOUT = 2500;46private static final long TIMEOUT =47Utils.adjustTimeout(CHILDREN_COUNT * CHILD_TIMEOUT * 2);4849public static void main(String[] args) throws Throwable {50if (args.length == 0 || args[0].equals("parent")) {51parentProcess();52}5354if (args.length > 0 && args[0].equals("child")) {55childProcess();56}57}5859private static void parentProcess() throws Throwable {60JDKToolLauncher launcher = JDKToolLauncher61.createUsingTestJDK("java")62.addToolArg("MultiDead")63.addToolArg("child");64ProcessBuilder pb = new ProcessBuilder(launcher.getCommand());6566AtomicReference<Process> child = new AtomicReference<>();67AtomicBoolean stopFlag = new AtomicBoolean(false);6869Thread th = new Thread(() -> {70for (int i = 0; i < CHILDREN_COUNT; ++i) {71System.out.println("child #" + (i + 1) + " of " +72CHILDREN_COUNT);73long start = System.nanoTime();74try {75child.set(pb.start());76child.get().waitFor();77if (stopFlag.get()) {78break;79}80} catch (Exception e) {81throw new RuntimeException(e);82}83if (System.nanoTime() - start >84MILLISECONDS.toNanos(CHILD_TIMEOUT)) {85System.err.println("Machine is too slow, " +86"skipping the test...");87break;88}89}90});9192th.start();93th.join(TIMEOUT);9495stopFlag.set(true);96if (th.isAlive()) {97if (child.get() != null) {98child.get().destroyForcibly();99}100throw new RuntimeException("Failed to complete on time.");101}102}103104private static void childProcess() {105CountDownLatch latch = new CountDownLatch(1);106for (int i = 0; i < THREAD_PAIR_COUNT; ++i) {107new Thread(() -> {108try {109latch.await();110try (MulticastSocket a = new MulticastSocket(6000)) {111}112} catch (Exception ignore) {113}114}).start();115116new Thread(() -> {117try {118latch.await();119try (DatagramSocket b = new DatagramSocket(6000)) {120}121} catch (Exception ignore) {122}123}).start();124}125latch.countDown();126}127}128129130