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.5 by dl, Thu Sep 25 11:02:42 2003 UTC vs.
Revision 1.26 by jsr166, Fri Jan 29 20:02:39 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 four units
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  
# Line 80 | Line 104 | public class TimeUnitTest extends JSR166
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  
# Line 98 | Line 127 | public class TimeUnitTest extends JSR166
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  
# Line 116 | Line 150 | public class TimeUnitTest extends JSR166
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  
# Line 134 | Line 173 | public class TimeUnitTest extends JSR166
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
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,
270 <                                                  TimeUnit.SECONDS));
269 >                     NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS));
270 >        assertEquals(Long.MIN_VALUE,
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 <                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
276 <                                                  TimeUnit.SECONDS));
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
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));
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  
175
297      /**
298 <     * toString returns string containing commn name of unit
298 >     * toString returns name of unit
299       */
300      public void testToString() {
301 <        String s = TimeUnit.SECONDS.toString();
181 <        assertTrue(s.indexOf("econd") >= 0);
301 >        assertEquals("SECONDS", SECONDS.toString());
302      }
303  
184    
185    /**
186     *  Timed wait without holding lock throws
187     *  IllegalMonitorStateException
188     */
189    public void testTimedWait_IllegalMonitorException() {
190        //created a new thread with anonymous runnable
191
192        Thread t = new Thread(new Runnable() {
193                public void run() {
194                    Object o = new Object();
195                    TimeUnit tu = TimeUnit.MILLISECONDS;
196                    try {
197                        tu.timedWait(o,LONG_DELAY_MS);
198                        threadShouldThrow();
199                    }
200                    catch (InterruptedException ie) {
201                        threadUnexpectedException();
202                    }
203                    catch(IllegalMonitorStateException success) {
204                    }
205                    
206                }
207            });
208        t.start();
209        try {
210            Thread.sleep(SHORT_DELAY_MS);
211            t.interrupt();
212            t.join();
213        } catch(Exception e) {
214            unexpectedException();
215        }
216    }
217    
304      /**
305 <     * timedWait throws InterruptedException when interrupted
305 >     * name returns name of unit
306       */
307 <    public void testTimedWait() {
308 <        Thread t = new Thread(new Runnable() {
223 <                public void run() {
224 <                    Object o = new Object();
225 <                    
226 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
227 <                    try {
228 <                        synchronized(o) {
229 <                            tu.timedWait(o,MEDIUM_DELAY_MS);
230 <                        }
231 <                        threadShouldThrow();
232 <                    }
233 <                    catch(InterruptedException success) {}
234 <                    catch(IllegalMonitorStateException failure) {
235 <                        threadUnexpectedException();
236 <                    }
237 <                }
238 <            });
239 <        t.start();
240 <        try {
241 <            Thread.sleep(SHORT_DELAY_MS);
242 <            t.interrupt();
243 <            t.join();
244 <        } catch(Exception e) {
245 <            unexpectedException();
246 <        }
307 >    public void testName() {
308 >        assertEquals("SECONDS", SECONDS.name());
309      }
310 <    
249 <    
310 >
311      /**
312 <     * timedJoin throws InterruptedException when interrupted
312 >     * Timed wait without holding lock throws
313 >     * IllegalMonitorStateException
314       */
315 <    public void testTimedJoin() {
316 <        Thread t = new Thread(new Runnable() {
317 <                public void run() {
318 <                    TimeUnit tu = TimeUnit.MILLISECONDS;        
319 <                    try {
320 <                        Thread s = new Thread(new Runnable() {
321 <                                public void run() {
322 <                                    try {
323 <                                        Thread.sleep(MEDIUM_DELAY_MS);
324 <                                    } catch(InterruptedException success){}
325 <                                }
326 <                            });
327 <                        s.start();
266 <                        tu.timedJoin(s,MEDIUM_DELAY_MS);
267 <                        threadShouldThrow();
268 <                    }
269 <                    catch(Exception e) {}
270 <                }
271 <            });
272 <        t.start();
273 <        try {
274 <            Thread.sleep(SHORT_DELAY_MS);
275 <            t.interrupt();
276 <            t.join();
277 <        } catch(Exception e) {
278 <            unexpectedException();
279 <        }
315 >    public void testTimedWait_IllegalMonitorException() {
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 <    
329 >
330      /**
331 <     *  timedSleep throws InterruptedException when interrupted
331 >     * timedWait throws InterruptedException when interrupted
332       */
333 <    public void testTimedSleep() {
334 <        //created a new thread with anonymous runnable
335 <
336 <        Thread t = new Thread(new Runnable() {
337 <                public void run() {
338 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
339 <                    try {
340 <                        tu.sleep(MEDIUM_DELAY_MS);
341 <                        threadShouldThrow();
342 <                    }
343 <                    catch(InterruptedException success) {}
344 <                }
345 <            });
346 <        t.start();
347 <        try {
348 <            Thread.sleep(SHORT_DELAY_MS);
349 <            t.interrupt();
350 <            t.join();
351 <        } catch(Exception e) {
352 <            unexpectedException();
353 <        }
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 >                    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 >                    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  
365      /**
366 <     * a deserialized serialized unit is equal
366 >     * timedJoin throws InterruptedException when interrupted
367       */
368 <    public void testSerialization() {
369 <        TimeUnit q = TimeUnit.MILLISECONDS;
370 <
371 <        try {
372 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
373 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
374 <            out.writeObject(q);
375 <            out.close();
376 <
377 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
378 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
379 <            TimeUnit r = (TimeUnit)in.readObject();
380 <            
381 <            assertEquals(q.toString(), r.toString());
382 <        } catch(Exception e){
383 <            e.printStackTrace();
384 <            unexpectedException();
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 >
400 >    /**
401 >     * timedSleep throws InterruptedException when interrupted
402 >     */
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 >
429 >    /**
430 >     * a deserialized serialized unit is the same instance
431 >     */
432 >    public void testSerialization() throws Exception {
433 >        TimeUnit x = MILLISECONDS;
434 >        assertSame(x, serialClone(x));
435 >    }
436 >
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 >    /**
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