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 |
|
|
* |
23 |
|
|
*/ |
24 |
dl |
1.1 |
public void testConstructor1() { |
25 |
|
|
Semaphore s = new Semaphore(0); |
26 |
|
|
assertEquals(0, s.availablePermits()); |
27 |
|
|
} |
28 |
|
|
|
29 |
dl |
1.4 |
/** |
30 |
|
|
* |
31 |
|
|
*/ |
32 |
dl |
1.1 |
public void testConstructor2() { |
33 |
|
|
Semaphore s = new Semaphore(-1); |
34 |
|
|
assertEquals(-1, s.availablePermits()); |
35 |
|
|
} |
36 |
|
|
|
37 |
dl |
1.4 |
/** |
38 |
|
|
* |
39 |
|
|
*/ |
40 |
dl |
1.1 |
public void testTryAcquireInSameThread() { |
41 |
|
|
Semaphore s = new Semaphore(2); |
42 |
|
|
assertEquals(2, s.availablePermits()); |
43 |
|
|
assertTrue(s.tryAcquire()); |
44 |
|
|
assertTrue(s.tryAcquire()); |
45 |
|
|
assertEquals(0, s.availablePermits()); |
46 |
|
|
assertFalse(s.tryAcquire()); |
47 |
|
|
} |
48 |
|
|
|
49 |
dl |
1.4 |
/** |
50 |
|
|
* |
51 |
|
|
*/ |
52 |
|
|
public void testAcquireReleaseInSameThread() { |
53 |
dl |
1.1 |
Semaphore s = new Semaphore(1); |
54 |
|
|
try { |
55 |
|
|
s.acquire(); |
56 |
|
|
s.release(); |
57 |
|
|
s.acquire(); |
58 |
|
|
s.release(); |
59 |
|
|
s.acquire(); |
60 |
|
|
s.release(); |
61 |
|
|
s.acquire(); |
62 |
|
|
s.release(); |
63 |
|
|
s.acquire(); |
64 |
|
|
s.release(); |
65 |
|
|
assertEquals(1, s.availablePermits()); |
66 |
|
|
} catch( InterruptedException e){ |
67 |
dl |
1.4 |
unexpectedException(); |
68 |
dl |
1.1 |
} |
69 |
|
|
} |
70 |
|
|
|
71 |
dl |
1.4 |
/** |
72 |
|
|
* |
73 |
|
|
*/ |
74 |
|
|
public void testAcquireUninterruptiblyReleaseInSameThread() { |
75 |
dl |
1.1 |
Semaphore s = new Semaphore(1); |
76 |
|
|
try { |
77 |
|
|
s.acquireUninterruptibly(); |
78 |
|
|
s.release(); |
79 |
|
|
s.acquireUninterruptibly(); |
80 |
|
|
s.release(); |
81 |
|
|
s.acquireUninterruptibly(); |
82 |
|
|
s.release(); |
83 |
|
|
s.acquireUninterruptibly(); |
84 |
|
|
s.release(); |
85 |
|
|
s.acquireUninterruptibly(); |
86 |
|
|
s.release(); |
87 |
|
|
assertEquals(1, s.availablePermits()); |
88 |
|
|
} finally { |
89 |
|
|
} |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
|
93 |
dl |
1.4 |
/** |
94 |
|
|
* |
95 |
|
|
*/ |
96 |
dl |
1.1 |
public void testAcquireReleaseInDifferentThreads() { |
97 |
|
|
final Semaphore s = new Semaphore(1); |
98 |
dl |
1.4 |
Thread t = new Thread(new Runnable() { |
99 |
|
|
public void run() { |
100 |
|
|
try { |
101 |
dl |
1.1 |
s.acquire(); |
102 |
|
|
s.release(); |
103 |
|
|
s.release(); |
104 |
|
|
s.acquire(); |
105 |
dl |
1.4 |
} catch(InterruptedException ie){ |
106 |
|
|
threadUnexpectedException(); |
107 |
dl |
1.1 |
} |
108 |
|
|
} |
109 |
|
|
}); |
110 |
|
|
t.start(); |
111 |
|
|
try { |
112 |
|
|
s.release(); |
113 |
|
|
s.release(); |
114 |
|
|
s.acquire(); |
115 |
|
|
s.acquire(); |
116 |
|
|
t.join(); |
117 |
|
|
} catch( InterruptedException e){ |
118 |
dl |
1.4 |
unexpectedException(); |
119 |
dl |
1.1 |
} |
120 |
|
|
} |
121 |
|
|
|
122 |
dl |
1.4 |
/** |
123 |
|
|
* |
124 |
|
|
*/ |
125 |
|
|
public void testTimedAcquireReleaseInSameThread() { |
126 |
dl |
1.1 |
Semaphore s = new Semaphore(1); |
127 |
|
|
try { |
128 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
129 |
|
|
s.release(); |
130 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
131 |
|
|
s.release(); |
132 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
133 |
|
|
s.release(); |
134 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
135 |
|
|
s.release(); |
136 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
137 |
|
|
s.release(); |
138 |
|
|
assertEquals(1, s.availablePermits()); |
139 |
|
|
} catch( InterruptedException e){ |
140 |
dl |
1.4 |
unexpectedException(); |
141 |
dl |
1.1 |
} |
142 |
|
|
} |
143 |
|
|
|
144 |
dl |
1.4 |
/** |
145 |
|
|
* |
146 |
|
|
*/ |
147 |
dl |
1.1 |
public void testTimedAcquireReleaseInDifferentThreads() { |
148 |
|
|
final Semaphore s = new Semaphore(1); |
149 |
dl |
1.4 |
Thread t = new Thread(new Runnable() { |
150 |
|
|
public void run() { |
151 |
|
|
try { |
152 |
dl |
1.1 |
s.release(); |
153 |
dl |
1.3 |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
154 |
dl |
1.1 |
s.release(); |
155 |
dl |
1.3 |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
156 |
dl |
1.1 |
|
157 |
dl |
1.4 |
} catch(InterruptedException ie){ |
158 |
|
|
threadUnexpectedException(); |
159 |
dl |
1.1 |
} |
160 |
|
|
} |
161 |
|
|
}); |
162 |
|
|
t.start(); |
163 |
|
|
try { |
164 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
165 |
|
|
s.release(); |
166 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
167 |
|
|
s.release(); |
168 |
|
|
t.join(); |
169 |
|
|
} catch( InterruptedException e){ |
170 |
dl |
1.4 |
unexpectedException(); |
171 |
dl |
1.1 |
} |
172 |
|
|
} |
173 |
|
|
|
174 |
dl |
1.4 |
/** |
175 |
|
|
* |
176 |
|
|
*/ |
177 |
|
|
public void testAcquire_InterruptedException() { |
178 |
dl |
1.1 |
final Semaphore s = new Semaphore(0); |
179 |
dl |
1.4 |
Thread t = new Thread(new Runnable() { |
180 |
|
|
public void run() { |
181 |
|
|
try { |
182 |
dl |
1.1 |
s.acquire(); |
183 |
dl |
1.4 |
threadShouldThrow(); |
184 |
|
|
} catch(InterruptedException success){} |
185 |
dl |
1.1 |
} |
186 |
|
|
}); |
187 |
|
|
t.start(); |
188 |
dl |
1.4 |
try { |
189 |
dl |
1.1 |
Thread.sleep(SHORT_DELAY_MS); |
190 |
|
|
t.interrupt(); |
191 |
|
|
t.join(); |
192 |
|
|
} catch(InterruptedException e){ |
193 |
dl |
1.4 |
unexpectedException(); |
194 |
dl |
1.1 |
} |
195 |
|
|
} |
196 |
|
|
|
197 |
dl |
1.4 |
/** |
198 |
|
|
* |
199 |
|
|
*/ |
200 |
|
|
public void testTryAcquire_InterruptedException() { |
201 |
dl |
1.1 |
final Semaphore s = new Semaphore(0); |
202 |
dl |
1.4 |
Thread t = new Thread(new Runnable() { |
203 |
|
|
public void run() { |
204 |
|
|
try { |
205 |
dl |
1.1 |
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
206 |
dl |
1.4 |
threadShouldThrow(); |
207 |
|
|
} catch(InterruptedException success){ |
208 |
dl |
1.1 |
} |
209 |
|
|
} |
210 |
|
|
}); |
211 |
|
|
t.start(); |
212 |
dl |
1.4 |
try { |
213 |
dl |
1.1 |
Thread.sleep(SHORT_DELAY_MS); |
214 |
|
|
t.interrupt(); |
215 |
|
|
t.join(); |
216 |
|
|
} catch(InterruptedException e){ |
217 |
dl |
1.4 |
unexpectedException(); |
218 |
dl |
1.1 |
} |
219 |
|
|
} |
220 |
dl |
1.2 |
|
221 |
dl |
1.4 |
/** |
222 |
|
|
* |
223 |
|
|
*/ |
224 |
dl |
1.2 |
public void testSerialization() { |
225 |
|
|
Semaphore l = new Semaphore(3); |
226 |
|
|
try { |
227 |
|
|
l.acquire(); |
228 |
|
|
l.release(); |
229 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
230 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
231 |
|
|
out.writeObject(l); |
232 |
|
|
out.close(); |
233 |
|
|
|
234 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
235 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
236 |
|
|
Semaphore r = (Semaphore) in.readObject(); |
237 |
|
|
assertEquals(3, r.availablePermits()); |
238 |
|
|
r.acquire(); |
239 |
|
|
r.release(); |
240 |
|
|
} catch(Exception e){ |
241 |
dl |
1.4 |
unexpectedException(); |
242 |
dl |
1.2 |
} |
243 |
|
|
} |
244 |
|
|
|
245 |
dl |
1.1 |
} |