#!/bin/bash # Wird von tests/run.sh eingebunden: Gewichtung, Balance pro Regel, Zeitlimits. # Erwartet: $TMP, $PORT, $P_GATED, $P_FAIL, ok/bad/contains, ./junction echo "== 19. Gewichtung, Balance pro Regel, Verbindungs-Zeitlimit ==" # Für die Verteilung braucht es ZWEI gesunde Ziele. GATED antwortet; ein zweiter Stub # mit eigenem Namen kommt dazu, weil der vorhandene "FAILING" absichtlich ausfällt und # dann immer das erste Ziel bedient würde. BA=$P_GATED BB=$((PORT+21)) BJ=$((PORT+23)) tests/stub $BB "ZWEIT" > /dev/null 2>&1 & PIDS+=($!) wait_port "$BB" 10 || bad "the weighted stub did not come up" cat > "$TMP/bal.conf" < "$TMP/bal.log" 2>&1 & PIDS+=($!) wait_port "$BJ" 30 || bad "the balance instance did not come up" # 1. Gewichtetes Reihum: 3:1 soll sich in der Verteilung zeigen A=0; B=0 for i in $(seq 1 40); do R=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' \ -d '{"model":"x","messages":[{"role":"user","content":"hi"}]}' \ "http://127.0.0.1:$BJ/v1/chat/completions") case "$R" in *"served by GATED"*) A=$((A+1));; *"served by ZWEIT"*) B=$((B+1));; esac done # Ein Ziel mit Gewicht 3 soll deutlich häufiger drankommen als eines mit Gewicht 1. if [ "$A" -ge 24 ] && [ "$B" -ge 6 ]; then ok "weighted round-robin: $A zu $B bei Gewicht 3:1" else bad "weighted round-robin verteilte $A zu $B, erwartet etwa 30 zu 10" fi # 2. balance first in der Regel schlägt das globale round-robin FIRST=0 for i in $(seq 1 6); do R=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' \ -d '{"model":"starr-modell","messages":[{"role":"user","content":"hi"}]}' \ "http://127.0.0.1:$BJ/v1/chat/completions") case "$R" in *"served by GATED"*) FIRST=$((FIRST+1));; esac done if [ "$FIRST" -eq 6 ]; then ok "a rule's own 'balance first' overrides the global round-robin" else bad "rule-level balance did not override the global mode ($FIRST/6 on the first target)" fi # 3. Verbindungs-Zeitlimit pro Endpunkt: ein unerreichbares Ziel darf die Kette nicht # aufhalten, sondern muss schnell weiterreichen. cat > "$TMP/conn.conf" < "$TMP/conn.log" 2>&1 & PIDS+=($!) wait_port $((PORT + 24)) 30 || bad "the connect-timeout instance did not come up" START=$(date +%s%N) R=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \ -d '{"model":"x","messages":[{"role":"user","content":"hi"}]}' \ "http://127.0.0.1:$((PORT+24))/v1/chat/completions") END=$(date +%s%N) MS=$(( (END-START)/1000000 )) contains "$R" "served by GATED" "the chain moves past an unreachable endpoint" if [ "$MS" -lt 5000 ]; then ok "the per-endpoint connect timeout fired after ${MS} ms, not the global 30 s" else bad "the per-endpoint connect timeout did not apply (${MS} ms)" fi