jdk/test/javax/management/eventService/LeaseManagerDeadlockTest.java
author dfuchs
Wed, 10 Sep 2008 16:27:13 +0200
changeset 1227 4546977d0d66
parent 1154 4561ee1fb2b7
permissions -rw-r--r--
6746754: jmx namespace: test for leading separator missing 6669137: RFE: InstanceNotFoundException should have a constructor that takes an ObjectName 6746796: jmx namespaces: Several tests are missing an @bug or @run keyword Summary: Note on 6669137: first implementation of 6669137 was actually pushed with 5072476 - here we only have a small update and a test case. Also re-fixes 6737133: Compilation failure of test/javax/management/eventService/LeaseManagerDeadlockTest.java which had failed. Reviewed-by: emcmanus, yjoan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     1
/*
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     4
 *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     8
 *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    13
 * accompanied this code).
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    14
 *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    18
 *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    21
 * have any questions.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    22
 */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    23
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    24
/*
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    25
 * @test
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    26
 * @bug 6717789
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    27
 * @summary Check that a lock is not held when a LeaseManager expires.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    28
 * @author Eamonn McManus
1154
4561ee1fb2b7 6405862: Allow CompositeType to have zero items
emcmanus
parents: 1004
diff changeset
    29
 * @compile -XDignore.symbol.file=true LeaseManagerDeadlockTest.java
1227
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1154
diff changeset
    30
 * @run main LeaseManagerDeadlockTest
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    31
 */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    32
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    33
import com.sun.jmx.event.LeaseManager;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    34
import java.lang.management.ManagementFactory;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    35
import java.lang.management.ThreadMXBean;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    36
import java.util.Arrays;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    37
import java.util.concurrent.Semaphore;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    38
import java.util.concurrent.TimeUnit;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    39
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    40
public class LeaseManagerDeadlockTest {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    41
    public static String failure;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    42
    public static LeaseManager leaseManager;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    43
    public static Semaphore callbackThreadCompleted = new Semaphore(0);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    44
    public static Object lock = new Object();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    45
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    46
    public static Runnable triggerDeadlock = new Runnable() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    47
        public void run() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    48
            Runnable pingLeaseManager = new Runnable() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    49
                public void run() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    50
                    System.out.println("Ping thread starts");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    51
                    synchronized (lock) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    52
                        leaseManager.lease(1);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    53
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    54
                    System.out.println("Ping thread completes");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    55
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    56
            };
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    57
            Thread t = new Thread(pingLeaseManager);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    58
            t.start();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    59
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    60
                Thread.sleep(10);  // enough time for ping thread to grab lock
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    61
                synchronized (lock) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    62
                    t.join();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    63
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    64
            } catch (InterruptedException e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    65
                fail(e.toString());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    66
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    67
            System.out.println("Callback thread completes");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    68
            callbackThreadCompleted.release();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    69
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    70
    };
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    71
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    72
    public static void main(String[] args) throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    73
        // Also test that we can shorten the lease from its initial value.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    74
        leaseManager = new LeaseManager(triggerDeadlock, 1000000);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    75
        leaseManager.lease(1L);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    76
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    77
        boolean callbackRan =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    78
                callbackThreadCompleted.tryAcquire(3, TimeUnit.SECONDS);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    79
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    80
        if (!callbackRan) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    81
            fail("Callback did not complete - probable deadlock");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    82
            ThreadMXBean threads = ManagementFactory.getThreadMXBean();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    83
            System.out.println(Arrays.toString(threads.findDeadlockedThreads()));
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    84
            System.out.println("PRESS RETURN");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    85
            System.in.read();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    86
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    87
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    88
        if (failure == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    89
            System.out.println("TEST PASSED");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    90
        else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    91
            throw new Exception("TEST FAILED: " + failure);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    92
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    93
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    94
    public static void fail(String why) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    95
        System.out.println("TEST FAILS: " + why);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    96
        failure = why;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    97
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    98
}