Fix flaky unit tests

PiperOrigin-RevId: 290910992
This commit is contained in:
christosts 2020-01-22 09:47:22 +00:00 committed by Ian Baker
parent d64388a075
commit 78fe1afa85
2 changed files with 8 additions and 2 deletions

View File

@ -290,11 +290,14 @@ public class DedicatedThreadAsyncMediaCodecAdapterTest {
AtomicInteger onCodecStartCount = new AtomicInteger(0);
adapter.setCodecStartRunnable(() -> onCodecStartCount.incrementAndGet());
adapter.start();
// Grab reference to Looper before shutting down the adapter otherwise handlerThread.getLooper()
// might return null.
Looper looper = handlerThread.getLooper();
adapter.flush();
adapter.shutdown();
// Wait until all tasks have been handled.
Shadows.shadowOf(handlerThread.getLooper()).idle();
Shadows.shadowOf(looper).idle();
// Only adapter.start() calls onCodecStart.
assertThat(onCodecStartCount.get()).isEqualTo(1);
}

View File

@ -290,10 +290,13 @@ public class MultiLockAsyncMediaCodecAdapterTest {
AtomicInteger codecStartCalls = new AtomicInteger(0);
adapter.setCodecStartRunnable(() -> codecStartCalls.incrementAndGet());
adapter.start();
// Grab reference to Looper before shutting down the adapter otherwise handlerThread.getLooper()
// might return null.
Looper looper = handlerThread.getLooper();
adapter.flush();
adapter.shutdown();
Shadows.shadowOf(handlerThread.getLooper()).idle();
Shadows.shadowOf(looper).idle();
// Only adapter.start() called codec#start()
assertThat(codecStartCalls.get()).isEqualTo(1);
}