10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
public class SemaphoreTest extends TestCase{ |
14 |
< |
|
13 |
> |
public class SemaphoreTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
18 |
– |
|
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(SemaphoreTest.class); |
19 |
|
} |
20 |
|
|
21 |
< |
private static long SHORT_DELAY_MS = 100; |
22 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
23 |
< |
private static long LONG_DELAY_MS = 10000; |
24 |
< |
|
25 |
< |
|
26 |
< |
public void testConstructor1() { |
27 |
< |
Semaphore s = new Semaphore(0); |
28 |
< |
assertEquals(0, s.availablePermits()); |
29 |
< |
} |
30 |
< |
|
33 |
< |
public void testConstructor2() { |
34 |
< |
Semaphore s = new Semaphore(-1); |
35 |
< |
assertEquals(-1, s.availablePermits()); |
21 |
> |
/** |
22 |
> |
* Zero, negative, and positive initial values are allowed in constructor |
23 |
> |
*/ |
24 |
> |
public void testConstructor() { |
25 |
> |
Semaphore s0 = new Semaphore(0); |
26 |
> |
assertEquals(0, s0.availablePermits()); |
27 |
> |
Semaphore s1 = new Semaphore(-1); |
28 |
> |
assertEquals(-1, s1.availablePermits()); |
29 |
> |
Semaphore s2 = new Semaphore(-1); |
30 |
> |
assertEquals(-1, s2.availablePermits()); |
31 |
|
} |
32 |
|
|
33 |
+ |
/** |
34 |
+ |
* tryAcquire succeeds when sufficent permits, else fails |
35 |
+ |
*/ |
36 |
|
public void testTryAcquireInSameThread() { |
37 |
|
Semaphore s = new Semaphore(2); |
38 |
|
assertEquals(2, s.availablePermits()); |
42 |
|
assertFalse(s.tryAcquire()); |
43 |
|
} |
44 |
|
|
45 |
< |
public void testAcquireReleaseInSameThread(){ |
45 |
> |
/** |
46 |
> |
* Acquire and release of semaphore succeed if initially available |
47 |
> |
*/ |
48 |
> |
public void testAcquireReleaseInSameThread() { |
49 |
|
Semaphore s = new Semaphore(1); |
50 |
|
try { |
51 |
|
s.acquire(); |
60 |
|
s.release(); |
61 |
|
assertEquals(1, s.availablePermits()); |
62 |
|
} catch( InterruptedException e){ |
63 |
< |
fail("unexpected exception"); |
63 |
> |
unexpectedException(); |
64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
< |
public void testAcquireUninterruptiblyReleaseInSameThread(){ |
67 |
> |
/** |
68 |
> |
* Uninterruptible acquire and release of semaphore succeed if |
69 |
> |
* initially available |
70 |
> |
*/ |
71 |
> |
public void testAcquireUninterruptiblyReleaseInSameThread() { |
72 |
|
Semaphore s = new Semaphore(1); |
73 |
|
try { |
74 |
|
s.acquireUninterruptibly(); |
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
+ |
/** |
90 |
+ |
* Timed Acquire and release of semaphore succeed if |
91 |
+ |
* initially available |
92 |
+ |
*/ |
93 |
+ |
public void testTimedAcquireReleaseInSameThread() { |
94 |
+ |
Semaphore s = new Semaphore(1); |
95 |
+ |
try { |
96 |
+ |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
97 |
+ |
s.release(); |
98 |
+ |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
99 |
+ |
s.release(); |
100 |
+ |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
101 |
+ |
s.release(); |
102 |
+ |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
103 |
+ |
s.release(); |
104 |
+ |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
105 |
+ |
s.release(); |
106 |
+ |
assertEquals(1, s.availablePermits()); |
107 |
+ |
} catch( InterruptedException e){ |
108 |
+ |
unexpectedException(); |
109 |
+ |
} |
110 |
+ |
} |
111 |
|
|
112 |
+ |
/** |
113 |
+ |
* A release in one thread enables an acquire in another thread |
114 |
+ |
*/ |
115 |
|
public void testAcquireReleaseInDifferentThreads() { |
116 |
< |
final Semaphore s = new Semaphore(1); |
117 |
< |
Thread t = new Thread(new Runnable(){ |
118 |
< |
public void run(){ |
119 |
< |
try{ |
116 |
> |
final Semaphore s = new Semaphore(0); |
117 |
> |
Thread t = new Thread(new Runnable() { |
118 |
> |
public void run() { |
119 |
> |
try { |
120 |
|
s.acquire(); |
121 |
|
s.release(); |
122 |
|
s.release(); |
123 |
|
s.acquire(); |
124 |
< |
}catch(InterruptedException ie){ |
125 |
< |
fail("unexpected exception"); |
124 |
> |
} catch(InterruptedException ie){ |
125 |
> |
threadUnexpectedException(); |
126 |
|
} |
127 |
|
} |
128 |
|
}); |
99 |
– |
t.start(); |
129 |
|
try { |
130 |
+ |
t.start(); |
131 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
132 |
|
s.release(); |
133 |
|
s.release(); |
134 |
|
s.acquire(); |
135 |
|
s.acquire(); |
136 |
+ |
s.release(); |
137 |
|
t.join(); |
138 |
|
} catch( InterruptedException e){ |
139 |
< |
fail("unexpected exception"); |
139 |
> |
unexpectedException(); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
< |
public void testTimedAcquireReleaseInSameThread(){ |
144 |
< |
Semaphore s = new Semaphore(1); |
143 |
> |
/** |
144 |
> |
* A release in one thread enables an uninterruptible acquire in another thread |
145 |
> |
*/ |
146 |
> |
public void testUninterruptibleAcquireReleaseInDifferentThreads() { |
147 |
> |
final Semaphore s = new Semaphore(0); |
148 |
> |
Thread t = new Thread(new Runnable() { |
149 |
> |
public void run() { |
150 |
> |
s.acquireUninterruptibly(); |
151 |
> |
s.release(); |
152 |
> |
s.release(); |
153 |
> |
s.acquireUninterruptibly(); |
154 |
> |
} |
155 |
> |
}); |
156 |
|
try { |
157 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
158 |
< |
s.release(); |
116 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
117 |
< |
s.release(); |
118 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
157 |
> |
t.start(); |
158 |
> |
Thread.sleep(SHORT_DELAY_MS); |
159 |
|
s.release(); |
120 |
– |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
160 |
|
s.release(); |
161 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
161 |
> |
s.acquireUninterruptibly(); |
162 |
> |
s.acquireUninterruptibly(); |
163 |
|
s.release(); |
164 |
< |
assertEquals(1, s.availablePermits()); |
164 |
> |
t.join(); |
165 |
|
} catch( InterruptedException e){ |
166 |
< |
fail("unexpected exception"); |
166 |
> |
unexpectedException(); |
167 |
|
} |
168 |
|
} |
169 |
|
|
170 |
+ |
|
171 |
+ |
/** |
172 |
+ |
* A release in one thread enables a timed acquire in another thread |
173 |
+ |
*/ |
174 |
|
public void testTimedAcquireReleaseInDifferentThreads() { |
175 |
|
final Semaphore s = new Semaphore(1); |
176 |
< |
Thread t = new Thread(new Runnable(){ |
177 |
< |
public void run(){ |
178 |
< |
try{ |
176 |
> |
Thread t = new Thread(new Runnable() { |
177 |
> |
public void run() { |
178 |
> |
try { |
179 |
|
s.release(); |
180 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
180 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
181 |
|
s.release(); |
182 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
182 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
183 |
|
|
184 |
< |
}catch(InterruptedException ie){ |
185 |
< |
fail("unexpected exception"); |
184 |
> |
} catch(InterruptedException ie){ |
185 |
> |
threadUnexpectedException(); |
186 |
|
} |
187 |
|
} |
188 |
|
}); |
145 |
– |
t.start(); |
189 |
|
try { |
190 |
+ |
t.start(); |
191 |
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
192 |
|
s.release(); |
193 |
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
194 |
|
s.release(); |
195 |
+ |
s.release(); |
196 |
|
t.join(); |
197 |
|
} catch( InterruptedException e){ |
198 |
< |
fail("unexpected exception"); |
198 |
> |
unexpectedException(); |
199 |
|
} |
200 |
|
} |
201 |
|
|
202 |
< |
public void testAcquire_InterruptedException(){ |
202 |
> |
/** |
203 |
> |
* A waiting acquire blocks interruptibly |
204 |
> |
*/ |
205 |
> |
public void testAcquire_InterruptedException() { |
206 |
|
final Semaphore s = new Semaphore(0); |
207 |
< |
Thread t = new Thread(new Runnable(){ |
208 |
< |
public void run(){ |
209 |
< |
try{ |
207 |
> |
Thread t = new Thread(new Runnable() { |
208 |
> |
public void run() { |
209 |
> |
try { |
210 |
|
s.acquire(); |
211 |
< |
fail("should throw"); |
212 |
< |
}catch(InterruptedException success){} |
211 |
> |
threadShouldThrow(); |
212 |
> |
} catch(InterruptedException success){} |
213 |
|
} |
214 |
|
}); |
215 |
|
t.start(); |
216 |
< |
try{ |
216 |
> |
try { |
217 |
|
Thread.sleep(SHORT_DELAY_MS); |
218 |
|
t.interrupt(); |
219 |
|
t.join(); |
220 |
|
} catch(InterruptedException e){ |
221 |
< |
fail("unexpected exception"); |
221 |
> |
unexpectedException(); |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
< |
public void testTryAcquire_InterruptedException(){ |
225 |
> |
/** |
226 |
> |
* A waiting timed acquire blocks interruptibly |
227 |
> |
*/ |
228 |
> |
public void testTryAcquire_InterruptedException() { |
229 |
|
final Semaphore s = new Semaphore(0); |
230 |
< |
Thread t = new Thread(new Runnable(){ |
231 |
< |
public void run(){ |
232 |
< |
try{ |
230 |
> |
Thread t = new Thread(new Runnable() { |
231 |
> |
public void run() { |
232 |
> |
try { |
233 |
|
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
234 |
< |
fail("should throw"); |
235 |
< |
}catch(InterruptedException success){ |
234 |
> |
threadShouldThrow(); |
235 |
> |
} catch(InterruptedException success){ |
236 |
|
} |
237 |
|
} |
238 |
|
}); |
239 |
|
t.start(); |
240 |
< |
try{ |
240 |
> |
try { |
241 |
|
Thread.sleep(SHORT_DELAY_MS); |
242 |
|
t.interrupt(); |
243 |
|
t.join(); |
244 |
|
} catch(InterruptedException e){ |
245 |
< |
fail("unexpected exception"); |
245 |
> |
unexpectedException(); |
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
+ |
/** |
250 |
+ |
* a deserialized serialized semaphore has same number of permits |
251 |
+ |
*/ |
252 |
|
public void testSerialization() { |
253 |
|
Semaphore l = new Semaphore(3); |
200 |
– |
|
254 |
|
try { |
255 |
|
l.acquire(); |
256 |
|
l.release(); |
266 |
|
r.acquire(); |
267 |
|
r.release(); |
268 |
|
} catch(Exception e){ |
269 |
< |
e.printStackTrace(); |
217 |
< |
fail("unexpected exception"); |
269 |
> |
unexpectedException(); |
270 |
|
} |
271 |
|
} |
272 |
|
|