ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.3
Committed: Mon Dec 29 14:19:27 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.2: +4 -4 lines
Log Message:
Simplify AQS hook methods

File Contents

# User Rev Content
1 dl 1.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 dl 1.3 public int acquireExclusiveState(boolean isQueued, int acquires) {
34 dl 1.1 assert acquires == 1; // Does not use multiple acquires
35     return state().compareAndSet(0, 1)? 0 : -1;
36     }
37    
38     public boolean releaseExclusiveState(int releases) {
39     state().set(0);
40     return true;
41     }
42    
43 dl 1.3 public int acquireSharedState(boolean isQueued, int acquires) {
44 dl 1.1 throw new UnsupportedOperationException();
45     }
46    
47     public boolean releaseSharedState(int releases) {
48     throw new UnsupportedOperationException();
49     }
50    
51     public void checkConditionAccess(Thread thread, boolean waiting) {
52     if (state().get() == 0) throw new IllegalMonitorStateException();
53     }
54    
55     Condition newCondition() { return new ConditionObject(); }
56    
57     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
58     s.defaultReadObject();
59     state().set(0); // reset to unlocked state
60     }
61     }
62    
63     private final Sync sync = new Sync();
64     public boolean tryLock() {
65 dl 1.3 return sync.acquireExclusiveState(false, 1) >= 0;
66 dl 1.2 }
67     public void lock() {
68 dl 1.3 sync.acquireExclusiveUninterruptibly(1);
69 dl 1.1 }
70     public void lockInterruptibly() throws InterruptedException {
71     sync.acquireExclusiveInterruptibly(1);
72     }
73     public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
74     return sync.acquireExclusiveTimed(1, unit.toNanos(timeout));
75     }
76     public void unlock() { sync.releaseExclusive(1); }
77     public Condition newCondition() { return sync.newCondition(); }
78     public boolean isLocked() { return sync.state().get() != 0; }
79     public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }
80     }
81    
82     /**
83     * A runnable calling lockInterruptibly
84     */
85     class InterruptibleLockRunnable implements Runnable {
86     final Mutex lock;
87     InterruptibleLockRunnable(Mutex l) { lock = l; }
88     public void run() {
89     try {
90     lock.lockInterruptibly();
91     } catch(InterruptedException success){}
92     }
93     }
94    
95    
96     /**
97     * A runnable calling lockInterruptibly that expects to be
98     * interrupted
99     */
100     class InterruptedLockRunnable implements Runnable {
101     final Mutex lock;
102     InterruptedLockRunnable(Mutex l) { lock = l; }
103     public void run() {
104     try {
105     lock.lockInterruptibly();
106     threadShouldThrow();
107     } catch(InterruptedException success){}
108     }
109     }
110    
111     /**
112     * locking an unlocked lock succeeds
113     */
114     public void testLock() {
115     Mutex rl = new Mutex();
116     rl.lock();
117     assertTrue(rl.isLocked());
118     rl.unlock();
119     }
120    
121     /**
122     * trylock on an unlocked lock succeeds
123     */
124     public void testTryLock() {
125     Mutex rl = new Mutex();
126     assertTrue(rl.tryLock());
127     assertTrue(rl.isLocked());
128     rl.unlock();
129     }
130    
131     /**
132     * hasQueuedThreads reports whether there are waiting threads
133     */
134     public void testhasQueuedThreads() {
135     final Mutex lock = new Mutex();
136     Thread t1 = new Thread(new InterruptedLockRunnable(lock));
137     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
138     try {
139     assertFalse(lock.hasQueuedThreads());
140     lock.lock();
141     t1.start();
142     Thread.sleep(SHORT_DELAY_MS);
143     assertTrue(lock.hasQueuedThreads());
144     t2.start();
145     Thread.sleep(SHORT_DELAY_MS);
146     assertTrue(lock.hasQueuedThreads());
147     t1.interrupt();
148     Thread.sleep(SHORT_DELAY_MS);
149     assertTrue(lock.hasQueuedThreads());
150     lock.unlock();
151     Thread.sleep(SHORT_DELAY_MS);
152     assertFalse(lock.hasQueuedThreads());
153     t1.join();
154     t2.join();
155     } catch(Exception e){
156     unexpectedException();
157     }
158     }
159    
160     /**
161     * timed trylock is interruptible.
162     */
163     public void testInterruptedException2() {
164     final Mutex lock = new Mutex();
165     lock.lock();
166     Thread t = new Thread(new Runnable() {
167     public void run() {
168     try {
169     lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
170     threadShouldThrow();
171     } catch(InterruptedException success){}
172     }
173     });
174     try {
175     t.start();
176     t.interrupt();
177     } catch(Exception e){
178     unexpectedException();
179     }
180     }
181    
182    
183     /**
184     * Trylock on a locked lock fails
185     */
186     public void testTryLockWhenLocked() {
187     final Mutex lock = new Mutex();
188     lock.lock();
189     Thread t = new Thread(new Runnable() {
190     public void run() {
191     threadAssertFalse(lock.tryLock());
192     }
193     });
194     try {
195     t.start();
196     t.join();
197     lock.unlock();
198     } catch(Exception e){
199     unexpectedException();
200     }
201     }
202    
203     /**
204     * Timed trylock on a locked lock times out
205     */
206     public void testTryLock_Timeout() {
207     final Mutex lock = new Mutex();
208     lock.lock();
209     Thread t = new Thread(new Runnable() {
210     public void run() {
211     try {
212     threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
213     } catch (Exception ex) {
214     threadUnexpectedException();
215     }
216     }
217     });
218     try {
219     t.start();
220     t.join();
221     lock.unlock();
222     } catch(Exception e){
223     unexpectedException();
224     }
225     }
226    
227    
228     /**
229     * isLocked is true when locked and false when not
230     */
231     public void testIsLocked() {
232     final Mutex lock = new Mutex();
233     lock.lock();
234     assertTrue(lock.isLocked());
235     lock.unlock();
236     assertFalse(lock.isLocked());
237     Thread t = new Thread(new Runnable() {
238     public void run() {
239     lock.lock();
240     try {
241     Thread.sleep(SMALL_DELAY_MS);
242     }
243     catch(Exception e) {
244     threadUnexpectedException();
245     }
246     lock.unlock();
247     }
248     });
249     try {
250     t.start();
251     Thread.sleep(SHORT_DELAY_MS);
252     assertTrue(lock.isLocked());
253     t.join();
254     assertFalse(lock.isLocked());
255     } catch(Exception e){
256     unexpectedException();
257     }
258     }
259    
260    
261     /**
262     * lockInterruptibly is interruptible.
263     */
264     public void testLockInterruptibly1() {
265     final Mutex lock = new Mutex();
266     lock.lock();
267     Thread t = new Thread(new InterruptedLockRunnable(lock));
268     try {
269     t.start();
270     t.interrupt();
271     lock.unlock();
272     t.join();
273     } catch(Exception e){
274     unexpectedException();
275     }
276     }
277    
278     /**
279     * lockInterruptibly succeeds when unlocked, else is interruptible
280     */
281     public void testLockInterruptibly2() {
282     final Mutex lock = new Mutex();
283     try {
284     lock.lockInterruptibly();
285     } catch(Exception e) {
286     unexpectedException();
287     }
288     Thread t = new Thread(new InterruptedLockRunnable(lock));
289     try {
290     t.start();
291     t.interrupt();
292     assertTrue(lock.isLocked());
293     t.join();
294     } catch(Exception e){
295     unexpectedException();
296     }
297     }
298    
299     /**
300     * Calling await without holding lock throws IllegalMonitorStateException
301     */
302     public void testAwait_IllegalMonitor() {
303     final Mutex lock = new Mutex();
304     final Condition c = lock.newCondition();
305     try {
306     c.await();
307     shouldThrow();
308     }
309     catch (IllegalMonitorStateException success) {
310     }
311     catch (Exception ex) {
312     unexpectedException();
313     }
314     }
315    
316     /**
317     * Calling signal without holding lock throws IllegalMonitorStateException
318     */
319     public void testSignal_IllegalMonitor() {
320     final Mutex lock = new Mutex();
321     final Condition c = lock.newCondition();
322     try {
323     c.signal();
324     shouldThrow();
325     }
326     catch (IllegalMonitorStateException success) {
327     }
328     catch (Exception ex) {
329     unexpectedException();
330     }
331     }
332    
333     /**
334     * awaitNanos without a signal times out
335     */
336     public void testAwaitNanos_Timeout() {
337     final Mutex lock = new Mutex();
338     final Condition c = lock.newCondition();
339     try {
340     lock.lock();
341     long t = c.awaitNanos(100);
342     assertTrue(t <= 0);
343     lock.unlock();
344     }
345     catch (Exception ex) {
346     unexpectedException();
347     }
348     }
349    
350     /**
351     * timed await without a signal times out
352     */
353     public void testAwait_Timeout() {
354     final Mutex lock = new Mutex();
355     final Condition c = lock.newCondition();
356     try {
357     lock.lock();
358     assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
359     lock.unlock();
360     }
361     catch (Exception ex) {
362     unexpectedException();
363     }
364     }
365    
366     /**
367     * awaitUntil without a signal times out
368     */
369     public void testAwaitUntil_Timeout() {
370     final Mutex lock = new Mutex();
371     final Condition c = lock.newCondition();
372     try {
373     lock.lock();
374     java.util.Date d = new java.util.Date();
375     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
376     lock.unlock();
377     }
378     catch (Exception ex) {
379     unexpectedException();
380     }
381     }
382    
383     /**
384     * await returns when signalled
385     */
386     public void testAwait() {
387     final Mutex lock = new Mutex();
388     final Condition c = lock.newCondition();
389     Thread t = new Thread(new Runnable() {
390     public void run() {
391     try {
392     lock.lock();
393     c.await();
394     lock.unlock();
395     }
396     catch(InterruptedException e) {
397     threadUnexpectedException();
398     }
399     }
400     });
401    
402     try {
403     t.start();
404     Thread.sleep(SHORT_DELAY_MS);
405     lock.lock();
406     c.signal();
407     lock.unlock();
408     t.join(SHORT_DELAY_MS);
409     assertFalse(t.isAlive());
410     }
411     catch (Exception ex) {
412     unexpectedException();
413     }
414     }
415    
416     }