diff --git a/bin/omarchy-toggle-hybrid-gpu b/bin/omarchy-toggle-hybrid-gpu index 7a85d7e1..aa7c0582 100755 --- a/bin/omarchy-toggle-hybrid-gpu +++ b/bin/omarchy-toggle-hybrid-gpu @@ -22,7 +22,20 @@ CONF sudo systemctl enable --now supergfxd fi -gpu_mode=$(supergfxctl -g) +gpu_mode="" +for attempt in {1..3}; do + if gpu_mode=$(timeout --kill-after=1s 3s supergfxctl -g 2>/dev/null) && [[ -n $gpu_mode ]]; then + break + fi + + gpu_mode="" + (( attempt < 3 )) && sleep 1 +done + +if [[ -z $gpu_mode ]]; then + echo "supergfxd is not responding. Try again, or check: systemctl status supergfxd" >&2 + exit 1 +fi case "$gpu_mode" in "Integrated") diff --git a/test/shell.d/hybrid-gpu-test.sh b/test/shell.d/hybrid-gpu-test.sh new file mode 100644 index 00000000..9bfc80e4 --- /dev/null +++ b/test/shell.d/hybrid-gpu-test.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +source "$(dirname "$0")/base-test.sh" + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +fake_bin="$test_tmp/bin" +mkdir -p "$fake_bin" + +cat >"$fake_bin/omarchy-cmd-missing" <<'STUB' +#!/bin/bash +exit 1 +STUB + +cat >"$fake_bin/sleep" <<'STUB' +#!/bin/bash +: +STUB + +cat >"$fake_bin/gum" <<'STUB' +#!/bin/bash +exit 1 +STUB + +cat >"$fake_bin/supergfxctl" <<'STUB' +#!/bin/bash +attempts_file="$TEST_TMP/attempts" +attempts=0 +[[ -f $attempts_file ]] && attempts=$(<"$attempts_file") +attempts=$((attempts + 1)) +printf '%s\n' "$attempts" >"$attempts_file" + +if (( attempts < ${SUCCEED_ON_ATTEMPT:-999} )); then + exit 1 +fi + +echo Hybrid +STUB + +chmod +x "$fake_bin"/* + +TEST_TMP="$test_tmp" SUCCEED_ON_ATTEMPT=3 \ + PATH="$fake_bin:$PATH" bash "$ROOT/bin/omarchy-toggle-hybrid-gpu" >/dev/null + +[[ $(<"$test_tmp/attempts") == "3" ]] || fail "hybrid GPU mode query retries transient failures" +pass "hybrid GPU mode query recovers from a transient supergfxd failure" + +rm -f "$test_tmp/attempts" + +set +e +error=$( + TEST_TMP="$test_tmp" \ + PATH="$fake_bin:$PATH" bash "$ROOT/bin/omarchy-toggle-hybrid-gpu" 2>&1 >/dev/null +) +status=$? +set -e + +(( status != 0 )) || fail "hybrid GPU mode query fails when supergfxd stays unavailable" +[[ $(<"$test_tmp/attempts") == "3" ]] || fail "hybrid GPU mode query stops after three attempts" +grep -qF 'supergfxd is not responding' <<<"$error" || + fail "hybrid GPU mode query explains how to diagnose supergfxd" "$error" +pass "hybrid GPU mode query fails clearly instead of hanging" + +cat >"$fake_bin/supergfxctl" <<'STUB' +#!/bin/bash +trap '' TERM +/usr/bin/sleep 30 +STUB +chmod +x "$fake_bin/supergfxctl" + +set +e +output=$(TEST_TMP="$test_tmp" PATH="$fake_bin:$PATH" timeout 25s bash "$ROOT/bin/omarchy-toggle-hybrid-gpu" 2>&1) +status=$? +set -e + +(( status != 124 )) || fail "hybrid GPU mode query terminates a blocked client" +(( status != 0 )) || fail "hybrid GPU mode query reports a blocked client as unavailable" +grep -qF 'supergfxd is not responding' <<<"$output" || + fail "hybrid GPU mode query diagnoses a blocked client" "$output" +pass "hybrid GPU mode query kills a client that ignores the timeout signal"