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.GwtCompatible;
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.base.Ticker;
024import java.util.concurrent.TimeUnit;
025import java.util.concurrent.atomic.AtomicLong;
026
027/**
028 * A Ticker whose value can be advanced programmatically in test.
029 *
030 * <p>The ticker can be configured so that the time is incremented whenever {@link #read} is called:
031 * see {@link #setAutoIncrementStep}.
032 *
033 * <p>This class is thread-safe.
034 *
035 * @author Jige Yu
036 * @since 10.0
037 */
038@GwtCompatible
039public class FakeTicker extends Ticker {
040
041  private final AtomicLong nanos = new AtomicLong();
042  private volatile long autoIncrementStepNanos;
043
044  /** Advances the ticker value by {@code time} in {@code timeUnit}. */
045  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
046  public FakeTicker advance(long time, TimeUnit timeUnit) {
047    return advance(timeUnit.toNanos(time));
048  }
049
050  /** Advances the ticker value by {@code nanoseconds}. */
051  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
052  public FakeTicker advance(long nanoseconds) {
053    nanos.addAndGet(nanoseconds);
054    return this;
055  }
056
057  /**
058   * Advances the ticker value by {@code duration}.
059   *
060   * @since 28.0
061   */
062  @GwtIncompatible
063  public FakeTicker advance(java.time.Duration duration) {
064    return advance(duration.toNanos());
065  }
066
067  /**
068   * Sets the increment applied to the ticker whenever it is queried.
069   *
070   * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
071   * queried.
072   */
073  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
074  public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit) {
075    checkArgument(autoIncrementStep >= 0, "May not auto-increment by a negative amount");
076    this.autoIncrementStepNanos = timeUnit.toNanos(autoIncrementStep);
077    return this;
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   * @since 28.0
087   */
088  @GwtIncompatible
089  public FakeTicker setAutoIncrementStep(java.time.Duration autoIncrementStep) {
090    return setAutoIncrementStep(autoIncrementStep.toNanos(), TimeUnit.NANOSECONDS);
091  }
092
093  @Override
094  public long read() {
095    return nanos.getAndAdd(autoIncrementStepNanos);
096  }
097}