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 |
|
12 |
public class ReentrantReadWriteLockTest extends TestCase { |
13 |
static int HOLD_COUNT_TEST_LIMIT = 20; |
14 |
|
15 |
public static void main(String[] args) { |
16 |
junit.textui.TestRunner.run (suite()); |
17 |
} |
18 |
|
19 |
public static Test suite() { |
20 |
return new TestSuite(ReentrantReadWriteLockTest.class); |
21 |
} |
22 |
|
23 |
|
24 |
private static long SHORT_DELAY_MS = 100; |
25 |
private static long MEDIUM_DELAY_MS = 1000; |
26 |
private static long LONG_DELAY_MS = 10000; |
27 |
|
28 |
/* |
29 |
* Unlocks an unlocked lock, throws Illegal Monitor State |
30 |
* |
31 |
*/ |
32 |
|
33 |
public void testIllegalMonitorStateException(){ |
34 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
35 |
try{ |
36 |
rl.writeLock().unlock(); |
37 |
fail("Should of thown Illegal Monitor State Exception"); |
38 |
|
39 |
}catch(IllegalMonitorStateException sucess){} |
40 |
|
41 |
|
42 |
} |
43 |
|
44 |
/* |
45 |
* makes a lock, locks it, tries to aquire the lock in another thread |
46 |
* interrupts that thread and waits for an interrupted Exception to |
47 |
* be thrown. |
48 |
*/ |
49 |
|
50 |
public void testInterruptedException(){ |
51 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
52 |
lock.writeLock().lock(); |
53 |
Thread t = new Thread(new Runnable() { |
54 |
public void run(){ |
55 |
try{ |
56 |
lock.writeLock().lockInterruptibly(); |
57 |
fail("should throw"); |
58 |
}catch(InterruptedException sucess){} |
59 |
} |
60 |
}); |
61 |
t.start(); |
62 |
t.interrupt(); |
63 |
lock.writeLock().unlock(); |
64 |
} |
65 |
|
66 |
/* |
67 |
* tests for interrupted exception on a timed wait |
68 |
* |
69 |
*/ |
70 |
|
71 |
|
72 |
public void testInterruptedException2(){ |
73 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
74 |
lock.writeLock().lock(); |
75 |
Thread t = new Thread(new Runnable() { |
76 |
public void run(){ |
77 |
try{ |
78 |
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
79 |
fail("should throw"); |
80 |
}catch(InterruptedException sucess){} |
81 |
} |
82 |
}); |
83 |
t.start(); |
84 |
t.interrupt(); |
85 |
} |
86 |
|
87 |
|
88 |
|
89 |
/* |
90 |
* current thread locks interruptibly the thread |
91 |
* another thread tries to aquire the lock and blocks |
92 |
* on the call. interrupt the attempted aquireLock |
93 |
* assert that the first lock() call actually locked the lock |
94 |
* assert that the current thread is the one holding the lock |
95 |
*/ |
96 |
|
97 |
public void testLockedInterruptibly() { |
98 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
99 |
try {lock.writeLock().lockInterruptibly();} catch(Exception e) {} |
100 |
Thread t = new Thread(new Runnable() { |
101 |
public void run() { |
102 |
try { |
103 |
lock.writeLock().lockInterruptibly(); |
104 |
fail("Failed to generate an Interrupted Exception"); |
105 |
} |
106 |
catch(InterruptedException e) {} |
107 |
} |
108 |
}); |
109 |
t.start(); |
110 |
t.interrupt(); |
111 |
} |
112 |
|
113 |
|
114 |
} |