diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/Clock.java b/library/common/src/main/java/com/google/android/exoplayer2/util/Clock.java index ffb8236bd1..f6b98a1c66 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/Clock.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/Clock.java @@ -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. diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/SystemClock.java b/library/common/src/main/java/com/google/android/exoplayer2/util/SystemClock.java index 89e1c60d7a..e315d8bf25 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/SystemClock.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/SystemClock.java @@ -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)); diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java index 9e90af4d83..9b8bccee14 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java @@ -38,7 +38,6 @@ import java.util.List; */ public class FakeClock implements Clock { - private final List wakeUpTimes; private final List 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); diff --git a/testutils/src/test/java/com/google/android/exoplayer2/testutil/FakeClockTest.java b/testutils/src/test/java/com/google/android/exoplayer2/testutil/FakeClockTest.java index ba0a022270..a50b4302e0 100644 --- a/testutils/src/test/java/com/google/android/exoplayer2/testutil/FakeClockTest.java +++ b/testutils/src/test/java/com/google/android/exoplayer2/testutil/FakeClockTest.java @@ -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;