46
46
import com .google .devtools .mobileharness .shared .util .command .Command ;
47
47
import com .google .devtools .mobileharness .shared .util .command .CommandException ;
48
48
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 ;
49
52
import com .google .devtools .mobileharness .shared .util .file .local .LocalFileUtil ;
50
53
import com .google .devtools .mobileharness .shared .util .file .local .ResUtil ;
51
54
import com .google .devtools .mobileharness .shared .util .path .PathUtil ;
@@ -177,6 +180,11 @@ public interface AppInstallFinishHandler {
177
180
private static final String TEST_ARG_GEN_FILE_DIR_RELATIVE_PATH =
178
181
DirCommon .KEY_NAME_OF_TEST_GEN_FILE_DIR_RELATIVE_PATH ;
179
182
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
+
180
188
/** Android SDK ADB command line tools executor. */
181
189
private final Adb adb ;
182
190
@@ -1029,14 +1037,34 @@ public void pullInstrumentationFilesFromDevice(
1029
1037
throws InterruptedException {
1030
1038
// b/132998532. Device may be disconnected within instrumentation test, so ensure device is
1031
1039
// online before pulling files from it.
1040
+ boolean isDeviceOnline = false ;
1032
1041
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 ();
1040
1068
if (isDeviceOnline ) {
1041
1069
processInstrumentOutputFiles (testInfo , deviceId , externalStoragePath );
1042
1070
processInstrumentPropertyFiles (testInfo , deviceId , externalStoragePath );
@@ -1053,14 +1081,21 @@ public void pullInstrumentationFilesFromDevice(
1053
1081
.log ()
1054
1082
.atInfo ()
1055
1083
.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 );
1057
1088
}
1058
- } catch (MobileHarnessException e ) {
1089
+ } catch (RetryException | MobileHarnessException e ) {
1059
1090
testInfo
1060
1091
.warnings ()
1061
1092
.addAndLog (
1062
1093
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 ),
1064
1099
logger );
1065
1100
}
1066
1101
}
0 commit comments