Add timeout to CountDownLatch awaits

This is to avoid tests hanging forever

PiperOrigin-RevId: 665826507
This commit is contained in:
kimvde 2024-08-21 04:59:44 -07:00 committed by Copybara-Service
parent 351593a250
commit 059ad62377
2 changed files with 22 additions and 6 deletions

View File

@ -22,6 +22,7 @@ import static androidx.media3.transformer.AndroidTestUtil.PNG_ASSET;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static com.google.common.collect.Iterables.getLast;
import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import android.content.Context;
import android.view.SurfaceView;
@ -46,6 +47,7 @@ import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.After;
import org.junit.Before;
@ -480,8 +482,10 @@ public class CompositionPlayerSeekTest {
latch = new CountDownLatch(count);
}
public void await() throws InterruptedException {
latch.await();
public void await() throws InterruptedException, TimeoutException {
if (!latch.await(TEST_TIMEOUT_MS, MILLISECONDS)) {
throw new TimeoutException();
}
}
public void countDown() {

View File

@ -48,6 +48,7 @@ import static androidx.media3.transformer.Transformer.PROGRESS_STATE_UNAVAILABLE
import static androidx.media3.transformer.Transformer.PROGRESS_STATE_WAITING_FOR_AVAILABILITY;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
@ -122,6 +123,9 @@ import org.robolectric.shadows.ShadowMediaCodec;
*/
@RunWith(AndroidJUnit4.class)
public final class MediaItemExportTest {
private static final long TEST_TIMEOUT_SECONDS = 10;
@Rule public final TemporaryFolder outputDir = new TemporaryFolder();
private final Context context = ApplicationProvider.getApplicationContext();
@ -1079,7 +1083,9 @@ public final class MediaItemExportTest {
countDownLatch.countDown();
}
});
countDownLatch.await();
if (!countDownLatch.await(TEST_TIMEOUT_SECONDS, SECONDS)) {
throw new TimeoutException();
}
assertThat(exception.get()).isNull();
DumpFileAsserts.assertOutput(
@ -1109,7 +1115,9 @@ public final class MediaItemExportTest {
countDownLatch.countDown();
}
});
countDownLatch.await();
if (!countDownLatch.await(TEST_TIMEOUT_SECONDS, SECONDS)) {
throw new TimeoutException();
}
assertThat(illegalStateException.get()).isNotNull();
}
@ -1422,7 +1430,9 @@ public final class MediaItemExportTest {
countDownLatch.countDown();
}
});
countDownLatch.await();
if (!countDownLatch.await(TEST_TIMEOUT_SECONDS, SECONDS)) {
throw new TimeoutException();
}
assertThat(illegalStateException.get()).isNotNull();
}
@ -1529,7 +1539,9 @@ public final class MediaItemExportTest {
countDownLatch.countDown();
}
});
countDownLatch.await();
if (!countDownLatch.await(TEST_TIMEOUT_SECONDS, SECONDS)) {
throw new TimeoutException();
}
assertThat(illegalStateException.get()).isNotNull();
}