• 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.annotations.J2ktIncompatible;
24 import com.google.common.base.Ticker;
25 import com.google.errorprone.annotations.CanIgnoreReturnValue;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicLong;
28 
29 /**
30  * A Ticker whose value can be advanced programmatically in test.
31  *
32  * <p>The ticker can be configured so that the time is incremented whenever {@link #read} is called:
33  * see {@link #setAutoIncrementStep}.
34  *
35  * <p>This class is thread-safe.
36  *
37  * @author Jige Yu
38  * @since 10.0
39  */
40 @ElementTypesAreNonnullByDefault
41 @GwtCompatible
42 public class FakeTicker extends Ticker {
43 
44   private final AtomicLong nanos = new AtomicLong();
45   private volatile long autoIncrementStepNanos;
46 
47   /** Advances the ticker value by {@code time} in {@code timeUnit}. */
48   @SuppressWarnings("GoodTime") // should accept a java.time.Duration
49   @CanIgnoreReturnValue
advance(long time, TimeUnit timeUnit)50   public FakeTicker advance(long time, TimeUnit timeUnit) {
51     return advance(timeUnit.toNanos(time));
52   }
53 
54   /** Advances the ticker value by {@code nanoseconds}. */
55   @SuppressWarnings("GoodTime") // should accept a java.time.Duration
56   @CanIgnoreReturnValue
advance(long nanoseconds)57   public FakeTicker advance(long nanoseconds) {
58     nanos.addAndGet(nanoseconds);
59     return this;
60   }
61 
62   /**
63    * Advances the ticker value by {@code duration}.
64    *
65    * @since 28.0
66    */
67   @GwtIncompatible
68   @J2ktIncompatible
69   @CanIgnoreReturnValue
advance(java.time.Duration duration)70   public FakeTicker advance(java.time.Duration duration) {
71     return advance(duration.toNanos());
72   }
73 
74   /**
75    * Sets the increment applied to the ticker whenever it is queried.
76    *
77    * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
78    * queried.
79    */
80   @SuppressWarnings("GoodTime") // should accept a java.time.Duration
81   @CanIgnoreReturnValue
setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit)82   public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit) {
83     checkArgument(autoIncrementStep >= 0, "May not auto-increment by a negative amount");
84     this.autoIncrementStepNanos = timeUnit.toNanos(autoIncrementStep);
85     return this;
86   }
87 
88   /**
89    * Sets the increment applied to the ticker whenever it is queried.
90    *
91    * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
92    * queried.
93    *
94    * @since 28.0
95    */
96   @GwtIncompatible
97   @J2ktIncompatible
98   @CanIgnoreReturnValue
setAutoIncrementStep(java.time.Duration autoIncrementStep)99   public FakeTicker setAutoIncrementStep(java.time.Duration autoIncrementStep) {
100     return setAutoIncrementStep(autoIncrementStep.toNanos(), TimeUnit.NANOSECONDS);
101   }
102 
103   @Override
read()104   public long read() {
105     return nanos.getAndAdd(autoIncrementStepNanos);
106   }
107 }
108