ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SemaphoreTest.java
Revision: 1.4
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.3: +67 -37 lines
Log Message:
Documentation scaffolding

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 *
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());
43 assertTrue(s.tryAcquire());
44 assertTrue(s.tryAcquire());
45 assertEquals(0, s.availablePermits());
46 assertFalse(s.tryAcquire());
47 }
48
49 /**
50 *
51 */
52 public void testAcquireReleaseInSameThread() {
53 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 unexpectedException();
68 }
69 }
70
71 /**
72 *
73 */
74 public void testAcquireUninterruptiblyReleaseInSameThread() {
75 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 /**
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 {
101 s.acquire();
102 s.release();
103 s.release();
104 s.acquire();
105 } catch(InterruptedException ie){
106 threadUnexpectedException();
107 }
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 unexpectedException();
119 }
120 }
121
122 /**
123 *
124 */
125 public void testTimedAcquireReleaseInSameThread() {
126 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 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 {
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 threadUnexpectedException();
159 }
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 unexpectedException();
171 }
172 }
173
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 {
182 s.acquire();
183 threadShouldThrow();
184 } catch(InterruptedException success){}
185 }
186 });
187 t.start();
188 try {
189 Thread.sleep(SHORT_DELAY_MS);
190 t.interrupt();
191 t.join();
192 } catch(InterruptedException e){
193 unexpectedException();
194 }
195 }
196
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 {
205 s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
206 threadShouldThrow();
207 } catch(InterruptedException success){
208 }
209 }
210 });
211 t.start();
212 try {
213 Thread.sleep(SHORT_DELAY_MS);
214 t.interrupt();
215 t.join();
216 } catch(InterruptedException e){
217 unexpectedException();
218 }
219 }
220
221 /**
222 *
223 */
224 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 unexpectedException();
242 }
243 }
244
245 }