Skip to content

Commit

Permalink
Expose FakeTicker Duration methods to Android users.
Browse files Browse the repository at this point in the history
This makes another good trial balloon for #6567 because it's in our testlib, where the stakes are lower. (Granted, the testlib is also much less commonly used, so we won't learn a lot from this trial.)

RELNOTES=`testing`: Exposed `FakeTicker` `Duration` methods to Android users.
PiperOrigin-RevId: 604629836
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Feb 6, 2024
1 parent 750ba3e commit f346bbb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Ticker;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

Expand Down Expand Up @@ -57,6 +61,22 @@ public FakeTicker advance(long nanoseconds) {
return this;
}

/**
* Advances the ticker value by {@code duration}.
*
* @since NEXT (but since 28.0 in the JRE <a
* href="https://github.com/google/guava#guava-google-core-libraries-for-java">flavor</a>)
*/
@GwtIncompatible
@J2ktIncompatible
@CanIgnoreReturnValue
@SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
@IgnoreJRERequirement // TODO: b/288085449 - Remove this once we use library-desugaring scents.
@Beta // TODO: b/288085449 - Remove @Beta after we're sure that Java 8 APIs are safe for Android
public FakeTicker advance(Duration duration) {
return advance(duration.toNanos());
}

/**
* Sets the increment applied to the ticker whenever it is queried.
*
Expand All @@ -71,6 +91,25 @@ public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit
return this;
}

/**
* Sets the increment applied to the ticker whenever it is queried.
*
* <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
* queried.
*
* @since NEXT (but since 28.0 in the JRE <a
* href="https://github.com/google/guava#guava-google-core-libraries-for-java">flavor</a>)
*/
@GwtIncompatible
@J2ktIncompatible
@CanIgnoreReturnValue
@SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
@IgnoreJRERequirement // TODO: b/288085449 - Remove this once we use library-desugaring scents.
@Beta // TODO: b/288085449 - Remove @Beta after we're sure that Java 8 APIs are safe for Android
public FakeTicker setAutoIncrementStep(Duration autoIncrementStep) {
return setAutoIncrementStep(autoIncrementStep.toNanos(), TimeUnit.NANOSECONDS);
}

@Override
public long read() {
return nanos.getAndAdd(autoIncrementStepNanos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import java.time.Duration;
import java.util.EnumSet;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
Expand All @@ -42,13 +43,18 @@ public void testNullPointerExceptions() {
tester.testAllPublicInstanceMethods(new FakeTicker());
}

@GwtIncompatible // java.time.Duration
@SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
@IgnoreJRERequirement // TODO: b/288085449 - Remove this once we use library-desugaring scents.
public void testAdvance() {
FakeTicker ticker = new FakeTicker();
assertEquals(0, ticker.read());
assertSame(ticker, ticker.advance(10));
assertEquals(10, ticker.read());
ticker.advance(1, TimeUnit.MILLISECONDS);
assertEquals(1000010L, ticker.read());
ticker.advance(Duration.ofMillis(1));
assertEquals(2000010L, ticker.read());
}

public void testAutoIncrementStep_returnsSameInstance() {
Expand Down Expand Up @@ -77,6 +83,16 @@ public void testAutoIncrementStep_seconds() {
assertEquals(6000000000L, ticker.read());
}

@GwtIncompatible // java.time.Duration
@SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
@IgnoreJRERequirement // TODO: b/288085449 - Remove this once we use library-desugaring scents.
public void testAutoIncrementStep_duration() {
FakeTicker ticker = new FakeTicker().setAutoIncrementStep(Duration.ofMillis(1));
assertEquals(0, ticker.read());
assertEquals(1000000, ticker.read());
assertEquals(2000000, ticker.read());
}

public void testAutoIncrementStep_resetToZero() {
FakeTicker ticker = new FakeTicker().setAutoIncrementStep(10, TimeUnit.NANOSECONDS);
assertEquals(0, ticker.read());
Expand Down
7 changes: 5 additions & 2 deletions guava-testlib/src/com/google/common/testing/FakeTicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Ticker;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

Expand Down Expand Up @@ -67,7 +68,8 @@ public FakeTicker advance(long nanoseconds) {
@GwtIncompatible
@J2ktIncompatible
@CanIgnoreReturnValue
public FakeTicker advance(java.time.Duration duration) {
@SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
public FakeTicker advance(Duration duration) {
return advance(duration.toNanos());
}

Expand Down Expand Up @@ -96,7 +98,8 @@ public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit
@GwtIncompatible
@J2ktIncompatible
@CanIgnoreReturnValue
public FakeTicker setAutoIncrementStep(java.time.Duration autoIncrementStep) {
@SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
public FakeTicker setAutoIncrementStep(Duration autoIncrementStep) {
return setAutoIncrementStep(autoIncrementStep.toNanos(), TimeUnit.NANOSECONDS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import java.time.Duration;
import java.util.EnumSet;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
Expand All @@ -43,14 +44,15 @@ public void testNullPointerExceptions() {
}

@GwtIncompatible // java.time.Duration
@SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
public void testAdvance() {
FakeTicker ticker = new FakeTicker();
assertEquals(0, ticker.read());
assertSame(ticker, ticker.advance(10));
assertEquals(10, ticker.read());
ticker.advance(1, TimeUnit.MILLISECONDS);
assertEquals(1000010L, ticker.read());
ticker.advance(java.time.Duration.ofMillis(1));
ticker.advance(Duration.ofMillis(1));
assertEquals(2000010L, ticker.read());
}

Expand Down Expand Up @@ -81,8 +83,9 @@ public void testAutoIncrementStep_seconds() {
}

@GwtIncompatible // java.time.Duration
@SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
public void testAutoIncrementStep_duration() {
FakeTicker ticker = new FakeTicker().setAutoIncrementStep(java.time.Duration.ofMillis(1));
FakeTicker ticker = new FakeTicker().setAutoIncrementStep(Duration.ofMillis(1));
assertEquals(0, ticker.read());
assertEquals(1000000, ticker.read());
assertEquals(2000000, ticker.read());
Expand Down

0 comments on commit f346bbb

Please sign in to comment.