KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > notification > bugs > DeadlockBugTest


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

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 /**
40  * @author Alphonse Bendt
41  * @version $Id: DeadlockBugTest.java,v 1.3 2005/04/27 10:49:51 alphonse.bendt Exp $
42  */

43 public class DeadlockBugTest extends TestCase
44 {
45     RWLockEventQueueDecorator objectUnderTest_;
46
47     Object JavaDoc lock_;
48
49     /*
50      * @see TestCase#setUp()
51      */

52     protected void setUp() throws Exception JavaDoc
53     {
54         lock_ = new Object JavaDoc();
55
56         MessageQueue queue = new BoundedFifoEventQueue(4, EventQueueOverflowStrategy.FIFO, lock_);
57
58         objectUnderTest_ = new RWLockEventQueueDecorator(new BasicMessageQueueAdapter(queue));
59     }
60
61     /**
62      * Constructor for AbstractProxySupplierTest.
63      *
64      * @param name
65      */

66     public DeadlockBugTest(String JavaDoc name)
67     {
68         super(name);
69     }
70
71     /**
72      * Test that should provoke bug ID 544. getter thread
73      * needs to invoke getMessageBlocking() on an empty queue first. then second putter thread tries to
74      * enqueue a message into the queue which then should be returned to the first thread.
75      * wrong synchronization led to a deadlock.
76      */

77     public void testDeadlock() throws Exception JavaDoc
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 JavaDoc()
87         {
88             public void run()
89             {
90                 threadsDone.release();
91             }
92         });
93
94         Runnable JavaDoc getCommand = new Runnable JavaDoc()
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 JavaDoc e)
110                 {
111                     fail();
112                 }
113             }
114         };
115
116         Runnable JavaDoc putCommand = new Runnable JavaDoc()
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 JavaDoc e)
130                 {
131                     fail();
132                 }
133             }
134         };
135
136         Thread JavaDoc getter = new Thread JavaDoc(getCommand);
137         getter.setDaemon(true);
138
139         Thread JavaDoc putter = new Thread JavaDoc(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