Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/connection/DeadLockTest.java
38867 views
/*1* Copyright (c) 2004, 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 503921026* @summary test on a client notification deadlock.27* @author Shanliang JIANG28* @run clean DeadLockTest29* @run build DeadLockTest30* @run main DeadLockTest31*/3233import java.net.MalformedURLException;34import java.io.IOException;35import java.util.HashMap;3637import javax.management.*;38import javax.management.remote.*;3940public class DeadLockTest {41private static final String[] protocols = {"rmi", "iiop", "jmxmp"};42private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4344public static void main(String[] args) {45System.out.println(">>> test on a client notification deadlock.");4647boolean ok = true;48for (int i = 0; i < protocols.length; i++) {49try {50test(protocols[i]);51} catch (Exception e) {52System.out.println(">>> Test failed for " + protocols[i]);53e.printStackTrace(System.out);54}55}5657System.out.println(">>> Test passed");58}5960private static void test(String proto)61throws Exception {62System.out.println(">>> Test for protocol " + proto);6364JMXServiceURL u = null;65JMXConnectorServer server = null;6667HashMap env = new HashMap(2);68// server will close a client connection after 1 second69env.put("jmx.remote.x.server.connection.timeout", "1000");7071// disable the client ping72env.put("jmx.remote.x.client.connection.check.period", "0");7374try {75u = new JMXServiceURL(proto, null, 0);76server = JMXConnectorServerFactory.newJMXConnectorServer(u, env, mbs);77} catch (MalformedURLException e) {78System.out.println(">>> Skipping unsupported URL " + proto);79}8081server.start();8283JMXServiceURL addr = server.getAddress();8485long st = 2000;86MyListener myListener;8788// a cycle to make sure that we test the blocking problem.89do {90JMXConnector client = JMXConnectorFactory.connect(addr, env);91MBeanServerConnection conn = client.getMBeanServerConnection();92myListener = new MyListener(conn);93client.addConnectionNotificationListener(myListener, null, null);9495// wait the server to close the client connection96Thread.sleep(st);9798// makes the listener to do a remote request via the connection99// which should be closed by the server.100conn.getDefaultDomain();101102// allow the listner to have time to work103Thread.sleep(100);104105// get a closed notif, should no block.106client.close();107Thread.sleep(100);108109st += 2000;110111} while(!myListener.isDone());112113server.stop();114}115116private static class MyListener implements NotificationListener {117public MyListener(MBeanServerConnection conn) {118this.conn = conn;119}120121public void handleNotification(Notification n, Object h) {122if (n instanceof JMXConnectionNotification) {123JMXConnectionNotification jcn = (JMXConnectionNotification)n;124final String type = jcn.getType();125System.out.println(">>> The listener receives notif with the type:"+type);126127if (JMXConnectionNotification.CLOSED.equals(type) ||128JMXConnectionNotification.FAILED.equals(type)) {129130synchronized(this) {131done = false;132}133134try {135conn.getDefaultDomain();136} catch (IOException ioe) {137// Greate !138}139140synchronized(this) {141done = true;142}143144System.out.println(">>> The listener is not blocked!");145}146}147}148149public boolean isDone() {150synchronized(this) {151return done;152}153}154155private boolean done = false;156private MBeanServerConnection conn;157}158}159160161