|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# check-server-ready-helper.sh — guard against bare server-ready sleeps |
| 4 | +# in the hooks_* integration tests (TASK-075). |
| 5 | +# |
| 6 | +# TASK-049 introduced wait_for_server_ready (now extracted to |
| 7 | +# test/integ/server_ready.hpp) to replace the fixed 50 ms sleep used as a |
| 8 | +# server-ready wait in test/integ/hooks_*.cpp. The old pattern races with |
| 9 | +# MHD_start_daemon on loaded CI runners and causes intermittent |
| 10 | +# CURLE_COULDNT_CONNECT failures. TASK-075 propagated the helper to every |
| 11 | +# affected file; this gate prevents the regression from reappearing. |
| 12 | +# |
| 13 | +# Watched files: every test/integ/hooks_*.cpp at scan time. Discovery is |
| 14 | +# dynamic so newly-added hooks_*.cpp files are automatically gated |
| 15 | +# without updating a static list. |
| 16 | +# |
| 17 | +# Detection logic: |
| 18 | +# 1. Any line matching `std::this_thread::sleep_for(.*ms)` or |
| 19 | +# `std::this_thread::sleep_for(std::chrono::milliseconds(...))` in a |
| 20 | +# watched file is a candidate violation. Whitespace before the call |
| 21 | +# is allowed; the call must syntactically be at statement scope. |
| 22 | +# 2. A candidate is allowed iff the candidate line OR the previous |
| 23 | +# non-blank line contains the marker |
| 24 | +# // NON-READINESS-SLEEP: <reason> |
| 25 | +# This is the documented escape hatch for legitimate non-readiness |
| 26 | +# sleeps (e.g. a test deliberately exercising connection-timeout |
| 27 | +# semantics). The marker must include a free-text reason after the |
| 28 | +# colon. |
| 29 | +# 3. Any candidate without an allowance marker is reported as a |
| 30 | +# violation. Exit 1 if any violation is found, 0 otherwise. |
| 31 | +# |
| 32 | +# Exit codes: |
| 33 | +# 0 no violations |
| 34 | +# 1 one or more watched files carries a bare server-ready sleep |
| 35 | +set -euo pipefail |
| 36 | + |
| 37 | +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 38 | +cd "$REPO_ROOT" |
| 39 | + |
| 40 | +WATCHED_FILES=() |
| 41 | +while IFS= read -r f; do |
| 42 | + WATCHED_FILES+=("$f") |
| 43 | +done < <(find test/integ -maxdepth 1 -name 'hooks_*.cpp' 2>/dev/null | sort) |
| 44 | + |
| 45 | +echo "check-server-ready-helper: scanning ${#WATCHED_FILES[@]} file(s)" |
| 46 | + |
| 47 | +violations=0 |
| 48 | +# Guard against the empty-array case: bash's `${arr[@]}` is unset rather |
| 49 | +# than empty under `set -u`. The `${arr[@]+"${arr[@]}"}` idiom expands to |
| 50 | +# nothing when the array is empty and to the expected element list |
| 51 | +# otherwise. Without this, an empty test/integ/ directory would crash |
| 52 | +# the gate. |
| 53 | +for file in ${WATCHED_FILES[@]+"${WATCHED_FILES[@]}"}; do |
| 54 | + # Find every candidate sleep_for line. The pattern matches either |
| 55 | + # the .*ms literal form or the std::chrono::milliseconds(...) form, |
| 56 | + # with optional leading whitespace. |
| 57 | + while IFS=: read -r lineno _; do |
| 58 | + # An allow-marker on the candidate line itself, or on the |
| 59 | + # nearest previous non-blank line, exempts the sleep. |
| 60 | + allowed=$(awk -v target="$lineno" ' |
| 61 | + NR == target { |
| 62 | + if ($0 ~ /\/\/[[:space:]]*NON-READINESS-SLEEP:/) { |
| 63 | + print "yes"; exit |
| 64 | + } |
| 65 | + # Look backwards for the nearest non-blank line. |
| 66 | + for (i = target - 1; i >= 1; i--) { |
| 67 | + if (lines[i] !~ /^[[:space:]]*$/) { |
| 68 | + if (lines[i] ~ /\/\/[[:space:]]*NON-READINESS-SLEEP:/) { |
| 69 | + print "yes" |
| 70 | + } else { |
| 71 | + print "no" |
| 72 | + } |
| 73 | + exit |
| 74 | + } |
| 75 | + } |
| 76 | + print "no" |
| 77 | + } |
| 78 | + { lines[NR] = $0 } |
| 79 | + ' "$file") |
| 80 | + |
| 81 | + if [ "$allowed" != "yes" ]; then |
| 82 | + echo " $file:$lineno: bare std::this_thread::sleep_for used as server-ready wait" >&2 |
| 83 | + violations=$((violations + 1)) |
| 84 | + fi |
| 85 | + done < <(grep -nE '^[[:space:]]*std::this_thread::sleep_for\(([^)]*ms\)|std::chrono::milliseconds)' "$file" || true) |
| 86 | +done |
| 87 | + |
| 88 | +if [ "$violations" -gt 0 ]; then |
| 89 | + echo "check-server-ready-helper: FAIL — $violations bare server-ready sleep(s) found" >&2 |
| 90 | + echo " Replace with httpserver_test::wait_for_server_ready(PORT) from test/integ/server_ready.hpp," >&2 |
| 91 | + echo " or add a '// NON-READINESS-SLEEP: <reason>' marker if the sleep is intentionally not a readiness wait." >&2 |
| 92 | + exit 1 |
| 93 | +fi |
| 94 | + |
| 95 | +echo "check-server-ready-helper: PASS — no bare server-ready sleeps in hooks_* integ tests" |
0 commit comments