#!/bin/bash # Wird von tests/run.sh eingebunden: Erreichbarkeit im Hintergrund prüfen. # # Der Fall: eine Box ist weg. Der Router soll das merken, bevor die erste Anfrage # daran hängen bleibt — sie für eine Weile offline nehmen und danach von selbst wieder # nachsehen, statt bei jeder Anfrage erneut in den Zeitablauf zu laufen. echo echo "== 38. Erreichbarkeit im Hintergrund (offline, dann wieder prüfen) ==" PP=$((PORT+222)) P_ALIVE=$((PORT+223)) P_DEAD=$((PORT+224)) tests/stub $P_ALIVE "ALIVE" > "$TMP/probe_stub.log" 2>&1 & PIDS+=($!) sleep 0.4 cat > "$TMP/probe.conf" < "$TMP/probe.log" 2>&1 & PIDS+=($!) wait_port "$PP" 10 || bad "the probe instance did not come up" # Zwei Intervalle abwarten: der tote Endpunkt muss ohne jede Anfrage auffallen. log_has "$TMP/probe.log" "probe_offline endpoint=dead" \ "a dead endpoint is noticed without a request being sent to it" dead_off=$(curl -s -m 5 "http://127.0.0.1:$PP/status" | python3 -c " import sys,json d=json.load(sys.stdin) for e in d['endpoints']: if e['name']=='dead': print(e['offline_s'], e['probe_ok'])") set -- $dead_off if [ "${1:-0}" -gt 0 ] && [ "${2:-1}" = "0" ]; then ok "it is offline for a while (${1}s left) and marked as not answering" else bad "/status does not show it offline (offline_s=$1 probe_ok=$2)" fi # Die Anfrage geht am toten Ziel vorbei, ohne dessen Zeitablauf zu bezahlen. read -r pcode ptime < <(curl -s -o /dev/null -m 15 -w '%{http_code} %{time_total}' \ -X POST -H 'Content-Type: application/json' \ -d '{"model":"m","messages":[{"role":"user","content":"x"}],"max_tokens":3}' \ "http://127.0.0.1:$PP/v1/chat/completions") if [ "$pcode" = "200" ] && awk "BEGIN{exit !($ptime < 2.0)}"; then ok "the request skips the offline target without waiting for it (${ptime}s)" else bad "the request still paid for the dead target: http=$pcode after ${ptime}s" fi # Jetzt kommt die Box zurück: die Prüfung muss sie von selbst wieder aufnehmen. tests/stub $P_DEAD "REVIVED" > "$TMP/probe_revived.log" 2>&1 & PIDS+=($!) log_has "$TMP/probe.log" "probe_recovered endpoint=dead" \ "the returning endpoint is picked up by the next check, not by a failure" revived=$(curl -s -m 5 "http://127.0.0.1:$PP/status" | python3 -c " import sys,json d=json.load(sys.stdin) for e in d['endpoints']: if e['name']=='dead': print(e['probe_ok'], int(e['offline_s']))") set -- $revived if [ "${1:-0}" = "1" ] && [ "${2:-1}" = "0" ]; then ok "it is back: probe_ok=1, no offline hold left" else bad "the endpoint did not come back (probe_ok=$1 offline_s=$2)" fi # Und die Prüfung ist abschaltbar (probe off) — nicht jedes Ziel darf gefragt werden. cat > "$TMP/probe_off.conf" < "$TMP/probe_off.log" 2>&1 & PIDS+=($!) wait_port "$((PORT+225))" 10 || bad "the probe-off instance did not come up" sleep 2.5 if grep -q "probe_offline endpoint=quiet" "$TMP/probe_off.log" 2>/dev/null; then bad "a probe off endpoint was checked anyway" else ok "probe off keeps an endpoint out of the check" fi # base_cmd ist die Adresse, nicht base. opencode serve bindet bei jedem Start einen # neuen Port; eine Prüfung gegen die alte Adresse nimmt den gesunden Dienst offline # und jede Anfrage bekommt 503, obwohl er läuft. echo echo "== 39. Hintergrundprüfung folgt base_cmd ==" PP2=$((PORT+300)) P_DYN=$((PORT+301)) P_STALE=$((PORT+302)) tests/stub $P_DYN "DYNAMIC" > "$TMP/probe_dynamic_stub.log" 2>&1 & PIDS+=($!) sleep 0.4 cat > "$TMP/probe_base_cmd.conf" < "$TMP/probe_base_cmd.log" 2>&1 & PIDS+=($!) wait_port "$PP2" 10 || bad "the base_cmd probe instance did not come up" sleep 2.5 dyn_state=$(curl -s -m 5 "http://127.0.0.1:$PP2/status" | python3 -c " import sys,json d=json.load(sys.stdin) for e in d['endpoints']: if e['name']=='dynamic': print(e['probe_ok'], int(e['offline_s']))") set -- $dyn_state if [ "${1:-0}" = "1" ] && [ "${2:-1}" = "0" ]; then ok "the probe checks the address reported by base_cmd" else bad "the probe used the stale base address (probe_ok=$1 offline_s=$2)" fi R=$(curl -s -m 15 -X POST -H 'Content-Type: application/json' \ -d '{"model":"m","messages":[{"role":"user","content":"x"}],"max_tokens":3}' \ "http://127.0.0.1:$PP2/v1/chat/completions") contains "$R" "served by DYNAMIC" "a base_cmd endpoint is not skipped as offline"