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.*; 12 13 public class SystemTest extends JSR166TestCase { 14 15 /** 16 * Worst case rounding for millisecs; set for 60 cycle millis clock. 17 * This value might need to be changed on JVMs with coarser 18 * System.currentTimeMillis clocks. 19 */ 20 static final long MILLIS_ROUND = 17; 21 22 /** 23 * Nanos between readings of millis is no longer than millis (plus 24 * possible rounding). 25 * This shows only that nano timing not (much) worse than milli. 26 */ testNanoTime1()27 public void testNanoTime1() throws InterruptedException { 28 long m1 = System.currentTimeMillis(); 29 Thread.sleep(1); 30 long n1 = System.nanoTime(); 31 Thread.sleep(SHORT_DELAY_MS); 32 long n2 = System.nanoTime(); 33 Thread.sleep(1); 34 long m2 = System.currentTimeMillis(); 35 long millis = m2 - m1; 36 long nanos = n2 - n1; 37 assertTrue(nanos >= 0); 38 long nanosAsMillis = nanos / 1000000; 39 assertTrue(nanosAsMillis <= millis + MILLIS_ROUND); 40 } 41 42 /** 43 * Millis between readings of nanos is less than nanos, adjusting 44 * for rounding. 45 * This shows only that nano timing not (much) worse than milli. 46 */ testNanoTime2()47 public void testNanoTime2() throws InterruptedException { 48 long n1 = System.nanoTime(); 49 Thread.sleep(1); 50 long m1 = System.currentTimeMillis(); 51 Thread.sleep(SHORT_DELAY_MS); 52 long m2 = System.currentTimeMillis(); 53 Thread.sleep(1); 54 long n2 = System.nanoTime(); 55 long millis = m2 - m1; 56 long nanos = n2 - n1; 57 58 assertTrue(nanos >= 0); 59 long nanosAsMillis = nanos / 1000000; 60 assertTrue(millis <= nanosAsMillis + MILLIS_ROUND); 61 } 62 63 } 64