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 |
/** |
24 |
* |
25 |
*/ |
26 |
public void testConstructor1() { |
27 |
FairSemaphore s = new FairSemaphore(0); |
28 |
assertEquals(0, s.availablePermits()); |
29 |
} |
30 |
|
31 |
/** |
32 |
* |
33 |
*/ |
34 |
public void testConstructor2() { |
35 |
FairSemaphore s = new FairSemaphore(-1); |
36 |
assertEquals(-1, s.availablePermits()); |
37 |
} |
38 |
|
39 |
/** |
40 |
* |
41 |
*/ |
42 |
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 |
/** |
52 |
* |
53 |
*/ |
54 |
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 |
/** |
63 |
* |
64 |
*/ |
65 |
public void testAcquireReleaseInSameThread() { |
66 |
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 |
unexpectedException(); |
81 |
} |
82 |
} |
83 |
|
84 |
/** |
85 |
* |
86 |
*/ |
87 |
public void testAcquireReleaseNInSameThread() { |
88 |
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 |
unexpectedException(); |
103 |
} |
104 |
} |
105 |
|
106 |
/** |
107 |
* |
108 |
*/ |
109 |
public void testAcquireUninterruptiblyReleaseNInSameThread() { |
110 |
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 |
/** |
128 |
* |
129 |
*/ |
130 |
public void testAcquireReleaseInDifferentThreads() { |
131 |
final FairSemaphore s = new FairSemaphore(1); |
132 |
Thread t = new Thread(new Runnable() { |
133 |
public void run() { |
134 |
try { |
135 |
s.acquire(); |
136 |
s.acquire(); |
137 |
s.acquire(); |
138 |
s.acquire(); |
139 |
s.acquire(); |
140 |
} catch(InterruptedException ie){ |
141 |
threadUnexpectedException(); |
142 |
} |
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 |
unexpectedException(); |
156 |
} |
157 |
} |
158 |
|
159 |
/** |
160 |
* |
161 |
*/ |
162 |
public void testAcquireReleaseNInDifferentThreads() { |
163 |
final FairSemaphore s = new FairSemaphore(2); |
164 |
Thread t = new Thread(new Runnable() { |
165 |
public void run() { |
166 |
try { |
167 |
s.release(2); |
168 |
s.release(2); |
169 |
s.acquire(2); |
170 |
s.acquire(2); |
171 |
} catch(InterruptedException ie){ |
172 |
threadUnexpectedException(); |
173 |
} |
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 |
unexpectedException(); |
185 |
} |
186 |
} |
187 |
|
188 |
/** |
189 |
* |
190 |
*/ |
191 |
public void testTimedAcquireReleaseInSameThread() { |
192 |
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 |
unexpectedException(); |
207 |
} |
208 |
} |
209 |
|
210 |
/** |
211 |
* |
212 |
*/ |
213 |
public void testTimedAcquireReleaseNInSameThread() { |
214 |
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 |
unexpectedException(); |
229 |
} |
230 |
} |
231 |
|
232 |
/** |
233 |
* |
234 |
*/ |
235 |
public void testTimedAcquireReleaseInDifferentThreads() { |
236 |
final FairSemaphore s = new FairSemaphore(1); |
237 |
Thread t = new Thread(new Runnable() { |
238 |
public void run() { |
239 |
try { |
240 |
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 |
|
246 |
} catch(InterruptedException ie){ |
247 |
threadUnexpectedException(); |
248 |
} |
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 |
unexpectedException(); |
261 |
} |
262 |
} |
263 |
|
264 |
/** |
265 |
* |
266 |
*/ |
267 |
public void testTimedAcquireReleaseNInDifferentThreads() { |
268 |
final FairSemaphore s = new FairSemaphore(2); |
269 |
Thread t = new Thread(new Runnable() { |
270 |
public void run() { |
271 |
try { |
272 |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
273 |
s.release(2); |
274 |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
275 |
s.release(2); |
276 |
} catch(InterruptedException ie){ |
277 |
threadUnexpectedException(); |
278 |
} |
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 |
unexpectedException(); |
290 |
} |
291 |
} |
292 |
|
293 |
/** |
294 |
* |
295 |
*/ |
296 |
public void testAcquire_InterruptedException() { |
297 |
final FairSemaphore s = new FairSemaphore(0); |
298 |
Thread t = new Thread(new Runnable() { |
299 |
public void run() { |
300 |
try { |
301 |
s.acquire(); |
302 |
threadShouldThrow(); |
303 |
} catch(InterruptedException success){} |
304 |
} |
305 |
}); |
306 |
t.start(); |
307 |
try { |
308 |
Thread.sleep(SHORT_DELAY_MS); |
309 |
t.interrupt(); |
310 |
t.join(); |
311 |
} catch(InterruptedException e){ |
312 |
unexpectedException(); |
313 |
} |
314 |
} |
315 |
|
316 |
/** |
317 |
* |
318 |
*/ |
319 |
public void testAcquireN_InterruptedException() { |
320 |
final FairSemaphore s = new FairSemaphore(2); |
321 |
Thread t = new Thread(new Runnable() { |
322 |
public void run() { |
323 |
try { |
324 |
s.acquire(3); |
325 |
threadShouldThrow(); |
326 |
} catch(InterruptedException success){} |
327 |
} |
328 |
}); |
329 |
t.start(); |
330 |
try { |
331 |
Thread.sleep(SHORT_DELAY_MS); |
332 |
t.interrupt(); |
333 |
t.join(); |
334 |
} catch(InterruptedException e){ |
335 |
unexpectedException(); |
336 |
} |
337 |
} |
338 |
|
339 |
/** |
340 |
* |
341 |
*/ |
342 |
public void testTryAcquire_InterruptedException() { |
343 |
final FairSemaphore s = new FairSemaphore(0); |
344 |
Thread t = new Thread(new Runnable() { |
345 |
public void run() { |
346 |
try { |
347 |
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
348 |
threadShouldThrow(); |
349 |
} catch(InterruptedException success){ |
350 |
} |
351 |
} |
352 |
}); |
353 |
t.start(); |
354 |
try { |
355 |
Thread.sleep(SHORT_DELAY_MS); |
356 |
t.interrupt(); |
357 |
t.join(); |
358 |
} catch(InterruptedException e){ |
359 |
unexpectedException(); |
360 |
} |
361 |
} |
362 |
|
363 |
/** |
364 |
* |
365 |
*/ |
366 |
public void testTryAcquireN_InterruptedException() { |
367 |
final FairSemaphore s = new FairSemaphore(1); |
368 |
Thread t = new Thread(new Runnable() { |
369 |
public void run() { |
370 |
try { |
371 |
s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
372 |
threadShouldThrow(); |
373 |
} catch(InterruptedException success){ |
374 |
} |
375 |
} |
376 |
}); |
377 |
t.start(); |
378 |
try { |
379 |
Thread.sleep(SHORT_DELAY_MS); |
380 |
t.interrupt(); |
381 |
t.join(); |
382 |
} catch(InterruptedException e){ |
383 |
unexpectedException(); |
384 |
} |
385 |
} |
386 |
|
387 |
/** |
388 |
* |
389 |
*/ |
390 |
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 |
unexpectedException(); |
409 |
} |
410 |
} |
411 |
|
412 |
} |