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