10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
public class FairSemaphoreTest extends TestCase{ |
13 |
> |
public class FairSemaphoreTest extends JSR166TestCase{ |
14 |
|
|
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (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 |
– |
|
23 |
|
public void testConstructor1() { |
24 |
|
FairSemaphore s = new FairSemaphore(0); |
25 |
|
assertEquals(0, s.availablePermits()); |
114 |
|
s.acquire(); |
115 |
|
s.acquire(); |
116 |
|
}catch(InterruptedException ie){ |
117 |
< |
fail("unexpected exception"); |
117 |
> |
threadFail("unexpected exception"); |
118 |
|
} |
119 |
|
} |
120 |
|
}); |
142 |
|
s.acquire(2); |
143 |
|
s.acquire(2); |
144 |
|
}catch(InterruptedException ie){ |
145 |
< |
fail("unexpected exception"); |
145 |
> |
threadFail("unexpected exception"); |
146 |
|
} |
147 |
|
} |
148 |
|
}); |
201 |
|
Thread t = new Thread(new Runnable(){ |
202 |
|
public void run(){ |
203 |
|
try{ |
204 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
205 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
206 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
207 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
208 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
204 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
205 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
206 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
207 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
208 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
209 |
|
|
210 |
|
}catch(InterruptedException ie){ |
211 |
< |
fail("unexpected exception"); |
211 |
> |
threadFail("unexpected exception"); |
212 |
|
} |
213 |
|
} |
214 |
|
}); |
230 |
|
Thread t = new Thread(new Runnable(){ |
231 |
|
public void run(){ |
232 |
|
try{ |
233 |
< |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
233 |
> |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
234 |
|
s.release(2); |
235 |
< |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
235 |
> |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
236 |
|
s.release(2); |
237 |
|
}catch(InterruptedException ie){ |
238 |
< |
fail("unexpected exception"); |
238 |
> |
threadFail("unexpected exception"); |
239 |
|
} |
240 |
|
} |
241 |
|
}); |
257 |
|
public void run(){ |
258 |
|
try{ |
259 |
|
s.acquire(); |
260 |
< |
fail("should throw"); |
260 |
> |
threadFail("should throw"); |
261 |
|
}catch(InterruptedException success){} |
262 |
|
} |
263 |
|
}); |
277 |
|
public void run(){ |
278 |
|
try{ |
279 |
|
s.acquire(3); |
280 |
< |
fail("should throw"); |
280 |
> |
threadFail("should throw"); |
281 |
|
}catch(InterruptedException success){} |
282 |
|
} |
283 |
|
}); |
297 |
|
public void run(){ |
298 |
|
try{ |
299 |
|
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
300 |
< |
fail("should throw"); |
300 |
> |
threadFail("should throw"); |
301 |
|
}catch(InterruptedException success){ |
302 |
|
} |
303 |
|
} |
318 |
|
public void run(){ |
319 |
|
try{ |
320 |
|
s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
321 |
< |
fail("should throw"); |
321 |
> |
threadFail("should throw"); |
322 |
|
}catch(InterruptedException success){ |
323 |
|
} |
324 |
|
} |