1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.lock; |
8 |
| |
9 |
| import junit.framework.Test; |
10 |
| import junit.framework.TestCase; |
11 |
| import junit.framework.TestSuite; |
12 |
| |
13 |
| import java.util.concurrent.locks.ReentrantReadWriteLock; |
14 |
| import java.util.concurrent.TimeUnit; |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class ReentrantWriterPreference2Readers1WriterLockTest extends TestCase { |
22 |
| ReentrantReadWriteLock lock; |
23 |
| ReentrantReadWriteLock.ReadLock rl; |
24 |
| ReentrantReadWriteLock.WriteLock wl; |
25 |
| Exception thread_ex=null; |
26 |
| |
27 |
2
| protected void setUp() throws Exception {
|
28 |
2
| super.setUp();
|
29 |
2
| lock=new ReentrantReadWriteLock();
|
30 |
2
| rl=lock.readLock();
|
31 |
2
| wl=lock.writeLock();
|
32 |
2
| thread_ex=null;
|
33 |
| } |
34 |
| |
35 |
2
| protected void tearDown() throws Exception {
|
36 |
2
| super.tearDown();
|
37 |
2
| lock=null;
|
38 |
2
| if(thread_ex != null)
|
39 |
0
| throw thread_ex;
|
40 |
| } |
41 |
| |
42 |
| |
43 |
7
| private void log(String msg) {
|
44 |
7
| System.out.println(System.currentTimeMillis() + " " + Thread.currentThread() +
|
45 |
| " [" + Thread.currentThread().getName() + "]: " + msg); |
46 |
| } |
47 |
| |
48 |
| |
49 |
1
| public void testSimpleUpgradeFromReadLockToWriteLock() {
|
50 |
1
| int readers, writers;
|
51 |
1
| try {
|
52 |
1
| rl.lock();
|
53 |
1
| readers=lock.getReadLockCount();
|
54 |
1
| assertEquals(1, readers);
|
55 |
1
| boolean wl_acquired=wl.tryLock(500, TimeUnit.MILLISECONDS);
|
56 |
1
| if(!wl_acquired) {
|
57 |
1
| fail("write lock could not be acquired");
|
58 |
0
| return;
|
59 |
| } |
60 |
0
| readers=lock.getReadLockCount();
|
61 |
0
| assertEquals(1, readers);
|
62 |
0
| writers=lock.getWriteHoldCount();
|
63 |
0
| assertEquals(1, writers);
|
64 |
| } |
65 |
| catch(InterruptedException e) { |
66 |
| |
67 |
| } |
68 |
| finally { |
69 |
1
| rl.unlock();
|
70 |
1
| if(lock.getWriteHoldCount() > 0)
|
71 |
0
| wl.unlock();
|
72 |
| } |
73 |
| } |
74 |
| |
75 |
1
| public void test2ReadersAnd1Writer() throws InterruptedException {
|
76 |
1
| int readers, writers;
|
77 |
1
| Upgrader upgrader=new Upgrader("Upgrader");
|
78 |
1
| Reader reader=new Reader("Reader");
|
79 |
1
| reader.start();
|
80 |
1
| sleepThread(500);
|
81 |
| |
82 |
1
| readers=lock.getReadLockCount();
|
83 |
1
| assertEquals(1, readers);
|
84 |
| |
85 |
1
| upgrader.start();
|
86 |
1
| sleepThread(500);
|
87 |
| |
88 |
1
| readers=lock.getReadLockCount();
|
89 |
1
| assertEquals(2, readers);
|
90 |
| |
91 |
1
| synchronized(upgrader) {
|
92 |
1
| upgrader.notify();
|
93 |
| } |
94 |
1
| sleepThread(500);
|
95 |
| |
96 |
1
| readers=lock.getReadLockCount();
|
97 |
1
| assertEquals(2, readers);
|
98 |
1
| writers=lock.getWriteHoldCount();
|
99 |
1
| assertEquals(0, writers);
|
100 |
| |
101 |
1
| synchronized(reader) {
|
102 |
1
| reader.notify();
|
103 |
| } |
104 |
1
| reader.join();
|
105 |
| |
106 |
1
| readers=lock.getReadLockCount();
|
107 |
1
| assertEquals(1, readers);
|
108 |
1
| writers=lock.getWriteHoldCount();
|
109 |
1
| assertEquals(1, writers);
|
110 |
| |
111 |
0
| synchronized(upgrader) {
|
112 |
0
| upgrader.notify();
|
113 |
| } |
114 |
0
| sleepThread(500);
|
115 |
0
| readers=lock.getReadLockCount();
|
116 |
0
| assertEquals(0, readers);
|
117 |
0
| writers=lock.getWriteHoldCount();
|
118 |
0
| assertEquals(0, writers);
|
119 |
| |
120 |
0
| upgrader.join(3000);
|
121 |
0
| assertTrue("Known failure. See JBCACHE-461; This is due to a potential bug in ReentrantWriterPreferenceReadWriteLock !",
|
122 |
| upgrader.wasUpgradeSuccessful()); |
123 |
| } |
124 |
| |
125 |
| |
126 |
| private class Reader extends Thread { |
127 |
| |
128 |
1
| public Reader(String name) {
|
129 |
1
| super(name);
|
130 |
| } |
131 |
| |
132 |
1
| public void run() {
|
133 |
1
| try {
|
134 |
1
| log("acquiring RL");
|
135 |
1
| rl.lock();
|
136 |
1
| log("acquired RL");
|
137 |
1
| synchronized(this) {
|
138 |
1
| this.wait();
|
139 |
| } |
140 |
| } |
141 |
| catch(InterruptedException e) { |
142 |
| } |
143 |
| finally { |
144 |
1
| log("releasing RL");
|
145 |
1
| rl.unlock();
|
146 |
1
| log("released RL");
|
147 |
| } |
148 |
| } |
149 |
| } |
150 |
| |
151 |
| |
152 |
| private class Upgrader extends Thread { |
153 |
| boolean upgradeSuccessful=false; |
154 |
| |
155 |
1
| public Upgrader(String name) {
|
156 |
1
| super(name);
|
157 |
| } |
158 |
| |
159 |
0
| public boolean wasUpgradeSuccessful() {
|
160 |
0
| return upgradeSuccessful;
|
161 |
| } |
162 |
| |
163 |
| |
164 |
1
| public void run() {
|
165 |
1
| try {
|
166 |
1
| log("acquiring RL");
|
167 |
1
| rl.lock();
|
168 |
1
| log("acquired RL");
|
169 |
1
| synchronized(this) {
|
170 |
1
| this.wait();
|
171 |
| } |
172 |
1
| log("attempting to acquire WL");
|
173 |
1
| wl.lock();
|
174 |
0
| upgradeSuccessful=true;
|
175 |
0
| log("acquired WL");
|
176 |
| |
177 |
| |
178 |
0
| synchronized(this) {
|
179 |
0
| this.wait();
|
180 |
| } |
181 |
0
| log("releasing WL");
|
182 |
0
| rl.unlock();
|
183 |
0
| log("released WL");
|
184 |
| } |
185 |
| catch(InterruptedException e) { |
186 |
| ; |
187 |
| } |
188 |
| finally { |
189 |
0
| wl.unlock();
|
190 |
0
| rl.unlock();
|
191 |
| } |
192 |
| } |
193 |
| } |
194 |
| |
195 |
| |
196 |
3
| static void sleepThread(long timeout) {
|
197 |
3
| try {
|
198 |
3
| Thread.sleep(timeout);
|
199 |
| } |
200 |
| catch(InterruptedException e) { |
201 |
| } |
202 |
| } |
203 |
| |
204 |
1
| public static Test suite() {
|
205 |
1
| return new TestSuite(ReentrantWriterPreference2Readers1WriterLockTest.class);
|
206 |
| } |
207 |
| |
208 |
0
| public static void main(String[] args) {
|
209 |
0
| junit.textui.TestRunner.run(ReentrantWriterPreference2Readers1WriterLockTest.suite());
|
210 |
| } |
211 |
| |
212 |
| } |