Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/StringMonitorDeadlockTest.java
38838 views
/*1* Copyright (c) 2005, 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 630318726* @summary Test that no locks are held when a monitor attribute is sampled27* or notif delivered.28* @author Eamonn McManus29* @run clean StringMonitorDeadlockTest30* @run build StringMonitorDeadlockTest31* @run main StringMonitorDeadlockTest 132* @run main StringMonitorDeadlockTest 233* @run main StringMonitorDeadlockTest 334* @run main StringMonitorDeadlockTest 435*/3637import java.lang.management.ManagementFactory;38import java.util.concurrent.atomic.AtomicInteger;39import javax.management.JMX;40import javax.management.MBeanServer;41import javax.management.Notification;42import javax.management.NotificationListener;43import javax.management.ObjectName;44import javax.management.monitor.StringMonitor;45import javax.management.monitor.StringMonitorMBean;4647public class StringMonitorDeadlockTest {4849public static void main(String[] args) throws Exception {50if (args.length != 1)51throw new Exception("Arg should be test number");52int testNo = Integer.parseInt(args[0]) - 1;53TestCase test = testCases[testNo];54System.out.println("Test: " + test.getDescription());55test.run();56System.out.println("Test passed");57}5859private static enum When {IN_GET_ATTRIBUTE, IN_NOTIFY};6061private static abstract class TestCase {62TestCase(String description, When when) {63this.description = description;64this.when = when;65}6667void run() throws Exception {68final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();69final ObjectName observedName = new ObjectName("a:b=c");70final ObjectName monitorName = new ObjectName("a:type=Monitor");71mbs.registerMBean(new StringMonitor(), monitorName);72final StringMonitorMBean monitorProxy =73JMX.newMBeanProxy(mbs, monitorName, StringMonitorMBean.class);74final TestMBean observedProxy =75JMX.newMBeanProxy(mbs, observedName, TestMBean.class);7677final Runnable sensitiveThing = new Runnable() {78public void run() {79doSensitiveThing(monitorProxy, observedName);80}81};8283final Runnable nothing = new Runnable() {84public void run() {}85};8687final Runnable withinGetAttribute =88(when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;8990mbs.registerMBean(new Test(withinGetAttribute), observedName);91monitorProxy.addObservedObject(observedName);92monitorProxy.setObservedAttribute("Thing");93monitorProxy.setStringToCompare("old");94monitorProxy.setGranularityPeriod(10L); // 10 ms95monitorProxy.setNotifyDiffer(true);9697final int initGetCount = observedProxy.getGetCount();98monitorProxy.start();99100int getCount = initGetCount;101for (int i = 0; i < 500; i++) { // 500 * 10 = 5 seconds102getCount = observedProxy.getGetCount();103if (getCount != initGetCount)104break;105Thread.sleep(10);106}107if (getCount <= initGetCount)108throw new Exception("Test failed: presumable deadlock");109// This won't show up as a deadlock in CTRL-\ or in110// ThreadMXBean.findDeadlockedThreads(), because they don't111// see that thread A is waiting for thread B (B.join()), and112// thread B is waiting for a lock held by thread A113114// Now we know the monitor has observed the initial value,115// so if we want to test notify behaviour we can trigger by116// exceeding the threshold.117if (when == When.IN_NOTIFY) {118final AtomicInteger notifCount = new AtomicInteger();119final NotificationListener listener = new NotificationListener() {120public void handleNotification(Notification n, Object h) {121Thread t = new Thread(sensitiveThing);122t.start();123try {124t.join();125} catch (InterruptedException e) {126throw new RuntimeException(e);127}128notifCount.incrementAndGet();129}130};131mbs.addNotificationListener(monitorName, listener, null, null);132observedProxy.setThing("new");133for (int i = 0; i < 500 && notifCount.get() == 0; i++)134Thread.sleep(10);135if (notifCount.get() == 0)136throw new Exception("Test failed: presumable deadlock");137}138139}140141abstract void doSensitiveThing(StringMonitorMBean monitorProxy,142ObjectName observedName);143144String getDescription() {145return description;146}147148private final String description;149private final When when;150}151152private static final TestCase[] testCases = {153new TestCase("Remove monitored MBean within monitored getAttribute",154When.IN_GET_ATTRIBUTE) {155@Override156void doSensitiveThing(StringMonitorMBean monitorProxy,157ObjectName observedName) {158monitorProxy.removeObservedObject(observedName);159}160},161new TestCase("Stop monitor within monitored getAttribute",162When.IN_GET_ATTRIBUTE) {163@Override164void doSensitiveThing(StringMonitorMBean monitorProxy,165ObjectName observedName) {166monitorProxy.stop();167}168},169new TestCase("Remove monitored MBean within threshold listener",170When.IN_NOTIFY) {171@Override172void doSensitiveThing(StringMonitorMBean monitorProxy,173ObjectName observedName) {174monitorProxy.removeObservedObject(observedName);175}176},177new TestCase("Stop monitor within threshold listener",178When.IN_NOTIFY) {179@Override180void doSensitiveThing(StringMonitorMBean monitorProxy,181ObjectName observedName) {182monitorProxy.stop();183}184},185};186187public static interface TestMBean {188public String getThing();189public void setThing(String thing);190public int getGetCount();191}192193public static class Test implements TestMBean {194public Test(Runnable runWithinGetAttribute) {195this.runWithinGetAttribute = runWithinGetAttribute;196}197198public String getThing() {199Thread t = new Thread(runWithinGetAttribute);200t.start();201try {202t.join();203} catch (InterruptedException e) {204throw new RuntimeException(e);205}206getCount++;207return thing;208}209210public void setThing(String thing) {211this.thing = thing;212}213214public int getGetCount() {215return getCount;216}217218private final Runnable runWithinGetAttribute;219private volatile int getCount;220private volatile String thing;221}222}223224225