Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
1 package org.apidesign.javamonitorflaws;
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import org.netbeans.junit.NbTestCase;
7 public class CacheTest extends NbTestCase {
8 public CacheTest(String n) {
13 protected int timeOut() {
18 * To simulate deadlock between the cache's setMultiply method and
19 * ToDeadlock's own lock. The root cause is that setMultiply is calling
20 * foreign code while holding the caches's <code>this</code> lock:
22 Thread Test Watch Dog: testDeadlockWithSetter
23 org.apidesign.javamonitorflaws.MultiplyCache.setMultiply:19
24 org.apidesign.javamonitorflaws.CacheTest$1ToDeadlock.assertMultiplyByTen:40
25 org.apidesign.javamonitorflaws.CacheTest.testDeadlockWithSetter:50
26 sun.reflect.NativeMethodAccessorImpl.invoke0:-2
27 sun.reflect.NativeMethodAccessorImpl.invoke:39
28 sun.reflect.DelegatingMethodAccessorImpl.invoke:25
29 java.lang.reflect.Method.invoke:597
30 junit.framework.TestCase.runTest:168
31 org.netbeans.junit.NbTestCase.access$200:84
32 org.netbeans.junit.NbTestCase$2.doSomething:328
33 org.netbeans.junit.NbTestCase$1Guard.run:265
34 java.lang.Thread.run:619
36 org.apidesign.javamonitorflaws.CacheTest$1ToDeadlock.assertMultiplyByTen:40
37 org.apidesign.javamonitorflaws.CacheTest$1ToDeadlock.propertyChange:36
38 java.beans.PropertyChangeSupport.firePropertyChange:339
39 java.beans.PropertyChangeSupport.firePropertyChange:276
40 java.beans.PropertyChangeSupport.firePropertyChange:297
41 org.apidesign.javamonitorflaws.MultiplyCache.setMultiply:21
42 org.apidesign.javamonitorflaws.CacheTest$1ToDeadlock.run:28
43 java.lang.Thread.run:619
46 public void testDeadlockWithSetter() throws Exception {
47 if (Boolean.getBoolean("no.failures")) return;
49 final CacheToTest cache = new MultiplyCache();
51 class ToDeadlock implements Runnable, PropertyChangeListener {
55 cache.setMultiply(10);
57 public void propertyChange(PropertyChangeEvent evt) {
60 } catch (InterruptedException ex) {
63 changeMultiplyToSeven();
66 public synchronized void changeMultiplyToSeven() {
70 ToDeadlock toDeadlock = new ToDeadlock();
71 cache.addPropertyChangeListener(toDeadlock);
72 Thread t = new Thread(toDeadlock, "to deadlock");
77 toDeadlock.changeMultiplyToSeven();
80 /** Shows that one can deadlock with the cache's API even the API
81 * is locally correctly synchronized.
82 Thread Test Watch Dog: testDeadlockWithTheAPICacheItself
83 org.apidesign.javamonitorflaws.Cache.get:16
84 org.apidesign.javamonitorflaws.CacheTest$2ToDeadlock.assertMultiplyByTen
85 org.apidesign.javamonitorflaws.CacheTest.testDeadlockViaAPI:112
86 java.lang.reflect.Method.invoke:597
87 org.netbeans.junit.NbTestCase.access$200:84
88 org.netbeans.junit.NbTestCase$2.doSomething:328
89 org.netbeans.junit.NbTestCase$1Guard.run:265
90 java.lang.Thread.run:619
91 Thread Deadlock using API
92 org.apidesign.javamonitorflaws.CacheTest$2ToDeadlock.assertMultiplyByTen
93 org.apidesign.javamonitorflaws.CacheTest$2ToDeadlock.propertyChange:98
94 java.beans.PropertyChangeSupport.firePropertyChange:339
95 java.beans.PropertyChangeSupport.firePropertyChange:276
96 java.beans.PropertyChangeSupport.firePropertyChange:297
97 org.apidesign.javamonitorflaws.MultiplyCache.setMultiply:21
98 org.apidesign.javamonitorflaws.CacheTest$2ToDeadlock.run:90
99 java.lang.Thread.run:619
101 public void testDeadlockViaAPI() throws Exception {
102 if (Boolean.getBoolean("no.failures")) return;
103 testDeadlockViaAPI(new MultiplyCache());
106 // BEGIN: monitor.pitfalls.block.propertychange
107 private static void testDeadlockViaAPI(final CacheToTest cache)
109 class ToDeadlock implements Runnable, PropertyChangeListener {
113 cache.setMultiply(10);
115 public void propertyChange(PropertyChangeEvent evt) {
118 } catch (InterruptedException ex) {
123 private synchronized void storeMultiply()
124 throws InterruptedException {
125 lastMultiply = cache.getMultiply();
126 // simulates "starvation"
130 public void assertMultiplyByTen() {
133 ToDeadlock toDeadlock = new ToDeadlock();
134 cache.addPropertyChangeListener(toDeadlock);
135 Thread t = new Thread(toDeadlock, "Deadlock using API");
140 // BEGIN: monitor.pitfalls.brokencall
141 int value = cache.get("123");
142 assertEquals("3*10=30", 30, value);
143 // END: monitor.pitfalls.brokencall
145 // END: monitor.pitfalls.block.propertychange
147 public void testDeadlockViaAPIWithCacheOK() throws Exception {
148 testDeadlockViaAPI(new MultiplyCacheOK());
151 static interface CacheToTest {
152 public Integer get(String key);
154 public void setMultiply(int m);
155 public int getMultiply();
156 public void addPropertyChangeListener(PropertyChangeListener l);
157 public void removePropertyChangeListener(PropertyChangeListener l);