ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FairSemaphoreTest.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: +16 -21 lines
Log Message:
New base class JSR166TestCase

File Contents

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