MCPcopy Create free account
hub / github.com/grpc/grpc-java / FakeClock

Class FakeClock

core/src/testFixtures/java/io/grpc/internal/FakeClock.java:44–421  ·  view source on GitHub ↗

A manipulated clock that exports a Ticker and a ScheduledExecutorService. To simulate the locking scenario of using real executors, it never runs tasks within schedule() or execute(). Instead, you should call #runDueTasks in your test method to run all due

Source from the content-addressed store, hash-verified

42 * #runDueTasks} automatically.
43 */
44public final class FakeClock {
45
46 private static final TaskFilter ACCEPT_ALL_FILTER = new TaskFilter() {
47 @Override
48 public boolean shouldAccept(Runnable command) {
49 return true;
50 }
51 };
52
53 private final ScheduledExecutorService scheduledExecutorService = new ScheduledExecutorImpl();
54
55 private final PriorityBlockingQueue<ScheduledTask> scheduledTasks = new PriorityBlockingQueue<>();
56 private final LinkedBlockingQueue<ScheduledTask> dueTasks = new LinkedBlockingQueue<>();
57
58 private final Ticker ticker =
59 new Ticker() {
60 @Override public long read() {
61 return currentTimeNanos;
62 }
63 };
64
65 private final Deadline.Ticker deadlineTicker =
66 new Deadline.Ticker() {
67 @Override public long nanoTime() {
68 return currentTimeNanos;
69 }
70 };
71
72 private final Supplier<Stopwatch> stopwatchSupplier =
73 new Supplier<Stopwatch>() {
74 @Override public Stopwatch get() {
75 return Stopwatch.createUnstarted(ticker);
76 }
77 };
78
79 private final TimeProvider timeProvider =
80 new TimeProvider() {
81 @Override
82 public long currentTimeNanos() {
83 return currentTimeNanos;
84 }
85 };
86
87 private long currentTimeNanos;
88
89 public class ScheduledTask extends AbstractFuture<Void> implements ScheduledFuture<Void> {
90 public final Runnable command;
91 public long dueTimeNanos;
92
93 ScheduledTask(Runnable command) {
94 this.command = command;
95 }
96
97 void run() {
98 command.run();
99 set(null);
100 }
101

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected