aboutsummaryrefslogtreecommitdiff
path: root/testing/src/main/java/io/opencensus/testing/common/TestClock.java
blob: 7d88de43b48ed7231c72e39b5e3e416132a39dd1 (plain)
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
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright 2017, OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.opencensus.testing.common;

import com.google.common.math.LongMath;
import io.opencensus.common.Clock;
import io.opencensus.common.Duration;
import io.opencensus.common.Timestamp;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;

/** A {@link Clock} that allows the time to be set for testing. */
@ThreadSafe
public final class TestClock extends Clock {
  private static final int NUM_NANOS_PER_SECOND = 1000 * 1000 * 1000;

  @GuardedBy("this")
  private Timestamp currentTime = validateNanos(Timestamp.create(1493419949, 223123456));

  private TestClock() {}

  /**
   * Creates a clock initialized to a constant non-zero time. {@code Timestamp.create(0, 0)} is not
   * a good default, because it represents an invalid time.
   *
   * @return a clock initialized to a constant non-zero time.
   */
  public static TestClock create() {
    return new TestClock();
  }

  /**
   * Creates a clock with the given time.
   *
   * @param time the initial time.
   * @return a new {@code TestClock} with the given time.
   */
  public static TestClock create(Timestamp time) {
    TestClock clock = new TestClock();
    clock.setTime(time);
    return clock;
  }

  /**
   * Sets the time.
   *
   * @param time the new time.
   */
  public synchronized void setTime(Timestamp time) {
    currentTime = validateNanos(time);
  }

  /**
   * Advances the time by a duration.
   *
   * @param duration the increase in time.
   */
  public synchronized void advanceTime(Duration duration) {
    currentTime = validateNanos(currentTime.addDuration(duration));
  }

  @Override
  public synchronized Timestamp now() {
    return currentTime;
  }

  @Override
  public synchronized long nowNanos() {
    return getNanos(currentTime);
  }

  private static Timestamp validateNanos(Timestamp time) {
    getNanos(time);
    return time;
  }

  // Converts Timestamp into nanoseconds since time 0 and throws an exception if it overflows.
  private static long getNanos(Timestamp time) {
    return LongMath.checkedAdd(
        LongMath.checkedMultiply(time.getSeconds(), NUM_NANOS_PER_SECOND), time.getNanos());
  }
}