Skip to content

Commit d257dfc

Browse files
Avoid potentially exception throwing side effects from IdlingResourceTimeoutException being thrown
Interactions with `TestOutputEmitter` can throw, and if it does it suppresses the `IdlingResourceTimeoutException` exception from even being created. Instead of having this side effect in the `IdlingResourceTimeoutException` constructor move the thread dumping out of the constructor and wrap it in a try/catch, attaching any exceptions as suppressed by the `IdlingResourceTimeoutException` exception. In particular this can happen if more than one `TestOutputHandler` is registered. PiperOrigin-RevId: 802381773
1 parent 10bbad0 commit d257dfc

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

espresso/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The following artifacts were released:
1717
**Bug Fixes**
1818

1919
* Replace now-unnecessary reflection from TestLooperManagerCompat when using Android SDK 36 APIs
20+
* Don't suppress AppNotIdleException if dumpThreadStates throws.
2021

2122
**New Features**
2223

espresso/core/java/androidx/test/espresso/AppNotIdleException.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import android.os.Looper;
2222
import androidx.test.espresso.util.StringJoinerKt;
23-
import androidx.test.internal.platform.util.TestOutputEmitter;
2423
import java.util.List;
2524
import java.util.Locale;
2625

@@ -31,7 +30,6 @@ public final class AppNotIdleException extends RuntimeException implements Espre
3130

3231
private AppNotIdleException(String description) {
3332
super(description);
34-
TestOutputEmitter.dumpThreadStates("ThreadState-AppNotIdleException.txt");
3533
}
3634

3735
/**

espresso/core/java/androidx/test/espresso/IdlingPolicy.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static androidx.test.internal.util.Checks.checkNotNull;
2121

2222
import android.util.Log;
23+
import androidx.test.internal.platform.util.TestOutputEmitter;
2324
import java.util.List;
2425
import java.util.concurrent.TimeUnit;
2526

@@ -58,7 +59,14 @@ public TimeUnit getIdleTimeoutUnit() {
5859
public void handleTimeout(List<String> busyResources, String message) {
5960
switch (errorHandler) {
6061
case THROW_APP_NOT_IDLE:
61-
throw AppNotIdleException.create(busyResources, message);
62+
AppNotIdleException appNotIdleException =
63+
AppNotIdleException.create(busyResources, message);
64+
try {
65+
TestOutputEmitter.dumpThreadStates("ThreadState-AppNotIdleException.txt");
66+
} catch (RuntimeException e) {
67+
appNotIdleException.addSuppressed(e);
68+
}
69+
throw appNotIdleException;
6270
case THROW_IDLE_TIMEOUT:
6371
throw new IdlingResourceTimeoutException(busyResources);
6472
case LOG_ERROR:

0 commit comments

Comments
 (0)