Skip to content

Commit 473b64d

Browse files
isarkiscopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 757805978
1 parent 9d8e300 commit 473b64d

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

src/java/com/google/devtools/mobileharness/platform/android/instrumentation/AndroidInstrumentationUtil.java

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
import com.google.devtools.mobileharness.shared.util.command.Command;
4747
import com.google.devtools.mobileharness.shared.util.command.CommandException;
4848
import com.google.devtools.mobileharness.shared.util.command.CommandExecutor;
49+
import com.google.devtools.mobileharness.shared.util.concurrent.retry.RetryException;
50+
import com.google.devtools.mobileharness.shared.util.concurrent.retry.RetryStrategy;
51+
import com.google.devtools.mobileharness.shared.util.concurrent.retry.RetryingCallable;
4952
import com.google.devtools.mobileharness.shared.util.file.local.LocalFileUtil;
5053
import com.google.devtools.mobileharness.shared.util.file.local.ResUtil;
5154
import com.google.devtools.mobileharness.shared.util.path.PathUtil;
@@ -177,6 +180,11 @@ public interface AppInstallFinishHandler {
177180
private static final String TEST_ARG_GEN_FILE_DIR_RELATIVE_PATH =
178181
DirCommon.KEY_NAME_OF_TEST_GEN_FILE_DIR_RELATIVE_PATH;
179182

183+
/* Device reconnect configuration parameters. */
184+
private static final int DEVICE_RECONNECT_MAX_RETRIES = 3;
185+
private static final Duration DEVICE_RECONNECT_INITIAL_DELAY = Duration.ofSeconds(5);
186+
private static final int DEVICE_RECONNECT_DELAY_MULTIPLIER = 3;
187+
180188
/** Android SDK ADB command line tools executor. */
181189
private final Adb adb;
182190

@@ -1029,14 +1037,34 @@ public void pullInstrumentationFilesFromDevice(
10291037
throws InterruptedException {
10301038
// b/132998532. Device may be disconnected within instrumentation test, so ensure device is
10311039
// online before pulling files from it.
1040+
boolean isDeviceOnline = false;
10321041
try {
1033-
boolean isDeviceOnline;
1034-
try {
1035-
isDeviceOnline = systemStateManager.isOnline(deviceId);
1036-
} catch (MobileHarnessException e) {
1037-
throw new MobileHarnessException(
1038-
AndroidErrorId.ANDROID_INSTRUMENTATION_GET_ONLINE_DEVICES_ERROR, e.getMessage(), e);
1039-
}
1042+
isDeviceOnline =
1043+
RetryingCallable.newBuilder(
1044+
() -> {
1045+
boolean isOnline = false;
1046+
try {
1047+
isOnline = systemStateManager.isOnline(deviceId);
1048+
} catch (MobileHarnessException e) {
1049+
throw new MobileHarnessException(
1050+
AndroidErrorId.ANDROID_INSTRUMENTATION_GET_ONLINE_DEVICES_ERROR,
1051+
e.getMessage(),
1052+
e);
1053+
}
1054+
if (!isOnline) {
1055+
throw new MobileHarnessException(
1056+
AndroidErrorId.ANDROID_INSTRUMENTATION_GET_ONLINE_DEVICES_ERROR,
1057+
String.format("Device %s is not online", deviceId));
1058+
}
1059+
return true;
1060+
},
1061+
RetryStrategy.exponentialBackoff(
1062+
DEVICE_RECONNECT_INITIAL_DELAY,
1063+
DEVICE_RECONNECT_DELAY_MULTIPLIER,
1064+
DEVICE_RECONNECT_MAX_RETRIES))
1065+
.setPredicate(e -> e instanceof MobileHarnessException)
1066+
.build()
1067+
.call();
10401068
if (isDeviceOnline) {
10411069
processInstrumentOutputFiles(testInfo, deviceId, externalStoragePath);
10421070
processInstrumentPropertyFiles(testInfo, deviceId, externalStoragePath);
@@ -1053,14 +1081,21 @@ public void pullInstrumentationFilesFromDevice(
10531081
.log()
10541082
.atInfo()
10551083
.alsoTo(logger)
1056-
.log("Device %s is not online, skip pulling instrumentation files from it", deviceId);
1084+
.log(
1085+
"Device %s is not online after %d retries, skip pulling instrumentation files from"
1086+
+ " it",
1087+
deviceId, DEVICE_RECONNECT_MAX_RETRIES);
10571088
}
1058-
} catch (MobileHarnessException e) {
1089+
} catch (RetryException | MobileHarnessException e) {
10591090
testInfo
10601091
.warnings()
10611092
.addAndLog(
10621093
new MobileHarnessException(
1063-
AndroidErrorId.ANDROID_INSTRUMENTATION_PULL_FILE_ERROR, e.getMessage(), e),
1094+
AndroidErrorId.ANDROID_INSTRUMENTATION_PULL_FILE_ERROR,
1095+
String.format(
1096+
"Failed to check if device %s is online after %d retries",
1097+
deviceId, DEVICE_RECONNECT_MAX_RETRIES),
1098+
e),
10641099
logger);
10651100
}
10661101
}

src/java/com/google/devtools/mobileharness/platform/android/instrumentation/BUILD

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ java_library(
4747
"//src/java/com/google/devtools/mobileharness/shared/util/auto:auto_value",
4848
"//src/java/com/google/devtools/mobileharness/shared/util/base",
4949
"//src/java/com/google/devtools/mobileharness/shared/util/command",
50+
"//src/java/com/google/devtools/mobileharness/shared/util/concurrent/retry",
5051
"//src/java/com/google/devtools/mobileharness/shared/util/file/local",
5152
"//src/java/com/google/devtools/mobileharness/shared/util/file/local:res_util",
5253
"//src/java/com/google/devtools/mobileharness/shared/util/logging:google_logger",
@@ -60,8 +61,6 @@ java_library(
6061
"//src/java/com/google/wireless/qa/mobileharness/shared/constant:property",
6162
"//src/java/com/google/wireless/qa/mobileharness/shared/model/job",
6263
"//src/java/com/google/wireless/qa/mobileharness/shared/model/job/out",
63-
"//src/java/com/google/wireless/qa/mobileharness/shared/model/job/util:result_util",
64-
"//src/java/com/google/wireless/qa/mobileharness/shared/proto:job_java_proto",
6564
"//src/java/com/google/wireless/qa/mobileharness/shared/proto/spec:android_instrumentation_spec_java_proto",
6665
"@maven//:androidx_test_services_storage",
6766
"@maven//:com_google_code_findbugs_jsr305",

0 commit comments

Comments
 (0)