• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.testing;
18 
19 import static com.google.common.base.Preconditions.checkArgument;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.base.Ticker;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.AtomicLong;
26 
27 /**
28  * A Ticker whose value can be advanced programmatically in test.
29  *
30  * <p>The ticker can be configured so that the time is incremented whenever {@link #read} is called:
31  * see {@link #setAutoIncrementStep}.
32  *
33  * <p>This class is thread-safe.
34  *
35  * @author Jige Yu
36  * @since 10.0
37  */
38 @GwtCompatible
39 public class FakeTicker extends Ticker {
40 
41   private final AtomicLong nanos = new AtomicLong();
42   private volatile long autoIncrementStepNanos;
43 
44   /** Advances the ticker value by {@code time} in {@code timeUnit}. */
45   @SuppressWarnings("GoodTime") // should accept a java.time.Duration
advance(long time, TimeUnit timeUnit)46   public FakeTicker advance(long time, TimeUnit timeUnit) {
47     return advance(timeUnit.toNanos(time));
48   }
49 
50   /** Advances the ticker value by {@code nanoseconds}. */
51   @SuppressWarnings("GoodTime") // should accept a java.time.Duration
advance(long nanoseconds)52   public FakeTicker advance(long nanoseconds) {
53     nanos.addAndGet(nanoseconds);
54     return this;
55   }
56 
57   /**
58    * Advances the ticker value by {@code duration}.
59    *
60    * @since 28.0
61    */
62   @GwtIncompatible
advance(java.time.Duration duration)63   public FakeTicker advance(java.time.Duration duration) {
64     return advance(duration.toNanos());
65   }
66 
67   /**
68    * Sets the increment applied to the ticker whenever it is queried.
69    *
70    * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
71    * queried.
72    */
73   @SuppressWarnings("GoodTime") // should accept a java.time.Duration
setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit)74   public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit) {
75     checkArgument(autoIncrementStep >= 0, "May not auto-increment by a negative amount");
76     this.autoIncrementStepNanos = timeUnit.toNanos(autoIncrementStep);
77     return this;
78   }
79 
80   /**
81    * Sets the increment applied to the ticker whenever it is queried.
82    *
83    * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
84    * queried.
85    *
86    * @since 28.0
87    */
88   @GwtIncompatible
setAutoIncrementStep(java.time.Duration autoIncrementStep)89   public FakeTicker setAutoIncrementStep(java.time.Duration autoIncrementStep) {
90     return setAutoIncrementStep(autoIncrementStep.toNanos(), TimeUnit.NANOSECONDS);
91   }
92 
93   @Override
read()94   public long read() {
95     return nanos.getAndAdd(autoIncrementStepNanos);
96   }
97 }
98