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