ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
(Generate patch)

Comparing jsr166/src/test/tck/TimeUnitTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.23 by jsr166, Tue Sep 24 16:48:52 2013 UTC

# Line 1 | Line 1
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.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
8
9   import junit.framework.*;
10 < import java.util.concurrent.*;
11 < import java.io.*;
12 <
13 < public class TimeUnitTest extends TestCase {
14 <      static public boolean DEBUG = false;
15 <
10 > import java.util.concurrent.CountDownLatch;
11 > import java.util.concurrent.TimeUnit;
12 > import static java.util.concurrent.TimeUnit.*;
13  
14 + public class TimeUnitTest extends JSR166TestCase {
15      public static void main(String[] args) {
16 <        junit.textui.TestRunner.run(suite());  
16 >        junit.textui.TestRunner.run(suite());
17      }
18 <    
18 >
19      public static Test suite() {
20 <        return new TestSuite(TimeUnitTest.class);
20 >        return new TestSuite(TimeUnitTest.class);
21      }
22  
23 +    // (loops to 88888 check increments at all time divisions.)
24 +
25 +    /**
26 +     * convert correctly converts sample values across the units
27 +     */
28      public void testConvert() {
29 <        for (long t = 0; t < 10; ++t) {
30 <            assertEquals(t,
31 <                         TimeUnit.SECONDS.convert(t,
32 <                                                  TimeUnit.SECONDS));
33 <            assertEquals(t,
34 <                         TimeUnit.SECONDS.convert(1000 * t,
35 <                                                  TimeUnit.MILLISECONDS));
36 <            assertEquals(t,
37 <                         TimeUnit.SECONDS.convert(1000000 * t,
38 <                                                  TimeUnit.MICROSECONDS));
39 <            assertEquals(t,
40 <                         TimeUnit.SECONDS.convert(1000000000 * t,
41 <                                                  TimeUnit.NANOSECONDS));
42 <            assertEquals(1000 * t,
43 <                         TimeUnit.MILLISECONDS.convert(t,
44 <                                                  TimeUnit.SECONDS));
45 <            assertEquals(t,
46 <                         TimeUnit.MILLISECONDS.convert(t,
47 <                                                  TimeUnit.MILLISECONDS));
48 <            assertEquals(t,
49 <                         TimeUnit.MILLISECONDS.convert(1000 * t,
50 <                                                  TimeUnit.MICROSECONDS));
51 <            assertEquals(t,
52 <                         TimeUnit.MILLISECONDS.convert(1000000 * t,
53 <                                                  TimeUnit.NANOSECONDS));
54 <            assertEquals(1000000 * t,
55 <                         TimeUnit.MICROSECONDS.convert(t,
56 <                                                  TimeUnit.SECONDS));
57 <            assertEquals(1000 * t,
58 <                         TimeUnit.MICROSECONDS.convert(t,
59 <                                                  TimeUnit.MILLISECONDS));
60 <            assertEquals(t,
61 <                         TimeUnit.MICROSECONDS.convert(t,
62 <                                                  TimeUnit.MICROSECONDS));
63 <            assertEquals(t,
64 <                         TimeUnit.MICROSECONDS.convert(1000 * t,
65 <                                                  TimeUnit.NANOSECONDS));
66 <            assertEquals(1000000000 * t,
67 <                         TimeUnit.NANOSECONDS.convert(t,
68 <                                                  TimeUnit.SECONDS));
69 <            assertEquals(1000000 * t,
70 <                         TimeUnit.NANOSECONDS.convert(t,
71 <                                                  TimeUnit.MILLISECONDS));
72 <            assertEquals(1000 * t,
73 <                         TimeUnit.NANOSECONDS.convert(t,
74 <                                                  TimeUnit.MICROSECONDS));
75 <            assertEquals(t,
76 <                         TimeUnit.NANOSECONDS.convert(t,
77 <                                                  TimeUnit.NANOSECONDS));
29 >        for (long t = 0; t < 88888; ++t) {
30 >            assertEquals(t*60*60*24,
31 >                         SECONDS.convert(t, DAYS));
32 >            assertEquals(t*60*60,
33 >                         SECONDS.convert(t, HOURS));
34 >            assertEquals(t*60,
35 >                         SECONDS.convert(t, MINUTES));
36 >            assertEquals(t,
37 >                         SECONDS.convert(t, SECONDS));
38 >            assertEquals(t,
39 >                         SECONDS.convert(1000L*t, MILLISECONDS));
40 >            assertEquals(t,
41 >                         SECONDS.convert(1000000L*t, MICROSECONDS));
42 >            assertEquals(t,
43 >                         SECONDS.convert(1000000000L*t, NANOSECONDS));
44 >
45 >            assertEquals(1000L*t*60*60*24,
46 >                         MILLISECONDS.convert(t, DAYS));
47 >            assertEquals(1000L*t*60*60,
48 >                         MILLISECONDS.convert(t, HOURS));
49 >            assertEquals(1000L*t*60,
50 >                         MILLISECONDS.convert(t, MINUTES));
51 >            assertEquals(1000L*t,
52 >                         MILLISECONDS.convert(t, SECONDS));
53 >            assertEquals(t,
54 >                         MILLISECONDS.convert(t, MILLISECONDS));
55 >            assertEquals(t,
56 >                         MILLISECONDS.convert(1000L*t, MICROSECONDS));
57 >            assertEquals(t,
58 >                         MILLISECONDS.convert(1000000L*t, NANOSECONDS));
59 >
60 >            assertEquals(1000000L*t*60*60*24,
61 >                         MICROSECONDS.convert(t, DAYS));
62 >            assertEquals(1000000L*t*60*60,
63 >                         MICROSECONDS.convert(t, HOURS));
64 >            assertEquals(1000000L*t*60,
65 >                         MICROSECONDS.convert(t, MINUTES));
66 >            assertEquals(1000000L*t,
67 >                         MICROSECONDS.convert(t, SECONDS));
68 >            assertEquals(1000L*t,
69 >                         MICROSECONDS.convert(t, MILLISECONDS));
70 >            assertEquals(t,
71 >                         MICROSECONDS.convert(t, MICROSECONDS));
72 >            assertEquals(t,
73 >                         MICROSECONDS.convert(1000L*t, NANOSECONDS));
74 >
75 >            assertEquals(1000000000L*t*60*60*24,
76 >                         NANOSECONDS.convert(t, DAYS));
77 >            assertEquals(1000000000L*t*60*60,
78 >                         NANOSECONDS.convert(t, HOURS));
79 >            assertEquals(1000000000L*t*60,
80 >                         NANOSECONDS.convert(t, MINUTES));
81 >            assertEquals(1000000000L*t,
82 >                         NANOSECONDS.convert(t, SECONDS));
83 >            assertEquals(1000000L*t,
84 >                         NANOSECONDS.convert(t, MILLISECONDS));
85 >            assertEquals(1000L*t,
86 >                         NANOSECONDS.convert(t, MICROSECONDS));
87 >            assertEquals(t,
88 >                         NANOSECONDS.convert(t, NANOSECONDS));
89          }
90      }
91  
92 +    /**
93 +     * toNanos correctly converts sample values in different units to
94 +     * nanoseconds
95 +     */
96      public void testToNanos() {
97 <        for (long t = 0; t < 10; ++t) {
98 <            assertEquals(1000000000 * t,
99 <                         TimeUnit.SECONDS.toNanos(t));
100 <
101 <            assertEquals(1000000 * t,
102 <                         TimeUnit.MILLISECONDS.toNanos(t));
103 <            assertEquals(1000 * t,
104 <                         TimeUnit.MICROSECONDS.toNanos(t));
105 <            assertEquals(t,
106 <                         TimeUnit.SECONDS.NANOSECONDS.toNanos(t));
97 >        for (long t = 0; t < 88888; ++t) {
98 >            assertEquals(t*1000000000L*60*60*24,
99 >                         DAYS.toNanos(t));
100 >            assertEquals(t*1000000000L*60*60,
101 >                         HOURS.toNanos(t));
102 >            assertEquals(t*1000000000L*60,
103 >                         MINUTES.toNanos(t));
104 >            assertEquals(1000000000L*t,
105 >                         SECONDS.toNanos(t));
106 >            assertEquals(1000000L*t,
107 >                         MILLISECONDS.toNanos(t));
108 >            assertEquals(1000L*t,
109 >                         MICROSECONDS.toNanos(t));
110 >            assertEquals(t,
111 >                         NANOSECONDS.toNanos(t));
112 >        }
113 >    }
114 >
115 >    /**
116 >     * toMicros correctly converts sample values in different units to
117 >     * microseconds
118 >     */
119 >    public void testToMicros() {
120 >        for (long t = 0; t < 88888; ++t) {
121 >            assertEquals(t*1000000L*60*60*24,
122 >                         DAYS.toMicros(t));
123 >            assertEquals(t*1000000L*60*60,
124 >                         HOURS.toMicros(t));
125 >            assertEquals(t*1000000L*60,
126 >                         MINUTES.toMicros(t));
127 >            assertEquals(1000000L*t,
128 >                         SECONDS.toMicros(t));
129 >            assertEquals(1000L*t,
130 >                         MILLISECONDS.toMicros(t));
131 >            assertEquals(t,
132 >                         MICROSECONDS.toMicros(t));
133 >            assertEquals(t,
134 >                         NANOSECONDS.toMicros(t*1000L));
135 >        }
136 >    }
137 >
138 >    /**
139 >     * toMillis correctly converts sample values in different units to
140 >     * milliseconds
141 >     */
142 >    public void testToMillis() {
143 >        for (long t = 0; t < 88888; ++t) {
144 >            assertEquals(t*1000L*60*60*24,
145 >                         DAYS.toMillis(t));
146 >            assertEquals(t*1000L*60*60,
147 >                         HOURS.toMillis(t));
148 >            assertEquals(t*1000L*60,
149 >                         MINUTES.toMillis(t));
150 >            assertEquals(1000L*t,
151 >                         SECONDS.toMillis(t));
152 >            assertEquals(t,
153 >                         MILLISECONDS.toMillis(t));
154 >            assertEquals(t,
155 >                         MICROSECONDS.toMillis(t*1000L));
156 >            assertEquals(t,
157 >                         NANOSECONDS.toMillis(t*1000000L));
158          }
159      }
160  
161 +    /**
162 +     * toSeconds correctly converts sample values in different units to
163 +     * seconds
164 +     */
165 +    public void testToSeconds() {
166 +        for (long t = 0; t < 88888; ++t) {
167 +            assertEquals(t*60*60*24,
168 +                         DAYS.toSeconds(t));
169 +            assertEquals(t*60*60,
170 +                         HOURS.toSeconds(t));
171 +            assertEquals(t*60,
172 +                         MINUTES.toSeconds(t));
173 +            assertEquals(t,
174 +                         SECONDS.toSeconds(t));
175 +            assertEquals(t,
176 +                         MILLISECONDS.toSeconds(t*1000L));
177 +            assertEquals(t,
178 +                         MICROSECONDS.toSeconds(t*1000000L));
179 +            assertEquals(t,
180 +                         NANOSECONDS.toSeconds(t*1000000000L));
181 +        }
182 +    }
183 +
184 +    /**
185 +     * toMinutes correctly converts sample values in different units to
186 +     * minutes
187 +     */
188 +    public void testToMinutes() {
189 +        for (long t = 0; t < 88888; ++t) {
190 +            assertEquals(t*60*24,
191 +                         DAYS.toMinutes(t));
192 +            assertEquals(t*60,
193 +                         HOURS.toMinutes(t));
194 +            assertEquals(t,
195 +                         MINUTES.toMinutes(t));
196 +            assertEquals(t,
197 +                         SECONDS.toMinutes(t*60));
198 +            assertEquals(t,
199 +                         MILLISECONDS.toMinutes(t*1000L*60));
200 +            assertEquals(t,
201 +                         MICROSECONDS.toMinutes(t*1000000L*60));
202 +            assertEquals(t,
203 +                         NANOSECONDS.toMinutes(t*1000000000L*60));
204 +        }
205 +    }
206 +
207 +    /**
208 +     * toHours correctly converts sample values in different units to
209 +     * hours
210 +     */
211 +    public void testToHours() {
212 +        for (long t = 0; t < 88888; ++t) {
213 +            assertEquals(t*24,
214 +                         DAYS.toHours(t));
215 +            assertEquals(t,
216 +                         HOURS.toHours(t));
217 +            assertEquals(t,
218 +                         MINUTES.toHours(t*60));
219 +            assertEquals(t,
220 +                         SECONDS.toHours(t*60*60));
221 +            assertEquals(t,
222 +                         MILLISECONDS.toHours(t*1000L*60*60));
223 +            assertEquals(t,
224 +                         MICROSECONDS.toHours(t*1000000L*60*60));
225 +            assertEquals(t,
226 +                         NANOSECONDS.toHours(t*1000000000L*60*60));
227 +        }
228 +    }
229 +
230 +    /**
231 +     * toDays correctly converts sample values in different units to
232 +     * days
233 +     */
234 +    public void testToDays() {
235 +        for (long t = 0; t < 88888; ++t) {
236 +            assertEquals(t,
237 +                         DAYS.toDays(t));
238 +            assertEquals(t,
239 +                         HOURS.toDays(t*24));
240 +            assertEquals(t,
241 +                         MINUTES.toDays(t*60*24));
242 +            assertEquals(t,
243 +                         SECONDS.toDays(t*60*60*24));
244 +            assertEquals(t,
245 +                         MILLISECONDS.toDays(t*1000L*60*60*24));
246 +            assertEquals(t,
247 +                         MICROSECONDS.toDays(t*1000000L*60*60*24));
248 +            assertEquals(t,
249 +                         NANOSECONDS.toDays(t*1000000000L*60*60*24));
250 +        }
251 +    }
252  
253 +    /**
254 +     * convert saturates positive too-large values to Long.MAX_VALUE
255 +     * and negative to LONG.MIN_VALUE
256 +     */
257      public void testConvertSaturate() {
258          assertEquals(Long.MAX_VALUE,
259 <                     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
96 <                                                  TimeUnit.SECONDS));
259 >                     NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS));
260          assertEquals(Long.MIN_VALUE,
261 <                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
262 <                                                  TimeUnit.SECONDS));
263 <
261 >                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS));
262 >        assertEquals(Long.MAX_VALUE,
263 >                     NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES));
264 >        assertEquals(Long.MIN_VALUE,
265 >                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES));
266 >        assertEquals(Long.MAX_VALUE,
267 >                     NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS));
268 >        assertEquals(Long.MIN_VALUE,
269 >                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS));
270 >        assertEquals(Long.MAX_VALUE,
271 >                     NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS));
272 >        assertEquals(Long.MIN_VALUE,
273 >                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS));
274      }
275  
276 +    /**
277 +     * toNanos saturates positive too-large values to Long.MAX_VALUE
278 +     * and negative to LONG.MIN_VALUE
279 +     */
280      public void testToNanosSaturate() {
281 <            assertEquals(Long.MAX_VALUE,
282 <                         TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
283 <            assertEquals(Long.MIN_VALUE,
284 <                         TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
108 <            
281 >        assertEquals(Long.MAX_VALUE,
282 >                     MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
283 >        assertEquals(Long.MIN_VALUE,
284 >                     MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
285      }
286  
287 <
287 >    /**
288 >     * toString returns name of unit
289 >     */
290      public void testToString() {
291 <        String s = TimeUnit.SECONDS.toString();
114 <        assertTrue(s.indexOf("econd") >= 0);
291 >        assertEquals("SECONDS", SECONDS.toString());
292      }
293  
117    // Exception tests
118    
294      /**
295 <     *  This test specifically to catch the unreported exception
295 >     * name returns name of unit
296       */
297 <    public void testTimedWaitForUnreportedIllegalMonitorException() {
298 <        //created a new thread with anonymous runnable
124 <
125 <        Thread t = new Thread(new Runnable() {
126 <                public void run() {
127 <                    Object o = new Object();
128 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
129 <                    try {
130 <                        tu.timedWait(o,40000);
131 <                        fail("should throw");
132 <                    }
133 <                    catch (InterruptedException ie) {
134 <                        fail("should not throw IE here");
135 <                    }
136 <                    catch(IllegalMonitorStateException success) {
137 <                    }
138 <                    
139 <                }
140 <            });
141 <        t.start();
142 <        try {
143 <            Thread.sleep(100);
144 <            t.interrupt();
145 <            t.join();
146 <        } catch(Exception e) {
147 <            fail("Unexpected exception");
148 <        }
297 >    public void testName() {
298 >        assertEquals("SECONDS", SECONDS.name());
299      }
300 <    
300 >
301      /**
302 <     *  Test to verify that timedWait will throw InterruptedException.
303 <     *  Thread t waits on timedWait while the main thread interrupts it.
304 <     *  Note:  This does not throw IllegalMonitorException since timeWait
305 <     *         is synchronized on o
306 <     */
307 <    public void testTimedWait() {
308 <        Thread t = new Thread(new Runnable() {
309 <                public void run() {
310 <                    Object o = new Object();
311 <                    
312 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
313 <                    try {
314 <                        synchronized(o) {
315 <                            tu.timedWait(o,1000);
316 <                        }
317 <                        fail("should throw");
168 <                    }
169 <                    catch(InterruptedException success) {}
170 <                    catch(IllegalMonitorStateException failure) {
171 <                        fail("should not throw");
172 <                    }
173 <                }
174 <            });
175 <        t.start();
176 <        try {
177 <            Thread.sleep(100);
178 <            t.interrupt();
179 <            t.join();
180 <        } catch(Exception e) {
181 <            fail("Unexpected exception");
182 <        }
302 >     * Timed wait without holding lock throws
303 >     * IllegalMonitorStateException
304 >     */
305 >    public void testTimedWait_IllegalMonitorException() {
306 >        Thread t = newStartedThread(new CheckedRunnable() {
307 >            public void realRun() throws InterruptedException {
308 >                Object o = new Object();
309 >                TimeUnit tu = MILLISECONDS;
310 >
311 >                try {
312 >                    tu.timedWait(o, LONG_DELAY_MS);
313 >                    threadShouldThrow();
314 >                } catch (IllegalMonitorStateException success) {}
315 >            }});
316 >
317 >        awaitTermination(t);
318      }
319 <    
185 <    
319 >
320      /**
321 <     *  Test to verify that timedJoin will throw InterruptedException.
188 <     *  Thread t waits on timedJoin while the main thread interrupts it.
321 >     * timedWait throws InterruptedException when interrupted
322       */
323 <    public void testTimedJoin() {
324 <        Thread t = new Thread(new Runnable() {
325 <                public void run() {
326 <                    TimeUnit tu = TimeUnit.MILLISECONDS;        
327 <                    try {
328 <                        Thread s = new Thread(new Runnable() {
329 <                                public void run() {
330 <                                    try{
331 <                                        Thread.sleep(1000);
332 <                                    }catch(InterruptedException success){}
333 <                                }
334 <                            });
335 <                        s.start();
336 <                        tu.timedJoin(s,1000);
337 <                        fail("should throw");
338 <                    }
339 <                    catch(Exception e) {}
340 <                }
341 <            });
342 <        t.start();
343 <        try {
344 <            Thread.sleep(100);
345 <            t.interrupt();
346 <            t.join();
347 <        } catch(Exception e) {
348 <            fail("Unexpected exception");
349 <        }
323 >    public void testTimedWait_Interruptible() {
324 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
325 >        Thread t = newStartedThread(new CheckedRunnable() {
326 >            public void realRun() throws InterruptedException {
327 >                Object o = new Object();
328 >                TimeUnit tu = MILLISECONDS;
329 >
330 >                Thread.currentThread().interrupt();
331 >                try {
332 >                    synchronized (o) {
333 >                        tu.timedWait(o, LONG_DELAY_MS);
334 >                    }
335 >                    shouldThrow();
336 >                } catch (InterruptedException success) {}
337 >                assertFalse(Thread.interrupted());
338 >
339 >                pleaseInterrupt.countDown();
340 >                try {
341 >                    synchronized (o) {
342 >                        tu.timedWait(o, LONG_DELAY_MS);
343 >                    }
344 >                    shouldThrow();
345 >                } catch (InterruptedException success) {}
346 >                assertFalse(Thread.interrupted());
347 >            }});
348 >
349 >        await(pleaseInterrupt);
350 >        assertThreadStaysAlive(t);
351 >        t.interrupt();
352 >        awaitTermination(t);
353      }
354 <    
354 >
355      /**
356 <     *  Test to verify that timedSleep will throw InterruptedException.
221 <     *  Thread t waits on timedSleep while the main thread interrupts it.
356 >     * timedJoin throws InterruptedException when interrupted
357       */
358 <    public void testTimedSleep() {
359 <        //created a new thread with anonymous runnable
360 <
361 <        Thread t = new Thread(new Runnable() {
362 <                public void run() {
363 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
364 <                    try {
365 <                        tu.sleep(1000);
366 <                        fail("should throw");
367 <                    }
368 <                    catch(InterruptedException success) {}
369 <                }
370 <            });
371 <        t.start();
372 <        try {
373 <            Thread.sleep(100);
374 <            t.interrupt();
375 <            t.join();
376 <        } catch(Exception e) {
377 <            fail("Unexpected exception");
378 <        }
358 >    public void testTimedJoin_Interruptible() {
359 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
360 >        final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
361 >            public void realRun() throws InterruptedException {
362 >                Thread.sleep(LONG_DELAY_MS);
363 >            }});
364 >        final Thread t = newStartedThread(new CheckedRunnable() {
365 >            public void realRun() throws InterruptedException {
366 >                TimeUnit tu = MILLISECONDS;
367 >                Thread.currentThread().interrupt();
368 >                try {
369 >                    tu.timedJoin(s, LONG_DELAY_MS);
370 >                    shouldThrow();
371 >                } catch (InterruptedException success) {}
372 >                assertFalse(Thread.interrupted());
373 >
374 >                pleaseInterrupt.countDown();
375 >                try {
376 >                    tu.timedJoin(s, LONG_DELAY_MS);
377 >                    shouldThrow();
378 >                } catch (InterruptedException success) {}
379 >                assertFalse(Thread.interrupted());
380 >            }});
381 >
382 >        await(pleaseInterrupt);
383 >        assertThreadStaysAlive(t);
384 >        t.interrupt();
385 >        awaitTermination(t);
386 >        s.interrupt();
387 >        awaitTermination(s);
388      }
389  
390 <    public void testSerialization() {
391 <        TimeUnit q = TimeUnit.MILLISECONDS;
390 >    /**
391 >     * timedSleep throws InterruptedException when interrupted
392 >     */
393 >    public void testTimedSleep_Interruptible() {
394 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
395 >        Thread t = newStartedThread(new CheckedRunnable() {
396 >            public void realRun() throws InterruptedException {
397 >                TimeUnit tu = MILLISECONDS;
398 >                Thread.currentThread().interrupt();
399 >                try {
400 >                    tu.sleep(LONG_DELAY_MS);
401 >                    shouldThrow();
402 >                } catch (InterruptedException success) {}
403 >                assertFalse(Thread.interrupted());
404 >
405 >                pleaseInterrupt.countDown();
406 >                try {
407 >                    tu.sleep(LONG_DELAY_MS);
408 >                    shouldThrow();
409 >                } catch (InterruptedException success) {}
410 >                assertFalse(Thread.interrupted());
411 >            }});
412 >
413 >        await(pleaseInterrupt);
414 >        assertThreadStaysAlive(t);
415 >        t.interrupt();
416 >        awaitTermination(t);
417 >    }
418  
419 <        try {
420 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
421 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
422 <            out.writeObject(q);
423 <            out.close();
424 <
255 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
256 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
257 <            TimeUnit r = (TimeUnit)in.readObject();
258 <            
259 <            assertEquals(q.toString(), r.toString());
260 <        } catch(Exception e){
261 <            e.printStackTrace();
262 <            fail("unexpected exception");
263 <        }
419 >    /**
420 >     * a deserialized serialized unit is the same instance
421 >     */
422 >    public void testSerialization() throws Exception {
423 >        TimeUnit x = MILLISECONDS;
424 >        assertSame(x, serialClone(x));
425      }
426  
427   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines