11#! /bin/bash
22
3- # Retry on Device Farm API throttling
4- export AWS_MAX_ATTEMPTS=6
3+ # Retry on Device Farm API throttling (~60s total backoff)
4+ export AWS_MAX_ATTEMPTS=8
55
66project_arn=$DEVICEFARM_PROJECT_ARN
77max_devices=$NUMBER_OF_DEVICES_TO_TEST
@@ -20,36 +20,56 @@ if [[ -z "${max_devices}" ]]; then
2020 max_devices=1
2121fi
2222
23- # Function to setup the app uploads in device farm
24- function createUpload {
23+ # Function to setup the app uploads in device farm.
24+ # Retries with random jitter on throttling failures.
25+ function createUploadWithRetry {
2526 test_type=$1
26- upload_response=` aws devicefarm create-upload --type $test_type \
27- --content-type=" application/octet-stream" \
28- --project-arn=" $project_arn " \
29- --name=" $file_name " \
30- --query=" upload.[url, arn]" \
31- --region=" us-west-2" \
32- --output=text`
33- echo $upload_response
27+ max_upload_attempts=3
28+ for upload_attempt in $( seq 1 $max_upload_attempts ) ; do
29+ upload_response=` aws devicefarm create-upload --type $test_type \
30+ --content-type=" application/octet-stream" \
31+ --project-arn=" $project_arn " \
32+ --name=" $file_name " \
33+ --query=" upload.[url, arn]" \
34+ --region=" us-west-2" \
35+ --output=text 2>&1 `
36+ # Check if we got a valid response (URL + ARN)
37+ read -a parts <<< " $upload_response"
38+ if [[ -n " ${parts[1]} " && " ${parts[1]} " == arn:* ]]; then
39+ echo " $upload_response "
40+ return 0
41+ fi
42+ if [ $upload_attempt -lt $max_upload_attempts ]; then
43+ jitter=$(( 30 + RANDOM % 60 ))
44+ echo " [RUN_IN_DEVICEFARM] CreateUpload throttled (attempt $upload_attempt /$max_upload_attempts ). Retrying in ${jitter} s..." >&2
45+ sleep $jitter
46+ fi
47+ done
48+ echo " [RUN_IN_DEVICEFARM] CreateUpload failed after $max_upload_attempts attempts" >&2
49+ echo " "
50+ return 1
3451}
3552
3653echo ' Uploading test package'
37- # Create an upload for the instrumentation test package
38- read -a result <<< $( createUpload " INSTRUMENTATION_TEST_PACKAGE" )
54+ read -a result <<< $( createUploadWithRetry " INSTRUMENTATION_TEST_PACKAGE" )
3955test_package_url=${result[0]}
4056test_package_upload_arn=${result[1]}
41- # Upload the apk
57+ if [[ -z " $test_package_upload_arn " ]]; then
58+ echo " Failed to create test package upload (see logs above). Exiting."
59+ exit 1
60+ fi
4261curl -H " Content-Type:application/octet-stream" -T $file_name $test_package_url
4362
44- # Create an upload for the app package (They're the same, but they have to be setup in device farm)
4563echo ' Uploading app package'
46- read -a result <<< $( createUpload " ANDROID_APP" )
64+ read -a result <<< $( createUploadWithRetry " ANDROID_APP" )
4765app_package_url=${result[0]}
4866app_package_upload_arn=${result[1]}
49- # Upload the apk
67+ if [[ -z " $app_package_upload_arn " ]]; then
68+ echo " Failed to create app package upload (see logs above). Exiting."
69+ exit 1
70+ fi
5071curl -H " Content-Type:application/octet-stream" -T $file_name $app_package_url
5172
52- # Wait to make sure the upload completes. This should actually make a get-upload call and check the status.
5373echo " Waiting for uploads to complete"
5474sleep 10
5575
0 commit comments