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.7 by dl, Mon Dec 29 19:05:40 2003 UTC vs.
Revision 1.23 by jsr166, Tue Sep 24 16:48:52 2013 UTC

# Line 1 | Line 1
1   /*
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/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9
9   import junit.framework.*;
10 < import java.util.concurrent.*;
11 < import java.io.*;
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 four units
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  
# Line 81 | Line 94 | public class TimeUnitTest extends JSR166
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.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  
# Line 99 | Line 117 | public class TimeUnitTest extends JSR166
117       * microseconds
118       */
119      public void testToMicros() {
120 <        for (long t = 0; t < 10; ++t) {
121 <            assertEquals(1000000 * t,
122 <                         TimeUnit.SECONDS.toMicros(t));
123 <
124 <            assertEquals(1000 * t,
125 <                         TimeUnit.MILLISECONDS.toMicros(t));
126 <            assertEquals(t,
127 <                         TimeUnit.MICROSECONDS.toMicros(t));
128 <            assertEquals(t,
129 <                         TimeUnit.NANOSECONDS.toMicros(t * 1000));
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  
# Line 117 | Line 140 | public class TimeUnitTest extends JSR166
140       * milliseconds
141       */
142      public void testToMillis() {
143 <        for (long t = 0; t < 10; ++t) {
144 <            assertEquals(1000 * t,
145 <                         TimeUnit.SECONDS.toMillis(t));
146 <
147 <            assertEquals(t,
148 <                         TimeUnit.MILLISECONDS.toMillis(t));
149 <            assertEquals(t,
150 <                         TimeUnit.MICROSECONDS.toMillis(t * 1000));
151 <            assertEquals(t,
152 <                         TimeUnit.NANOSECONDS.toMillis(t * 1000000));
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  
# Line 135 | Line 163 | public class TimeUnitTest extends JSR166
163       * seconds
164       */
165      public void testToSeconds() {
166 <        for (long t = 0; t < 10; ++t) {
167 <            assertEquals(t,
168 <                         TimeUnit.SECONDS.toSeconds(t));
169 <
170 <            assertEquals(t,
171 <                         TimeUnit.MILLISECONDS.toSeconds(t * 1000));
172 <            assertEquals(t,
173 <                         TimeUnit.MICROSECONDS.toSeconds(t * 1000000));
174 <            assertEquals(t,
175 <                         TimeUnit.NANOSECONDS.toSeconds(t * 1000000000));
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 <     * convert saturates positive too-large values to Long.MAX_VALUE
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,
260 <                                                  TimeUnit.SECONDS));
259 >                     NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS));
260 >        assertEquals(Long.MIN_VALUE,
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 <                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
266 <                                                  TimeUnit.SECONDS));
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
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));
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  
176
287      /**
288 <     * toString returns string containing common name of unit
288 >     * toString returns name of unit
289       */
290      public void testToString() {
291 <        String s = TimeUnit.SECONDS.toString();
182 <        assertTrue(s.indexOf("econd") >= 0);
291 >        assertEquals("SECONDS", SECONDS.toString());
292      }
293  
185    
294      /**
295 <     *  Timed wait without holding lock throws
188 <     *  IllegalMonitorStateException
295 >     * name returns name of unit
296       */
297 <    public void testTimedWait_IllegalMonitorException() {
298 <        //created a new thread with anonymous runnable
192 <
193 <        Thread t = new Thread(new Runnable() {
194 <                public void run() {
195 <                    Object o = new Object();
196 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
197 <                    try {
198 <                        tu.timedWait(o,LONG_DELAY_MS);
199 <                        threadShouldThrow();
200 <                    }
201 <                    catch (InterruptedException ie) {
202 <                        threadUnexpectedException();
203 <                    }
204 <                    catch(IllegalMonitorStateException success) {
205 <                    }
206 <                    
207 <                }
208 <            });
209 <        t.start();
210 <        try {
211 <            Thread.sleep(SHORT_DELAY_MS);
212 <            t.interrupt();
213 <            t.join();
214 <        } catch(Exception e) {
215 <            unexpectedException();
216 <        }
297 >    public void testName() {
298 >        assertEquals("SECONDS", SECONDS.name());
299      }
300 <    
219 <    /**
220 <     * timedWait throws InterruptedException when interrupted
221 <     */
222 <    public void testTimedWait() {
223 <        Thread t = new Thread(new Runnable() {
224 <                public void run() {
225 <                    Object o = new Object();
226 <                    
227 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
228 <                    try {
229 <                        synchronized(o) {
230 <                            tu.timedWait(o,MEDIUM_DELAY_MS);
231 <                        }
232 <                        threadShouldThrow();
233 <                    }
234 <                    catch(InterruptedException success) {}
235 <                    catch(IllegalMonitorStateException failure) {
236 <                        threadUnexpectedException();
237 <                    }
238 <                }
239 <            });
240 <        t.start();
241 <        try {
242 <            Thread.sleep(SHORT_DELAY_MS);
243 <            t.interrupt();
244 <            t.join();
245 <        } catch(Exception e) {
246 <            unexpectedException();
247 <        }
248 <    }
249 <    
250 <    
300 >
301      /**
302 <     * timedJoin throws InterruptedException when interrupted
302 >     * Timed wait without holding lock throws
303 >     * IllegalMonitorStateException
304       */
305 <    public void testTimedJoin() {
306 <        Thread t = new Thread(new Runnable() {
307 <                public void run() {
308 <                    TimeUnit tu = TimeUnit.MILLISECONDS;        
309 <                    try {
310 <                        Thread s = new Thread(new Runnable() {
311 <                                public void run() {
312 <                                    try {
313 <                                        Thread.sleep(MEDIUM_DELAY_MS);
314 <                                    } catch(InterruptedException success){}
315 <                                }
316 <                            });
317 <                        s.start();
267 <                        tu.timedJoin(s,MEDIUM_DELAY_MS);
268 <                        threadShouldThrow();
269 <                    }
270 <                    catch(Exception e) {}
271 <                }
272 <            });
273 <        t.start();
274 <        try {
275 <            Thread.sleep(SHORT_DELAY_MS);
276 <            t.interrupt();
277 <            t.join();
278 <        } catch(Exception e) {
279 <            unexpectedException();
280 <        }
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 <    
319 >
320      /**
321 <     *  timedSleep throws InterruptedException when interrupted
321 >     * timedWait throws InterruptedException when interrupted
322       */
323 <    public void testTimedSleep() {
324 <        //created a new thread with anonymous runnable
325 <
326 <        Thread t = new Thread(new Runnable() {
327 <                public void run() {
328 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
329 <                    try {
330 <                        tu.sleep(MEDIUM_DELAY_MS);
331 <                        threadShouldThrow();
332 <                    }
333 <                    catch(InterruptedException success) {}
334 <                }
335 <            });
336 <        t.start();
337 <        try {
338 <            Thread.sleep(SHORT_DELAY_MS);
339 <            t.interrupt();
340 <            t.join();
341 <        } catch(Exception e) {
342 <            unexpectedException();
343 <        }
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  
355      /**
356 <     * a deserialized serialized unit is equal
356 >     * timedJoin throws InterruptedException when interrupted
357       */
358 <    public void testSerialization() {
359 <        TimeUnit q = TimeUnit.MILLISECONDS;
360 <
361 <        try {
362 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
363 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
364 <            out.writeObject(q);
365 <            out.close();
366 <
367 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
368 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
369 <            TimeUnit r = (TimeUnit)in.readObject();
370 <            
371 <            assertEquals(q.toString(), r.toString());
372 <        } catch(Exception e){
373 <            e.printStackTrace();
374 <            unexpectedException();
375 <        }
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 >    /**
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 >    /**
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