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