1 |
dl |
1.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 |
dl |
1.2 |
import java.io.*; |
12 |
dl |
1.1 |
|
13 |
dl |
1.3 |
public class SemaphoreTest extends JSR166TestCase { |
14 |
dl |
1.1 |
public static void main(String[] args) { |
15 |
|
|
junit.textui.TestRunner.run (suite()); |
16 |
|
|
} |
17 |
|
|
public static Test suite() { |
18 |
|
|
return new TestSuite(SemaphoreTest.class); |
19 |
|
|
} |
20 |
|
|
|
21 |
dl |
1.4 |
/** |
22 |
dl |
1.5 |
* Zero, negative, and positive initial values are allowed in constructor |
23 |
dl |
1.4 |
*/ |
24 |
dl |
1.5 |
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 |
dl |
1.1 |
} |
32 |
|
|
|
33 |
dl |
1.4 |
/** |
34 |
dl |
1.5 |
* tryAcquire succeeds when sufficent permits, else fails |
35 |
dl |
1.4 |
*/ |
36 |
dl |
1.1 |
public void testTryAcquireInSameThread() { |
37 |
|
|
Semaphore s = new Semaphore(2); |
38 |
|
|
assertEquals(2, s.availablePermits()); |
39 |
|
|
assertTrue(s.tryAcquire()); |
40 |
|
|
assertTrue(s.tryAcquire()); |
41 |
|
|
assertEquals(0, s.availablePermits()); |
42 |
|
|
assertFalse(s.tryAcquire()); |
43 |
|
|
} |
44 |
|
|
|
45 |
dl |
1.4 |
/** |
46 |
dl |
1.5 |
* Acquire and release of semaphore succeed if initially available |
47 |
dl |
1.4 |
*/ |
48 |
|
|
public void testAcquireReleaseInSameThread() { |
49 |
dl |
1.1 |
Semaphore s = new Semaphore(1); |
50 |
|
|
try { |
51 |
|
|
s.acquire(); |
52 |
|
|
s.release(); |
53 |
|
|
s.acquire(); |
54 |
|
|
s.release(); |
55 |
|
|
s.acquire(); |
56 |
|
|
s.release(); |
57 |
|
|
s.acquire(); |
58 |
|
|
s.release(); |
59 |
|
|
s.acquire(); |
60 |
|
|
s.release(); |
61 |
|
|
assertEquals(1, s.availablePermits()); |
62 |
|
|
} catch( InterruptedException e){ |
63 |
dl |
1.4 |
unexpectedException(); |
64 |
dl |
1.1 |
} |
65 |
|
|
} |
66 |
|
|
|
67 |
dl |
1.4 |
/** |
68 |
dl |
1.5 |
* Uninterruptible acquire and release of semaphore succeed if |
69 |
|
|
* initially available |
70 |
dl |
1.4 |
*/ |
71 |
|
|
public void testAcquireUninterruptiblyReleaseInSameThread() { |
72 |
dl |
1.1 |
Semaphore s = new Semaphore(1); |
73 |
|
|
try { |
74 |
|
|
s.acquireUninterruptibly(); |
75 |
|
|
s.release(); |
76 |
|
|
s.acquireUninterruptibly(); |
77 |
|
|
s.release(); |
78 |
|
|
s.acquireUninterruptibly(); |
79 |
|
|
s.release(); |
80 |
|
|
s.acquireUninterruptibly(); |
81 |
|
|
s.release(); |
82 |
|
|
s.acquireUninterruptibly(); |
83 |
|
|
s.release(); |
84 |
|
|
assertEquals(1, s.availablePermits()); |
85 |
|
|
} finally { |
86 |
|
|
} |
87 |
|
|
} |
88 |
|
|
|
89 |
dl |
1.5 |
/** |
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 |
dl |
1.1 |
|
112 |
dl |
1.4 |
/** |
113 |
dl |
1.5 |
* A release in one thread enables an acquire in another thread |
114 |
dl |
1.4 |
*/ |
115 |
dl |
1.1 |
public void testAcquireReleaseInDifferentThreads() { |
116 |
dl |
1.5 |
final Semaphore s = new Semaphore(0); |
117 |
dl |
1.4 |
Thread t = new Thread(new Runnable() { |
118 |
|
|
public void run() { |
119 |
|
|
try { |
120 |
dl |
1.1 |
s.acquire(); |
121 |
|
|
s.release(); |
122 |
|
|
s.release(); |
123 |
|
|
s.acquire(); |
124 |
dl |
1.4 |
} catch(InterruptedException ie){ |
125 |
|
|
threadUnexpectedException(); |
126 |
dl |
1.1 |
} |
127 |
|
|
} |
128 |
|
|
}); |
129 |
|
|
try { |
130 |
dl |
1.5 |
t.start(); |
131 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
132 |
dl |
1.1 |
s.release(); |
133 |
|
|
s.release(); |
134 |
|
|
s.acquire(); |
135 |
|
|
s.acquire(); |
136 |
dl |
1.5 |
s.release(); |
137 |
dl |
1.1 |
t.join(); |
138 |
|
|
} catch( InterruptedException e){ |
139 |
dl |
1.4 |
unexpectedException(); |
140 |
dl |
1.1 |
} |
141 |
|
|
} |
142 |
|
|
|
143 |
dl |
1.4 |
/** |
144 |
dl |
1.5 |
* A release in one thread enables an uninterruptible acquire in another thread |
145 |
dl |
1.4 |
*/ |
146 |
dl |
1.5 |
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 |
dl |
1.1 |
try { |
157 |
dl |
1.5 |
t.start(); |
158 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
159 |
dl |
1.1 |
s.release(); |
160 |
|
|
s.release(); |
161 |
dl |
1.5 |
s.acquireUninterruptibly(); |
162 |
|
|
s.acquireUninterruptibly(); |
163 |
dl |
1.1 |
s.release(); |
164 |
dl |
1.5 |
t.join(); |
165 |
dl |
1.1 |
} catch( InterruptedException e){ |
166 |
dl |
1.4 |
unexpectedException(); |
167 |
dl |
1.1 |
} |
168 |
|
|
} |
169 |
|
|
|
170 |
dl |
1.5 |
|
171 |
dl |
1.4 |
/** |
172 |
dl |
1.5 |
* A release in one thread enables a timed acquire in another thread |
173 |
dl |
1.4 |
*/ |
174 |
dl |
1.1 |
public void testTimedAcquireReleaseInDifferentThreads() { |
175 |
|
|
final Semaphore s = new Semaphore(1); |
176 |
dl |
1.4 |
Thread t = new Thread(new Runnable() { |
177 |
|
|
public void run() { |
178 |
|
|
try { |
179 |
dl |
1.1 |
s.release(); |
180 |
dl |
1.3 |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
181 |
dl |
1.1 |
s.release(); |
182 |
dl |
1.3 |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
183 |
dl |
1.1 |
|
184 |
dl |
1.4 |
} catch(InterruptedException ie){ |
185 |
|
|
threadUnexpectedException(); |
186 |
dl |
1.1 |
} |
187 |
|
|
} |
188 |
|
|
}); |
189 |
|
|
try { |
190 |
dl |
1.5 |
t.start(); |
191 |
dl |
1.1 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
192 |
|
|
s.release(); |
193 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
194 |
|
|
s.release(); |
195 |
dl |
1.5 |
s.release(); |
196 |
dl |
1.1 |
t.join(); |
197 |
|
|
} catch( InterruptedException e){ |
198 |
dl |
1.4 |
unexpectedException(); |
199 |
dl |
1.1 |
} |
200 |
|
|
} |
201 |
|
|
|
202 |
dl |
1.4 |
/** |
203 |
dl |
1.5 |
* A waiting acquire blocks interruptibly |
204 |
dl |
1.4 |
*/ |
205 |
|
|
public void testAcquire_InterruptedException() { |
206 |
dl |
1.1 |
final Semaphore s = new Semaphore(0); |
207 |
dl |
1.4 |
Thread t = new Thread(new Runnable() { |
208 |
|
|
public void run() { |
209 |
|
|
try { |
210 |
dl |
1.1 |
s.acquire(); |
211 |
dl |
1.4 |
threadShouldThrow(); |
212 |
|
|
} catch(InterruptedException success){} |
213 |
dl |
1.1 |
} |
214 |
|
|
}); |
215 |
|
|
t.start(); |
216 |
dl |
1.4 |
try { |
217 |
dl |
1.1 |
Thread.sleep(SHORT_DELAY_MS); |
218 |
|
|
t.interrupt(); |
219 |
|
|
t.join(); |
220 |
|
|
} catch(InterruptedException e){ |
221 |
dl |
1.4 |
unexpectedException(); |
222 |
dl |
1.1 |
} |
223 |
|
|
} |
224 |
|
|
|
225 |
dl |
1.4 |
/** |
226 |
dl |
1.5 |
* A waiting timed acquire blocks interruptibly |
227 |
dl |
1.4 |
*/ |
228 |
|
|
public void testTryAcquire_InterruptedException() { |
229 |
dl |
1.1 |
final Semaphore s = new Semaphore(0); |
230 |
dl |
1.4 |
Thread t = new Thread(new Runnable() { |
231 |
|
|
public void run() { |
232 |
|
|
try { |
233 |
dl |
1.1 |
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
234 |
dl |
1.4 |
threadShouldThrow(); |
235 |
|
|
} catch(InterruptedException success){ |
236 |
dl |
1.1 |
} |
237 |
|
|
} |
238 |
|
|
}); |
239 |
|
|
t.start(); |
240 |
dl |
1.4 |
try { |
241 |
dl |
1.1 |
Thread.sleep(SHORT_DELAY_MS); |
242 |
|
|
t.interrupt(); |
243 |
|
|
t.join(); |
244 |
|
|
} catch(InterruptedException e){ |
245 |
dl |
1.4 |
unexpectedException(); |
246 |
dl |
1.1 |
} |
247 |
|
|
} |
248 |
dl |
1.2 |
|
249 |
dl |
1.4 |
/** |
250 |
dl |
1.5 |
* a deserialized serialized semaphore has same number of permits |
251 |
dl |
1.4 |
*/ |
252 |
dl |
1.2 |
public void testSerialization() { |
253 |
|
|
Semaphore l = new Semaphore(3); |
254 |
|
|
try { |
255 |
|
|
l.acquire(); |
256 |
|
|
l.release(); |
257 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
258 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
259 |
|
|
out.writeObject(l); |
260 |
|
|
out.close(); |
261 |
|
|
|
262 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
263 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
264 |
|
|
Semaphore r = (Semaphore) in.readObject(); |
265 |
|
|
assertEquals(3, r.availablePermits()); |
266 |
|
|
r.acquire(); |
267 |
|
|
r.release(); |
268 |
|
|
} catch(Exception e){ |
269 |
dl |
1.4 |
unexpectedException(); |
270 |
dl |
1.2 |
} |
271 |
|
|
} |
272 |
|
|
|
273 |
dl |
1.1 |
} |