KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > util > ReadPrioritaryFIFOWriteLockTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.util;
26
27 import java.util.Random JavaDoc;
28
29 import org.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock;
30 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
31
32 /**
33  * Reader/Writer Lock test class.
34  *
35  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
36  * @version 1.0
37  */

38 public class ReadPrioritaryFIFOWriteLockTest extends NoTemplate
39 {
40   static final long TIMEOUT = 1000; // in ms
41

42   private ReadPrioritaryFIFOWriteLock lock;
43   private boolean done = false; // To check test
44
// completion
45

46   private int randomTest;
47   private Random JavaDoc r = new Random JavaDoc();
48
49   /**
50    * @see junit.framework.TestCase#setUp()
51    */

52   protected void setUp()
53   {
54     lock = new ReadPrioritaryFIFOWriteLock();
55   }
56
57   /**
58    * Acquire 4 read locks, then release them. Acquire 1 write lock and release
59    * it. Finally acquire 1 read lock and release it.
60    */

61   public void testNonBlocking()
62   {
63     done = false;
64
65     Thread JavaDoc ti[] = new Thread JavaDoc[4];
66
67     // Acquire 4 read locks
68
for (int i = 0; i < 4; i++)
69     {
70       ti[i] = acquireReadDoneTrue();
71       ti[i].start();
72     }
73     try
74     {
75       for (int i = 0; i < 4; i++)
76         ti[i].join(TIMEOUT);
77     }
78     catch (InterruptedException JavaDoc ignore)
79     {
80     }
81     assertEquals("Timeout read lock in NonBlocking test", done, true);
82     // Release read locks
83
for (int i = 0; i < 4; i++)
84       lock.releaseRead();
85
86     // Acquire write lock
87
done = false;
88     Thread JavaDoc t = acquireWriteDoneTrue();
89     t.start();
90     try
91     {
92       t.join(TIMEOUT);
93     }
94     catch (InterruptedException JavaDoc ignore)
95     {
96     }
97     assertEquals("Timeout write lock 1 in NonBlocking test", done, true);
98     // Release write lock
99
lock.releaseWrite();
100
101     // Acquire write lock
102
done = false;
103     t = acquireWriteDoneTrue();
104     t.start();
105     try
106     {
107       t.join(TIMEOUT);
108     }
109     catch (InterruptedException JavaDoc ignore)
110     {
111     }
112     assertEquals("Timeout write lock 2 in NonBlocking test", done, true);
113     // Release write lock
114
lock.releaseWrite();
115
116     done = false;
117     t = acquireReadDoneTrue();
118     t.start();
119     try
120     {
121       t.join(TIMEOUT);
122     }
123     catch (InterruptedException JavaDoc ignore)
124     {
125     }
126     assertEquals("Timeout read lock 2 in NonBlocking test", done, true);
127     // Release write lock
128
lock.releaseRead();
129   }
130
131   /**
132    * Acquire 4 read locks. Acquire 1 write lock and check that the write lock is
133    * blocked. Release each read lock in turn and check that the write lock is
134    * blocked. Finally check that the write lock has been released.
135    */

136   public void testWriteAfterRead()
137   {
138     done = true;
139     // t1 acquires 4 read then 1 write
140
Thread JavaDoc ti[] = new Thread JavaDoc[4];
141
142     // Acquire 4 read locks
143
for (int i = 0; i < 4; i++)
144     {
145       ti[i] = acquireReadDoneFalse();
146       ti[i].start();
147     }
148     try
149     {
150       for (int i = 0; i < 4; i++)
151         ti[i].join(TIMEOUT);
152     }
153     catch (InterruptedException JavaDoc ignore)
154     {
155     }
156     assertEquals("Timeout read lock in NonBlocking test", done, false);
157
158     // Acquire 1 read
159
Thread JavaDoc t = acquireWriteDoneTrue();
160     t.start();
161     try
162     {
163       t.join(TIMEOUT);
164     }
165     catch (InterruptedException JavaDoc ignore)
166     {
167     }
168     assertEquals("Timeout write lock in NonBlocking test", done, false);
169
170     // Release the read locks 1 by 1
171
for (int i = 0; i < 4; i++)
172     {
173       try
174       {
175         t.join(TIMEOUT);
176       }
177       catch (InterruptedException JavaDoc success)
178       {
179       }
180       assertEquals("Write lock should be blocked after (" + i + " release)",
181           done, false);
182       lock.releaseRead();
183     }
184
185     // Now the write lock should be released
186
try
187     {
188       t.join(TIMEOUT);
189     }
190     catch (InterruptedException JavaDoc e)
191     {
192       throw new RuntimeException JavaDoc(e.getMessage());
193     }
194     assertEquals("Timeout in WriteAfterRead test", done, true);
195   }
196
197   /**
198    * Acquire 1 write lock. Acquire 4 read locks and check that they are all
199    * blocked. Release the write lock and check that all read locks have been
200    * released.
201    */

202   public void testReadAfterWrite()
203   {
204     done = false;
205     // t1 acquires 1 write and then 4 read
206
try
207     {
208       lock.acquireWrite();
209     }
210     catch (InterruptedException JavaDoc e)
211     {
212       throw new RuntimeException JavaDoc(e.getMessage());
213     }
214
215     Thread JavaDoc t1 = acquireReadDoneFalse();
216     Thread JavaDoc t2 = acquireReadDoneFalse();
217     Thread JavaDoc t3 = acquireReadDoneTrue();
218
219     t1.start();
220     try
221     {
222       t1.join(TIMEOUT);
223     }
224     catch (InterruptedException JavaDoc success)
225     {
226     }
227     assertEquals("Read lock 1 should be blocked", done, false);
228
229     t2.start();
230     try
231     {
232       t2.join(TIMEOUT);
233     }
234     catch (InterruptedException JavaDoc success)
235     {
236     }
237     assertEquals("Read lock 2 should be blocked", done, false);
238
239     t3.start();
240     try
241     {
242       t3.join(TIMEOUT);
243     }
244     catch (InterruptedException JavaDoc success)
245     {
246     }
247     assertEquals("Read lock 3 should be blocked", done, false);
248
249     // Release the write lock
250
lock.releaseWrite();
251
252     // Now the read locks should be released
253
try
254     {
255       t1.join(TIMEOUT);
256       t2.join(TIMEOUT);
257       t3.join(TIMEOUT);
258     }
259     catch (InterruptedException JavaDoc e)
260     {
261       throw new RuntimeException JavaDoc(e.getMessage());
262     }
263     assertEquals("Timeout in ReadAfterWrite test", done, true);
264   }
265
266   /**
267    * Randomly acquire and release locks. This test should terminate
268    */

269   public void testRandom()
270   {
271     int threadNb = 100;
272     Thread JavaDoc t[] = new Thread JavaDoc[threadNb];
273
274     randomTest = threadNb;
275
276     for (int i = 0; i < threadNb; i++)
277     {
278       if (r.nextGaussian() > 0.5)
279         t[i] = randomAcquireReleaseWrite();
280       else
281         t[i] = randomAcquireReleaseRead();
282     }
283
284     for (int i = 0; i < threadNb; i++)
285       t[i].start();
286
287     try
288     {
289       Thread.sleep(10 * threadNb);
290     }
291     catch (InterruptedException JavaDoc ignore)
292     {
293     }
294
295     try
296     {
297       for (int i = 0; i < threadNb; i++)
298         t[i].join(TIMEOUT);
299     }
300     catch (InterruptedException JavaDoc ignore)
301     {
302     }
303     assertEquals("Bad random test state (" + randomTest
304         + " threads did not finish", randomTest, 0);
305     System.out.println();
306   }
307
308   private synchronized void randomTestComplete()
309   {
310     randomTest--;
311     // System.out.println(randomTest);
312
}
313
314   /*
315    * Private method to call lock acquire methods in a separate thread
316    */

317
318   private Thread JavaDoc acquireReadDoneTrue()
319   {
320     return new Thread JavaDoc(new Runnable JavaDoc()
321     {
322       public void run()
323       {
324         try
325         {
326           lock.acquireRead();
327         }
328         catch (InterruptedException JavaDoc e)
329         {
330           throw new RuntimeException JavaDoc(e.getMessage());
331         }
332         done = true;
333       }
334     });
335   }
336
337   private Thread JavaDoc acquireReadDoneFalse()
338   {
339     return new Thread JavaDoc(new Runnable JavaDoc()
340     {
341       public void run()
342       {
343         try
344         {
345           lock.acquireRead();
346         }
347         catch (InterruptedException JavaDoc e)
348         {
349           throw new RuntimeException JavaDoc(e.getMessage());
350         }
351         done = false;
352       }
353     });
354   }
355
356   private Thread JavaDoc acquireWriteDoneTrue()
357   {
358     return new Thread JavaDoc(new Runnable JavaDoc()
359     {
360       public void run()
361       {
362         try
363         {
364           lock.acquireWrite();
365         }
366         catch (InterruptedException JavaDoc e)
367         {
368           throw new RuntimeException JavaDoc(e.getMessage());
369         }
370         done = true;
371       }
372     });
373   }
374
375   private Thread JavaDoc randomAcquireReleaseRead()
376   {
377     return new Thread JavaDoc(new Runnable JavaDoc()
378     {
379       public void run()
380       {
381         try
382         {
383           long timeout = r.nextLong() % TIMEOUT;
384           if (timeout < 0)
385             timeout = -timeout;
386           else if (timeout == 0)
387             timeout = TIMEOUT;
388           Thread.sleep(timeout);
389
390           lock.acquireRead();
391           assertEquals("Acquired read lock but lock not held by a reader", lock
392               .isReadLocked(), true);
393           assertEquals("Acquired read lock but lock held by a writer", lock
394               .isWriteLocked(), false);
395           System.out.print("R");
396           lock.releaseRead();
397           randomTestComplete();
398         }
399         catch (InterruptedException JavaDoc e)
400         {
401           throw new RuntimeException JavaDoc(e.getMessage());
402         }
403       }
404     });
405   }
406
407   private Thread JavaDoc randomAcquireReleaseWrite()
408   {
409     return new Thread JavaDoc(new Runnable JavaDoc()
410     {
411       public void run()
412       {
413         try
414         {
415           long timeout = r.nextLong() % TIMEOUT;
416           if (timeout < 0)
417             timeout = -timeout;
418           else if (timeout == 0)
419             timeout = TIMEOUT;
420           Thread.sleep(timeout);
421
422           lock.acquireWrite();
423           assertEquals("Acquired write lock but lock not held by a writer",
424               lock.isWriteLocked(), true);
425           assertEquals("Acquired write lock but lock held by a reader", lock
426               .isReadLocked(), false);
427           System.out.print("W");
428           lock.releaseWrite();
429           randomTestComplete();
430         }
431         catch (InterruptedException JavaDoc e)
432         {
433           throw new RuntimeException JavaDoc(e.getMessage());
434         }
435       }
436     });
437   }
438 }
Popular Tags