summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/time
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespajlib/src/test/java/com/yahoo/time
Publish
Diffstat (limited to 'vespajlib/src/test/java/com/yahoo/time')
-rw-r--r--vespajlib/src/test/java/com/yahoo/time/WallClockSourceTestCase.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/vespajlib/src/test/java/com/yahoo/time/WallClockSourceTestCase.java b/vespajlib/src/test/java/com/yahoo/time/WallClockSourceTestCase.java
new file mode 100644
index 00000000000..e26591c9c64
--- /dev/null
+++ b/vespajlib/src/test/java/com/yahoo/time/WallClockSourceTestCase.java
@@ -0,0 +1,86 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.time;
+
+import org.junit.Test;
+import static org.junit.Assert.assertTrue;
+
+public class WallClockSourceTestCase {
+
+ @Test
+ public void testSimple() {
+ long actualBefore = System.currentTimeMillis();
+ WallClockSource clock = new WallClockSource();
+ long nanos = clock.currentTimeNanos();
+ long micros = nanos / 1000;
+ long millis = micros / 1000;
+ long actualAfter = System.currentTimeMillis();
+
+ assertTrue(actualBefore <= millis);
+ assertTrue(millis <= actualAfter);
+ }
+
+ @Test
+ public void testWithAdjust() {
+ WallClockSource clock = new WallClockSource();
+ long diffB = 0;
+ long diffA = 0;
+ for (int i = 0; i < 66666; i++) {
+ long actualB = System.currentTimeMillis();
+ clock.adjust();
+ long nanos = clock.currentTimeNanos();
+ long actualA = System.currentTimeMillis();
+ long micros = nanos / 1000;
+ long millis = micros / 1000;
+ diffB = Math.max(diffB, actualB - millis);
+ diffA = Math.max(diffA, millis - actualA);
+ // System.out.println("adj Timing values, before: "+actualB+" <= guess: "+millis+" <= after: "+actualA);
+ }
+ System.out.println("adjust test: biggest difference (beforeTime - guess): "+diffB);
+ System.out.println("adjust test: biggest difference (guess - afterTime): "+diffA);
+ assertTrue("actual time before sample must be <= wallclocksource, diff: " + diffB, diffB < 2);
+ assertTrue("actual time after sample must be >= wallclocksource, diff: " + diffA, diffA < 2);
+ }
+
+ @Test
+ public void testNoAdjust() {
+ WallClockSource clock = new WallClockSource();
+ long diffB = 0;
+ long diffA = 0;
+ for (int i = 0; i < 66666; i++) {
+ long actualB = System.currentTimeMillis();
+ long nanos = clock.currentTimeNanos();
+ long actualA = System.currentTimeMillis();
+ long micros = nanos / 1000;
+ long millis = micros / 1000;
+ diffB = Math.max(diffB, actualB - millis);
+ diffA = Math.max(diffA, millis - actualA);
+ // System.out.println("noadj Timing values, before: "+actualB+" <= guess: "+millis+" <= after: "+actualA);
+ }
+ System.out.println("noadjust test: biggest difference (beforeTime - guess): "+diffB);
+ System.out.println("noadjust test: biggest difference (guess - afterTime): "+diffA);
+ assertTrue("actual time before sample must be <= wallclocksource, diff: " + diffB, diffB < 3);
+ assertTrue("actual time after sample must be >= wallclocksource, diff: " + diffA, diffA < 3);
+ }
+
+ @Test
+ public void testAutoAdjust() {
+ WallClockSource clock = WallClockSource.get();
+ long diffB = 0;
+ long diffA = 0;
+ for (int i = 0; i < 66666; i++) {
+ long actualB = System.currentTimeMillis();
+ long nanos = clock.currentTimeNanos();
+ long actualA = System.currentTimeMillis();
+ long micros = nanos / 1000;
+ long millis = micros / 1000;
+ diffB = Math.max(diffB, actualB - millis);
+ diffA = Math.max(diffA, millis - actualA);
+ // System.out.println("noadj Timing values, before: "+actualB+" <= guess: "+millis+" <= after: "+actualA);
+ }
+ System.out.println("autoadjust test: biggest difference (beforeTime - guess): "+diffB);
+ System.out.println("autoadjust test: biggest difference (guess - afterTime): "+diffA);
+ assertTrue("actual time before sample must be <= wallclocksource, diff: " + diffB, diffB < 3);
+ assertTrue("actual time after sample must be >= wallclocksource, diff: " + diffA, diffA < 3);
+ }
+
+}