Coverage Summary for Class: FakeTicker (com.google.common.testing)

Class Class, % Method, % Line, %
FakeTicker 100% (1/1) 100% (7/7) 100% (11/11)


1 /* 2  * Copyright (C) 2008 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.testing; 18  19 import static com.google.common.base.Preconditions.checkArgument; 20  21 import com.google.common.annotations.Beta; 22 import com.google.common.annotations.GwtCompatible; 23 import com.google.common.annotations.GwtIncompatible; 24 import com.google.common.base.Ticker; 25 import java.util.concurrent.TimeUnit; 26 import java.util.concurrent.atomic.AtomicLong; 27  28 /** 29  * A Ticker whose value can be advanced programmatically in test. 30  * 31  * <p>The ticker can be configured so that the time is incremented whenever {@link #read} is called: 32  * see {@link #setAutoIncrementStep}. 33  * 34  * <p>This class is thread-safe. 35  * 36  * @author Jige Yu 37  * @since 10.0 38  */ 39 @Beta 40 @GwtCompatible 41 public class FakeTicker extends Ticker { 42  43  private final AtomicLong nanos = new AtomicLong(); 44  private volatile long autoIncrementStepNanos; 45  46  /** Advances the ticker value by {@code time} in {@code timeUnit}. */ 47  @SuppressWarnings("GoodTime") // should accept a java.time.Duration 48  public FakeTicker advance(long time, TimeUnit timeUnit) { 49  return advance(timeUnit.toNanos(time)); 50  } 51  52  /** Advances the ticker value by {@code nanoseconds}. */ 53  @SuppressWarnings("GoodTime") // should accept a java.time.Duration 54  public FakeTicker advance(long nanoseconds) { 55  nanos.addAndGet(nanoseconds); 56  return this; 57  } 58  59  /** 60  * Advances the ticker value by {@code duration}. 61  * 62  * @since 28.0 63  */ 64  @GwtIncompatible 65  public FakeTicker advance(java.time.Duration duration) { 66  return advance(duration.toNanos()); 67  } 68  69  /** 70  * Sets the increment applied to the ticker whenever it is queried. 71  * 72  * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when 73  * queried. 74  */ 75  @SuppressWarnings("GoodTime") // should accept a java.time.Duration 76  public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit) { 77  checkArgument(autoIncrementStep >= 0, "May not auto-increment by a negative amount"); 78  this.autoIncrementStepNanos = timeUnit.toNanos(autoIncrementStep); 79  return this; 80  } 81  82  /** 83  * Sets the increment applied to the ticker whenever it is queried. 84  * 85  * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when 86  * queried. 87  * 88  * @since 28.0 89  */ 90  @GwtIncompatible 91  public FakeTicker setAutoIncrementStep(java.time.Duration autoIncrementStep) { 92  return setAutoIncrementStep(autoIncrementStep.toNanos(), TimeUnit.NANOSECONDS); 93  } 94  95  @Override 96  public long read() { 97  return nanos.getAndAdd(autoIncrementStepNanos); 98  } 99 }