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()); |
48 |
|
assertFalse(s.tryAcquire()); |
49 |
|
} |
50 |
|
|
51 |
+ |
/** |
52 |
+ |
* |
53 |
+ |
*/ |
54 |
|
public void testTryAcquireNInSameThread() { |
55 |
|
FairSemaphore s = new FairSemaphore(2); |
56 |
|
assertEquals(2, s.availablePermits()); |
59 |
|
assertFalse(s.tryAcquire()); |
60 |
|
} |
61 |
|
|
62 |
< |
public void testAcquireReleaseInSameThread(){ |
62 |
> |
/** |
63 |
> |
* |
64 |
> |
*/ |
65 |
> |
public void testAcquireReleaseInSameThread() { |
66 |
|
FairSemaphore s = new FairSemaphore(1); |
67 |
|
try { |
68 |
|
s.acquire(); |
77 |
|
s.release(); |
78 |
|
assertEquals(1, s.availablePermits()); |
79 |
|
} catch( InterruptedException e){ |
80 |
< |
fail("unexpected exception"); |
80 |
> |
unexpectedException(); |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
< |
public void testAcquireReleaseNInSameThread(){ |
84 |
> |
/** |
85 |
> |
* |
86 |
> |
*/ |
87 |
> |
public void testAcquireReleaseNInSameThread() { |
88 |
|
FairSemaphore s = new FairSemaphore(1); |
89 |
|
try { |
90 |
|
s.release(1); |
99 |
|
s.acquire(5); |
100 |
|
assertEquals(1, s.availablePermits()); |
101 |
|
} catch( InterruptedException e){ |
102 |
< |
fail("unexpected exception"); |
102 |
> |
unexpectedException(); |
103 |
|
} |
104 |
|
} |
105 |
|
|
106 |
< |
public void testAcquireUninterruptiblyReleaseNInSameThread(){ |
106 |
> |
/** |
107 |
> |
* |
108 |
> |
*/ |
109 |
> |
public void testAcquireUninterruptiblyReleaseNInSameThread() { |
110 |
|
FairSemaphore s = new FairSemaphore(1); |
111 |
|
try { |
112 |
|
s.release(1); |
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{ |
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 |
< |
threadFail("unexpected exception"); |
140 |
> |
} catch(InterruptedException ie){ |
141 |
> |
threadUnexpectedException(); |
142 |
|
} |
143 |
|
} |
144 |
|
}); |
152 |
|
t.join(); |
153 |
|
assertEquals(1, s.availablePermits()); |
154 |
|
} catch( InterruptedException e){ |
155 |
< |
fail("unexpected exception"); |
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{ |
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 |
< |
threadFail("unexpected exception"); |
171 |
> |
} catch(InterruptedException ie){ |
172 |
> |
threadUnexpectedException(); |
173 |
|
} |
174 |
|
} |
175 |
|
}); |
181 |
|
s.release(2); |
182 |
|
t.join(); |
183 |
|
} catch( InterruptedException e){ |
184 |
< |
fail("unexpected exception"); |
184 |
> |
unexpectedException(); |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
< |
public void testTimedAcquireReleaseInSameThread(){ |
188 |
> |
/** |
189 |
> |
* |
190 |
> |
*/ |
191 |
> |
public void testTimedAcquireReleaseInSameThread() { |
192 |
|
FairSemaphore s = new FairSemaphore(1); |
193 |
|
try { |
194 |
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
203 |
|
s.release(); |
204 |
|
assertEquals(1, s.availablePermits()); |
205 |
|
} catch( InterruptedException e){ |
206 |
< |
fail("unexpected exception"); |
206 |
> |
unexpectedException(); |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
< |
public void testTimedAcquireReleaseNInSameThread(){ |
210 |
> |
/** |
211 |
> |
* |
212 |
> |
*/ |
213 |
> |
public void testTimedAcquireReleaseNInSameThread() { |
214 |
|
FairSemaphore s = new FairSemaphore(1); |
215 |
|
try { |
216 |
|
s.release(1); |
225 |
|
assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
226 |
|
assertEquals(1, s.availablePermits()); |
227 |
|
} catch( InterruptedException e){ |
228 |
< |
fail("unexpected exception"); |
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{ |
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 |
< |
threadFail("unexpected exception"); |
246 |
> |
} catch(InterruptedException ie){ |
247 |
> |
threadUnexpectedException(); |
248 |
|
} |
249 |
|
} |
250 |
|
}); |
257 |
|
s.release(); |
258 |
|
t.join(); |
259 |
|
} catch( InterruptedException e){ |
260 |
< |
fail("unexpected exception"); |
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{ |
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 |
< |
threadFail("unexpected exception"); |
276 |
> |
} catch(InterruptedException ie){ |
277 |
> |
threadUnexpectedException(); |
278 |
|
} |
279 |
|
} |
280 |
|
}); |
286 |
|
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
287 |
|
t.join(); |
288 |
|
} catch( InterruptedException e){ |
289 |
< |
fail("unexpected exception"); |
289 |
> |
unexpectedException(); |
290 |
|
} |
291 |
|
} |
292 |
|
|
293 |
< |
public void testAcquire_InterruptedException(){ |
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{ |
298 |
> |
Thread t = new Thread(new Runnable() { |
299 |
> |
public void run() { |
300 |
> |
try { |
301 |
|
s.acquire(); |
302 |
< |
threadFail("should throw"); |
303 |
< |
}catch(InterruptedException success){} |
302 |
> |
threadShouldThrow(); |
303 |
> |
} catch(InterruptedException success){} |
304 |
|
} |
305 |
|
}); |
306 |
|
t.start(); |
307 |
< |
try{ |
307 |
> |
try { |
308 |
|
Thread.sleep(SHORT_DELAY_MS); |
309 |
|
t.interrupt(); |
310 |
|
t.join(); |
311 |
|
} catch(InterruptedException e){ |
312 |
< |
fail("unexpected exception"); |
312 |
> |
unexpectedException(); |
313 |
|
} |
314 |
|
} |
315 |
|
|
316 |
< |
public void testAcquireN_InterruptedException(){ |
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{ |
321 |
> |
Thread t = new Thread(new Runnable() { |
322 |
> |
public void run() { |
323 |
> |
try { |
324 |
|
s.acquire(3); |
325 |
< |
threadFail("should throw"); |
326 |
< |
}catch(InterruptedException success){} |
325 |
> |
threadShouldThrow(); |
326 |
> |
} catch(InterruptedException success){} |
327 |
|
} |
328 |
|
}); |
329 |
|
t.start(); |
330 |
< |
try{ |
330 |
> |
try { |
331 |
|
Thread.sleep(SHORT_DELAY_MS); |
332 |
|
t.interrupt(); |
333 |
|
t.join(); |
334 |
|
} catch(InterruptedException e){ |
335 |
< |
fail("unexpected exception"); |
335 |
> |
unexpectedException(); |
336 |
|
} |
337 |
|
} |
338 |
|
|
339 |
< |
public void testTryAcquire_InterruptedException(){ |
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{ |
344 |
> |
Thread t = new Thread(new Runnable() { |
345 |
> |
public void run() { |
346 |
> |
try { |
347 |
|
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
348 |
< |
threadFail("should throw"); |
349 |
< |
}catch(InterruptedException success){ |
348 |
> |
threadShouldThrow(); |
349 |
> |
} catch(InterruptedException success){ |
350 |
|
} |
351 |
|
} |
352 |
|
}); |
353 |
|
t.start(); |
354 |
< |
try{ |
354 |
> |
try { |
355 |
|
Thread.sleep(SHORT_DELAY_MS); |
356 |
|
t.interrupt(); |
357 |
|
t.join(); |
358 |
|
} catch(InterruptedException e){ |
359 |
< |
fail("unexpected exception"); |
359 |
> |
unexpectedException(); |
360 |
|
} |
361 |
|
} |
362 |
|
|
363 |
< |
public void testTryAcquireN_InterruptedException(){ |
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{ |
368 |
> |
Thread t = new Thread(new Runnable() { |
369 |
> |
public void run() { |
370 |
> |
try { |
371 |
|
s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
372 |
< |
threadFail("should throw"); |
373 |
< |
}catch(InterruptedException success){ |
372 |
> |
threadShouldThrow(); |
373 |
> |
} catch(InterruptedException success){ |
374 |
|
} |
375 |
|
} |
376 |
|
}); |
377 |
|
t.start(); |
378 |
< |
try{ |
378 |
> |
try { |
379 |
|
Thread.sleep(SHORT_DELAY_MS); |
380 |
|
t.interrupt(); |
381 |
|
t.join(); |
382 |
|
} catch(InterruptedException e){ |
383 |
< |
fail("unexpected exception"); |
383 |
> |
unexpectedException(); |
384 |
|
} |
385 |
|
} |
386 |
|
|
387 |
+ |
/** |
388 |
+ |
* |
389 |
+ |
*/ |
390 |
|
public void testSerialization() { |
391 |
|
FairSemaphore l = new FairSemaphore(3); |
392 |
|
|
405 |
|
r.acquire(); |
406 |
|
r.release(); |
407 |
|
} catch(Exception e){ |
408 |
< |
e.printStackTrace(); |
355 |
< |
fail("unexpected exception"); |
408 |
> |
unexpectedException(); |
409 |
|
} |
410 |
|
} |
411 |
|
|