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.23 by jsr166, Tue Sep 24 16:48:52 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines