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.11 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.22 by jsr166, Tue May 31 16:16:24 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9
9   import junit.framework.*;
10 < import java.util.concurrent.*;
11 < import java.io.*;
10 > import java.util.concurrent.CountDownLatch;
11 > import java.util.concurrent.TimeUnit;
12  
13   public class TimeUnitTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run(suite());
15 >        junit.textui.TestRunner.run(suite());
16      }
17  
18      public static Test suite() {
19 <        return new TestSuite(TimeUnitTest.class);
19 >        return new TestSuite(TimeUnitTest.class);
20      }
21  
22      // (loops to 88888 check increments at all time divisions.)
# Line 49 | Line 48 | public class TimeUnitTest extends JSR166
48                           TimeUnit.SECONDS.convert(1000000000L*t,
49                                                    TimeUnit.NANOSECONDS));
50  
52
51              assertEquals(1000L*t*60*60*24,
52                           TimeUnit.MILLISECONDS.convert(t,
53                                                    TimeUnit.DAYS));
# Line 279 | Line 277 | public class TimeUnitTest extends JSR166
277          }
278      }
279  
282
280      /**
281       * convert saturates positive too-large values to Long.MAX_VALUE
282       * and negative to LONG.MIN_VALUE
# Line 309 | Line 306 | public class TimeUnitTest extends JSR166
306          assertEquals(Long.MIN_VALUE,
307                       TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
308                                                    TimeUnit.DAYS));
312
309      }
310  
311      /**
# Line 317 | Line 313 | public class TimeUnitTest extends JSR166
313       * and negative to LONG.MIN_VALUE
314       */
315      public void testToNanosSaturate() {
316 <            assertEquals(Long.MAX_VALUE,
317 <                         TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
318 <            assertEquals(Long.MIN_VALUE,
319 <                         TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
316 >        assertEquals(Long.MAX_VALUE,
317 >                     TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
318 >        assertEquals(Long.MIN_VALUE,
319 >                     TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
320      }
321  
326
322      /**
323 <     * toString returns string containing common name of unit
323 >     * toString returns name of unit
324       */
325      public void testToString() {
326 <        String s = TimeUnit.SECONDS.toString();
332 <        assertTrue(s.indexOf("ECOND") >= 0);
326 >        assertEquals("SECONDS", TimeUnit.SECONDS.toString());
327      }
328  
335
329      /**
330 <     *  Timed wait without holding lock throws
338 <     *  IllegalMonitorStateException
330 >     * name returns name of unit
331       */
332 <    public void testTimedWait_IllegalMonitorException() {
333 <        //created a new thread with anonymous runnable
342 <
343 <        Thread t = new Thread(new Runnable() {
344 <                public void run() {
345 <                    Object o = new Object();
346 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
347 <                    try {
348 <                        tu.timedWait(o,LONG_DELAY_MS);
349 <                        threadShouldThrow();
350 <                    }
351 <                    catch (InterruptedException ie) {
352 <                        threadUnexpectedException();
353 <                    }
354 <                    catch (IllegalMonitorStateException success) {
355 <                    }
356 <
357 <                }
358 <            });
359 <        t.start();
360 <        try {
361 <            Thread.sleep(SHORT_DELAY_MS);
362 <            t.interrupt();
363 <            t.join();
364 <        } catch (Exception e) {
365 <            unexpectedException();
366 <        }
332 >    public void testName() {
333 >        assertEquals("SECONDS", TimeUnit.SECONDS.name());
334      }
335  
336      /**
337 <     * timedWait throws InterruptedException when interrupted
337 >     * Timed wait without holding lock throws
338 >     * IllegalMonitorStateException
339       */
340 <    public void testTimedWait() {
341 <        Thread t = new Thread(new Runnable() {
342 <                public void run() {
343 <                    Object o = new Object();
344 <
345 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
346 <                    try {
347 <                        synchronized(o) {
348 <                            tu.timedWait(o,MEDIUM_DELAY_MS);
349 <                        }
350 <                        threadShouldThrow();
383 <                    }
384 <                    catch (InterruptedException success) {}
385 <                    catch (IllegalMonitorStateException failure) {
386 <                        threadUnexpectedException();
387 <                    }
388 <                }
389 <            });
390 <        t.start();
391 <        try {
392 <            Thread.sleep(SHORT_DELAY_MS);
393 <            t.interrupt();
394 <            t.join();
395 <        } catch (Exception e) {
396 <            unexpectedException();
397 <        }
398 <    }
399 <
340 >    public void testTimedWait_IllegalMonitorException() {
341 >        Thread t = newStartedThread(new CheckedRunnable() {
342 >            public void realRun() throws InterruptedException {
343 >                Object o = new Object();
344 >                TimeUnit tu = TimeUnit.MILLISECONDS;
345 >
346 >                try {
347 >                    tu.timedWait(o, LONG_DELAY_MS);
348 >                    threadShouldThrow();
349 >                } catch (IllegalMonitorStateException success) {}
350 >            }});
351  
352 <    /**
402 <     * timedJoin throws InterruptedException when interrupted
403 <     */
404 <    public void testTimedJoin() {
405 <        Thread t = new Thread(new Runnable() {
406 <                public void run() {
407 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
408 <                    try {
409 <                        Thread s = new Thread(new Runnable() {
410 <                                public void run() {
411 <                                    try {
412 <                                        Thread.sleep(MEDIUM_DELAY_MS);
413 <                                    } catch (InterruptedException success){}
414 <                                }
415 <                            });
416 <                        s.start();
417 <                        tu.timedJoin(s,MEDIUM_DELAY_MS);
418 <                        threadShouldThrow();
419 <                    }
420 <                    catch (Exception e) {}
421 <                }
422 <            });
423 <        t.start();
424 <        try {
425 <            Thread.sleep(SHORT_DELAY_MS);
426 <            t.interrupt();
427 <            t.join();
428 <        } catch (Exception e) {
429 <            unexpectedException();
430 <        }
352 >        awaitTermination(t);
353      }
354  
355      /**
356 <     *  timedSleep throws InterruptedException when interrupted
356 >     * timedWait throws InterruptedException when interrupted
357       */
358 <    public void testTimedSleep() {
359 <        //created a new thread with anonymous runnable
360 <
361 <        Thread t = new Thread(new Runnable() {
362 <                public void run() {
363 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
364 <                    try {
365 <                        tu.sleep(MEDIUM_DELAY_MS);
366 <                        threadShouldThrow();
367 <                    }
368 <                    catch (InterruptedException success) {}
369 <                }
370 <            });
371 <        t.start();
372 <        try {
373 <            Thread.sleep(SHORT_DELAY_MS);
374 <            t.interrupt();
375 <            t.join();
376 <        } catch (Exception e) {
377 <            unexpectedException();
378 <        }
358 >    public void testTimedWait_Interruptible() {
359 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
360 >        Thread t = newStartedThread(new CheckedRunnable() {
361 >            public void realRun() throws InterruptedException {
362 >                Object o = new Object();
363 >                TimeUnit tu = TimeUnit.MILLISECONDS;
364 >
365 >                Thread.currentThread().interrupt();
366 >                try {
367 >                    synchronized (o) {
368 >                        tu.timedWait(o, LONG_DELAY_MS);
369 >                    }
370 >                    shouldThrow();
371 >                } catch (InterruptedException success) {}
372 >                assertFalse(Thread.interrupted());
373 >
374 >                pleaseInterrupt.countDown();
375 >                try {
376 >                    synchronized (o) {
377 >                        tu.timedWait(o, LONG_DELAY_MS);
378 >                    }
379 >                    shouldThrow();
380 >                } catch (InterruptedException success) {}
381 >                assertFalse(Thread.interrupted());
382 >            }});
383 >
384 >        await(pleaseInterrupt);
385 >        assertThreadStaysAlive(t);
386 >        t.interrupt();
387 >        awaitTermination(t);
388      }
389  
390      /**
391 <     * a deserialized serialized unit is equal
391 >     * timedJoin throws InterruptedException when interrupted
392       */
393 <    public void testSerialization() {
394 <        TimeUnit q = TimeUnit.MILLISECONDS;
395 <
396 <        try {
397 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
398 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
399 <            out.writeObject(q);
400 <            out.close();
401 <
402 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
403 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
404 <            TimeUnit r = (TimeUnit)in.readObject();
405 <
406 <            assertEquals(q.toString(), r.toString());
407 <        } catch (Exception e){
408 <            e.printStackTrace();
409 <            unexpectedException();
410 <        }
393 >    public void testTimedJoin_Interruptible() {
394 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
395 >        final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
396 >            public void realRun() throws InterruptedException {
397 >                Thread.sleep(LONG_DELAY_MS);
398 >            }});
399 >        final Thread t = newStartedThread(new CheckedRunnable() {
400 >            public void realRun() throws InterruptedException {
401 >                TimeUnit tu = TimeUnit.MILLISECONDS;
402 >                Thread.currentThread().interrupt();
403 >                try {
404 >                    tu.timedJoin(s, LONG_DELAY_MS);
405 >                    shouldThrow();
406 >                } catch (InterruptedException success) {}
407 >                assertFalse(Thread.interrupted());
408 >
409 >                pleaseInterrupt.countDown();
410 >                try {
411 >                    tu.timedJoin(s, LONG_DELAY_MS);
412 >                    shouldThrow();
413 >                } catch (InterruptedException success) {}
414 >                assertFalse(Thread.interrupted());
415 >            }});
416 >
417 >        await(pleaseInterrupt);
418 >        assertThreadStaysAlive(t);
419 >        t.interrupt();
420 >        awaitTermination(t);
421 >        s.interrupt();
422 >        awaitTermination(s);
423 >    }
424 >
425 >    /**
426 >     * timedSleep throws InterruptedException when interrupted
427 >     */
428 >    public void testTimedSleep_Interruptible() {
429 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
430 >        Thread t = newStartedThread(new CheckedRunnable() {
431 >            public void realRun() throws InterruptedException {
432 >                TimeUnit tu = TimeUnit.MILLISECONDS;
433 >                Thread.currentThread().interrupt();
434 >                try {
435 >                    tu.sleep(LONG_DELAY_MS);
436 >                    shouldThrow();
437 >                } catch (InterruptedException success) {}
438 >                assertFalse(Thread.interrupted());
439 >
440 >                pleaseInterrupt.countDown();
441 >                try {
442 >                    tu.sleep(LONG_DELAY_MS);
443 >                    shouldThrow();
444 >                } catch (InterruptedException success) {}
445 >                assertFalse(Thread.interrupted());
446 >            }});
447 >
448 >        await(pleaseInterrupt);
449 >        assertThreadStaysAlive(t);
450 >        t.interrupt();
451 >        awaitTermination(t);
452 >    }
453 >
454 >    /**
455 >     * a deserialized serialized unit is the same instance
456 >     */
457 >    public void testSerialization() throws Exception {
458 >        TimeUnit x = TimeUnit.MILLISECONDS;
459 >        assertSame(x, serialClone(x));
460      }
461  
462   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines