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