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