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. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
|
15 |
|
public class SemaphoreTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(SemaphoreTest.class); |
20 |
> |
return new TestSuite(SemaphoreTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
/** |
24 |
< |
* |
24 |
> |
* Subclass to expose protected methods |
25 |
|
*/ |
26 |
< |
public void testConstructor1() { |
27 |
< |
Semaphore s = new Semaphore(0); |
28 |
< |
assertEquals(0, s.availablePermits()); |
26 |
> |
static class PublicSemaphore extends Semaphore { |
27 |
> |
PublicSemaphore(int p, boolean f) { super(p, f); } |
28 |
> |
public Collection<Thread> getQueuedThreads() { |
29 |
> |
return super.getQueuedThreads(); |
30 |
> |
} |
31 |
> |
public void reducePermits(int p) { |
32 |
> |
super.reducePermits(p); |
33 |
> |
} |
34 |
> |
} |
35 |
> |
|
36 |
> |
/** |
37 |
> |
* A runnable calling acquire |
38 |
> |
*/ |
39 |
> |
class InterruptibleLockRunnable extends CheckedRunnable { |
40 |
> |
final Semaphore lock; |
41 |
> |
InterruptibleLockRunnable(Semaphore l) { lock = l; } |
42 |
> |
public void realRun() { |
43 |
> |
try { |
44 |
> |
lock.acquire(); |
45 |
> |
} |
46 |
> |
catch (InterruptedException ignored) {} |
47 |
> |
} |
48 |
> |
} |
49 |
> |
|
50 |
> |
|
51 |
> |
/** |
52 |
> |
* A runnable calling acquire that expects to be interrupted |
53 |
> |
*/ |
54 |
> |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
55 |
> |
final Semaphore lock; |
56 |
> |
InterruptedLockRunnable(Semaphore l) { lock = l; } |
57 |
> |
public void realRun() throws InterruptedException { |
58 |
> |
lock.acquire(); |
59 |
> |
} |
60 |
|
} |
61 |
|
|
62 |
|
/** |
63 |
< |
* |
63 |
> |
* Zero, negative, and positive initial values are allowed in constructor |
64 |
> |
*/ |
65 |
> |
public void testConstructor() { |
66 |
> |
for (int permits : new int[] { -1, 0, 1 }) { |
67 |
> |
for (boolean fair : new boolean[] { false, true }) { |
68 |
> |
Semaphore s = new Semaphore(permits, fair); |
69 |
> |
assertEquals(permits, s.availablePermits()); |
70 |
> |
assertEquals(fair, s.isFair()); |
71 |
> |
} |
72 |
> |
} |
73 |
> |
} |
74 |
> |
|
75 |
> |
/** |
76 |
> |
* Constructor without fairness argument behaves as nonfair |
77 |
|
*/ |
78 |
|
public void testConstructor2() { |
79 |
< |
Semaphore s = new Semaphore(-1); |
80 |
< |
assertEquals(-1, s.availablePermits()); |
79 |
> |
for (int permits : new int[] { -1, 0, 1 }) { |
80 |
> |
Semaphore s = new Semaphore(permits); |
81 |
> |
assertEquals(permits, s.availablePermits()); |
82 |
> |
assertFalse(s.isFair()); |
83 |
> |
} |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* |
87 |
> |
* tryAcquire succeeds when sufficient permits, else fails |
88 |
|
*/ |
89 |
|
public void testTryAcquireInSameThread() { |
90 |
< |
Semaphore s = new Semaphore(2); |
90 |
> |
Semaphore s = new Semaphore(2, false); |
91 |
|
assertEquals(2, s.availablePermits()); |
92 |
|
assertTrue(s.tryAcquire()); |
93 |
|
assertTrue(s.tryAcquire()); |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
< |
* |
99 |
> |
* Acquire and release of semaphore succeed if initially available |
100 |
|
*/ |
101 |
< |
public void testAcquireReleaseInSameThread() { |
102 |
< |
Semaphore s = new Semaphore(1); |
103 |
< |
try { |
104 |
< |
s.acquire(); |
105 |
< |
s.release(); |
106 |
< |
s.acquire(); |
107 |
< |
s.release(); |
108 |
< |
s.acquire(); |
109 |
< |
s.release(); |
110 |
< |
s.acquire(); |
111 |
< |
s.release(); |
112 |
< |
s.acquire(); |
113 |
< |
s.release(); |
114 |
< |
assertEquals(1, s.availablePermits()); |
115 |
< |
} catch( InterruptedException e){ |
116 |
< |
unexpectedException(); |
117 |
< |
} |
118 |
< |
} |
119 |
< |
|
120 |
< |
/** |
121 |
< |
* |
122 |
< |
*/ |
123 |
< |
public void testAcquireUninterruptiblyReleaseInSameThread() { |
124 |
< |
Semaphore s = new Semaphore(1); |
125 |
< |
try { |
126 |
< |
s.acquireUninterruptibly(); |
127 |
< |
s.release(); |
128 |
< |
s.acquireUninterruptibly(); |
129 |
< |
s.release(); |
130 |
< |
s.acquireUninterruptibly(); |
131 |
< |
s.release(); |
132 |
< |
s.acquireUninterruptibly(); |
133 |
< |
s.release(); |
134 |
< |
s.acquireUninterruptibly(); |
135 |
< |
s.release(); |
136 |
< |
assertEquals(1, s.availablePermits()); |
137 |
< |
} finally { |
138 |
< |
} |
101 |
> |
public void testAcquireReleaseInSameThread() |
102 |
> |
throws InterruptedException { |
103 |
> |
Semaphore s = new Semaphore(1, false); |
104 |
> |
s.acquire(); |
105 |
> |
s.release(); |
106 |
> |
s.acquire(); |
107 |
> |
s.release(); |
108 |
> |
s.acquire(); |
109 |
> |
s.release(); |
110 |
> |
s.acquire(); |
111 |
> |
s.release(); |
112 |
> |
s.acquire(); |
113 |
> |
s.release(); |
114 |
> |
assertEquals(1, s.availablePermits()); |
115 |
> |
} |
116 |
> |
|
117 |
> |
/** |
118 |
> |
* Uninterruptible acquire and release of semaphore succeed if |
119 |
> |
* initially available |
120 |
> |
*/ |
121 |
> |
public void testAcquireUninterruptiblyReleaseInSameThread() |
122 |
> |
throws InterruptedException { |
123 |
> |
Semaphore s = new Semaphore(1, false); |
124 |
> |
s.acquireUninterruptibly(); |
125 |
> |
s.release(); |
126 |
> |
s.acquireUninterruptibly(); |
127 |
> |
s.release(); |
128 |
> |
s.acquireUninterruptibly(); |
129 |
> |
s.release(); |
130 |
> |
s.acquireUninterruptibly(); |
131 |
> |
s.release(); |
132 |
> |
s.acquireUninterruptibly(); |
133 |
> |
s.release(); |
134 |
> |
assertEquals(1, s.availablePermits()); |
135 |
> |
} |
136 |
> |
|
137 |
> |
/** |
138 |
> |
* Timed Acquire and release of semaphore succeed if |
139 |
> |
* initially available |
140 |
> |
*/ |
141 |
> |
public void testTimedAcquireReleaseInSameThread() |
142 |
> |
throws InterruptedException { |
143 |
> |
Semaphore s = new Semaphore(1, false); |
144 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
145 |
> |
s.release(); |
146 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
147 |
> |
s.release(); |
148 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
149 |
> |
s.release(); |
150 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
151 |
> |
s.release(); |
152 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
153 |
> |
s.release(); |
154 |
> |
assertEquals(1, s.availablePermits()); |
155 |
> |
} |
156 |
> |
|
157 |
> |
/** |
158 |
> |
* A release in one thread enables an acquire in another thread |
159 |
> |
*/ |
160 |
> |
public void testAcquireReleaseInDifferentThreads() |
161 |
> |
throws InterruptedException { |
162 |
> |
final Semaphore s = new Semaphore(0, false); |
163 |
> |
Thread t = new Thread(new CheckedRunnable() { |
164 |
> |
public void realRun() throws InterruptedException { |
165 |
> |
s.acquire(); |
166 |
> |
s.release(); |
167 |
> |
s.release(); |
168 |
> |
s.acquire(); |
169 |
> |
}}); |
170 |
> |
|
171 |
> |
t.start(); |
172 |
> |
Thread.sleep(SHORT_DELAY_MS); |
173 |
> |
s.release(); |
174 |
> |
s.release(); |
175 |
> |
s.acquire(); |
176 |
> |
s.acquire(); |
177 |
> |
s.release(); |
178 |
> |
t.join(); |
179 |
> |
} |
180 |
> |
|
181 |
> |
/** |
182 |
> |
* A release in one thread enables an uninterruptible acquire in another thread |
183 |
> |
*/ |
184 |
> |
public void testUninterruptibleAcquireReleaseInDifferentThreads() |
185 |
> |
throws InterruptedException { |
186 |
> |
final Semaphore s = new Semaphore(0, false); |
187 |
> |
Thread t = new Thread(new CheckedRunnable() { |
188 |
> |
public void realRun() throws InterruptedException { |
189 |
> |
s.acquireUninterruptibly(); |
190 |
> |
s.release(); |
191 |
> |
s.release(); |
192 |
> |
s.acquireUninterruptibly(); |
193 |
> |
}}); |
194 |
> |
|
195 |
> |
t.start(); |
196 |
> |
Thread.sleep(SHORT_DELAY_MS); |
197 |
> |
s.release(); |
198 |
> |
s.release(); |
199 |
> |
s.acquireUninterruptibly(); |
200 |
> |
s.acquireUninterruptibly(); |
201 |
> |
s.release(); |
202 |
> |
t.join(); |
203 |
> |
} |
204 |
> |
|
205 |
> |
|
206 |
> |
/** |
207 |
> |
* A release in one thread enables a timed acquire in another thread |
208 |
> |
*/ |
209 |
> |
public void testTimedAcquireReleaseInDifferentThreads() |
210 |
> |
throws InterruptedException { |
211 |
> |
final Semaphore s = new Semaphore(1, false); |
212 |
> |
Thread t = new Thread(new CheckedRunnable() { |
213 |
> |
public void realRun() throws InterruptedException { |
214 |
> |
s.release(); |
215 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
216 |
> |
s.release(); |
217 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
218 |
> |
}}); |
219 |
> |
|
220 |
> |
t.start(); |
221 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
222 |
> |
s.release(); |
223 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
224 |
> |
s.release(); |
225 |
> |
s.release(); |
226 |
> |
t.join(); |
227 |
> |
} |
228 |
> |
|
229 |
> |
/** |
230 |
> |
* A waiting acquire blocks interruptibly |
231 |
> |
*/ |
232 |
> |
public void testAcquire_InterruptedException() |
233 |
> |
throws InterruptedException { |
234 |
> |
final Semaphore s = new Semaphore(0, false); |
235 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
236 |
> |
public void realRun() throws InterruptedException { |
237 |
> |
s.acquire(); |
238 |
> |
}}); |
239 |
> |
|
240 |
> |
t.start(); |
241 |
> |
Thread.sleep(SHORT_DELAY_MS); |
242 |
> |
t.interrupt(); |
243 |
> |
t.join(); |
244 |
> |
} |
245 |
> |
|
246 |
> |
/** |
247 |
> |
* A waiting timed acquire blocks interruptibly |
248 |
> |
*/ |
249 |
> |
public void testTryAcquire_InterruptedException() |
250 |
> |
throws InterruptedException { |
251 |
> |
final Semaphore s = new Semaphore(0, false); |
252 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
253 |
> |
public void realRun() throws InterruptedException { |
254 |
> |
s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS); |
255 |
> |
}}); |
256 |
> |
|
257 |
> |
t.start(); |
258 |
> |
Thread.sleep(SHORT_DELAY_MS); |
259 |
> |
t.interrupt(); |
260 |
> |
t.join(); |
261 |
> |
} |
262 |
> |
|
263 |
> |
/** |
264 |
> |
* hasQueuedThreads reports whether there are waiting threads |
265 |
> |
*/ |
266 |
> |
public void testHasQueuedThreads() throws InterruptedException { |
267 |
> |
final Semaphore lock = new Semaphore(1, false); |
268 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
269 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
270 |
> |
assertFalse(lock.hasQueuedThreads()); |
271 |
> |
lock.acquireUninterruptibly(); |
272 |
> |
t1.start(); |
273 |
> |
Thread.sleep(SHORT_DELAY_MS); |
274 |
> |
assertTrue(lock.hasQueuedThreads()); |
275 |
> |
t2.start(); |
276 |
> |
Thread.sleep(SHORT_DELAY_MS); |
277 |
> |
assertTrue(lock.hasQueuedThreads()); |
278 |
> |
t1.interrupt(); |
279 |
> |
Thread.sleep(SHORT_DELAY_MS); |
280 |
> |
assertTrue(lock.hasQueuedThreads()); |
281 |
> |
lock.release(); |
282 |
> |
Thread.sleep(SHORT_DELAY_MS); |
283 |
> |
assertFalse(lock.hasQueuedThreads()); |
284 |
> |
t1.join(); |
285 |
> |
t2.join(); |
286 |
> |
} |
287 |
> |
|
288 |
> |
/** |
289 |
> |
* getQueueLength reports number of waiting threads |
290 |
> |
*/ |
291 |
> |
public void testGetQueueLength() throws InterruptedException { |
292 |
> |
final Semaphore lock = new Semaphore(1, false); |
293 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
294 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
295 |
> |
assertEquals(0, lock.getQueueLength()); |
296 |
> |
lock.acquireUninterruptibly(); |
297 |
> |
t1.start(); |
298 |
> |
Thread.sleep(SHORT_DELAY_MS); |
299 |
> |
assertEquals(1, lock.getQueueLength()); |
300 |
> |
t2.start(); |
301 |
> |
Thread.sleep(SHORT_DELAY_MS); |
302 |
> |
assertEquals(2, lock.getQueueLength()); |
303 |
> |
t1.interrupt(); |
304 |
> |
Thread.sleep(SHORT_DELAY_MS); |
305 |
> |
assertEquals(1, lock.getQueueLength()); |
306 |
> |
lock.release(); |
307 |
> |
Thread.sleep(SHORT_DELAY_MS); |
308 |
> |
assertEquals(0, lock.getQueueLength()); |
309 |
> |
t1.join(); |
310 |
> |
t2.join(); |
311 |
> |
} |
312 |
> |
|
313 |
> |
/** |
314 |
> |
* getQueuedThreads includes waiting threads |
315 |
> |
*/ |
316 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
317 |
> |
final PublicSemaphore lock = new PublicSemaphore(1, false); |
318 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
319 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
320 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
321 |
> |
lock.acquireUninterruptibly(); |
322 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
323 |
> |
t1.start(); |
324 |
> |
Thread.sleep(SHORT_DELAY_MS); |
325 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
326 |
> |
t2.start(); |
327 |
> |
Thread.sleep(SHORT_DELAY_MS); |
328 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
329 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
330 |
> |
t1.interrupt(); |
331 |
> |
Thread.sleep(SHORT_DELAY_MS); |
332 |
> |
assertFalse(lock.getQueuedThreads().contains(t1)); |
333 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
334 |
> |
lock.release(); |
335 |
> |
Thread.sleep(SHORT_DELAY_MS); |
336 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
337 |
> |
t1.join(); |
338 |
> |
t2.join(); |
339 |
|
} |
340 |
|
|
341 |
+ |
/** |
342 |
+ |
* drainPermits reports and removes given number of permits |
343 |
+ |
*/ |
344 |
+ |
public void testDrainPermits() { |
345 |
+ |
Semaphore s = new Semaphore(0, false); |
346 |
+ |
assertEquals(0, s.availablePermits()); |
347 |
+ |
assertEquals(0, s.drainPermits()); |
348 |
+ |
s.release(10); |
349 |
+ |
assertEquals(10, s.availablePermits()); |
350 |
+ |
assertEquals(10, s.drainPermits()); |
351 |
+ |
assertEquals(0, s.availablePermits()); |
352 |
+ |
assertEquals(0, s.drainPermits()); |
353 |
+ |
} |
354 |
|
|
355 |
|
/** |
356 |
< |
* |
357 |
< |
*/ |
358 |
< |
public void testAcquireReleaseInDifferentThreads() { |
359 |
< |
final Semaphore s = new Semaphore(1); |
360 |
< |
Thread t = new Thread(new Runnable() { |
361 |
< |
public void run() { |
362 |
< |
try { |
363 |
< |
s.acquire(); |
364 |
< |
s.release(); |
365 |
< |
s.release(); |
366 |
< |
s.acquire(); |
367 |
< |
} catch(InterruptedException ie){ |
368 |
< |
threadUnexpectedException(); |
369 |
< |
} |
370 |
< |
} |
371 |
< |
}); |
372 |
< |
t.start(); |
373 |
< |
try { |
374 |
< |
s.release(); |
375 |
< |
s.release(); |
376 |
< |
s.acquire(); |
377 |
< |
s.acquire(); |
378 |
< |
t.join(); |
379 |
< |
} catch( InterruptedException e){ |
380 |
< |
unexpectedException(); |
381 |
< |
} |
382 |
< |
} |
383 |
< |
|
384 |
< |
/** |
385 |
< |
* |
386 |
< |
*/ |
387 |
< |
public void testTimedAcquireReleaseInSameThread() { |
388 |
< |
Semaphore s = new Semaphore(1); |
389 |
< |
try { |
390 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
391 |
< |
s.release(); |
392 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
393 |
< |
s.release(); |
394 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
395 |
< |
s.release(); |
396 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
397 |
< |
s.release(); |
398 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
399 |
< |
s.release(); |
400 |
< |
assertEquals(1, s.availablePermits()); |
401 |
< |
} catch( InterruptedException e){ |
402 |
< |
unexpectedException(); |
403 |
< |
} |
404 |
< |
} |
405 |
< |
|
406 |
< |
/** |
407 |
< |
* |
408 |
< |
*/ |
409 |
< |
public void testTimedAcquireReleaseInDifferentThreads() { |
410 |
< |
final Semaphore s = new Semaphore(1); |
411 |
< |
Thread t = new Thread(new Runnable() { |
412 |
< |
public void run() { |
413 |
< |
try { |
414 |
< |
s.release(); |
415 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
416 |
< |
s.release(); |
417 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
418 |
< |
|
419 |
< |
} catch(InterruptedException ie){ |
420 |
< |
threadUnexpectedException(); |
421 |
< |
} |
422 |
< |
} |
423 |
< |
}); |
424 |
< |
t.start(); |
425 |
< |
try { |
426 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
427 |
< |
s.release(); |
428 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
429 |
< |
s.release(); |
430 |
< |
t.join(); |
431 |
< |
} catch( InterruptedException e){ |
432 |
< |
unexpectedException(); |
433 |
< |
} |
434 |
< |
} |
435 |
< |
|
436 |
< |
/** |
437 |
< |
* |
438 |
< |
*/ |
439 |
< |
public void testAcquire_InterruptedException() { |
440 |
< |
final Semaphore s = new Semaphore(0); |
441 |
< |
Thread t = new Thread(new Runnable() { |
442 |
< |
public void run() { |
443 |
< |
try { |
444 |
< |
s.acquire(); |
445 |
< |
threadShouldThrow(); |
446 |
< |
} catch(InterruptedException success){} |
447 |
< |
} |
448 |
< |
}); |
449 |
< |
t.start(); |
450 |
< |
try { |
451 |
< |
Thread.sleep(SHORT_DELAY_MS); |
452 |
< |
t.interrupt(); |
453 |
< |
t.join(); |
454 |
< |
} catch(InterruptedException e){ |
455 |
< |
unexpectedException(); |
456 |
< |
} |
457 |
< |
} |
458 |
< |
|
459 |
< |
/** |
460 |
< |
* |
461 |
< |
*/ |
462 |
< |
public void testTryAcquire_InterruptedException() { |
463 |
< |
final Semaphore s = new Semaphore(0); |
464 |
< |
Thread t = new Thread(new Runnable() { |
465 |
< |
public void run() { |
466 |
< |
try { |
467 |
< |
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
468 |
< |
threadShouldThrow(); |
469 |
< |
} catch(InterruptedException success){ |
470 |
< |
} |
471 |
< |
} |
472 |
< |
}); |
473 |
< |
t.start(); |
474 |
< |
try { |
475 |
< |
Thread.sleep(SHORT_DELAY_MS); |
476 |
< |
t.interrupt(); |
477 |
< |
t.join(); |
478 |
< |
} catch(InterruptedException e){ |
479 |
< |
unexpectedException(); |
480 |
< |
} |
481 |
< |
} |
482 |
< |
|
483 |
< |
/** |
484 |
< |
* |
485 |
< |
*/ |
486 |
< |
public void testSerialization() { |
487 |
< |
Semaphore l = new Semaphore(3); |
488 |
< |
try { |
489 |
< |
l.acquire(); |
490 |
< |
l.release(); |
491 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
492 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
493 |
< |
out.writeObject(l); |
494 |
< |
out.close(); |
495 |
< |
|
496 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
497 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
498 |
< |
Semaphore r = (Semaphore) in.readObject(); |
499 |
< |
assertEquals(3, r.availablePermits()); |
500 |
< |
r.acquire(); |
501 |
< |
r.release(); |
502 |
< |
} catch(Exception e){ |
503 |
< |
unexpectedException(); |
504 |
< |
} |
356 |
> |
* reducePermits reduces number of permits |
357 |
> |
*/ |
358 |
> |
public void testReducePermits() { |
359 |
> |
PublicSemaphore s = new PublicSemaphore(10, false); |
360 |
> |
assertEquals(10, s.availablePermits()); |
361 |
> |
s.reducePermits(1); |
362 |
> |
assertEquals(9, s.availablePermits()); |
363 |
> |
s.reducePermits(10); |
364 |
> |
assertEquals(-1, s.availablePermits()); |
365 |
> |
} |
366 |
> |
|
367 |
> |
/** |
368 |
> |
* a deserialized serialized semaphore has same number of permits |
369 |
> |
*/ |
370 |
> |
public void testSerialization() throws Exception { |
371 |
> |
Semaphore l = new Semaphore(3, false); |
372 |
> |
l.acquire(); |
373 |
> |
l.release(); |
374 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
375 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
376 |
> |
out.writeObject(l); |
377 |
> |
out.close(); |
378 |
> |
|
379 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
380 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
381 |
> |
Semaphore r = (Semaphore) in.readObject(); |
382 |
> |
assertEquals(3, r.availablePermits()); |
383 |
> |
assertFalse(r.isFair()); |
384 |
> |
r.acquire(); |
385 |
> |
r.release(); |
386 |
> |
} |
387 |
> |
|
388 |
> |
|
389 |
> |
/** |
390 |
> |
* Zero, negative, and positive initial values are allowed in constructor |
391 |
> |
*/ |
392 |
> |
public void testConstructor_fair() { |
393 |
> |
Semaphore s0 = new Semaphore(0, true); |
394 |
> |
assertEquals(0, s0.availablePermits()); |
395 |
> |
assertTrue(s0.isFair()); |
396 |
> |
Semaphore s1 = new Semaphore(-1, true); |
397 |
> |
assertEquals(-1, s1.availablePermits()); |
398 |
> |
Semaphore s2 = new Semaphore(-1, true); |
399 |
> |
assertEquals(-1, s2.availablePermits()); |
400 |
> |
} |
401 |
> |
|
402 |
> |
/** |
403 |
> |
* tryAcquire succeeds when sufficient permits, else fails |
404 |
> |
*/ |
405 |
> |
public void testTryAcquireInSameThread_fair() { |
406 |
> |
Semaphore s = new Semaphore(2, true); |
407 |
> |
assertEquals(2, s.availablePermits()); |
408 |
> |
assertTrue(s.tryAcquire()); |
409 |
> |
assertTrue(s.tryAcquire()); |
410 |
> |
assertEquals(0, s.availablePermits()); |
411 |
> |
assertFalse(s.tryAcquire()); |
412 |
> |
} |
413 |
> |
|
414 |
> |
/** |
415 |
> |
* tryAcquire(n) succeeds when sufficient permits, else fails |
416 |
> |
*/ |
417 |
> |
public void testTryAcquireNInSameThread_fair() { |
418 |
> |
Semaphore s = new Semaphore(2, true); |
419 |
> |
assertEquals(2, s.availablePermits()); |
420 |
> |
assertTrue(s.tryAcquire(2)); |
421 |
> |
assertEquals(0, s.availablePermits()); |
422 |
> |
assertFalse(s.tryAcquire()); |
423 |
> |
} |
424 |
> |
|
425 |
> |
/** |
426 |
> |
* Acquire and release of semaphore succeed if initially available |
427 |
> |
*/ |
428 |
> |
public void testAcquireReleaseInSameThread_fair() |
429 |
> |
throws InterruptedException { |
430 |
> |
Semaphore s = new Semaphore(1, true); |
431 |
> |
s.acquire(); |
432 |
> |
s.release(); |
433 |
> |
s.acquire(); |
434 |
> |
s.release(); |
435 |
> |
s.acquire(); |
436 |
> |
s.release(); |
437 |
> |
s.acquire(); |
438 |
> |
s.release(); |
439 |
> |
s.acquire(); |
440 |
> |
s.release(); |
441 |
> |
assertEquals(1, s.availablePermits()); |
442 |
> |
} |
443 |
> |
|
444 |
> |
/** |
445 |
> |
* Acquire(n) and release(n) of semaphore succeed if initially available |
446 |
> |
*/ |
447 |
> |
public void testAcquireReleaseNInSameThread_fair() |
448 |
> |
throws InterruptedException { |
449 |
> |
Semaphore s = new Semaphore(1, true); |
450 |
> |
s.release(1); |
451 |
> |
s.acquire(1); |
452 |
> |
s.release(2); |
453 |
> |
s.acquire(2); |
454 |
> |
s.release(3); |
455 |
> |
s.acquire(3); |
456 |
> |
s.release(4); |
457 |
> |
s.acquire(4); |
458 |
> |
s.release(5); |
459 |
> |
s.acquire(5); |
460 |
> |
assertEquals(1, s.availablePermits()); |
461 |
> |
} |
462 |
> |
|
463 |
> |
/** |
464 |
> |
* Acquire(n) and release(n) of semaphore succeed if initially available |
465 |
> |
*/ |
466 |
> |
public void testAcquireUninterruptiblyReleaseNInSameThread_fair() { |
467 |
> |
Semaphore s = new Semaphore(1, true); |
468 |
> |
s.release(1); |
469 |
> |
s.acquireUninterruptibly(1); |
470 |
> |
s.release(2); |
471 |
> |
s.acquireUninterruptibly(2); |
472 |
> |
s.release(3); |
473 |
> |
s.acquireUninterruptibly(3); |
474 |
> |
s.release(4); |
475 |
> |
s.acquireUninterruptibly(4); |
476 |
> |
s.release(5); |
477 |
> |
s.acquireUninterruptibly(5); |
478 |
> |
assertEquals(1, s.availablePermits()); |
479 |
> |
} |
480 |
> |
|
481 |
> |
/** |
482 |
> |
* release(n) in one thread enables timed acquire(n) in another thread |
483 |
> |
*/ |
484 |
> |
public void testTimedAcquireReleaseNInSameThread_fair() |
485 |
> |
throws InterruptedException { |
486 |
> |
Semaphore s = new Semaphore(1, true); |
487 |
> |
s.release(1); |
488 |
> |
assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, MILLISECONDS)); |
489 |
> |
s.release(2); |
490 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
491 |
> |
s.release(3); |
492 |
> |
assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, MILLISECONDS)); |
493 |
> |
s.release(4); |
494 |
> |
assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, MILLISECONDS)); |
495 |
> |
s.release(5); |
496 |
> |
assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, MILLISECONDS)); |
497 |
> |
assertEquals(1, s.availablePermits()); |
498 |
> |
} |
499 |
> |
|
500 |
> |
/** |
501 |
> |
* release in one thread enables timed acquire in another thread |
502 |
> |
*/ |
503 |
> |
public void testTimedAcquireReleaseInSameThread_fair() |
504 |
> |
throws InterruptedException { |
505 |
> |
Semaphore s = new Semaphore(1, true); |
506 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
507 |
> |
s.release(); |
508 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
509 |
> |
s.release(); |
510 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
511 |
> |
s.release(); |
512 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
513 |
> |
s.release(); |
514 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
515 |
> |
s.release(); |
516 |
> |
assertEquals(1, s.availablePermits()); |
517 |
> |
} |
518 |
> |
|
519 |
> |
/** |
520 |
> |
* A release in one thread enables an acquire in another thread |
521 |
> |
*/ |
522 |
> |
public void testAcquireReleaseInDifferentThreads_fair() |
523 |
> |
throws InterruptedException { |
524 |
> |
final Semaphore s = new Semaphore(0, true); |
525 |
> |
Thread t = new Thread(new CheckedRunnable() { |
526 |
> |
public void realRun() throws InterruptedException { |
527 |
> |
s.acquire(); |
528 |
> |
s.acquire(); |
529 |
> |
s.acquire(); |
530 |
> |
s.acquire(); |
531 |
> |
}}); |
532 |
> |
|
533 |
> |
t.start(); |
534 |
> |
Thread.sleep(SHORT_DELAY_MS); |
535 |
> |
s.release(); |
536 |
> |
s.release(); |
537 |
> |
s.release(); |
538 |
> |
s.release(); |
539 |
> |
s.release(); |
540 |
> |
s.release(); |
541 |
> |
t.join(); |
542 |
> |
assertEquals(2, s.availablePermits()); |
543 |
> |
} |
544 |
> |
|
545 |
> |
/** |
546 |
> |
* release(n) in one thread enables acquire(n) in another thread |
547 |
> |
*/ |
548 |
> |
public void testAcquireReleaseNInDifferentThreads_fair() |
549 |
> |
throws InterruptedException { |
550 |
> |
final Semaphore s = new Semaphore(0, true); |
551 |
> |
Thread t = new Thread(new CheckedRunnable() { |
552 |
> |
public void realRun() throws InterruptedException { |
553 |
> |
s.acquire(); |
554 |
> |
s.release(2); |
555 |
> |
s.acquire(); |
556 |
> |
}}); |
557 |
> |
|
558 |
> |
t.start(); |
559 |
> |
Thread.sleep(SHORT_DELAY_MS); |
560 |
> |
s.release(2); |
561 |
> |
s.acquire(2); |
562 |
> |
s.release(1); |
563 |
> |
t.join(); |
564 |
> |
} |
565 |
> |
|
566 |
> |
/** |
567 |
> |
* release(n) in one thread enables acquire(n) in another thread |
568 |
> |
*/ |
569 |
> |
public void testAcquireReleaseNInDifferentThreads_fair2() |
570 |
> |
throws InterruptedException { |
571 |
> |
final Semaphore s = new Semaphore(0, true); |
572 |
> |
Thread t = new Thread(new CheckedRunnable() { |
573 |
> |
public void realRun() throws InterruptedException { |
574 |
> |
s.acquire(2); |
575 |
> |
s.acquire(2); |
576 |
> |
s.release(4); |
577 |
> |
}}); |
578 |
> |
|
579 |
> |
t.start(); |
580 |
> |
Thread.sleep(SHORT_DELAY_MS); |
581 |
> |
s.release(6); |
582 |
> |
s.acquire(2); |
583 |
> |
s.acquire(2); |
584 |
> |
s.release(2); |
585 |
> |
t.join(); |
586 |
> |
} |
587 |
> |
|
588 |
> |
|
589 |
> |
/** |
590 |
> |
* release in one thread enables timed acquire in another thread |
591 |
> |
*/ |
592 |
> |
public void testTimedAcquireReleaseInDifferentThreads_fair() |
593 |
> |
throws InterruptedException { |
594 |
> |
final Semaphore s = new Semaphore(1, true); |
595 |
> |
Thread t = new Thread(new CheckedRunnable() { |
596 |
> |
public void realRun() throws InterruptedException { |
597 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
598 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
599 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
600 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
601 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
602 |
> |
}}); |
603 |
> |
|
604 |
> |
t.start(); |
605 |
> |
s.release(); |
606 |
> |
s.release(); |
607 |
> |
s.release(); |
608 |
> |
s.release(); |
609 |
> |
s.release(); |
610 |
> |
t.join(); |
611 |
> |
} |
612 |
> |
|
613 |
> |
/** |
614 |
> |
* release(n) in one thread enables timed acquire(n) in another thread |
615 |
> |
*/ |
616 |
> |
public void testTimedAcquireReleaseNInDifferentThreads_fair() |
617 |
> |
throws InterruptedException { |
618 |
> |
final Semaphore s = new Semaphore(2, true); |
619 |
> |
Thread t = new Thread(new CheckedRunnable() { |
620 |
> |
public void realRun() throws InterruptedException { |
621 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
622 |
> |
s.release(2); |
623 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
624 |
> |
s.release(2); |
625 |
> |
}}); |
626 |
> |
|
627 |
> |
t.start(); |
628 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
629 |
> |
s.release(2); |
630 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
631 |
> |
s.release(2); |
632 |
> |
t.join(); |
633 |
> |
} |
634 |
> |
|
635 |
> |
/** |
636 |
> |
* A waiting acquire blocks interruptibly |
637 |
> |
*/ |
638 |
> |
public void testAcquire_InterruptedException_fair() |
639 |
> |
throws InterruptedException { |
640 |
> |
final Semaphore s = new Semaphore(0, true); |
641 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
642 |
> |
public void realRun() throws InterruptedException { |
643 |
> |
s.acquire(); |
644 |
> |
}}); |
645 |
> |
|
646 |
> |
t.start(); |
647 |
> |
Thread.sleep(SHORT_DELAY_MS); |
648 |
> |
t.interrupt(); |
649 |
> |
t.join(); |
650 |
> |
} |
651 |
> |
|
652 |
> |
/** |
653 |
> |
* A waiting acquire(n) blocks interruptibly |
654 |
> |
*/ |
655 |
> |
public void testAcquireN_InterruptedException_fair() |
656 |
> |
throws InterruptedException { |
657 |
> |
final Semaphore s = new Semaphore(2, true); |
658 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
659 |
> |
public void realRun() throws InterruptedException { |
660 |
> |
s.acquire(3); |
661 |
> |
}}); |
662 |
> |
|
663 |
> |
t.start(); |
664 |
> |
Thread.sleep(SHORT_DELAY_MS); |
665 |
> |
t.interrupt(); |
666 |
> |
t.join(); |
667 |
> |
} |
668 |
> |
|
669 |
> |
/** |
670 |
> |
* A waiting tryAcquire blocks interruptibly |
671 |
> |
*/ |
672 |
> |
public void testTryAcquire_InterruptedException_fair() |
673 |
> |
throws InterruptedException { |
674 |
> |
final Semaphore s = new Semaphore(0, true); |
675 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
676 |
> |
public void realRun() throws InterruptedException { |
677 |
> |
s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS); |
678 |
> |
}}); |
679 |
> |
|
680 |
> |
t.start(); |
681 |
> |
Thread.sleep(SHORT_DELAY_MS); |
682 |
> |
t.interrupt(); |
683 |
> |
t.join(); |
684 |
> |
} |
685 |
> |
|
686 |
> |
/** |
687 |
> |
* A waiting tryAcquire(n) blocks interruptibly |
688 |
> |
*/ |
689 |
> |
public void testTryAcquireN_InterruptedException_fair() |
690 |
> |
throws InterruptedException { |
691 |
> |
final Semaphore s = new Semaphore(1, true); |
692 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
693 |
> |
public void realRun() throws InterruptedException { |
694 |
> |
s.tryAcquire(4, MEDIUM_DELAY_MS, MILLISECONDS); |
695 |
> |
}}); |
696 |
> |
|
697 |
> |
t.start(); |
698 |
> |
Thread.sleep(SHORT_DELAY_MS); |
699 |
> |
t.interrupt(); |
700 |
> |
t.join(); |
701 |
> |
} |
702 |
> |
|
703 |
> |
/** |
704 |
> |
* getQueueLength reports number of waiting threads |
705 |
> |
*/ |
706 |
> |
public void testGetQueueLength_fair() throws InterruptedException { |
707 |
> |
final Semaphore lock = new Semaphore(1, true); |
708 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
709 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
710 |
> |
assertEquals(0, lock.getQueueLength()); |
711 |
> |
lock.acquireUninterruptibly(); |
712 |
> |
t1.start(); |
713 |
> |
Thread.sleep(SHORT_DELAY_MS); |
714 |
> |
assertEquals(1, lock.getQueueLength()); |
715 |
> |
t2.start(); |
716 |
> |
Thread.sleep(SHORT_DELAY_MS); |
717 |
> |
assertEquals(2, lock.getQueueLength()); |
718 |
> |
t1.interrupt(); |
719 |
> |
Thread.sleep(SHORT_DELAY_MS); |
720 |
> |
assertEquals(1, lock.getQueueLength()); |
721 |
> |
lock.release(); |
722 |
> |
Thread.sleep(SHORT_DELAY_MS); |
723 |
> |
assertEquals(0, lock.getQueueLength()); |
724 |
> |
t1.join(); |
725 |
> |
t2.join(); |
726 |
> |
} |
727 |
> |
|
728 |
> |
|
729 |
> |
/** |
730 |
> |
* a deserialized serialized semaphore has same number of permits |
731 |
> |
*/ |
732 |
> |
public void testSerialization_fair() throws Exception { |
733 |
> |
Semaphore l = new Semaphore(3, true); |
734 |
> |
|
735 |
> |
l.acquire(); |
736 |
> |
l.release(); |
737 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
738 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
739 |
> |
out.writeObject(l); |
740 |
> |
out.close(); |
741 |
> |
|
742 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
743 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
744 |
> |
Semaphore r = (Semaphore) in.readObject(); |
745 |
> |
assertEquals(3, r.availablePermits()); |
746 |
> |
assertTrue(r.isFair()); |
747 |
> |
r.acquire(); |
748 |
> |
r.release(); |
749 |
> |
} |
750 |
> |
|
751 |
> |
/** |
752 |
> |
* toString indicates current number of permits |
753 |
> |
*/ |
754 |
> |
public void testToString() { |
755 |
> |
Semaphore s = new Semaphore(0); |
756 |
> |
String us = s.toString(); |
757 |
> |
assertTrue(us.indexOf("Permits = 0") >= 0); |
758 |
> |
s.release(); |
759 |
> |
String s1 = s.toString(); |
760 |
> |
assertTrue(s1.indexOf("Permits = 1") >= 0); |
761 |
> |
s.release(); |
762 |
> |
String s2 = s.toString(); |
763 |
> |
assertTrue(s2.indexOf("Permits = 2") >= 0); |
764 |
|
} |
765 |
|
|
766 |
|
} |