|
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 |
|
29 */ |
|
30 |
|
31 import com.sun.jmx.event.LeaseManager; |
|
32 import java.lang.management.ManagementFactory; |
|
33 import java.lang.management.ThreadMXBean; |
|
34 import java.util.Arrays; |
|
35 import java.util.concurrent.Semaphore; |
|
36 import java.util.concurrent.TimeUnit; |
|
37 |
|
38 public class LeaseManagerDeadlockTest { |
|
39 public static String failure; |
|
40 public static LeaseManager leaseManager; |
|
41 public static Semaphore callbackThreadCompleted = new Semaphore(0); |
|
42 public static Object lock = new Object(); |
|
43 |
|
44 public static Runnable triggerDeadlock = new Runnable() { |
|
45 public void run() { |
|
46 Runnable pingLeaseManager = new Runnable() { |
|
47 public void run() { |
|
48 System.out.println("Ping thread starts"); |
|
49 synchronized (lock) { |
|
50 leaseManager.lease(1); |
|
51 } |
|
52 System.out.println("Ping thread completes"); |
|
53 } |
|
54 }; |
|
55 Thread t = new Thread(pingLeaseManager); |
|
56 t.start(); |
|
57 try { |
|
58 Thread.sleep(10); // enough time for ping thread to grab lock |
|
59 synchronized (lock) { |
|
60 t.join(); |
|
61 } |
|
62 } catch (InterruptedException e) { |
|
63 fail(e.toString()); |
|
64 } |
|
65 System.out.println("Callback thread completes"); |
|
66 callbackThreadCompleted.release(); |
|
67 } |
|
68 }; |
|
69 |
|
70 public static void main(String[] args) throws Exception { |
|
71 // Also test that we can shorten the lease from its initial value. |
|
72 leaseManager = new LeaseManager(triggerDeadlock, 1000000); |
|
73 leaseManager.lease(1L); |
|
74 |
|
75 boolean callbackRan = |
|
76 callbackThreadCompleted.tryAcquire(3, TimeUnit.SECONDS); |
|
77 |
|
78 if (!callbackRan) { |
|
79 fail("Callback did not complete - probable deadlock"); |
|
80 ThreadMXBean threads = ManagementFactory.getThreadMXBean(); |
|
81 System.out.println(Arrays.toString(threads.findDeadlockedThreads())); |
|
82 System.out.println("PRESS RETURN"); |
|
83 System.in.read(); |
|
84 } |
|
85 |
|
86 if (failure == null) |
|
87 System.out.println("TEST PASSED"); |
|
88 else |
|
89 throw new Exception("TEST FAILED: " + failure); |
|
90 } |
|
91 |
|
92 public static void fail(String why) { |
|
93 System.out.println("TEST FAILS: " + why); |
|
94 failure = why; |
|
95 } |
|
96 } |