Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/DeadLockTest.java
41159 views
/*1* Copyright (c) 2004, 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 503921026* @summary test on a client notification deadlock.27* @author Shanliang JIANG28*29* @run clean DeadLockTest30* @run build DeadLockTest31* @run main DeadLockTest32*/3334import java.net.MalformedURLException;35import java.io.IOException;36import java.util.HashMap;3738import javax.management.*;39import javax.management.remote.*;4041public class DeadLockTest {42private static final String[] protocols = {"rmi", "iiop", "jmxmp"};43private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4445public static void main(String[] args) {46System.out.println(">>> test on a client notification deadlock.");4748boolean ok = true;49for (int i = 0; i < protocols.length; i++) {50try {51test(protocols[i]);52} catch (Exception e) {53System.out.println(">>> Test failed for " + protocols[i]);54e.printStackTrace(System.out);55}56}5758System.out.println(">>> Test passed");59}6061private static void test(String proto)62throws Exception {63System.out.println(">>> Test for protocol " + proto);6465JMXServiceURL u = null;66JMXConnectorServer server = null;6768HashMap env = new HashMap(2);69// server will close a client connection after 1 second70env.put("jmx.remote.x.server.connection.timeout", "1000");7172// disable the client ping73env.put("jmx.remote.x.client.connection.check.period", "0");7475try {76u = new JMXServiceURL(proto, null, 0);77server = JMXConnectorServerFactory.newJMXConnectorServer(u, env, mbs);78} catch (MalformedURLException e) {79System.out.println(">>> Skipping unsupported URL " + proto);80}8182server.start();8384JMXServiceURL addr = server.getAddress();8586long st = 2000;87MyListener myListener;8889// a cycle to make sure that we test the blocking problem.90do {91JMXConnector client = JMXConnectorFactory.connect(addr, env);92MBeanServerConnection conn = client.getMBeanServerConnection();93myListener = new MyListener(conn);94client.addConnectionNotificationListener(myListener, null, null);9596// wait the server to close the client connection97Thread.sleep(st);9899// makes the listener to do a remote request via the connection100// which should be closed by the server.101conn.getDefaultDomain();102103// allow the listner to have time to work104Thread.sleep(100);105106// get a closed notif, should no block.107client.close();108Thread.sleep(100);109110st += 2000;111112} while(!myListener.isDone());113114server.stop();115}116117private static class MyListener implements NotificationListener {118public MyListener(MBeanServerConnection conn) {119this.conn = conn;120}121122public void handleNotification(Notification n, Object h) {123if (n instanceof JMXConnectionNotification) {124JMXConnectionNotification jcn = (JMXConnectionNotification)n;125final String type = jcn.getType();126System.out.println(">>> The listener receives notif with the type:"+type);127128if (JMXConnectionNotification.CLOSED.equals(type) ||129JMXConnectionNotification.FAILED.equals(type)) {130131synchronized(this) {132done = false;133}134135try {136conn.getDefaultDomain();137} catch (IOException ioe) {138// Greate !139}140141synchronized(this) {142done = true;143}144145System.out.println(">>> The listener is not blocked!");146}147}148}149150public boolean isDone() {151synchronized(this) {152return done;153}154}155156private boolean done = false;157private MBeanServerConnection conn;158}159}160161162