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 |
|
12 |
public class SemaphoreTest extends TestCase{ |
13 |
|
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run (suite()); |
16 |
} |
17 |
|
18 |
public static Test suite() { |
19 |
return new TestSuite(SemaphoreTest.class); |
20 |
} |
21 |
|
22 |
private static long SHORT_DELAY_MS = 100; |
23 |
private static long MEDIUM_DELAY_MS = 1000; |
24 |
private static long LONG_DELAY_MS = 10000; |
25 |
|
26 |
|
27 |
public void testConstructor1() { |
28 |
Semaphore s = new Semaphore(0); |
29 |
assertEquals(0, s.availablePermits()); |
30 |
} |
31 |
|
32 |
public void testConstructor2() { |
33 |
Semaphore s = new Semaphore(-1); |
34 |
assertEquals(-1, s.availablePermits()); |
35 |
} |
36 |
|
37 |
public void testTryAcquireInSameThread() { |
38 |
Semaphore s = new Semaphore(2); |
39 |
assertEquals(2, s.availablePermits()); |
40 |
assertTrue(s.tryAcquire()); |
41 |
assertTrue(s.tryAcquire()); |
42 |
assertEquals(0, s.availablePermits()); |
43 |
assertFalse(s.tryAcquire()); |
44 |
} |
45 |
|
46 |
public void testAcquireReleaseInSameThread(){ |
47 |
Semaphore s = new Semaphore(1); |
48 |
try { |
49 |
s.acquire(); |
50 |
s.release(); |
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 |
assertEquals(1, s.availablePermits()); |
60 |
} catch( InterruptedException e){ |
61 |
fail("unexpected exception"); |
62 |
} |
63 |
} |
64 |
|
65 |
public void testAcquireUninterruptiblyReleaseInSameThread(){ |
66 |
Semaphore s = new Semaphore(1); |
67 |
try { |
68 |
s.acquireUninterruptibly(); |
69 |
s.release(); |
70 |
s.acquireUninterruptibly(); |
71 |
s.release(); |
72 |
s.acquireUninterruptibly(); |
73 |
s.release(); |
74 |
s.acquireUninterruptibly(); |
75 |
s.release(); |
76 |
s.acquireUninterruptibly(); |
77 |
s.release(); |
78 |
assertEquals(1, s.availablePermits()); |
79 |
} finally { |
80 |
} |
81 |
} |
82 |
|
83 |
|
84 |
public void testAcquireReleaseInDifferentThreads() { |
85 |
final Semaphore s = new Semaphore(1); |
86 |
Thread t = new Thread(new Runnable(){ |
87 |
public void run(){ |
88 |
try{ |
89 |
s.acquire(); |
90 |
s.release(); |
91 |
s.release(); |
92 |
s.acquire(); |
93 |
}catch(InterruptedException ie){ |
94 |
fail("unexpected exception"); |
95 |
} |
96 |
} |
97 |
}); |
98 |
t.start(); |
99 |
try { |
100 |
s.release(); |
101 |
s.release(); |
102 |
s.acquire(); |
103 |
s.acquire(); |
104 |
t.join(); |
105 |
} catch( InterruptedException e){ |
106 |
fail("unexpected exception"); |
107 |
} |
108 |
} |
109 |
|
110 |
public void testTimedAcquireReleaseInSameThread(){ |
111 |
Semaphore s = new Semaphore(1); |
112 |
try { |
113 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
114 |
s.release(); |
115 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
116 |
s.release(); |
117 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
118 |
s.release(); |
119 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
120 |
s.release(); |
121 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
122 |
s.release(); |
123 |
assertEquals(1, s.availablePermits()); |
124 |
} catch( InterruptedException e){ |
125 |
fail("unexpected exception"); |
126 |
} |
127 |
} |
128 |
|
129 |
public void testTimedAcquireReleaseInDifferentThreads() { |
130 |
final Semaphore s = new Semaphore(1); |
131 |
Thread t = new Thread(new Runnable(){ |
132 |
public void run(){ |
133 |
try{ |
134 |
s.release(); |
135 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
136 |
s.release(); |
137 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
138 |
|
139 |
}catch(InterruptedException ie){ |
140 |
fail("unexpected exception"); |
141 |
} |
142 |
} |
143 |
}); |
144 |
t.start(); |
145 |
try { |
146 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
147 |
s.release(); |
148 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
149 |
s.release(); |
150 |
t.join(); |
151 |
} catch( InterruptedException e){ |
152 |
fail("unexpected exception"); |
153 |
} |
154 |
} |
155 |
|
156 |
public void testAcquire_InterruptedException(){ |
157 |
final Semaphore s = new Semaphore(0); |
158 |
Thread t = new Thread(new Runnable(){ |
159 |
public void run(){ |
160 |
try{ |
161 |
s.acquire(); |
162 |
fail("should throw"); |
163 |
}catch(InterruptedException success){} |
164 |
} |
165 |
}); |
166 |
t.start(); |
167 |
try{ |
168 |
Thread.sleep(SHORT_DELAY_MS); |
169 |
t.interrupt(); |
170 |
t.join(); |
171 |
} catch(InterruptedException e){ |
172 |
fail("unexpected exception"); |
173 |
} |
174 |
} |
175 |
|
176 |
public void testTryAcquire_InterruptedException(){ |
177 |
final Semaphore s = new Semaphore(0); |
178 |
Thread t = new Thread(new Runnable(){ |
179 |
public void run(){ |
180 |
try{ |
181 |
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
182 |
fail("should throw"); |
183 |
}catch(InterruptedException success){ |
184 |
} |
185 |
} |
186 |
}); |
187 |
t.start(); |
188 |
try{ |
189 |
Thread.sleep(SHORT_DELAY_MS); |
190 |
t.interrupt(); |
191 |
t.join(); |
192 |
} catch(InterruptedException e){ |
193 |
fail("unexpected exception"); |
194 |
} |
195 |
} |
196 |
} |