ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SemaphoreTest.java
Revision: 1.2
Committed: Sun Sep 7 20:39:11 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.1: +25 -0 lines
Log Message:
Added serialization and lock 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 TestCase{
14
15 public static void main(String[] args) {
16 junit.textui.TestRunner.run (suite());
17 }
18
19 public static Test suite() {
20 return new TestSuite(SemaphoreTest.class);
21 }
22
23 private static long SHORT_DELAY_MS = 100;
24 private static long MEDIUM_DELAY_MS = 1000;
25 private static long LONG_DELAY_MS = 10000;
26
27
28 public void testConstructor1() {
29 Semaphore s = new Semaphore(0);
30 assertEquals(0, s.availablePermits());
31 }
32
33 public void testConstructor2() {
34 Semaphore s = new Semaphore(-1);
35 assertEquals(-1, s.availablePermits());
36 }
37
38 public void testTryAcquireInSameThread() {
39 Semaphore s = new Semaphore(2);
40 assertEquals(2, s.availablePermits());
41 assertTrue(s.tryAcquire());
42 assertTrue(s.tryAcquire());
43 assertEquals(0, s.availablePermits());
44 assertFalse(s.tryAcquire());
45 }
46
47 public void testAcquireReleaseInSameThread(){
48 Semaphore s = new Semaphore(1);
49 try {
50 s.acquire();
51 s.release();
52 s.acquire();
53 s.release();
54 s.acquire();
55 s.release();
56 s.acquire();
57 s.release();
58 s.acquire();
59 s.release();
60 assertEquals(1, s.availablePermits());
61 } catch( InterruptedException e){
62 fail("unexpected exception");
63 }
64 }
65
66 public void testAcquireUninterruptiblyReleaseInSameThread(){
67 Semaphore s = new Semaphore(1);
68 try {
69 s.acquireUninterruptibly();
70 s.release();
71 s.acquireUninterruptibly();
72 s.release();
73 s.acquireUninterruptibly();
74 s.release();
75 s.acquireUninterruptibly();
76 s.release();
77 s.acquireUninterruptibly();
78 s.release();
79 assertEquals(1, s.availablePermits());
80 } finally {
81 }
82 }
83
84
85 public void testAcquireReleaseInDifferentThreads() {
86 final Semaphore s = new Semaphore(1);
87 Thread t = new Thread(new Runnable(){
88 public void run(){
89 try{
90 s.acquire();
91 s.release();
92 s.release();
93 s.acquire();
94 }catch(InterruptedException ie){
95 fail("unexpected exception");
96 }
97 }
98 });
99 t.start();
100 try {
101 s.release();
102 s.release();
103 s.acquire();
104 s.acquire();
105 t.join();
106 } catch( InterruptedException e){
107 fail("unexpected exception");
108 }
109 }
110
111 public void testTimedAcquireReleaseInSameThread(){
112 Semaphore s = new Semaphore(1);
113 try {
114 assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
115 s.release();
116 assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
117 s.release();
118 assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
119 s.release();
120 assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
121 s.release();
122 assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
123 s.release();
124 assertEquals(1, s.availablePermits());
125 } catch( InterruptedException e){
126 fail("unexpected exception");
127 }
128 }
129
130 public void testTimedAcquireReleaseInDifferentThreads() {
131 final Semaphore s = new Semaphore(1);
132 Thread t = new Thread(new Runnable(){
133 public void run(){
134 try{
135 s.release();
136 assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
137 s.release();
138 assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
139
140 }catch(InterruptedException ie){
141 fail("unexpected exception");
142 }
143 }
144 });
145 t.start();
146 try {
147 assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
148 s.release();
149 assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
150 s.release();
151 t.join();
152 } catch( InterruptedException e){
153 fail("unexpected exception");
154 }
155 }
156
157 public void testAcquire_InterruptedException(){
158 final Semaphore s = new Semaphore(0);
159 Thread t = new Thread(new Runnable(){
160 public void run(){
161 try{
162 s.acquire();
163 fail("should throw");
164 }catch(InterruptedException success){}
165 }
166 });
167 t.start();
168 try{
169 Thread.sleep(SHORT_DELAY_MS);
170 t.interrupt();
171 t.join();
172 } catch(InterruptedException e){
173 fail("unexpected exception");
174 }
175 }
176
177 public void testTryAcquire_InterruptedException(){
178 final Semaphore s = new Semaphore(0);
179 Thread t = new Thread(new Runnable(){
180 public void run(){
181 try{
182 s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
183 fail("should throw");
184 }catch(InterruptedException success){
185 }
186 }
187 });
188 t.start();
189 try{
190 Thread.sleep(SHORT_DELAY_MS);
191 t.interrupt();
192 t.join();
193 } catch(InterruptedException e){
194 fail("unexpected exception");
195 }
196 }
197
198 public void testSerialization() {
199 Semaphore l = new Semaphore(3);
200
201 try {
202 l.acquire();
203 l.release();
204 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
205 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
206 out.writeObject(l);
207 out.close();
208
209 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
210 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
211 Semaphore r = (Semaphore) in.readObject();
212 assertEquals(3, r.availablePermits());
213 r.acquire();
214 r.release();
215 } catch(Exception e){
216 e.printStackTrace();
217 fail("unexpected exception");
218 }
219 }
220
221 }