• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/publicdomain/zero/1.0/
5  * Other contributors include Andrew Wright, Jeffrey Hayes,
6  * Pat Fisher, Mike Judd.
7  */
8 
9 package jsr166;
10 
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13 
14 public class SystemTest extends JSR166TestCase {
15     // android-note: Removed because the CTS runner does a bad job of
16     // retrying tests that have suite() declarations.
17     //
18     // public static void main(String[] args) {
19     //     main(suite(), args);
20     // }
21     // public static Test suite() {
22     //     return new TestSuite(SystemTest.class);
23     // }
24 
25     /**
26      * Worst case rounding for millisecs; set for 60 cycle millis clock.
27      * This value might need to be changed on JVMs with coarser
28      * System.currentTimeMillis clocks.
29      */
30     static final long MILLIS_ROUND = 17;
31 
32     /**
33      * Nanos between readings of millis is no longer than millis (plus
34      * possible rounding).
35      * This shows only that nano timing not (much) worse than milli.
36      */
testNanoTime1()37     public void testNanoTime1() throws InterruptedException {
38         long m1 = System.currentTimeMillis();
39         Thread.sleep(1);
40         long n1 = System.nanoTime();
41         Thread.sleep(SHORT_DELAY_MS);
42         long n2 = System.nanoTime();
43         Thread.sleep(1);
44         long m2 = System.currentTimeMillis();
45         long millis = m2 - m1;
46         long nanos = n2 - n1;
47         assertTrue(nanos >= 0);
48         long nanosAsMillis = nanos / 1000000;
49         assertTrue(nanosAsMillis <= millis + MILLIS_ROUND);
50     }
51 
52     /**
53      * Millis between readings of nanos is less than nanos, adjusting
54      * for rounding.
55      * This shows only that nano timing not (much) worse than milli.
56      */
testNanoTime2()57     public void testNanoTime2() throws InterruptedException {
58         long n1 = System.nanoTime();
59         Thread.sleep(1);
60         long m1 = System.currentTimeMillis();
61         Thread.sleep(SHORT_DELAY_MS);
62         long m2 = System.currentTimeMillis();
63         Thread.sleep(1);
64         long n2 = System.nanoTime();
65         long millis = m2 - m1;
66         long nanos = n2 - n1;
67 
68         assertTrue(nanos >= 0);
69         long nanosAsMillis = nanos / 1000000;
70         assertTrue(millis <= nanosAsMillis + MILLIS_ROUND);
71     }
72 
73 }
74