ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.7
Committed: Fri Jan 2 21:06:38 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.6: +4 -4 lines
Log Message:
Use new AQS methods

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9
10 import junit.framework.*;
11 import java.util.*;
12 import java.util.concurrent.*;
13 import java.util.concurrent.locks.*;
14 import java.io.*;
15
16 public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
17 public static void main(String[] args) {
18 junit.textui.TestRunner.run (suite());
19 }
20 public static Test suite() {
21 return new TestSuite(AbstractQueuedSynchronizerTest.class);
22 }
23
24 /**
25 * A simple mutex class, from the AbstractQueuedSynchronizer
26 * javadoc. All tests exercise this as a sample user extension.
27 * Other methods/features of AbstractQueuedSynchronizerTest are
28 * tested implicitly in ReentrantLock, ReentrantReadWriteLock, and
29 * Semaphore test classes
30 */
31 static class Mutex implements Lock, java.io.Serializable {
32 private static class Sync extends AbstractQueuedSynchronizer {
33 boolean isLocked() { return getState() == 1; }
34
35 public boolean tryAcquireExclusive(boolean isQueued, int acquires) {
36 assert acquires == 1; // Does not use multiple acquires
37 return compareAndSetState(0, 1);
38 }
39
40 public boolean tryReleaseExclusive(int releases) {
41 setState(0);
42 return true;
43 }
44
45 public void checkConditionAccess(Thread thread, boolean waiting) {
46 if (getState() == 0) throw new IllegalMonitorStateException();
47 }
48
49 Condition newCondition() { return new ConditionObject(); }
50
51 private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
52 s.defaultReadObject();
53 setState(0); // reset to unlocked state
54 }
55 }
56
57 private final Sync sync = new Sync();
58 public boolean tryLock() {
59 return sync.tryAcquireExclusive(false, 1);
60 }
61 public void lock() {
62 sync.acquireExclusiveUninterruptibly(1);
63 }
64 public void lockInterruptibly() throws InterruptedException {
65 sync.acquireExclusiveInterruptibly(1);
66 }
67 public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
68 return sync.acquireExclusiveTimed(1, unit.toNanos(timeout));
69 }
70 public void unlock() { sync.releaseExclusive(1); }
71 public Condition newCondition() { return sync.newCondition(); }
72 public boolean isLocked() { return sync.isLocked(); }
73 public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }
74 }
75
76 /**
77 * A runnable calling lockInterruptibly
78 */
79 class InterruptibleLockRunnable implements Runnable {
80 final Mutex lock;
81 InterruptibleLockRunnable(Mutex l) { lock = l; }
82 public void run() {
83 try {
84 lock.lockInterruptibly();
85 } catch(InterruptedException success){}
86 }
87 }
88
89
90 /**
91 * A runnable calling lockInterruptibly that expects to be
92 * interrupted
93 */
94 class InterruptedLockRunnable implements Runnable {
95 final Mutex lock;
96 InterruptedLockRunnable(Mutex l) { lock = l; }
97 public void run() {
98 try {
99 lock.lockInterruptibly();
100 threadShouldThrow();
101 } catch(InterruptedException success){}
102 }
103 }
104
105 /**
106 * locking an unlocked lock succeeds
107 */
108 public void testLock() {
109 Mutex rl = new Mutex();
110 rl.lock();
111 assertTrue(rl.isLocked());
112 rl.unlock();
113 }
114
115 /**
116 * tryLock on an unlocked lock succeeds
117 */
118 public void testTryLock() {
119 Mutex rl = new Mutex();
120 assertTrue(rl.tryLock());
121 assertTrue(rl.isLocked());
122 rl.unlock();
123 }
124
125 /**
126 * hasQueuedThreads reports whether there are waiting threads
127 */
128 public void testhasQueuedThreads() {
129 final Mutex lock = new Mutex();
130 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
131 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
132 try {
133 assertFalse(lock.hasQueuedThreads());
134 lock.lock();
135 t1.start();
136 Thread.sleep(SHORT_DELAY_MS);
137 assertTrue(lock.hasQueuedThreads());
138 t2.start();
139 Thread.sleep(SHORT_DELAY_MS);
140 assertTrue(lock.hasQueuedThreads());
141 t1.interrupt();
142 Thread.sleep(SHORT_DELAY_MS);
143 assertTrue(lock.hasQueuedThreads());
144 lock.unlock();
145 Thread.sleep(SHORT_DELAY_MS);
146 assertFalse(lock.hasQueuedThreads());
147 t1.join();
148 t2.join();
149 } catch(Exception e){
150 unexpectedException();
151 }
152 }
153
154 /**
155 * timed tryLock is interruptible.
156 */
157 public void testInterruptedException2() {
158 final Mutex lock = new Mutex();
159 lock.lock();
160 Thread t = new Thread(new Runnable() {
161 public void run() {
162 try {
163 lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
164 threadShouldThrow();
165 } catch(InterruptedException success){}
166 }
167 });
168 try {
169 t.start();
170 t.interrupt();
171 } catch(Exception e){
172 unexpectedException();
173 }
174 }
175
176
177 /**
178 * TryLock on a locked lock fails
179 */
180 public void testTryLockWhenLocked() {
181 final Mutex lock = new Mutex();
182 lock.lock();
183 Thread t = new Thread(new Runnable() {
184 public void run() {
185 threadAssertFalse(lock.tryLock());
186 }
187 });
188 try {
189 t.start();
190 t.join();
191 lock.unlock();
192 } catch(Exception e){
193 unexpectedException();
194 }
195 }
196
197 /**
198 * Timed tryLock on a locked lock times out
199 */
200 public void testTryLock_Timeout() {
201 final Mutex lock = new Mutex();
202 lock.lock();
203 Thread t = new Thread(new Runnable() {
204 public void run() {
205 try {
206 threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
207 } catch (Exception ex) {
208 threadUnexpectedException();
209 }
210 }
211 });
212 try {
213 t.start();
214 t.join();
215 lock.unlock();
216 } catch(Exception e){
217 unexpectedException();
218 }
219 }
220
221
222 /**
223 * isLocked is true when locked and false when not
224 */
225 public void testIsLocked() {
226 final Mutex lock = new Mutex();
227 lock.lock();
228 assertTrue(lock.isLocked());
229 lock.unlock();
230 assertFalse(lock.isLocked());
231 Thread t = new Thread(new Runnable() {
232 public void run() {
233 lock.lock();
234 try {
235 Thread.sleep(SMALL_DELAY_MS);
236 }
237 catch(Exception e) {
238 threadUnexpectedException();
239 }
240 lock.unlock();
241 }
242 });
243 try {
244 t.start();
245 Thread.sleep(SHORT_DELAY_MS);
246 assertTrue(lock.isLocked());
247 t.join();
248 assertFalse(lock.isLocked());
249 } catch(Exception e){
250 unexpectedException();
251 }
252 }
253
254
255 /**
256 * lockInterruptibly is interruptible.
257 */
258 public void testLockInterruptibly1() {
259 final Mutex lock = new Mutex();
260 lock.lock();
261 Thread t = new Thread(new InterruptedLockRunnable(lock));
262 try {
263 t.start();
264 t.interrupt();
265 lock.unlock();
266 t.join();
267 } catch(Exception e){
268 unexpectedException();
269 }
270 }
271
272 /**
273 * lockInterruptibly succeeds when unlocked, else is interruptible
274 */
275 public void testLockInterruptibly2() {
276 final Mutex lock = new Mutex();
277 try {
278 lock.lockInterruptibly();
279 } catch(Exception e) {
280 unexpectedException();
281 }
282 Thread t = new Thread(new InterruptedLockRunnable(lock));
283 try {
284 t.start();
285 t.interrupt();
286 assertTrue(lock.isLocked());
287 t.join();
288 } catch(Exception e){
289 unexpectedException();
290 }
291 }
292
293 /**
294 * Calling await without holding lock throws IllegalMonitorStateException
295 */
296 public void testAwait_IllegalMonitor() {
297 final Mutex lock = new Mutex();
298 final Condition c = lock.newCondition();
299 try {
300 c.await();
301 shouldThrow();
302 }
303 catch (IllegalMonitorStateException success) {
304 }
305 catch (Exception ex) {
306 unexpectedException();
307 }
308 }
309
310 /**
311 * Calling signal without holding lock throws IllegalMonitorStateException
312 */
313 public void testSignal_IllegalMonitor() {
314 final Mutex lock = new Mutex();
315 final Condition c = lock.newCondition();
316 try {
317 c.signal();
318 shouldThrow();
319 }
320 catch (IllegalMonitorStateException success) {
321 }
322 catch (Exception ex) {
323 unexpectedException();
324 }
325 }
326
327 /**
328 * awaitNanos without a signal times out
329 */
330 public void testAwaitNanos_Timeout() {
331 final Mutex lock = new Mutex();
332 final Condition c = lock.newCondition();
333 try {
334 lock.lock();
335 long t = c.awaitNanos(100);
336 assertTrue(t <= 0);
337 lock.unlock();
338 }
339 catch (Exception ex) {
340 unexpectedException();
341 }
342 }
343
344 /**
345 * timed await without a signal times out
346 */
347 public void testAwait_Timeout() {
348 final Mutex lock = new Mutex();
349 final Condition c = lock.newCondition();
350 try {
351 lock.lock();
352 assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
353 lock.unlock();
354 }
355 catch (Exception ex) {
356 unexpectedException();
357 }
358 }
359
360 /**
361 * awaitUntil without a signal times out
362 */
363 public void testAwaitUntil_Timeout() {
364 final Mutex lock = new Mutex();
365 final Condition c = lock.newCondition();
366 try {
367 lock.lock();
368 java.util.Date d = new java.util.Date();
369 assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
370 lock.unlock();
371 }
372 catch (Exception ex) {
373 unexpectedException();
374 }
375 }
376
377 /**
378 * await returns when signalled
379 */
380 public void testAwait() {
381 final Mutex lock = new Mutex();
382 final Condition c = lock.newCondition();
383 Thread t = new Thread(new Runnable() {
384 public void run() {
385 try {
386 lock.lock();
387 c.await();
388 lock.unlock();
389 }
390 catch(InterruptedException e) {
391 threadUnexpectedException();
392 }
393 }
394 });
395
396 try {
397 t.start();
398 Thread.sleep(SHORT_DELAY_MS);
399 lock.lock();
400 c.signal();
401 lock.unlock();
402 t.join(SHORT_DELAY_MS);
403 assertFalse(t.isAlive());
404 }
405 catch (Exception ex) {
406 unexpectedException();
407 }
408 }
409
410 }