Deflake the DefaultPreloadManagerTest

From [ the last change in `DefaultPreloadManagerTest`](2b54b1ebbe), the preloadManager began to use a separate `preloadThread` in `release_returnZeroCount_sourcesAndRendererCapabilitiesListReleased`, which unveils a bug in `PreloadMediaSource`. When `PreloadMediaSource.releasePreloadMediaSource` is called, `preloadHandler` will post a `Runnable` on the preload looper to release the internal resources. Before this `Runnable` is executed, it is possible that the [`stopPreloading`](https://github.com/androidx/media/blob/main/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/preload/PreloadMediaSource.java#L442) method is executed just as the result of preloading has completed. This is expected to remove the posted `Runnable`s for further preloading, however, the posted `Runnable` for releasing will also be removed from the message queue.

Ideally we should use `postDelayed(runnable, token, delayMillis)` to post the runnables so that the token will be useful to identify which messages to remove in `removeCallbacksAndMessages(token)`, but that `postDelayed` method is only available from API 28. So in this change we are using a separate handler for releasing, and then the call of `preloadHandler.removeCallbacksAndMessages` won't impact the runnable for releasing.

#cherrypick

PiperOrigin-RevId: 696894483
(cherry picked from commit 0143884cd7ac06b042532e328ea69c5ac7da2cb3)
This commit is contained in:
tianyifeng 2024-11-15 08:20:00 -08:00 committed by Ian Baker
parent fd02ee182c
commit 737fdd8693
2 changed files with 5 additions and 3 deletions

View File

@ -221,6 +221,7 @@ public final class PreloadMediaSource extends WrappingMediaSource {
private final RendererCapabilities[] rendererCapabilities;
private final Allocator allocator;
private final Handler preloadHandler;
private final Handler releaseHandler;
private boolean preloadCalled;
private boolean prepareChildSourceCalled;
private long startPositionUs;
@ -246,6 +247,7 @@ public final class PreloadMediaSource extends WrappingMediaSource {
this.allocator = allocator;
preloadHandler = Util.createHandler(preloadLooper, /* callback= */ null);
releaseHandler = Util.createHandler(preloadLooper, /* callback= */ null);
startPositionUs = C.TIME_UNSET;
}
@ -396,7 +398,7 @@ public final class PreloadMediaSource extends WrappingMediaSource {
* <p>Can be called from any thread.
*/
public void releasePreloadMediaSource() {
preloadHandler.post(
releaseHandler.post(
() -> {
preloadCalled = false;
startPositionUs = C.TIME_UNSET;
@ -407,6 +409,7 @@ public final class PreloadMediaSource extends WrappingMediaSource {
}
releaseSourceInternal();
preloadHandler.removeCallbacksAndMessages(null);
releaseHandler.removeCallbacksAndMessages(null);
});
}

View File

@ -830,8 +830,7 @@ public class DefaultPreloadManagerTest {
}
@Test
public void release_returnZeroCount_sourcesAndRendererCapabilitiesListReleased()
throws Exception {
public void release_returnZeroCount_sourcesAndRendererCapabilitiesListReleased() {
TargetPreloadStatusControl<Integer> targetPreloadStatusControl =
rankingData -> new DefaultPreloadManager.Status(STAGE_SOURCE_PREPARED);
MediaSource.Factory mockMediaSourceFactory = mock(MediaSource.Factory.class);