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