Skip to content

Commit

Permalink
Hardening AndroidJUnitRunner against exceptions that can be thrown wh…
Browse files Browse the repository at this point in the history
…en there are problems in the underlying test libraries.

PiperOrigin-RevId: 664908801
  • Loading branch information
copybara-androidxtest committed Aug 19, 2024
1 parent dd2b59a commit 2c9f475
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions runner/android_junit_runner/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
**Bug Fixes**

* Exceptions during `@AfterClass` were not being reported via `InstrumentationResultPrinter`.
* Exceptions arising in AndroidJUnitRunner.buildRequest are now handled.

**New Features**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,12 @@ public void onStart() {
try {
setJsBridgeClassName("androidx.test.espresso.web.bridge.JavaScriptBridge");
super.onStart();
Request testRequest = buildRequest(runnerArgs, getArguments());
Request testRequest = null;
try {
testRequest = buildRequest(runnerArgs, getArguments());
} catch (Throwable t) {
onException(this, t);
}

if (runnerArgs.remoteMethod != null) {
try {
Expand Down Expand Up @@ -623,6 +628,12 @@ public boolean onException(Object obj, Throwable e) {
final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
try {
instResultPrinter.reportProcessCrash(e);
} catch (Throwable t) {
// It is possible for the test infrastructure to get sufficiently messed up that even this
// can fail. Don't let that get in the way of sending events reporting the problems, since
// that can make the difference between "test failed with a useful error message" and "test
// mysteriously timed out and the developer has to go spelunking in the system logcat".
Log.e(LOG_TAG, "Failed to report process crash.", t);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
Expand Down
1 change: 1 addition & 0 deletions services/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
**Bug Fixes**

* TestStorage: Use input directory location for internal files
* StackTrimmer: harden against exceptions coming from Failure.getMessage().

**New Features**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ public static String getTrimmedStackTrace(Failure failure) {
}

public static String getTrimmedMessage(Failure failure) {
String message = failure.getMessage();
String message = null;
try {
// Even this can fail! If so, don't let it wreck error reporting.
message = failure.getMessage();
} catch (Throwable t) {
Log.e(TAG, "Failed to get message from " + failure.toString(), t);
message = "Couldn't get message due to " + t.getMessage();
}
if (message != null && message.length() > MAX_TRACE_SIZE) {
// Since AJUR needs to report failures back to AM via a binder IPC, we need to make sure that
// we don't exceed the Binder transaction limit - which is 1MB per process.
Expand Down

0 comments on commit 2c9f475

Please sign in to comment.