001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.testing;
018
019import static com.google.common.base.Preconditions.checkArgument;
020
021import com.google.common.annotations.Beta;
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.annotations.GwtIncompatible;
024import com.google.common.annotations.J2ktIncompatible;
025import com.google.common.base.Ticker;
026import com.google.errorprone.annotations.CanIgnoreReturnValue;
027import java.time.Duration;
028import java.util.concurrent.TimeUnit;
029import java.util.concurrent.atomic.AtomicLong;
030
031/**
032 * A Ticker whose value can be advanced programmatically in test.
033 *
034 * <p>The ticker can be configured so that the time is incremented whenever {@link #read} is called:
035 * see {@link #setAutoIncrementStep}.
036 *
037 * <p>This class is thread-safe.
038 *
039 * @author Jige Yu
040 * @since 10.0
041 */
042@ElementTypesAreNonnullByDefault
043@GwtCompatible
044public class FakeTicker extends Ticker {
045
046  private final AtomicLong nanos = new AtomicLong();
047  private volatile long autoIncrementStepNanos;
048
049  /** Advances the ticker value by {@code time} in {@code timeUnit}. */
050  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
051  @CanIgnoreReturnValue
052  public FakeTicker advance(long time, TimeUnit timeUnit) {
053    return advance(timeUnit.toNanos(time));
054  }
055
056  /** Advances the ticker value by {@code nanoseconds}. */
057  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
058  @CanIgnoreReturnValue
059  public FakeTicker advance(long nanoseconds) {
060    nanos.addAndGet(nanoseconds);
061    return this;
062  }
063
064  /**
065   * Advances the ticker value by {@code duration}.
066   *
067   * @since 33.1.0 (but since 28.0 in the JRE <a
068   *     href="https://github.com/google/guava#guava-google-core-libraries-for-java">flavor</a>)
069   */
070  @GwtIncompatible
071  @J2ktIncompatible
072  @CanIgnoreReturnValue
073  @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
074  @IgnoreJRERequirement // TODO: b/288085449 - Remove this once we use library-desugaring scents.
075  @Beta // TODO: b/288085449 - Remove @Beta after we're sure that Java 8 APIs are safe for Android
076  public FakeTicker advance(Duration duration) {
077    return advance(duration.toNanos());
078  }
079
080  /**
081   * Sets the increment applied to the ticker whenever it is queried.
082   *
083   * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
084   * queried.
085   */
086  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
087  @CanIgnoreReturnValue
088  public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit) {
089    checkArgument(autoIncrementStep >= 0, "May not auto-increment by a negative amount");
090    this.autoIncrementStepNanos = timeUnit.toNanos(autoIncrementStep);
091    return this;
092  }
093
094  /**
095   * Sets the increment applied to the ticker whenever it is queried.
096   *
097   * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
098   * queried.
099   *
100   * @since 33.1.0 (but since 28.0 in the JRE <a
101   *     href="https://github.com/google/guava#guava-google-core-libraries-for-java">flavor</a>)
102   */
103  @GwtIncompatible
104  @J2ktIncompatible
105  @CanIgnoreReturnValue
106  @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
107  @IgnoreJRERequirement // TODO: b/288085449 - Remove this once we use library-desugaring scents.
108  @Beta // TODO: b/288085449 - Remove @Beta after we're sure that Java 8 APIs are safe for Android
109  public FakeTicker setAutoIncrementStep(Duration autoIncrementStep) {
110    return setAutoIncrementStep(autoIncrementStep.toNanos(), TimeUnit.NANOSECONDS);
111  }
112
113  @Override
114  public long read() {
115    return nanos.getAndAdd(autoIncrementStepNanos);
116  }
117}