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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines