ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FairSemaphoreTest.java
Revision: 1.4
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.3: +120 -67 lines
Log Message:
Documentation scaffolding

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.*;
10     import java.util.concurrent.*;
11 dl 1.2 import java.io.*;
12 dl 1.1
13 dl 1.3 public class FairSemaphoreTest extends JSR166TestCase{
14 dl 1.1
15     public static void main(String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18    
19     public static Test suite() {
20     return new TestSuite(FairSemaphoreTest.class);
21     }
22    
23 dl 1.4 /**
24     *
25     */
26 dl 1.1 public void testConstructor1() {
27     FairSemaphore s = new FairSemaphore(0);
28     assertEquals(0, s.availablePermits());
29     }
30    
31 dl 1.4 /**
32     *
33     */
34 dl 1.1 public void testConstructor2() {
35     FairSemaphore s = new FairSemaphore(-1);
36     assertEquals(-1, s.availablePermits());
37     }
38    
39 dl 1.4 /**
40     *
41     */
42 dl 1.1 public void testTryAcquireInSameThread() {
43     FairSemaphore s = new FairSemaphore(2);
44     assertEquals(2, s.availablePermits());
45     assertTrue(s.tryAcquire());
46     assertTrue(s.tryAcquire());
47     assertEquals(0, s.availablePermits());
48     assertFalse(s.tryAcquire());
49     }
50    
51 dl 1.4 /**
52     *
53     */
54 dl 1.1 public void testTryAcquireNInSameThread() {
55     FairSemaphore s = new FairSemaphore(2);
56     assertEquals(2, s.availablePermits());
57     assertTrue(s.tryAcquire(2));
58     assertEquals(0, s.availablePermits());
59     assertFalse(s.tryAcquire());
60     }
61    
62 dl 1.4 /**
63     *
64     */
65     public void testAcquireReleaseInSameThread() {
66 dl 1.1 FairSemaphore s = new FairSemaphore(1);
67     try {
68     s.acquire();
69     s.release();
70     s.acquire();
71     s.release();
72     s.acquire();
73     s.release();
74     s.acquire();
75     s.release();
76     s.acquire();
77     s.release();
78     assertEquals(1, s.availablePermits());
79     } catch( InterruptedException e){
80 dl 1.4 unexpectedException();
81 dl 1.1 }
82     }
83    
84 dl 1.4 /**
85     *
86     */
87     public void testAcquireReleaseNInSameThread() {
88 dl 1.1 FairSemaphore s = new FairSemaphore(1);
89     try {
90     s.release(1);
91     s.acquire(1);
92     s.release(2);
93     s.acquire(2);
94     s.release(3);
95     s.acquire(3);
96     s.release(4);
97     s.acquire(4);
98     s.release(5);
99     s.acquire(5);
100     assertEquals(1, s.availablePermits());
101     } catch( InterruptedException e){
102 dl 1.4 unexpectedException();
103 dl 1.1 }
104     }
105    
106 dl 1.4 /**
107     *
108     */
109     public void testAcquireUninterruptiblyReleaseNInSameThread() {
110 dl 1.1 FairSemaphore s = new FairSemaphore(1);
111     try {
112     s.release(1);
113     s.acquireUninterruptibly(1);
114     s.release(2);
115     s.acquireUninterruptibly(2);
116     s.release(3);
117     s.acquireUninterruptibly(3);
118     s.release(4);
119     s.acquireUninterruptibly(4);
120     s.release(5);
121     s.acquireUninterruptibly(5);
122     assertEquals(1, s.availablePermits());
123     } finally {
124     }
125     }
126    
127 dl 1.4 /**
128     *
129     */
130 dl 1.1 public void testAcquireReleaseInDifferentThreads() {
131     final FairSemaphore s = new FairSemaphore(1);
132 dl 1.4 Thread t = new Thread(new Runnable() {
133     public void run() {
134     try {
135 dl 1.1 s.acquire();
136     s.acquire();
137     s.acquire();
138     s.acquire();
139     s.acquire();
140 dl 1.4 } catch(InterruptedException ie){
141     threadUnexpectedException();
142 dl 1.1 }
143     }
144     });
145     t.start();
146     try {
147     s.release();
148     s.release();
149     s.release();
150     s.release();
151     s.release();
152     t.join();
153     assertEquals(1, s.availablePermits());
154     } catch( InterruptedException e){
155 dl 1.4 unexpectedException();
156 dl 1.1 }
157     }
158    
159 dl 1.4 /**
160     *
161     */
162 dl 1.1 public void testAcquireReleaseNInDifferentThreads() {
163     final FairSemaphore s = new FairSemaphore(2);
164 dl 1.4 Thread t = new Thread(new Runnable() {
165     public void run() {
166     try {
167 dl 1.1 s.release(2);
168     s.release(2);
169     s.acquire(2);
170     s.acquire(2);
171 dl 1.4 } catch(InterruptedException ie){
172     threadUnexpectedException();
173 dl 1.1 }
174     }
175     });
176     t.start();
177     try {
178     s.acquire(2);
179     s.acquire(2);
180     s.release(2);
181     s.release(2);
182     t.join();
183     } catch( InterruptedException e){
184 dl 1.4 unexpectedException();
185 dl 1.1 }
186     }
187    
188 dl 1.4 /**
189     *
190     */
191     public void testTimedAcquireReleaseInSameThread() {
192 dl 1.1 FairSemaphore s = new FairSemaphore(1);
193     try {
194     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
195     s.release();
196     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
197     s.release();
198     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
199     s.release();
200     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
201     s.release();
202     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
203     s.release();
204     assertEquals(1, s.availablePermits());
205     } catch( InterruptedException e){
206 dl 1.4 unexpectedException();
207 dl 1.1 }
208     }
209    
210 dl 1.4 /**
211     *
212     */
213     public void testTimedAcquireReleaseNInSameThread() {
214 dl 1.1 FairSemaphore s = new FairSemaphore(1);
215     try {
216     s.release(1);
217     assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
218     s.release(2);
219     assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
220     s.release(3);
221     assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
222     s.release(4);
223     assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
224     s.release(5);
225     assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
226     assertEquals(1, s.availablePermits());
227     } catch( InterruptedException e){
228 dl 1.4 unexpectedException();
229 dl 1.1 }
230     }
231    
232 dl 1.4 /**
233     *
234     */
235 dl 1.1 public void testTimedAcquireReleaseInDifferentThreads() {
236     final FairSemaphore s = new FairSemaphore(1);
237 dl 1.4 Thread t = new Thread(new Runnable() {
238     public void run() {
239     try {
240 dl 1.3 threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
241     threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
242     threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
243     threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
244     threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
245 dl 1.1
246 dl 1.4 } catch(InterruptedException ie){
247     threadUnexpectedException();
248 dl 1.1 }
249     }
250     });
251     t.start();
252     try {
253     s.release();
254     s.release();
255     s.release();
256     s.release();
257     s.release();
258     t.join();
259     } catch( InterruptedException e){
260 dl 1.4 unexpectedException();
261 dl 1.1 }
262     }
263    
264 dl 1.4 /**
265     *
266     */
267 dl 1.1 public void testTimedAcquireReleaseNInDifferentThreads() {
268     final FairSemaphore s = new FairSemaphore(2);
269 dl 1.4 Thread t = new Thread(new Runnable() {
270     public void run() {
271     try {
272 dl 1.3 threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
273 dl 1.1 s.release(2);
274 dl 1.3 threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
275 dl 1.1 s.release(2);
276 dl 1.4 } catch(InterruptedException ie){
277     threadUnexpectedException();
278 dl 1.1 }
279     }
280     });
281     t.start();
282     try {
283     s.release(2);
284     assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
285     s.release(2);
286     assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
287     t.join();
288     } catch( InterruptedException e){
289 dl 1.4 unexpectedException();
290 dl 1.1 }
291     }
292    
293 dl 1.4 /**
294     *
295     */
296     public void testAcquire_InterruptedException() {
297 dl 1.1 final FairSemaphore s = new FairSemaphore(0);
298 dl 1.4 Thread t = new Thread(new Runnable() {
299     public void run() {
300     try {
301 dl 1.1 s.acquire();
302 dl 1.4 threadShouldThrow();
303     } catch(InterruptedException success){}
304 dl 1.1 }
305     });
306     t.start();
307 dl 1.4 try {
308 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
309     t.interrupt();
310     t.join();
311     } catch(InterruptedException e){
312 dl 1.4 unexpectedException();
313 dl 1.1 }
314     }
315    
316 dl 1.4 /**
317     *
318     */
319     public void testAcquireN_InterruptedException() {
320 dl 1.1 final FairSemaphore s = new FairSemaphore(2);
321 dl 1.4 Thread t = new Thread(new Runnable() {
322     public void run() {
323     try {
324 dl 1.1 s.acquire(3);
325 dl 1.4 threadShouldThrow();
326     } catch(InterruptedException success){}
327 dl 1.1 }
328     });
329     t.start();
330 dl 1.4 try {
331 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
332     t.interrupt();
333     t.join();
334     } catch(InterruptedException e){
335 dl 1.4 unexpectedException();
336 dl 1.1 }
337     }
338    
339 dl 1.4 /**
340     *
341     */
342     public void testTryAcquire_InterruptedException() {
343 dl 1.1 final FairSemaphore s = new FairSemaphore(0);
344 dl 1.4 Thread t = new Thread(new Runnable() {
345     public void run() {
346     try {
347 dl 1.1 s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
348 dl 1.4 threadShouldThrow();
349     } catch(InterruptedException success){
350 dl 1.1 }
351     }
352     });
353     t.start();
354 dl 1.4 try {
355 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
356     t.interrupt();
357     t.join();
358     } catch(InterruptedException e){
359 dl 1.4 unexpectedException();
360 dl 1.1 }
361     }
362    
363 dl 1.4 /**
364     *
365     */
366     public void testTryAcquireN_InterruptedException() {
367 dl 1.1 final FairSemaphore s = new FairSemaphore(1);
368 dl 1.4 Thread t = new Thread(new Runnable() {
369     public void run() {
370     try {
371 dl 1.1 s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
372 dl 1.4 threadShouldThrow();
373     } catch(InterruptedException success){
374 dl 1.1 }
375     }
376     });
377     t.start();
378 dl 1.4 try {
379 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
380     t.interrupt();
381     t.join();
382     } catch(InterruptedException e){
383 dl 1.4 unexpectedException();
384 dl 1.2 }
385     }
386    
387 dl 1.4 /**
388     *
389     */
390 dl 1.2 public void testSerialization() {
391     FairSemaphore l = new FairSemaphore(3);
392    
393     try {
394     l.acquire();
395     l.release();
396     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
397     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
398     out.writeObject(l);
399     out.close();
400    
401     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
402     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
403     FairSemaphore r = (FairSemaphore) in.readObject();
404     assertEquals(3, r.availablePermits());
405     r.acquire();
406     r.release();
407     } catch(Exception e){
408 dl 1.4 unexpectedException();
409 dl 1.1 }
410     }
411    
412     }