samples/deadlock/test/org/apidesign/javamonitorflaws/CacheTest.java
changeset 317 e101649dbd17
parent 315 08dd52950883
child 319 6c1d8b5553d8
     1.1 --- a/samples/deadlock/test/org/apidesign/javamonitorflaws/CacheTest.java	Tue Feb 10 18:36:21 2009 +0100
     1.2 +++ b/samples/deadlock/test/org/apidesign/javamonitorflaws/CacheTest.java	Wed Feb 11 08:52:00 2009 +0100
     1.3 @@ -98,25 +98,32 @@
     1.4    org.apidesign.javamonitorflaws.CacheTest$2ToDeadlock.run:90
     1.5    java.lang.Thread.run:619
     1.6       */
     1.7 +    // BEGIN: monitor.pitfalls.block.propertychange
     1.8      public void testDeadlockViaAPI() throws Exception {
     1.9          if (Boolean.getBoolean("no.failures")) return;
    1.10          
    1.11          class ToDeadlock implements Runnable, PropertyChangeListener {
    1.12 +            int lastMultiply;
    1.13 +
    1.14              public void run() {
    1.15                  cache.setMultiply(10);
    1.16              }
    1.17              public void propertyChange(PropertyChangeEvent evt) {
    1.18                  try {
    1.19 -                    Thread.sleep(500);
    1.20 +                    storeMultiply();
    1.21                  } catch (InterruptedException ex) {
    1.22                      // ok
    1.23                  }
    1.24 -                assertMultiplyByTen();
    1.25              }
    1.26  
    1.27 -            public synchronized void assertMultiplyByTen() {
    1.28 -                int value =  cache.get("123");
    1.29 -                assertEquals("3*10=30", 30, value);
    1.30 +            private synchronized void storeMultiply()
    1.31 +            throws InterruptedException {
    1.32 +                lastMultiply = cache.getMultiply();
    1.33 +                // simulates "starvation"
    1.34 +                wait();
    1.35 +            }
    1.36 +
    1.37 +            public void assertMultiplyByTen() {
    1.38              }
    1.39          }
    1.40          ToDeadlock toDeadlock = new ToDeadlock();
    1.41 @@ -126,6 +133,10 @@
    1.42  
    1.43          Thread.sleep(100);
    1.44  
    1.45 -        toDeadlock.assertMultiplyByTen();
    1.46 +        // BEGIN: monitor.pitfalls.brokencall
    1.47 +        int value =  cache.get("123");
    1.48 +        assertEquals("3*10=30", 30, value);
    1.49 +        // END: monitor.pitfalls.brokencall
    1.50      }
    1.51 +    // END: monitor.pitfalls.block.propertychange
    1.52  }
    1.53 \ No newline at end of file