jdk/test/javax/management/monitor/StringMonitorDeadlockTest.java
author stefank
Fri, 17 Apr 2015 17:10:38 +0000
changeset 30266 ef82cd1f2db3
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6303187
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that no locks are held when a monitor attribute is sampled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * or notif delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean StringMonitorDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build StringMonitorDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main StringMonitorDeadlockTest 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main StringMonitorDeadlockTest 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run main StringMonitorDeadlockTest 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @run main StringMonitorDeadlockTest 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.concurrent.atomic.AtomicInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.JMX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.monitor.StringMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.monitor.StringMonitorMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class StringMonitorDeadlockTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (args.length != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            throw new Exception("Arg should be test number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        int testNo = Integer.parseInt(args[0]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        TestCase test = testCases[testNo];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        System.out.println("Test: " + test.getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static enum When {IN_GET_ATTRIBUTE, IN_NOTIFY};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static abstract class TestCase {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        TestCase(String description, When when) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            this.when = when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            final ObjectName observedName = new ObjectName("a:b=c");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            final ObjectName monitorName = new ObjectName("a:type=Monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            mbs.registerMBean(new StringMonitor(), monitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            final StringMonitorMBean monitorProxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                JMX.newMBeanProxy(mbs, monitorName, StringMonitorMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            final TestMBean observedProxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                JMX.newMBeanProxy(mbs, observedName, TestMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            final Runnable sensitiveThing = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    doSensitiveThing(monitorProxy, observedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            final Runnable nothing = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                public void run() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            final Runnable withinGetAttribute =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            mbs.registerMBean(new Test(withinGetAttribute), observedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            monitorProxy.addObservedObject(observedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            monitorProxy.setObservedAttribute("Thing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            monitorProxy.setStringToCompare("old");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            monitorProxy.setGranularityPeriod(10L); // 10 ms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            monitorProxy.setNotifyDiffer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            monitorProxy.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            final int initGetCount = observedProxy.getGetCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            int getCount = initGetCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            for (int i = 0; i < 500; i++) { // 500 * 10 = 5 seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                getCount = observedProxy.getGetCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                if (getCount != initGetCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (getCount <= initGetCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                throw new Exception("Test failed: presumable deadlock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // This won't show up as a deadlock in CTRL-\ or in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // ThreadMXBean.findDeadlockedThreads(), because they don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // see that thread A is waiting for thread B (B.join()), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // thread B is waiting for a lock held by thread A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            // Now we know the monitor has observed the initial value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // so if we want to test notify behaviour we can trigger by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            // exceeding the threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (when == When.IN_NOTIFY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                final AtomicInteger notifCount = new AtomicInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                final NotificationListener listener = new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    public void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        Thread t = new Thread(sensitiveThing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                            t.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        notifCount.incrementAndGet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                mbs.addNotificationListener(monitorName, listener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                observedProxy.setThing("new");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                for (int i = 0; i < 500 && notifCount.get() == 0; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (notifCount.get() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    throw new Exception("Test failed: presumable deadlock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        abstract void doSensitiveThing(StringMonitorMBean monitorProxy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                       ObjectName observedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        String getDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        private final String description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        private final When when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static final TestCase[] testCases = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        new TestCase("Remove monitored MBean within monitored getAttribute",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                     When.IN_GET_ATTRIBUTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            void doSensitiveThing(StringMonitorMBean monitorProxy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                  ObjectName observedName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                monitorProxy.removeObservedObject(observedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        new TestCase("Stop monitor within monitored getAttribute",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                     When.IN_GET_ATTRIBUTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            void doSensitiveThing(StringMonitorMBean monitorProxy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                  ObjectName observedName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                monitorProxy.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        new TestCase("Remove monitored MBean within threshold listener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                     When.IN_NOTIFY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            void doSensitiveThing(StringMonitorMBean monitorProxy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                  ObjectName observedName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                monitorProxy.removeObservedObject(observedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        new TestCase("Stop monitor within threshold listener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                     When.IN_NOTIFY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            void doSensitiveThing(StringMonitorMBean monitorProxy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                  ObjectName observedName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                monitorProxy.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public static interface TestMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        public String getThing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        public void setThing(String thing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        public int getGetCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public static class Test implements TestMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        public Test(Runnable runWithinGetAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            this.runWithinGetAttribute = runWithinGetAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        public String getThing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            Thread t = new Thread(runWithinGetAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                t.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            getCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return thing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        public void setThing(String thing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            this.thing = thing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        public int getGetCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return getCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        private final Runnable runWithinGetAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        private volatile int getCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        private volatile String thing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
}