jdk/test/javax/management/notification/BroadcasterSupportDeadlockTest.java
author rprotacio
Wed, 02 Mar 2016 15:10:38 -0500
changeset 36396 26a241a959de
parent 30376 2ccf2cf7ea48
child 44423 306c020eb154
permissions -rw-r--r--
8145098: JNI GetVersion should return JNI_VERSION_9 Summary: Updated JNI_VERSION for current version to be JNI_VERSION_9 Reviewed-by: hseigel, gtriantafill, dholmes, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2015, 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 5093922 2120055
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that NotificationBroadcasterSupport can be subclassed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * and used with synchronized(this) without causing deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Eamonn McManus
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    30
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean BroadcasterSupportDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build BroadcasterSupportDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main BroadcasterSupportDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class BroadcasterSupportDeadlockTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            Class.forName(ManagementFactory.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            System.out.println("TEST CANNOT RUN: needs JDK 5 at least");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        final BroadcasterMBean mbean = new Broadcaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        final ObjectName name = new ObjectName("test:type=Broadcaster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        mbs.registerMBean(mbean, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        ThreadMXBean threads = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        threads.setThreadContentionMonitoringEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        final Semaphore semaphore = new Semaphore(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // Thread 1 - block the Broadcaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Thread t1 = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    mbs.invoke(name, "block",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                               new Object[] {semaphore},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                               new String[] {Semaphore.class.getName()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    System.out.println("TEST INCORRECT: block returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        t1.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        t1.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        /* Wait for Thread 1 to be doing Object.wait().  It's very
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
           difficult to synchronize properly here so we wait for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
           semaphore, then wait a little longer for the mbs.invoke to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
           run, then just in case that isn't enough, we wait for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
           thread to be in WAITING state.  This isn't foolproof,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
           because the machine could be very slow and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
           Thread.getState() could find the thread in WAITING state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
           due to some operation it does on its way to the one we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
           interested in.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        semaphore.acquire();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        while (t1.getState() != Thread.State.WAITING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            Thread.sleep(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // Thread 2 - try to add a listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        final NotificationListener listener = new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            public void handleNotification(Notification n, Object h) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Thread t2 = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    mbs.addNotificationListener(name, listener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    System.out.println("TEST INCORRECT: addNL failed:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        t2.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        t2.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /* Wait for Thread 2 to be blocked on the monitor or to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
           succeed.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for (int i = 0; i < 1000/*ms*/; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            t2.join(1/*ms*/);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            switch (t2.getState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            case TERMINATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                System.out.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            case BLOCKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                java.util.Map<Thread,StackTraceElement[]> traces =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    Thread.getAllStackTraces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                showStackTrace("Thread 1", traces.get(t1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                showStackTrace("Thread 2", traces.get(t2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                System.out.println("TEST FAILED: deadlock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println("TEST FAILED BUT DID NOT NOTICE DEADLOCK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Thread.sleep(10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static void showStackTrace(String title,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                       StackTraceElement[] stack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        System.out.println("---" + title + "---");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (stack == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            System.out.println("<no stack trace???>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            for (StackTraceElement elmt : stack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                System.out.println("    " + elmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static interface BroadcasterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        public void block(Semaphore semaphore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static class Broadcaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            implements BroadcasterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        public synchronized void block(Semaphore semaphore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    // Let the caller know that it can now wait for us to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    // hit the WAITING state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    semaphore.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    lock.wait(); // block forever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    System.out.println("TEST INCORRECT: lock interrupted:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}