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