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 |
|
|
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 |
|
} |