ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.4
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.3: +33 -27 lines
Log Message:
New base class JSR166TestCase

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import junit.framework.*;
9     import java.util.concurrent.locks.*;
10     import java.util.concurrent.*;
11 dl 1.3 import java.io.*;
12 dl 1.1
13 dl 1.4 public class ReentrantLockTest extends JSR166TestCase {
14 dl 1.1 static int HOLD_COUNT_TEST_LIMIT = 20;
15    
16     public static void main(String[] args) {
17     junit.textui.TestRunner.run (suite());
18     }
19    
20     public static Test suite() {
21     return new TestSuite(ReentrantLockTest.class);
22     }
23    
24     /*
25     * Unlocks an unlocked lock, throws Illegal Monitor State
26     *
27     */
28     public void testIllegalMonitorStateException(){
29     ReentrantLock rl = new ReentrantLock();
30     try{
31     rl.unlock();
32     fail("Should of thown Illegal Monitor State Exception");
33    
34 dl 1.3 } catch(IllegalMonitorStateException success){}
35 dl 1.1
36    
37     }
38    
39     /*
40     * makes a lock, locks it, tries to aquire the lock in another thread
41     * interrupts that thread and waits for an interrupted Exception to
42     * be thrown.
43     */
44    
45     public void testInterruptedException(){
46     final ReentrantLock lock = new ReentrantLock();
47     lock.lock();
48     Thread t = new Thread(new Runnable() {
49     public void run(){
50     try{
51     lock.lockInterruptibly();
52 dl 1.4 threadFail("should throw");
53 dl 1.3 } catch(InterruptedException success){}
54 dl 1.1 }
55     });
56 dl 1.4 try {
57     t.start();
58     t.interrupt();
59     lock.unlock();
60     t.join();
61     } catch(Exception e){
62     fail("unexpected exception");
63     }
64 dl 1.1 }
65    
66     /*
67     * tests for interrupted exception on a timed wait
68     *
69     */
70    
71    
72     public void testInterruptedException2(){
73     final ReentrantLock lock = new ReentrantLock();
74     lock.lock();
75     Thread t = new Thread(new Runnable() {
76     public void run(){
77     try{
78 dl 1.4 lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
79     threadFail("should throw");
80 dl 1.3 } catch(InterruptedException success){}
81 dl 1.1 }
82     });
83 dl 1.4 try {
84     t.start();
85     t.interrupt();
86     } catch(Exception e){
87     fail("unexpected exception");
88     }
89 dl 1.1 }
90    
91 dl 1.3
92     public void testTryLockWhenLocked() {
93     final ReentrantLock lock = new ReentrantLock();
94     lock.lock();
95     Thread t = new Thread(new Runnable() {
96     public void run(){
97 dl 1.4 threadAssertFalse(lock.tryLock());
98 dl 1.3 }
99     });
100     try {
101     t.start();
102     t.join();
103     lock.unlock();
104     } catch(Exception e){
105     fail("unexpected exception");
106     }
107     }
108    
109     public void testTryLock_Timeout(){
110     final ReentrantLock lock = new ReentrantLock();
111     lock.lock();
112     Thread t = new Thread(new Runnable() {
113     public void run(){
114     try {
115 dl 1.4 threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
116 dl 1.3 } catch (Exception ex) {
117 dl 1.4 threadFail("unexpected exception");
118 dl 1.3 }
119     }
120     });
121     try {
122     t.start();
123     t.join();
124     lock.unlock();
125     } catch(Exception e){
126     fail("unexpected exception");
127     }
128     }
129 dl 1.1
130     public void testGetHoldCount() {
131     ReentrantLock lock = new ReentrantLock();
132     for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) {
133     lock.lock();
134     assertEquals(i,lock.getHoldCount());
135     }
136     for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) {
137     lock.unlock();
138     assertEquals(i-1,lock.getHoldCount());
139     }
140     }
141    
142    
143    
144    
145     public void testIsLocked() {
146     final ReentrantLock lock = new ReentrantLock();
147     lock.lock();
148     assertTrue(lock.isLocked());
149     lock.unlock();
150     assertFalse(lock.isLocked());
151     Thread t = new Thread(new Runnable() {
152     public void run() {
153     lock.lock();
154     try {
155 dl 1.4 Thread.sleep(SMALL_DELAY_MS);
156 dl 1.1 }
157 dl 1.4 catch(Exception e) {
158     threadFail("unexpected exception");
159     }
160 dl 1.1 lock.unlock();
161     }
162     });
163     try{
164     t.start();
165     Thread.sleep(SHORT_DELAY_MS);
166     assertTrue(lock.isLocked());
167     t.join();
168     assertFalse(lock.isLocked());
169     } catch(Exception e){
170     fail("unexpected exception");
171     }
172     }
173    
174    
175 dl 1.3 public void testLockInterruptibly() {
176 dl 1.1 final ReentrantLock lock = new ReentrantLock();
177 dl 1.3 try {
178     lock.lockInterruptibly();
179     } catch(Exception e) {
180     fail("unexpected exception");
181     }
182 dl 1.1 Thread t = new Thread(new Runnable() {
183     public void run() {
184     try {
185     lock.lockInterruptibly();
186 dl 1.4 threadFail("should throw");
187 dl 1.1 }
188     catch(InterruptedException e) {}
189     }
190     });
191 dl 1.3 try {
192     t.start();
193     t.interrupt();
194     assertTrue(lock.isLocked());
195     assertTrue(lock.isHeldByCurrentThread());
196     t.join();
197     } catch(Exception e){
198     fail("unexpected exception");
199     }
200 dl 1.1 }
201 dl 1.2
202     public void testAwait_IllegalMonitor() {
203     final ReentrantLock lock = new ReentrantLock();
204     final Condition c = lock.newCondition();
205     try {
206     c.await();
207     fail("should throw");
208     }
209     catch (IllegalMonitorStateException success) {
210     }
211     catch (Exception ex) {
212     fail("should throw IMSE");
213     }
214     }
215    
216     public void testSignal_IllegalMonitor() {
217     final ReentrantLock lock = new ReentrantLock();
218     final Condition c = lock.newCondition();
219     try {
220     c.signal();
221     fail("should throw");
222     }
223     catch (IllegalMonitorStateException success) {
224     }
225     catch (Exception ex) {
226     fail("should throw IMSE");
227     }
228     }
229    
230     public void testAwaitNanos_Timeout() {
231     final ReentrantLock lock = new ReentrantLock();
232     final Condition c = lock.newCondition();
233     try {
234     lock.lock();
235     long t = c.awaitNanos(100);
236     assertTrue(t <= 0);
237     lock.unlock();
238     }
239     catch (Exception ex) {
240     fail("unexpected exception");
241     }
242     }
243    
244     public void testAwait_Timeout() {
245     final ReentrantLock lock = new ReentrantLock();
246     final Condition c = lock.newCondition();
247     try {
248     lock.lock();
249 dl 1.4 assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
250 dl 1.2 lock.unlock();
251     }
252     catch (Exception ex) {
253     fail("unexpected exception");
254     }
255     }
256    
257     public void testAwaitUntil_Timeout() {
258     final ReentrantLock lock = new ReentrantLock();
259     final Condition c = lock.newCondition();
260     try {
261     lock.lock();
262     java.util.Date d = new java.util.Date();
263     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
264     lock.unlock();
265     }
266     catch (Exception ex) {
267     fail("unexpected exception");
268     }
269     }
270    
271     public void testAwait() {
272     final ReentrantLock lock = new ReentrantLock();
273     final Condition c = lock.newCondition();
274     Thread t = new Thread(new Runnable() {
275     public void run() {
276     try {
277     lock.lock();
278     c.await();
279     lock.unlock();
280     }
281     catch(InterruptedException e) {
282 dl 1.4 threadFail("unexpected exception");
283 dl 1.2 }
284     }
285     });
286    
287     try {
288     t.start();
289     Thread.sleep(SHORT_DELAY_MS);
290     lock.lock();
291     c.signal();
292     lock.unlock();
293     t.join(SHORT_DELAY_MS);
294     assertFalse(t.isAlive());
295     }
296     catch (Exception ex) {
297     fail("unexpected exception");
298     }
299     }
300    
301     public void testAwaitUninterruptibly() {
302     final ReentrantLock lock = new ReentrantLock();
303     final Condition c = lock.newCondition();
304     Thread t = new Thread(new Runnable() {
305     public void run() {
306     lock.lock();
307     c.awaitUninterruptibly();
308     lock.unlock();
309     }
310     });
311    
312     try {
313     t.start();
314     Thread.sleep(SHORT_DELAY_MS);
315     t.interrupt();
316     lock.lock();
317     c.signal();
318     lock.unlock();
319     t.join(SHORT_DELAY_MS);
320     assertFalse(t.isAlive());
321     }
322     catch (Exception ex) {
323     fail("unexpected exception");
324     }
325     }
326    
327     public void testAwait_Interrupt() {
328     final ReentrantLock lock = new ReentrantLock();
329     final Condition c = lock.newCondition();
330     Thread t = new Thread(new Runnable() {
331     public void run() {
332     try {
333     lock.lock();
334     c.await();
335     lock.unlock();
336 dl 1.4 threadFail("should throw");
337 dl 1.2 }
338     catch(InterruptedException success) {
339     }
340     }
341     });
342    
343     try {
344     t.start();
345     Thread.sleep(SHORT_DELAY_MS);
346     t.interrupt();
347     t.join(SHORT_DELAY_MS);
348     assertFalse(t.isAlive());
349     }
350     catch (Exception ex) {
351     fail("unexpected exception");
352     }
353     }
354    
355     public void testAwaitNanos_Interrupt() {
356     final ReentrantLock lock = new ReentrantLock();
357     final Condition c = lock.newCondition();
358     Thread t = new Thread(new Runnable() {
359     public void run() {
360     try {
361     lock.lock();
362     c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
363     lock.unlock();
364 dl 1.4 threadFail("should throw");
365 dl 1.2 }
366     catch(InterruptedException success) {
367     }
368     }
369     });
370    
371     try {
372     t.start();
373     Thread.sleep(SHORT_DELAY_MS);
374     t.interrupt();
375     t.join(SHORT_DELAY_MS);
376     assertFalse(t.isAlive());
377     }
378     catch (Exception ex) {
379     fail("unexpected exception");
380     }
381     }
382    
383     public void testAwaitUntil_Interrupt() {
384     final ReentrantLock lock = new ReentrantLock();
385     final Condition c = lock.newCondition();
386     Thread t = new Thread(new Runnable() {
387     public void run() {
388     try {
389     lock.lock();
390     java.util.Date d = new java.util.Date();
391     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
392     lock.unlock();
393 dl 1.4 threadFail("should throw");
394 dl 1.2 }
395     catch(InterruptedException success) {
396     }
397     }
398     });
399    
400     try {
401     t.start();
402     Thread.sleep(SHORT_DELAY_MS);
403     t.interrupt();
404     t.join(SHORT_DELAY_MS);
405     assertFalse(t.isAlive());
406     }
407     catch (Exception ex) {
408     fail("unexpected exception");
409     }
410     }
411    
412     public void testSignalAll() {
413     final ReentrantLock lock = new ReentrantLock();
414     final Condition c = lock.newCondition();
415     Thread t1 = new Thread(new Runnable() {
416     public void run() {
417     try {
418     lock.lock();
419     c.await();
420     lock.unlock();
421     }
422     catch(InterruptedException e) {
423 dl 1.4 threadFail("unexpected exception");
424 dl 1.2 }
425     }
426     });
427    
428     Thread t2 = new Thread(new Runnable() {
429     public void run() {
430     try {
431     lock.lock();
432     c.await();
433     lock.unlock();
434     }
435     catch(InterruptedException e) {
436 dl 1.4 threadFail("unexpected exception");
437 dl 1.2 }
438     }
439     });
440    
441     try {
442     t1.start();
443     t2.start();
444     Thread.sleep(SHORT_DELAY_MS);
445     lock.lock();
446     c.signalAll();
447     lock.unlock();
448     t1.join(SHORT_DELAY_MS);
449     t2.join(SHORT_DELAY_MS);
450     assertFalse(t1.isAlive());
451     assertFalse(t2.isAlive());
452     }
453     catch (Exception ex) {
454 dl 1.3 fail("unexpected exception");
455     }
456     }
457    
458     public void testSerialization() {
459     ReentrantLock l = new ReentrantLock();
460     l.lock();
461     l.unlock();
462    
463     try {
464     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
465     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
466     out.writeObject(l);
467     out.close();
468    
469     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
470     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
471     ReentrantLock r = (ReentrantLock) in.readObject();
472     r.lock();
473     r.unlock();
474     } catch(Exception e){
475     e.printStackTrace();
476 dl 1.2 fail("unexpected exception");
477     }
478     }
479 dl 1.1
480     }