ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SemaphoreTest.java
Revision: 1.3
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +7 -13 lines
Log Message:
New base class JSR166TestCase

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 SemaphoreTest 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(SemaphoreTest.class);
21     }
22    
23     public void testConstructor1() {
24     Semaphore s = new Semaphore(0);
25     assertEquals(0, s.availablePermits());
26     }
27    
28     public void testConstructor2() {
29     Semaphore s = new Semaphore(-1);
30     assertEquals(-1, s.availablePermits());
31     }
32    
33     public void testTryAcquireInSameThread() {
34     Semaphore s = new Semaphore(2);
35     assertEquals(2, s.availablePermits());
36     assertTrue(s.tryAcquire());
37     assertTrue(s.tryAcquire());
38     assertEquals(0, s.availablePermits());
39     assertFalse(s.tryAcquire());
40     }
41    
42     public void testAcquireReleaseInSameThread(){
43     Semaphore s = new Semaphore(1);
44     try {
45     s.acquire();
46     s.release();
47     s.acquire();
48     s.release();
49     s.acquire();
50     s.release();
51     s.acquire();
52     s.release();
53     s.acquire();
54     s.release();
55     assertEquals(1, s.availablePermits());
56     } catch( InterruptedException e){
57     fail("unexpected exception");
58     }
59     }
60    
61     public void testAcquireUninterruptiblyReleaseInSameThread(){
62     Semaphore s = new Semaphore(1);
63     try {
64     s.acquireUninterruptibly();
65     s.release();
66     s.acquireUninterruptibly();
67     s.release();
68     s.acquireUninterruptibly();
69     s.release();
70     s.acquireUninterruptibly();
71     s.release();
72     s.acquireUninterruptibly();
73     s.release();
74     assertEquals(1, s.availablePermits());
75     } finally {
76     }
77     }
78    
79    
80     public void testAcquireReleaseInDifferentThreads() {
81     final Semaphore s = new Semaphore(1);
82     Thread t = new Thread(new Runnable(){
83     public void run(){
84     try{
85     s.acquire();
86     s.release();
87     s.release();
88     s.acquire();
89     }catch(InterruptedException ie){
90 dl 1.3 threadFail("unexpected exception");
91 dl 1.1 }
92     }
93     });
94     t.start();
95     try {
96     s.release();
97     s.release();
98     s.acquire();
99     s.acquire();
100     t.join();
101     } catch( InterruptedException e){
102     fail("unexpected exception");
103     }
104     }
105    
106     public void testTimedAcquireReleaseInSameThread(){
107     Semaphore s = new Semaphore(1);
108     try {
109     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
110     s.release();
111     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
112     s.release();
113     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
114     s.release();
115     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
116     s.release();
117     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
118     s.release();
119     assertEquals(1, s.availablePermits());
120     } catch( InterruptedException e){
121     fail("unexpected exception");
122     }
123     }
124    
125     public void testTimedAcquireReleaseInDifferentThreads() {
126     final Semaphore s = new Semaphore(1);
127     Thread t = new Thread(new Runnable(){
128     public void run(){
129     try{
130     s.release();
131 dl 1.3 threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
132 dl 1.1 s.release();
133 dl 1.3 threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
134 dl 1.1
135     }catch(InterruptedException ie){
136 dl 1.3 threadFail("unexpected exception");
137 dl 1.1 }
138     }
139     });
140     t.start();
141     try {
142     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
143     s.release();
144     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
145     s.release();
146     t.join();
147     } catch( InterruptedException e){
148     fail("unexpected exception");
149     }
150     }
151    
152     public void testAcquire_InterruptedException(){
153     final Semaphore s = new Semaphore(0);
154     Thread t = new Thread(new Runnable(){
155     public void run(){
156     try{
157     s.acquire();
158 dl 1.3 threadFail("should throw");
159 dl 1.1 }catch(InterruptedException success){}
160     }
161     });
162     t.start();
163     try{
164     Thread.sleep(SHORT_DELAY_MS);
165     t.interrupt();
166     t.join();
167     } catch(InterruptedException e){
168     fail("unexpected exception");
169     }
170     }
171    
172     public void testTryAcquire_InterruptedException(){
173     final Semaphore s = new Semaphore(0);
174     Thread t = new Thread(new Runnable(){
175     public void run(){
176     try{
177     s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
178 dl 1.3 threadFail("should throw");
179 dl 1.1 }catch(InterruptedException success){
180     }
181     }
182     });
183     t.start();
184     try{
185     Thread.sleep(SHORT_DELAY_MS);
186     t.interrupt();
187     t.join();
188     } catch(InterruptedException e){
189     fail("unexpected exception");
190     }
191     }
192 dl 1.2
193     public void testSerialization() {
194     Semaphore l = new Semaphore(3);
195     try {
196     l.acquire();
197     l.release();
198     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
199     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
200     out.writeObject(l);
201     out.close();
202    
203     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
204     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
205     Semaphore r = (Semaphore) in.readObject();
206     assertEquals(3, r.availablePermits());
207     r.acquire();
208     r.release();
209     } catch(Exception e){
210     e.printStackTrace();
211     fail("unexpected exception");
212     }
213     }
214    
215 dl 1.1 }