Skip to content

Commit c14c060

Browse files
rohitjoinscopybara-github
authored andcommitted
Fix flaky MetadataRetrieverTest by draining loopers in tearDown()
Ensures asynchronous cleanup messages are processed before each test ends, preventing state leakage between tests. Catches `IllegalStateException` in case a `looper` is already quitting when `idle()` is called. #cherrypick PiperOrigin-RevId: 781060600
1 parent a976752 commit c14c060

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

libraries/exoplayer/src/test/java/androidx/media3/exoplayer/MetadataRetrieverTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import static org.mockito.ArgumentMatchers.any;
2525
import static org.mockito.Mockito.mock;
2626
import static org.mockito.Mockito.when;
27+
import static org.robolectric.Shadows.shadowOf;
2728

2829
import android.content.Context;
2930
import android.net.Uri;
31+
import android.os.Looper;
3032
import androidx.media3.common.C;
3133
import androidx.media3.common.MediaItem;
3234
import androidx.media3.common.Metadata;
@@ -58,6 +60,7 @@
5860
import org.junit.Test;
5961
import org.junit.runner.RunWith;
6062
import org.robolectric.annotation.LooperMode;
63+
import org.robolectric.shadows.ShadowLooper;
6164

6265
/** Tests for {@link MetadataRetriever}. */
6366
@LooperMode(LooperMode.Mode.INSTRUMENTATION_TEST)
@@ -77,6 +80,15 @@ public void setUp() throws Exception {
7780

7881
@After
7982
public void tearDown() throws Exception {
83+
// Drain loopers to ensure async cleanup tasks complete before the test ends.
84+
// This prevents state leakage between tests.
85+
for (Looper looper : ShadowLooper.getAllLoopers()) {
86+
try {
87+
shadowOf(looper).idle();
88+
} catch (IllegalStateException e) {
89+
// Looper was already quit, safe to ignore.
90+
}
91+
}
8092
MetadataRetriever.setMaximumParallelRetrievals(
8193
MetadataRetriever.DEFAULT_MAXIMUM_PARALLEL_RETRIEVALS);
8294
}

0 commit comments

Comments
 (0)