ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SemaphoreTest.java
Revision: 1.5
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.4: +62 -34 lines
Log Message:
improve tck javadocs; rename and add a few tests

File Contents

# Content
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 import java.io.*;
12
13 public class SemaphoreTest extends JSR166TestCase {
14 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 /**
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());
39 assertTrue(s.tryAcquire());
40 assertTrue(s.tryAcquire());
41 assertEquals(0, s.availablePermits());
42 assertFalse(s.tryAcquire());
43 }
44
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();
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 unexpectedException();
64 }
65 }
66
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();
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 /**
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(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 threadUnexpectedException();
126 }
127 }
128 });
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 unexpectedException();
140 }
141 }
142
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 t.start();
158 Thread.sleep(SHORT_DELAY_MS);
159 s.release();
160 s.release();
161 s.acquireUninterruptibly();
162 s.acquireUninterruptibly();
163 s.release();
164 t.join();
165 } catch( InterruptedException e){
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 {
179 s.release();
180 threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
181 s.release();
182 threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
183
184 } catch(InterruptedException ie){
185 threadUnexpectedException();
186 }
187 }
188 });
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 unexpectedException();
199 }
200 }
201
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 {
210 s.acquire();
211 threadShouldThrow();
212 } catch(InterruptedException success){}
213 }
214 });
215 t.start();
216 try {
217 Thread.sleep(SHORT_DELAY_MS);
218 t.interrupt();
219 t.join();
220 } catch(InterruptedException e){
221 unexpectedException();
222 }
223 }
224
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 {
233 s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
234 threadShouldThrow();
235 } catch(InterruptedException success){
236 }
237 }
238 });
239 t.start();
240 try {
241 Thread.sleep(SHORT_DELAY_MS);
242 t.interrupt();
243 t.join();
244 } catch(InterruptedException e){
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);
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 unexpectedException();
270 }
271 }
272
273 }