17
package com.google.common.testing;
19
import com.google.common.annotations.GwtCompatible;
20
import com.google.common.annotations.GwtIncompatible;
21
import java.time.Duration;
22
import java.util.EnumSet;
23
import java.util.concurrent.Callable;
24
import java.util.concurrent.CountDownLatch;
25
import java.util.concurrent.ExecutorService;
26
import java.util.concurrent.Executors;
27
import java.util.concurrent.Future;
28
import java.util.concurrent.TimeUnit;
29
import junit.framework.TestCase;
30
import org.checkerframework.checker.nullness.qual.Nullable;
37
@GwtCompatible(emulated = true)
38
public class FakeTickerTest extends TestCase {
41
public void testNullPointerExceptions() {
42
NullPointerTester tester = new NullPointerTester();
43
tester.testAllPublicInstanceMethods(new FakeTicker());
47
@SuppressWarnings("Java7ApiChecker")
48
public void testAdvance() {
49
FakeTicker ticker = new FakeTicker();
50
assertEquals(0, ticker.read());
51
assertSame(ticker, ticker.advance(10));
52
assertEquals(10, ticker.read());
53
ticker.advance(1, TimeUnit.MILLISECONDS);
54
assertEquals(1000010L, ticker.read());
55
ticker.advance(Duration.ofMillis(1));
56
assertEquals(2000010L, ticker.read());
59
public void testAutoIncrementStep_returnsSameInstance() {
60
FakeTicker ticker = new FakeTicker();
61
assertSame(ticker, ticker.setAutoIncrementStep(10, TimeUnit.NANOSECONDS));
64
public void testAutoIncrementStep_nanos() {
65
FakeTicker ticker = new FakeTicker().setAutoIncrementStep(10, TimeUnit.NANOSECONDS);
66
assertEquals(0, ticker.read());
67
assertEquals(10, ticker.read());
68
assertEquals(20, ticker.read());
71
public void testAutoIncrementStep_millis() {
72
FakeTicker ticker = new FakeTicker().setAutoIncrementStep(1, TimeUnit.MILLISECONDS);
73
assertEquals(0, ticker.read());
74
assertEquals(1000000, ticker.read());
75
assertEquals(2000000, ticker.read());
78
public void testAutoIncrementStep_seconds() {
79
FakeTicker ticker = new FakeTicker().setAutoIncrementStep(3, TimeUnit.SECONDS);
80
assertEquals(0, ticker.read());
81
assertEquals(3000000000L, ticker.read());
82
assertEquals(6000000000L, ticker.read());
86
@SuppressWarnings("Java7ApiChecker")
87
public void testAutoIncrementStep_duration() {
88
FakeTicker ticker = new FakeTicker().setAutoIncrementStep(Duration.ofMillis(1));
89
assertEquals(0, ticker.read());
90
assertEquals(1000000, ticker.read());
91
assertEquals(2000000, ticker.read());
94
public void testAutoIncrementStep_resetToZero() {
95
FakeTicker ticker = new FakeTicker().setAutoIncrementStep(10, TimeUnit.NANOSECONDS);
96
assertEquals(0, ticker.read());
97
assertEquals(10, ticker.read());
98
assertEquals(20, ticker.read());
100
for (TimeUnit timeUnit : EnumSet.allOf(TimeUnit.class)) {
101
ticker.setAutoIncrementStep(0, timeUnit);
103
"Expected no auto-increment when setting autoIncrementStep to 0 " + timeUnit,
109
public void testAutoIncrement_negative() {
110
FakeTicker ticker = new FakeTicker();
112
ticker.setAutoIncrementStep(-1, TimeUnit.NANOSECONDS);
113
fail("Expected IllegalArgumentException");
114
} catch (IllegalArgumentException expected) {
120
public void testConcurrentAdvance() throws Exception {
121
final FakeTicker ticker = new FakeTicker();
123
int numberOfThreads = 64;
126
new Callable<@Nullable Void>() {
128
public @Nullable Void call() throws Exception {
137
assertEquals(numberOfThreads * 2, ticker.read());
142
public void testConcurrentAutoIncrementStep() throws Exception {
143
int incrementByNanos = 3;
144
final FakeTicker ticker =
145
new FakeTicker().setAutoIncrementStep(incrementByNanos, TimeUnit.NANOSECONDS);
147
int numberOfThreads = 64;
150
new Callable<@Nullable Void>() {
152
public @Nullable Void call() throws Exception {
153
long unused = ticker.read();
158
assertEquals(incrementByNanos * numberOfThreads, ticker.read());
163
private void runConcurrentTest(int numberOfThreads, final Callable<@Nullable Void> callable)
165
ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
166
final CountDownLatch startLatch = new CountDownLatch(numberOfThreads);
167
final CountDownLatch doneLatch = new CountDownLatch(numberOfThreads);
168
for (int i = numberOfThreads; i > 0; i--) {
169
@SuppressWarnings("unused")
170
Future<?> possiblyIgnoredError =
171
executorService.submit(
172
new Callable<@Nullable Void>() {
174
public @Nullable Void call() throws Exception {
175
startLatch.countDown();
178
doneLatch.countDown();