| 1 |
/* |
| 2 |
* Copyright (C) 2022 The Android Open Source Project |
| 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.android.server.art.testing; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.util.Pair; |
| 21 |
|
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.Comparator; |
| 24 |
import java.util.List; |
| 25 |
import java.util.PriorityQueue; |
| 26 |
import java.util.concurrent.RunnableScheduledFuture; |
| 27 |
import java.util.concurrent.ScheduledFuture; |
| 28 |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 29 |
import java.util.concurrent.TimeUnit; |
| 30 |
|
| 31 |
public class MockClock { |
| 32 |
private long mCurrentTimeMs = 0; |
| 33 |
@NonNull private List<ScheduledExecutor> mExecutors = new ArrayList<>(); |
| 34 |
|
| 35 |
@NonNull |
| 36 |
public ScheduledExecutor createScheduledExecutor() { |
| 37 |
var executor = new ScheduledExecutor(); |
| 38 |
mExecutors.add(executor); |
| 39 |
return executor; |
| 40 |
} |
| 41 |
|
| 42 |
public long getCurrentTimeMs() { |
| 43 |
return mCurrentTimeMs; |
| 44 |
} |
| 45 |
|
| 46 |
public void advanceTime(long timeMs) { |
| 47 |
mCurrentTimeMs += timeMs; |
| 48 |
for (ScheduledExecutor executor : mExecutors) { |
| 49 |
executor.notifyUpdate(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public class ScheduledExecutor extends ScheduledThreadPoolExecutor { |
| 54 |
// The second element of the pair is the scheduled time. |
| 55 |
@NonNull |
| 56 |
private PriorityQueue<Pair<RunnableScheduledFuture<?>, Long>> tasks = new PriorityQueue<>( |
| 57 |
1 /* initialCapacity */, Comparator.comparingLong(pair -> pair.second)); |
| 58 |
|
| 59 |
public ScheduledExecutor() { |
| 60 |
super(1 /* corePoolSize */); |
| 61 |
} |
| 62 |
|
| 63 |
@NonNull |
| 64 |
public ScheduledFuture<?> schedule( |
| 65 |
@NonNull Runnable command, long delay, @NonNull TimeUnit unit) { |
| 66 |
// Use `Long.MAX_VALUE` to prevent the task from being automatically run. |
| 67 |
var task = (RunnableScheduledFuture<?>) super.schedule( |
| 68 |
command, Long.MAX_VALUE, TimeUnit.MILLISECONDS); |
| 69 |
tasks.add(Pair.create(task, getCurrentTimeMs() + unit.toMillis(delay))); |
| 70 |
return task; |
| 71 |
} |
| 72 |
|
| 73 |
public void notifyUpdate() { |
| 74 |
while (!tasks.isEmpty()) { |
| 75 |
Pair<RunnableScheduledFuture<?>, Long> pair = tasks.peek(); |
| 76 |
RunnableScheduledFuture<?> task = pair.first; |
| 77 |
long scheduledTimeMs = pair.second; |
| 78 |
if (getCurrentTimeMs() >= scheduledTimeMs) { |
| 79 |
if (!task.isDone() && !task.isCancelled()) { |
| 80 |
task.run(); |
| 81 |
} |
| 82 |
tasks.poll(); |
| 83 |
// Remove the task from the queue of the executor. Terminate the executor if |
| 84 |
// it's shutdown and the queue is empty. |
| 85 |
super.remove(task); |
| 86 |
} else { |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |