1 21 22 package org.jacorb.test.notification.bugs; 23 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 28 import org.jacorb.notification.AnyMessage; 29 import org.jacorb.notification.queue.BasicMessageQueueAdapter; 30 import org.jacorb.notification.queue.BoundedFifoEventQueue; 31 import org.jacorb.notification.queue.EventQueueOverflowStrategy; 32 import org.jacorb.notification.queue.MessageQueue; 33 import org.jacorb.notification.queue.RWLockEventQueueDecorator; 34 35 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 36 import EDU.oswego.cs.dl.util.concurrent.Latch; 37 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean; 38 39 43 public class DeadlockBugTest extends TestCase 44 { 45 RWLockEventQueueDecorator objectUnderTest_; 46 47 Object lock_; 48 49 52 protected void setUp() throws Exception 53 { 54 lock_ = new Object (); 55 56 MessageQueue queue = new BoundedFifoEventQueue(4, EventQueueOverflowStrategy.FIFO, lock_); 57 58 objectUnderTest_ = new RWLockEventQueueDecorator(new BasicMessageQueueAdapter(queue)); 59 } 60 61 66 public DeadlockBugTest(String name) 67 { 68 super(name); 69 } 70 71 77 public void testDeadlock() throws Exception 78 { 79 final AnyMessage mesg = new AnyMessage(); 80 81 final SynchronizedBoolean received = new SynchronizedBoolean(false); 82 final SynchronizedBoolean delivered = new SynchronizedBoolean(false); 83 final Latch threadsDone = new Latch(); 84 final Latch getPutOrder = new Latch(); 85 86 final CyclicBarrier barrier = new CyclicBarrier(2, new Runnable () 87 { 88 public void run() 89 { 90 threadsDone.release(); 91 } 92 }); 93 94 Runnable getCommand = new Runnable () 95 { 96 public void run() 97 { 98 try 99 { 100 synchronized (lock_) 101 { 102 getPutOrder.release(); 103 objectUnderTest_.getMessageBlocking(); 104 } 105 106 received.set(true); 107 108 barrier.barrier(); 109 } catch (Exception e) 110 { 111 fail(); 112 } 113 } 114 }; 115 116 Runnable putCommand = new Runnable () 117 { 118 public void run() 119 { 120 try 121 { 122 getPutOrder.acquire(); 123 124 objectUnderTest_.enqeue(mesg.getHandle()); 125 126 delivered.set(true); 127 128 barrier.barrier(); 129 } catch (Exception e) 130 { 131 fail(); 132 } 133 } 134 }; 135 136 Thread getter = new Thread (getCommand); 137 getter.setDaemon(true); 138 139 Thread putter = new Thread (putCommand); 140 putter.setDaemon(true); 141 142 getter.start(); 143 144 putter.start(); 145 146 threadsDone.attempt(1000); 147 148 assertTrue(delivered.get()); 149 assertTrue(received.get()); 150 151 getter.interrupt(); 152 putter.interrupt(); 153 } 154 155 public static Test suite() 156 { 157 return new TestSuite(DeadlockBugTest.class); 158 } 159 } | Popular Tags |