#!/usr/bin/env bash

set -euo pipefail

# This script implements a general retry mechanism for a command. On each iteration, parallelism
# flags are halved and the command is retried.

retries="${PULUMI_TEST_RETRIES:-"0"}"
attempts=1
success=false
retried=false

run_tests() {
    export GO_TEST_PARALLELISM="${GO_TEST_PARALLELISM:-"8"}"
    export GO_TEST_PKG_PARALLELISM="${GO_TEST_PKG_PARALLELISM:-"2"}"
    # TODO: https://github.com/pulumi/pulumi/issues/10699
    # Enable running tests with -shuffle on.
    export GO_TEST_SHUFFLE=${GO_TEST_SHUFFLE:-"off"}

    echo "COMMAND     = " "${@}"

    until "${@}"; do
        retried=true
        if [ "${attempts}" -gt "${retries}" ]; then
            echo "::warning Failed after ${attempts} attempts"
            return
        else
            echo "::warning Retrying command"
        fi
        attempts=$((attempts + 1))

        export GO_TEST_PARALLELISM=$((GO_TEST_PARALLELISM <= 2 ? 1 : GO_TEST_PARALLELISM / 2))
        export GO_TEST_PKG_PARALLELISM=$((GO_TEST_PKG_PARALLELISM <= 2 ? 1 : GO_TEST_PKG_PARALLELISM / 2))
        export GO_TEST_SHUFFLE="off"
    done

    success=true
}

run_tests "${@}"

echo "::set-output name=TEST_SUCCESS::${success}"
echo "::set-output name=TEST_RETRIED::${retried}"

if ! "${success}"; then
    exit 1
fi
