summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/time/WallClockSourceTestCase.java
blob: 4c181235a383547e528f2541aa057ee71c1de8d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2017 Yahoo Holdings. 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);
    }

}