diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml index b60d283f4..141fa780e 100644 --- a/.semaphore/semaphore.yml +++ b/.semaphore/semaphore.yml @@ -25,6 +25,8 @@ global_job_config: value: /home/semaphore/Android/Sdk prologue: commands: + # remember the job start for deadlines inside the job + - export CI_JOB_STARTED_AT=$(date +%s) # use JAVA 21 by default - sem-version java 21 # general settings @@ -129,6 +131,11 @@ blocks: # reduces the risk of starving the processes that keep the job alive. - name: EMULATOR_CORES value: "1" + # Stop the tests 8 minutes before the 60-minute execution_time_limit + # of these jobs. A job stopped by the limit runs no epilogue, so it + # publishes no debug info. A job that fails in time still runs on_fail. + - name: INSTRUMENTATION_TESTS_JOB_DEADLINE_MINUTES + value: "52" prologue: commands: - ./script/ci-monitor-host-resources.sh start diff --git a/script/ci-stream-instrumentation-test-progress.sh b/script/ci-stream-instrumentation-test-progress.sh index b8b3697a0..4a09b8a5c 100755 --- a/script/ci-stream-instrumentation-test-progress.sh +++ b/script/ci-stream-instrumentation-test-progress.sh @@ -258,6 +258,16 @@ formatter_pid="" test_command_pid="" watchdog_pid="" watchdog_failure_file="${EMULATOR_WATCHDOG_FAILURE_FILE:-$HOME/emulator-watchdog.failed}" +# Optional deadline, counted from the job start, for the whole test command. +# A job stopped by Semaphore's execution_time_limit skips its epilogue, so it +# publishes no logcat, screenshots, or host metrics. Stopping the tests earlier +# makes the job fail instead, and on_fail still has time to collect them. +job_started_at="${CI_JOB_STARTED_AT:-}" +job_deadline_minutes="${INSTRUMENTATION_TESTS_JOB_DEADLINE_MINUTES:-}" +job_deadline_at="" +test_command_timeout_exit_code=124 +test_command_kill_after_seconds=30 +test_command_timed_out=false terminate_test_command() { [[ -n "$test_command_pid" ]] || return 0 @@ -297,7 +307,10 @@ cleanup() { print_non_passed_tests "$non_passed_tests_file" - if [[ "$test_command_result" -ne 0 && ! -f "$watchdog_failure_file" && -f "$test_summary_file" ]]; then + # Tests that were never started because of the deadline must not let the + # job pass, even if every test that did run passed. + if [[ "$test_command_result" -ne 0 && "$test_command_timed_out" == false && \ + ! -f "$watchdog_failure_file" && -f "$test_summary_file" ]]; then local PASSED=0 FAILED=0 INCOMPLETE=0 SKIPPED=0 # shellcheck disable=SC1090 source "$test_summary_file" @@ -319,6 +332,30 @@ if ! command -v setsid >/dev/null 2>&1; then exit 1 fi +test_command=("$@") +if [[ -n "$job_started_at" || -n "$job_deadline_minutes" ]]; then + if [[ ! "$job_started_at" =~ ^[0-9]+$ || ! "$job_deadline_minutes" =~ ^[0-9]+$ ]]; then + echo "CI_JOB_STARTED_AT (epoch seconds) and INSTRUMENTATION_TESTS_JOB_DEADLINE_MINUTES" \ + "must both be set to integers to enable the deadline" + exit 2 + fi + + job_deadline_at=$(( job_started_at + job_deadline_minutes * 60 )) + test_time_budget_seconds=$(( job_deadline_at - $(date +%s) )) + if (( test_time_budget_seconds <= 0 )); then + echo "[TEST] No time left before the ${job_deadline_minutes}-minute job deadline; not starting the tests." + test_command_timed_out=true + exit "$test_command_timeout_exit_code" + fi + + echo "[TEST] Tests will be stopped after ${test_time_budget_seconds}s" \ + "(the ${job_deadline_minutes}-minute job deadline)." + # timeout signals its whole process group, which setsid below creates, so + # Gradle and all of its adb children are stopped together. + test_command=(timeout --signal=TERM --kill-after="${test_command_kill_after_seconds}s" \ + "${test_time_budget_seconds}s" "${test_command[@]}") +fi + # AndroidJUnitRunner publishes per-test events with the TestRunner logcat tag. # Keep the original events as an artifact and print concise progress to the CI log. adb logcat -c @@ -336,7 +373,7 @@ rm -f "$watchdog_failure_file" # Give the test command its own process group. If the Android guest becomes # unusable, the watchdog can stop Gradle and all of its adb children together. -setsid "$@" > >(tee -a "$test_command_log_file") 2>&1 & +setsid "${test_command[@]}" > >(tee -a "$test_command_log_file") 2>&1 & test_command_pid=$! ./script/ci-emulator-watchdog.sh "$test_command_pid" & @@ -346,6 +383,14 @@ test_command_result=0 wait "$test_command_pid" || test_command_result=$? test_command_pid="" +# timeout exits with 124 after SIGTERM and 137 after SIGKILL, so the clock is +# a more reliable signal than the exit code. +if [[ -n "$job_deadline_at" && "$test_command_result" -ne 0 ]] && (( $(date +%s) >= job_deadline_at )); then + echo "[TEST] Tests were stopped at the ${job_deadline_minutes}-minute job deadline, so on_fail can publish debug info." + test_command_timed_out=true + test_command_result="$test_command_timeout_exit_code" +fi + # A short Gradle run can finish between watchdog probes. Inspect the completed # command log before stopping the watchdog so device-offline failures cannot be # reported as BUILD SUCCESSFUL with zero executed tests.