11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class SemaphoreTest extends JSR166TestCase { |
14 |
– |
|
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 |
+ |
/** |
22 |
+ |
* |
23 |
+ |
*/ |
24 |
|
public void testConstructor1() { |
25 |
|
Semaphore s = new Semaphore(0); |
26 |
|
assertEquals(0, s.availablePermits()); |
27 |
|
} |
28 |
|
|
29 |
+ |
/** |
30 |
+ |
* |
31 |
+ |
*/ |
32 |
|
public void testConstructor2() { |
33 |
|
Semaphore s = new Semaphore(-1); |
34 |
|
assertEquals(-1, s.availablePermits()); |
35 |
|
} |
36 |
|
|
37 |
+ |
/** |
38 |
+ |
* |
39 |
+ |
*/ |
40 |
|
public void testTryAcquireInSameThread() { |
41 |
|
Semaphore s = new Semaphore(2); |
42 |
|
assertEquals(2, s.availablePermits()); |
46 |
|
assertFalse(s.tryAcquire()); |
47 |
|
} |
48 |
|
|
49 |
< |
public void testAcquireReleaseInSameThread(){ |
49 |
> |
/** |
50 |
> |
* |
51 |
> |
*/ |
52 |
> |
public void testAcquireReleaseInSameThread() { |
53 |
|
Semaphore s = new Semaphore(1); |
54 |
|
try { |
55 |
|
s.acquire(); |
64 |
|
s.release(); |
65 |
|
assertEquals(1, s.availablePermits()); |
66 |
|
} catch( InterruptedException e){ |
67 |
< |
fail("unexpected exception"); |
67 |
> |
unexpectedException(); |
68 |
|
} |
69 |
|
} |
70 |
|
|
71 |
< |
public void testAcquireUninterruptiblyReleaseInSameThread(){ |
71 |
> |
/** |
72 |
> |
* |
73 |
> |
*/ |
74 |
> |
public void testAcquireUninterruptiblyReleaseInSameThread() { |
75 |
|
Semaphore s = new Semaphore(1); |
76 |
|
try { |
77 |
|
s.acquireUninterruptibly(); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
+ |
/** |
94 |
+ |
* |
95 |
+ |
*/ |
96 |
|
public void testAcquireReleaseInDifferentThreads() { |
97 |
|
final Semaphore s = new Semaphore(1); |
98 |
< |
Thread t = new Thread(new Runnable(){ |
99 |
< |
public void run(){ |
100 |
< |
try{ |
98 |
> |
Thread t = new Thread(new Runnable() { |
99 |
> |
public void run() { |
100 |
> |
try { |
101 |
|
s.acquire(); |
102 |
|
s.release(); |
103 |
|
s.release(); |
104 |
|
s.acquire(); |
105 |
< |
}catch(InterruptedException ie){ |
106 |
< |
threadFail("unexpected exception"); |
105 |
> |
} catch(InterruptedException ie){ |
106 |
> |
threadUnexpectedException(); |
107 |
|
} |
108 |
|
} |
109 |
|
}); |
115 |
|
s.acquire(); |
116 |
|
t.join(); |
117 |
|
} catch( InterruptedException e){ |
118 |
< |
fail("unexpected exception"); |
118 |
> |
unexpectedException(); |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
< |
public void testTimedAcquireReleaseInSameThread(){ |
122 |
> |
/** |
123 |
> |
* |
124 |
> |
*/ |
125 |
> |
public void testTimedAcquireReleaseInSameThread() { |
126 |
|
Semaphore s = new Semaphore(1); |
127 |
|
try { |
128 |
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
137 |
|
s.release(); |
138 |
|
assertEquals(1, s.availablePermits()); |
139 |
|
} catch( InterruptedException e){ |
140 |
< |
fail("unexpected exception"); |
140 |
> |
unexpectedException(); |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
+ |
/** |
145 |
+ |
* |
146 |
+ |
*/ |
147 |
|
public void testTimedAcquireReleaseInDifferentThreads() { |
148 |
|
final Semaphore s = new Semaphore(1); |
149 |
< |
Thread t = new Thread(new Runnable(){ |
150 |
< |
public void run(){ |
151 |
< |
try{ |
149 |
> |
Thread t = new Thread(new Runnable() { |
150 |
> |
public void run() { |
151 |
> |
try { |
152 |
|
s.release(); |
153 |
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
154 |
|
s.release(); |
155 |
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
156 |
|
|
157 |
< |
}catch(InterruptedException ie){ |
158 |
< |
threadFail("unexpected exception"); |
157 |
> |
} catch(InterruptedException ie){ |
158 |
> |
threadUnexpectedException(); |
159 |
|
} |
160 |
|
} |
161 |
|
}); |
167 |
|
s.release(); |
168 |
|
t.join(); |
169 |
|
} catch( InterruptedException e){ |
170 |
< |
fail("unexpected exception"); |
170 |
> |
unexpectedException(); |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
< |
public void testAcquire_InterruptedException(){ |
174 |
> |
/** |
175 |
> |
* |
176 |
> |
*/ |
177 |
> |
public void testAcquire_InterruptedException() { |
178 |
|
final Semaphore s = new Semaphore(0); |
179 |
< |
Thread t = new Thread(new Runnable(){ |
180 |
< |
public void run(){ |
181 |
< |
try{ |
179 |
> |
Thread t = new Thread(new Runnable() { |
180 |
> |
public void run() { |
181 |
> |
try { |
182 |
|
s.acquire(); |
183 |
< |
threadFail("should throw"); |
184 |
< |
}catch(InterruptedException success){} |
183 |
> |
threadShouldThrow(); |
184 |
> |
} catch(InterruptedException success){} |
185 |
|
} |
186 |
|
}); |
187 |
|
t.start(); |
188 |
< |
try{ |
188 |
> |
try { |
189 |
|
Thread.sleep(SHORT_DELAY_MS); |
190 |
|
t.interrupt(); |
191 |
|
t.join(); |
192 |
|
} catch(InterruptedException e){ |
193 |
< |
fail("unexpected exception"); |
193 |
> |
unexpectedException(); |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
< |
public void testTryAcquire_InterruptedException(){ |
197 |
> |
/** |
198 |
> |
* |
199 |
> |
*/ |
200 |
> |
public void testTryAcquire_InterruptedException() { |
201 |
|
final Semaphore s = new Semaphore(0); |
202 |
< |
Thread t = new Thread(new Runnable(){ |
203 |
< |
public void run(){ |
204 |
< |
try{ |
202 |
> |
Thread t = new Thread(new Runnable() { |
203 |
> |
public void run() { |
204 |
> |
try { |
205 |
|
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
206 |
< |
threadFail("should throw"); |
207 |
< |
}catch(InterruptedException success){ |
206 |
> |
threadShouldThrow(); |
207 |
> |
} catch(InterruptedException success){ |
208 |
|
} |
209 |
|
} |
210 |
|
}); |
211 |
|
t.start(); |
212 |
< |
try{ |
212 |
> |
try { |
213 |
|
Thread.sleep(SHORT_DELAY_MS); |
214 |
|
t.interrupt(); |
215 |
|
t.join(); |
216 |
|
} catch(InterruptedException e){ |
217 |
< |
fail("unexpected exception"); |
217 |
> |
unexpectedException(); |
218 |
|
} |
219 |
|
} |
220 |
|
|
221 |
+ |
/** |
222 |
+ |
* |
223 |
+ |
*/ |
224 |
|
public void testSerialization() { |
225 |
|
Semaphore l = new Semaphore(3); |
226 |
|
try { |
238 |
|
r.acquire(); |
239 |
|
r.release(); |
240 |
|
} catch(Exception e){ |
241 |
< |
e.printStackTrace(); |
211 |
< |
fail("unexpected exception"); |
241 |
> |
unexpectedException(); |
242 |
|
} |
243 |
|
} |
244 |
|
|