Remove FakeClock.sleep.

This functionality isn't used and there is no point in supporting
it.

PiperOrigin-RevId: 353876038
This commit is contained in:
tonihei 2021-01-26 16:52:31 +00:00 committed by Ian Baker
parent 0d85958a76
commit 06fe0900a9
4 changed files with 0 additions and 128 deletions

View File

@ -43,9 +43,6 @@ public interface Clock {
/** @see android.os.SystemClock#uptimeMillis() */
long uptimeMillis();
/** @see android.os.SystemClock#sleep(long) */
void sleep(long sleepTimeMs);
/**
* Creates a {@link HandlerWrapper} using a specified looper and a specified callback for handling
* messages.

View File

@ -43,11 +43,6 @@ public class SystemClock implements Clock {
return android.os.SystemClock.uptimeMillis();
}
@Override
public void sleep(long sleepTimeMs) {
android.os.SystemClock.sleep(sleepTimeMs);
}
@Override
public HandlerWrapper createHandler(Looper looper, @Nullable Callback callback) {
return new SystemHandlerWrapper(new Handler(looper, callback));

View File

@ -38,7 +38,6 @@ import java.util.List;
*/
public class FakeClock implements Clock {
private final List<Long> wakeUpTimes;
private final List<HandlerMessageData> handlerMessages;
private final long bootTimeMs;
@ -65,7 +64,6 @@ public class FakeClock implements Clock {
public FakeClock(long bootTimeMs, long initialTimeMs) {
this.bootTimeMs = bootTimeMs;
this.timeSinceBootMs = initialTimeMs;
this.wakeUpTimes = new ArrayList<>();
this.handlerMessages = new ArrayList<>();
SystemClock.setCurrentTimeMillis(initialTimeMs);
}
@ -78,12 +76,6 @@ public class FakeClock implements Clock {
public synchronized void advanceTime(long timeDiffMs) {
timeSinceBootMs += timeDiffMs;
SystemClock.setCurrentTimeMillis(timeSinceBootMs);
for (Long wakeUpTime : wakeUpTimes) {
if (wakeUpTime <= timeSinceBootMs) {
notifyAll();
break;
}
}
for (int i = handlerMessages.size() - 1; i >= 0; i--) {
if (handlerMessages.get(i).maybeSendToTarget(timeSinceBootMs)) {
handlerMessages.remove(i);
@ -106,23 +98,6 @@ public class FakeClock implements Clock {
return elapsedRealtime();
}
@Override
public synchronized void sleep(long sleepTimeMs) {
if (sleepTimeMs <= 0) {
return;
}
Long wakeUpTimeMs = timeSinceBootMs + sleepTimeMs;
wakeUpTimes.add(wakeUpTimeMs);
while (timeSinceBootMs < wakeUpTimeMs) {
try {
wait();
} catch (InterruptedException e) {
// Ignore InterruptedException as SystemClock.sleep does too.
}
}
wakeUpTimes.remove(wakeUpTimeMs);
}
@Override
public HandlerWrapper createHandler(Looper looper, @Nullable Callback callback) {
return new ClockHandler(looper, callback);

View File

@ -16,14 +16,11 @@
package com.google.android.exoplayer2.testutil;
import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import android.os.ConditionVariable;
import android.os.HandlerThread;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.util.Clock;
import com.google.android.exoplayer2.util.HandlerWrapper;
import java.util.concurrent.CountDownLatch;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -31,8 +28,6 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public final class FakeClockTest {
private static final long TIMEOUT_MS = 10_000;
@Test
public void currentTimeMillis_withoutBootTime() {
FakeClock fakeClock = new FakeClock(/* initialTimeMs= */ 10);
@ -62,48 +57,6 @@ public final class FakeClockTest {
assertThat(fakeClock.elapsedRealtime()).isEqualTo(2500);
}
@Test
public void testSleep() throws InterruptedException {
FakeClock fakeClock = new FakeClock(0);
SleeperThread sleeperThread = new SleeperThread(fakeClock, 1000);
sleeperThread.start();
assertThat(sleeperThread.waitUntilAsleep(TIMEOUT_MS)).isTrue();
assertThat(sleeperThread.isSleeping()).isTrue();
fakeClock.advanceTime(1000);
sleeperThread.join(TIMEOUT_MS);
assertThat(sleeperThread.isSleeping()).isFalse();
sleeperThread = new SleeperThread(fakeClock, 0);
sleeperThread.start();
sleeperThread.join();
assertThat(sleeperThread.isSleeping()).isFalse();
SleeperThread[] sleeperThreads = new SleeperThread[5];
sleeperThreads[0] = new SleeperThread(fakeClock, 1000);
sleeperThreads[1] = new SleeperThread(fakeClock, 1000);
sleeperThreads[2] = new SleeperThread(fakeClock, 2000);
sleeperThreads[3] = new SleeperThread(fakeClock, 3000);
sleeperThreads[4] = new SleeperThread(fakeClock, 4000);
for (SleeperThread thread : sleeperThreads) {
thread.start();
assertThat(thread.waitUntilAsleep(TIMEOUT_MS)).isTrue();
}
assertSleepingStates(new boolean[] {true, true, true, true, true}, sleeperThreads);
fakeClock.advanceTime(1500);
assertThat(sleeperThreads[0].waitUntilAwake(TIMEOUT_MS)).isTrue();
assertThat(sleeperThreads[1].waitUntilAwake(TIMEOUT_MS)).isTrue();
assertSleepingStates(new boolean[] {false, false, true, true, true}, sleeperThreads);
fakeClock.advanceTime(2000);
assertThat(sleeperThreads[2].waitUntilAwake(TIMEOUT_MS)).isTrue();
assertThat(sleeperThreads[3].waitUntilAwake(TIMEOUT_MS)).isTrue();
assertSleepingStates(new boolean[] {false, false, false, false, true}, sleeperThreads);
fakeClock.advanceTime(2000);
for (SleeperThread thread : sleeperThreads) {
thread.join(TIMEOUT_MS);
}
assertSleepingStates(new boolean[] {false, false, false, false, false}, sleeperThreads);
}
@Test
public void testPostDelayed() {
HandlerThread handlerThread = new HandlerThread("FakeClockTest");
@ -140,12 +93,6 @@ public final class FakeClockTest {
assertTestRunnableStates(new boolean[] {true, true, true, true, true}, testRunnables);
}
private static void assertSleepingStates(boolean[] states, SleeperThread[] sleeperThreads) {
for (int i = 0; i < sleeperThreads.length; i++) {
assertThat(sleeperThreads[i].isSleeping()).isEqualTo(states[i]);
}
}
private static void waitForHandler(HandlerWrapper handler) {
final ConditionVariable handlerFinished = new ConditionVariable();
handler.post(handlerFinished::open);
@ -158,48 +105,6 @@ public final class FakeClockTest {
}
}
private static final class SleeperThread extends Thread {
private final Clock clock;
private final long sleepDurationMs;
private final CountDownLatch fallAsleepCountDownLatch;
private final CountDownLatch wakeUpCountDownLatch;
private volatile boolean isSleeping;
public SleeperThread(Clock clock, long sleepDurationMs) {
this.clock = clock;
this.sleepDurationMs = sleepDurationMs;
this.fallAsleepCountDownLatch = new CountDownLatch(1);
this.wakeUpCountDownLatch = new CountDownLatch(1);
}
public boolean waitUntilAsleep(long timeoutMs) throws InterruptedException {
return fallAsleepCountDownLatch.await(timeoutMs, MILLISECONDS);
}
public boolean waitUntilAwake(long timeoutMs) throws InterruptedException {
return wakeUpCountDownLatch.await(timeoutMs, MILLISECONDS);
}
public boolean isSleeping() {
return isSleeping;
}
@Override
public void run() {
// This relies on the FakeClock's methods synchronizing on its own monitor to ensure that
// any interactions with it occur only after sleep() has called wait() or returned.
synchronized (clock) {
isSleeping = true;
fallAsleepCountDownLatch.countDown();
clock.sleep(sleepDurationMs);
isSleeping = false;
wakeUpCountDownLatch.countDown();
}
}
}
private static final class TestRunnable implements Runnable {
public boolean hasRun;