#!/bin/sh # # splinter -- a usage limit is waited out, not reported as a failure. # # The fake model refuses the first request with 429 and Retry-After: 2. The # run must say when it goes on, ask again and print the answer; with # on_limit = fail it must fail instead. set -u cd "$(dirname "$0")/.." || exit 1 . "$(dirname "$0")/sh/port.sh" D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-lw-XXXXXX") || exit 1 P=$(free_port) trap 'kill $F 2>/dev/null; rm -rf "$D"' EXIT fails=0 check() { d=$1; shift; if "$@"; then :; else echo "FAIL $d"; fails=$((fails + 1)); fi; } mkdir -p "$D/home" python3 -I tests/fake_openai_limit.py "$P" "$D/req.log" & F=$! for i in $(seq 1 50); do curl -s -o /dev/null "http://127.0.0.1:$P/x" && break sleep 0.2 done HOME="$D/home" SPLINTER_USAGE_DIR="$D/usage" SPLINTER_COOLDOWN_DIR="$D/cd" \ ./splinter --no-config --no-stream --endpoint "http://127.0.0.1:$P/v1/chat/completions" \ --model m -p "hallo" "$D/out.txt" 2> "$D/err.txt" check "the answer arrives after the wait" grep -q 'weiter gemacht' "$D/out.txt" check "it said when it goes on" grep -q 'usage limit reached' "$D/err.txt" check "the request was repeated" test "$(grep -c '^request' "$D/req.log")" -ge 2 # with on_limit = fail the first refusal ends the run rm -f "$D/req.log" "$D/cd"/* kill $F 2>/dev/null; wait $F 2>/dev/null python3 -I tests/fake_openai_limit.py "$P" "$D/req.log" & F=$! for i in $(seq 1 50); do curl -s -o /dev/null "http://127.0.0.1:$P/x" && break sleep 0.2 done printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\non_limit = fail\n' "$P" > "$D/home/splinter.conf" HOME="$D/home" SPLINTER_USAGE_DIR="$D/usage" SPLINTER_COOLDOWN_DIR="$D/cd" \ ./splinter --config "$D/home/splinter.conf" --no-stream -p "hallo" \ "$D/out2.txt" 2> "$D/err2.txt" check "on_limit = fail does not wait" test ! -s "$D/out2.txt" check "on_limit = fail says why" grep -q 'status 429' "$D/err2.txt" if [ "$fails" -eq 0 ]; then echo "test_limit_wait: ok"; exit 0; fi cat "$D/err.txt" "$D/err2.txt" exit 1