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.4 by dl, Sat Sep 20 18:20:08 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 <     *
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 <     *
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 <     *
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 <     *
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 <     *
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  
146
194      /**
195 <     *
195 >     * toMinutes correctly converts sample values in different units to
196 >     * minutes
197       */
198 <    public void testConvertSaturate() {
199 <        assertEquals(Long.MAX_VALUE,
200 <                     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
201 <                                                  TimeUnit.SECONDS));
202 <        assertEquals(Long.MIN_VALUE,
203 <                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
204 <                                                  TimeUnit.SECONDS));
205 <
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 <     *
218 >     * toHours correctly converts sample values in different units to
219 >     * hours
220       */
221 <    public void testToNanosSaturate() {
222 <            assertEquals(Long.MAX_VALUE,
223 <                         TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
224 <            assertEquals(Long.MIN_VALUE,
225 <                         TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
226 <            
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  
171
240      /**
241 <     *
241 >     * toDays correctly converts sample values in different units to
242 >     * days
243       */
244 <    public void testToString() {
245 <        String s = TimeUnit.SECONDS.toString();
246 <        assertTrue(s.indexOf("econd") >= 0);
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  
180    
263      /**
264 <     *  Timed wait without holding lock throws
265 <     *  IllegalMonitorStateException
264 >     * convert saturates positive too-large values to Long.MAX_VALUE
265 >     * and negative to LONG.MIN_VALUE
266       */
267 <    /**
268 <     *
269 <     */
270 <    public void testTimedWait_IllegalMonitorException() {
271 <        //created a new thread with anonymous runnable
272 <
273 <        Thread t = new Thread(new Runnable() {
274 <                public void run() {
275 <                    Object o = new Object();
276 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
277 <                    try {
278 <                        tu.timedWait(o,LONG_DELAY_MS);
279 <                        threadShouldThrow();
280 <                    }
281 <                    catch (InterruptedException ie) {
282 <                        threadUnexpectedException();
283 <                    }
202 <                    catch(IllegalMonitorStateException success) {
203 <                    }
204 <                    
205 <                }
206 <            });
207 <        t.start();
208 <        try {
209 <            Thread.sleep(SHORT_DELAY_MS);
210 <            t.interrupt();
211 <            t.join();
212 <        } catch(Exception e) {
213 <            unexpectedException();
214 <        }
215 <    }
216 <    
217 <    /**
218 <     *   timedWait will throw InterruptedException.
219 <     *  Thread t waits on timedWait while the main thread interrupts it.
220 <     *  Note:  This does not throw IllegalMonitorException since timeWait
221 <     *         is synchronized on o
222 <     */
223 <    /**
224 <     *
225 <     */
226 <    public void testTimedWait() {
227 <        Thread t = new Thread(new Runnable() {
228 <                public void run() {
229 <                    Object o = new Object();
230 <                    
231 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
232 <                    try {
233 <                        synchronized(o) {
234 <                            tu.timedWait(o,MEDIUM_DELAY_MS);
235 <                        }
236 <                        threadShouldThrow();
237 <                    }
238 <                    catch(InterruptedException success) {}
239 <                    catch(IllegalMonitorStateException failure) {
240 <                        threadUnexpectedException();
241 <                    }
242 <                }
243 <            });
244 <        t.start();
245 <        try {
246 <            Thread.sleep(SHORT_DELAY_MS);
247 <            t.interrupt();
248 <            t.join();
249 <        } catch(Exception e) {
250 <            unexpectedException();
251 <        }
267 >    public void testConvertSaturate() {
268 >        assertEquals(Long.MAX_VALUE,
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 >                     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 <    
254 <    
255 <    /**
256 <     *   timedJoin will throw InterruptedException.
257 <     *  Thread t waits on timedJoin while the main thread interrupts it.
258 <     */
285 >
286      /**
287 <     *
287 >     * toNanos saturates positive too-large values to Long.MAX_VALUE
288 >     * and negative to LONG.MIN_VALUE
289       */
290 <    public void testTimedJoin() {
291 <        Thread t = new Thread(new Runnable() {
292 <                public void run() {
293 <                    TimeUnit tu = TimeUnit.MILLISECONDS;        
294 <                    try {
267 <                        Thread s = new Thread(new Runnable() {
268 <                                public void run() {
269 <                                    try {
270 <                                        Thread.sleep(MEDIUM_DELAY_MS);
271 <                                    } catch(InterruptedException success){}
272 <                                }
273 <                            });
274 <                        s.start();
275 <                        tu.timedJoin(s,MEDIUM_DELAY_MS);
276 <                        threadShouldThrow();
277 <                    }
278 <                    catch(Exception e) {}
279 <                }
280 <            });
281 <        t.start();
282 <        try {
283 <            Thread.sleep(SHORT_DELAY_MS);
284 <            t.interrupt();
285 <            t.join();
286 <        } catch(Exception e) {
287 <            unexpectedException();
288 <        }
290 >    public void testToNanosSaturate() {
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 <    
296 >
297      /**
298 <     *   timedSleep will throw InterruptedException.
293 <     *  Thread t waits on timedSleep while the main thread interrupts it.
298 >     * toString returns name of unit
299       */
300 +    public void testToString() {
301 +        assertEquals("SECONDS", SECONDS.toString());
302 +    }
303 +
304      /**
305 <     *
305 >     * name returns name of unit
306       */
307 <    public void testTimedSleep() {
308 <        //created a new thread with anonymous runnable
300 <
301 <        Thread t = new Thread(new Runnable() {
302 <                public void run() {
303 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
304 <                    try {
305 <                        tu.sleep(MEDIUM_DELAY_MS);
306 <                        threadShouldThrow();
307 <                    }
308 <                    catch(InterruptedException success) {}
309 <                }
310 <            });
311 <        t.start();
312 <        try {
313 <            Thread.sleep(SHORT_DELAY_MS);
314 <            t.interrupt();
315 <            t.join();
316 <        } catch(Exception e) {
317 <            unexpectedException();
318 <        }
307 >    public void testName() {
308 >        assertEquals("SECONDS", SECONDS.name());
309      }
310  
311      /**
312 <     *
312 >     * Timed wait without holding lock throws
313 >     * IllegalMonitorStateException
314       */
315 <    public void testSerialization() {
316 <        TimeUnit q = TimeUnit.MILLISECONDS;
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  
330 <        try {
331 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
332 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
333 <            out.writeObject(q);
334 <            out.close();
335 <
336 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
337 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
338 <            TimeUnit r = (TimeUnit)in.readObject();
339 <            
340 <            assertEquals(q.toString(), r.toString());
341 <        } catch(Exception e){
342 <            e.printStackTrace();
343 <            unexpectedException();
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 >                    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 >     * 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 >
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