ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FairSemaphoreTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:55 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

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    
12     public class FairSemaphoreTest extends TestCase{
13    
14     public static void main(String[] args) {
15     junit.textui.TestRunner.run (suite());
16     }
17    
18     public static Test suite() {
19     return new TestSuite(FairSemaphoreTest.class);
20     }
21    
22     private static long SHORT_DELAY_MS = 100;
23     private static long MEDIUM_DELAY_MS = 1000;
24     private static long LONG_DELAY_MS = 10000;
25    
26    
27     public void testConstructor1() {
28     FairSemaphore s = new FairSemaphore(0);
29     assertEquals(0, s.availablePermits());
30     }
31    
32     public void testConstructor2() {
33     FairSemaphore s = new FairSemaphore(-1);
34     assertEquals(-1, s.availablePermits());
35     }
36    
37     public void testTryAcquireInSameThread() {
38     FairSemaphore s = new FairSemaphore(2);
39     assertEquals(2, s.availablePermits());
40     assertTrue(s.tryAcquire());
41     assertTrue(s.tryAcquire());
42     assertEquals(0, s.availablePermits());
43     assertFalse(s.tryAcquire());
44     }
45    
46     public void testTryAcquireNInSameThread() {
47     FairSemaphore s = new FairSemaphore(2);
48     assertEquals(2, s.availablePermits());
49     assertTrue(s.tryAcquire(2));
50     assertEquals(0, s.availablePermits());
51     assertFalse(s.tryAcquire());
52     }
53    
54     public void testAcquireReleaseInSameThread(){
55     FairSemaphore s = new FairSemaphore(1);
56     try {
57     s.acquire();
58     s.release();
59     s.acquire();
60     s.release();
61     s.acquire();
62     s.release();
63     s.acquire();
64     s.release();
65     s.acquire();
66     s.release();
67     assertEquals(1, s.availablePermits());
68     } catch( InterruptedException e){
69     fail("unexpected exception");
70     }
71     }
72    
73     public void testAcquireReleaseNInSameThread(){
74     FairSemaphore s = new FairSemaphore(1);
75     try {
76     s.release(1);
77     s.acquire(1);
78     s.release(2);
79     s.acquire(2);
80     s.release(3);
81     s.acquire(3);
82     s.release(4);
83     s.acquire(4);
84     s.release(5);
85     s.acquire(5);
86     assertEquals(1, s.availablePermits());
87     } catch( InterruptedException e){
88     fail("unexpected exception");
89     }
90     }
91    
92     public void testAcquireUninterruptiblyReleaseNInSameThread(){
93     FairSemaphore s = new FairSemaphore(1);
94     try {
95     s.release(1);
96     s.acquireUninterruptibly(1);
97     s.release(2);
98     s.acquireUninterruptibly(2);
99     s.release(3);
100     s.acquireUninterruptibly(3);
101     s.release(4);
102     s.acquireUninterruptibly(4);
103     s.release(5);
104     s.acquireUninterruptibly(5);
105     assertEquals(1, s.availablePermits());
106     } finally {
107     }
108     }
109    
110     public void testAcquireReleaseInDifferentThreads() {
111     final FairSemaphore s = new FairSemaphore(1);
112     Thread t = new Thread(new Runnable(){
113     public void run(){
114     try{
115     s.acquire();
116     s.acquire();
117     s.acquire();
118     s.acquire();
119     s.acquire();
120     }catch(InterruptedException ie){
121     fail("unexpected exception");
122     }
123     }
124     });
125     t.start();
126     try {
127     s.release();
128     s.release();
129     s.release();
130     s.release();
131     s.release();
132     t.join();
133     assertEquals(1, s.availablePermits());
134     } catch( InterruptedException e){
135     fail("unexpected exception");
136     }
137     }
138    
139     public void testAcquireReleaseNInDifferentThreads() {
140     final FairSemaphore s = new FairSemaphore(2);
141     Thread t = new Thread(new Runnable(){
142     public void run(){
143     try{
144     s.release(2);
145     s.release(2);
146     s.acquire(2);
147     s.acquire(2);
148     }catch(InterruptedException ie){
149     fail("unexpected exception");
150     }
151     }
152     });
153     t.start();
154     try {
155     s.acquire(2);
156     s.acquire(2);
157     s.release(2);
158     s.release(2);
159     t.join();
160     } catch( InterruptedException e){
161     fail("unexpected exception");
162     }
163     }
164    
165     public void testTimedAcquireReleaseInSameThread(){
166     FairSemaphore s = new FairSemaphore(1);
167     try {
168     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
169     s.release();
170     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
171     s.release();
172     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
173     s.release();
174     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
175     s.release();
176     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
177     s.release();
178     assertEquals(1, s.availablePermits());
179     } catch( InterruptedException e){
180     fail("unexpected exception");
181     }
182     }
183    
184     public void testTimedAcquireReleaseNInSameThread(){
185     FairSemaphore s = new FairSemaphore(1);
186     try {
187     s.release(1);
188     assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
189     s.release(2);
190     assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
191     s.release(3);
192     assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
193     s.release(4);
194     assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
195     s.release(5);
196     assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
197     assertEquals(1, s.availablePermits());
198     } catch( InterruptedException e){
199     fail("unexpected exception");
200     }
201     }
202    
203     public void testTimedAcquireReleaseInDifferentThreads() {
204     final FairSemaphore s = new FairSemaphore(1);
205     Thread t = new Thread(new Runnable(){
206     public void run(){
207     try{
208     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
209     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
210     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
211     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
212     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
213    
214     }catch(InterruptedException ie){
215     fail("unexpected exception");
216     }
217     }
218     });
219     t.start();
220     try {
221     s.release();
222     s.release();
223     s.release();
224     s.release();
225     s.release();
226     t.join();
227     } catch( InterruptedException e){
228     fail("unexpected exception");
229     }
230     }
231    
232     public void testTimedAcquireReleaseNInDifferentThreads() {
233     final FairSemaphore s = new FairSemaphore(2);
234     Thread t = new Thread(new Runnable(){
235     public void run(){
236     try{
237     assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
238     s.release(2);
239     assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
240     s.release(2);
241     }catch(InterruptedException ie){
242     fail("unexpected exception");
243     }
244     }
245     });
246     t.start();
247     try {
248     s.release(2);
249     assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
250     s.release(2);
251     assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
252     t.join();
253     } catch( InterruptedException e){
254     fail("unexpected exception");
255     }
256     }
257    
258     public void testAcquire_InterruptedException(){
259     final FairSemaphore s = new FairSemaphore(0);
260     Thread t = new Thread(new Runnable(){
261     public void run(){
262     try{
263     s.acquire();
264     fail("should throw");
265     }catch(InterruptedException success){}
266     }
267     });
268     t.start();
269     try{
270     Thread.sleep(SHORT_DELAY_MS);
271     t.interrupt();
272     t.join();
273     } catch(InterruptedException e){
274     fail("unexpected exception");
275     }
276     }
277    
278     public void testAcquireN_InterruptedException(){
279     final FairSemaphore s = new FairSemaphore(2);
280     Thread t = new Thread(new Runnable(){
281     public void run(){
282     try{
283     s.acquire(3);
284     fail("should throw");
285     }catch(InterruptedException success){}
286     }
287     });
288     t.start();
289     try{
290     Thread.sleep(SHORT_DELAY_MS);
291     t.interrupt();
292     t.join();
293     } catch(InterruptedException e){
294     fail("unexpected exception");
295     }
296     }
297    
298     public void testTryAcquire_InterruptedException(){
299     final FairSemaphore s = new FairSemaphore(0);
300     Thread t = new Thread(new Runnable(){
301     public void run(){
302     try{
303     s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
304     fail("should throw");
305     }catch(InterruptedException success){
306     }
307     }
308     });
309     t.start();
310     try{
311     Thread.sleep(SHORT_DELAY_MS);
312     t.interrupt();
313     t.join();
314     } catch(InterruptedException e){
315     fail("unexpected exception");
316     }
317     }
318    
319     public void testTryAcquireN_InterruptedException(){
320     final FairSemaphore s = new FairSemaphore(1);
321     Thread t = new Thread(new Runnable(){
322     public void run(){
323     try{
324     s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
325     fail("should throw");
326     }catch(InterruptedException success){
327     }
328     }
329     });
330     t.start();
331     try{
332     Thread.sleep(SHORT_DELAY_MS);
333     t.interrupt();
334     t.join();
335     } catch(InterruptedException e){
336     fail("unexpected exception");
337     }
338     }
339    
340     }