From 0aaf51404272e0b16d0e670d469ece0730412c35 Mon Sep 17 00:00:00 2001 From: Dimitrios Apostolou Date: Tue, 4 Jan 2022 17:41:47 +0100 Subject: [PATCH] Wait for android emulator to be up and running properly Pick-to: 6.3 6.2 Fixes: QTQAINFRA-4681 Change-Id: Icfa75b6982964970172726379e5a2a2bb640f8bf Reviewed-by: Daniel Smith --- util/android/android_emulator_launcher.sh | 52 ++++++++++++++++++----- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/util/android/android_emulator_launcher.sh b/util/android/android_emulator_launcher.sh index fe8b164b9b..92f78f64b2 100755 --- a/util/android/android_emulator_launcher.sh +++ b/util/android/android_emulator_launcher.sh @@ -31,7 +31,6 @@ set -ex -EMULATOR_TIMEOUT=30 EMULATOR_MAX_RETRIES=5 EMULATOR_EXEC="$ANDROID_SDK_ROOT/emulator/emulator" ADB_EXEC="$ANDROID_SDK_ROOT/platform-tools/adb" @@ -40,35 +39,66 @@ if [[ -z "${ANDROID_EMULATOR}" ]]; then else EMULATOR_NAME="$ANDROID_EMULATOR" fi -RESULT=0 + + +function check_for_android_device +{ + $ADB_EXEC devices \ + | awk 'NR==2{print $2}' | grep -qE '^(online|device)$' +} + +function check_if_fully_booted +{ + # The "getprop" command separates lines with \r\n so we trim them + bootanim=` $ADB_EXEC shell getprop init.svc.bootanim | tr -d '\r\n'` + boot_completed=`$ADB_EXEC shell getprop sys.boot_completed | tr -d '\r\n'` + [ "$bootanim" = stopped ] && [ "$boot_completed" = 1 ] +} + for counter in `seq 1 ${EMULATOR_MAX_RETRIES}`; do $ADB_EXEC start-server + + if check_for_android_device + then + echo "Emulator is already running but it shouldn't be. Aborting\!" + exit 3 + fi + echo "Starting emulator, try ${counter}/${EMULATOR_MAX_RETRIES}" $EMULATOR_EXEC $EMULATOR_NAME -gpu swiftshader_indirect -no-audio -partition-size 4096 -cores 4 -memory 3500 -no-snapshot-load -no-snapshot-save &>/dev/null & emulator_pid=$! - # Give emulator time to start - sleep $EMULATOR_TIMEOUT + $ADB_EXEC wait-for-device - emulator_status=`$ADB_EXEC devices | tail -n -2 | awk '{print $2}'` + # Wait about one minute for the emulator to come up + emulator_status=down + for i in `seq 60` + do + sleep 1 + if check_for_android_device && check_if_fully_booted + then + emulator_status=up + break + fi + done # If emulator status is still offline after timeout period, # we can assume it's stuck, and we must restart it - if [[ $emulator_status == 'online' || $emulator_status == 'device' ]]; then + if [ $emulator_status = up ] + then echo "Emulator started successfully" break else if [ $counter -lt $EMULATOR_MAX_RETRIES ]; then - echo "Emulator failed to start, forcefully killing current instance" + echo "Emulator failed to start, forcefully killing current instance and re-starting emulator" kill $emulator_pid || true sleep 5 elif [ $counter -eq $EMULATOR_MAX_RETRIES ]; then - echo "Emulator failed to start, reached maximum number of retries" - RESULT=-1 - break + echo "Emulator failed to start, reached maximum number of retries. Aborting\!" + exit 2 fi fi done -exit $RESULT +exit 0