Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only drain main looper when it is paused. #2320

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions espresso/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The following artifacts were released:

**Bug Fixes**

* Fix deadlock in espresso in Robolectric INSTRUMENTATION_TEST + paused looper.

**New Features**

**Breaking Changes**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.Nullable;
import androidx.test.internal.platform.ServiceLoaderWrapper;
import androidx.test.internal.platform.os.ControlledLooper;
import java.lang.reflect.Field;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
Expand All @@ -42,6 +44,10 @@ final class ThreadPoolExecutorExtractor {
private static final String MODERN_ASYNC_TASK_FIELD_NAME = "THREAD_POOL_EXECUTOR";
private final Handler mainHandler;

private final ControlledLooper controlledLooper =
ServiceLoaderWrapper.loadSingleService(
ControlledLooper.class, () -> ControlledLooper.NO_OP_CONTROLLED_LOOPER);

@Inject
ThreadPoolExecutorExtractor(Looper looper) {
mainHandler = new Handler(looper);
Expand Down Expand Up @@ -85,6 +91,7 @@ public void run() {
}
});
try {
controlledLooper.drainMainThreadUntilIdle();
latch.await();
} catch (InterruptedException ie) {
if (!futureToRun.isDone()) {
Expand Down
Loading