warpgate/tests/10-scheduled/test-adaptive-bandwidth.sh
grabbit faf9d80824 feat: fill implementation gaps — preset unification, cron, adaptive bw, update cmd, tests
Step 1 — Unify preset logic (eliminate dual implementation)
- src/cli/preset.rs: add missing fields (chunk_limit, multi_thread_streams,
  multi_thread_cutoff), fix Office buffer_size 64M→128M, implement FromStr
- src/web/api.rs: post_preset() now calls Preset::apply() — no more inlined
  params; Office write_back unified to 5s (was 3s in API)

Step 2 — Fix setup.rs connection test: warn→bail
- All 4 "Warning: Could not connect/resolve" prints replaced with anyhow::bail!
  matching deploy/setup.rs behavior

Step 3 — Web UI: add [web] and [notifications] edit sections
- templates/web/tabs/config.html: new collapsible Web UI (password) and
  Notifications (webhook_url, cache_threshold_pct, nas_offline_minutes,
  writeback_depth) sections, both tagged "No restart"
- Also adds [log] section (file path + level select, "Full restart")

Step 4 — Full cron expression support in warmup scheduler
- Cargo.toml: add cron = "0.12", chrono = "0.4"
- supervisor.rs: normalize_cron_schedule() converts 5-field standard cron to
  7-field cron crate format; replaces naive hour-only matching

Step 5 — Adaptive bandwidth algorithm
- supervisor.rs: extract compute_adaptive_limit() pure function; sliding
  window of 6 samples, cv>0.3→congested (−25%, floor 1MiB/s), stable
  near-limit→maintain, under-utilizing→+10% (capped at limit_up)

Step 6 — warpgate update command
- src/cli/update.rs: query GitHub Releases API, compare with CARGO_PKG_VERSION
- src/main.rs: add Update{apply}, SetupWifi, CloneMac{interface} commands
- src/cli/wifi.rs: TODO stub for WiFi AP setup

Unit tests (+35, total 188→223)
- cli/preset.rs: 10 tests — FromStr, all fields for each preset, idempotency,
  connection/share isolation, write_back consistency regression
- supervisor.rs: 14 tests — normalize_cron_schedule (5 cases),
  compute_adaptive_limit (9 cases: congestion, floor, stable, under-utilizing,
  cap, zero-current, zero-max, empty window)
- config.rs: 11 tests — WebConfig (3), NotificationsConfig (4), LogConfig (4)

Shell tests (+4 scripts)
- tests/09-cli/test-preset-cli.sh: preset CLI without daemon; checks all
  three presets write correct values including unified buffer_size/write_back
- tests/09-cli/test-update-command.sh: update command; skips on no-network
- tests/10-scheduled/test-cron-warmup-schedule.sh: "* * * * *" fires in <90s
- tests/10-scheduled/test-adaptive-bandwidth.sh: adaptive loop stability
- tests/harness/config-gen.sh: add warmup.warmup_schedule override support
- tests/run-all.sh: add 10-scheduled category

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-19 16:55:00 +08:00

70 lines
2.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# Test: adaptive bandwidth throttling engages and adjusts the bwlimit.
#
# Strategy:
# 1. Configure a small limit_up (e.g. 5M) with adaptive=true.
# 2. Start warpgate and induce steady write-back traffic by writing files
# that need syncing to the NAS.
# 3. Wait for the supervisor's adaptive window to fill (6 × 2 s = 12 s).
# 4. Query /core/bwlimit via RC API on the rclone port and verify the
# limit has been adjusted from the original configured value.
#
# Requires: root (for FUSE mounts), mock NAS.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/../harness/helpers.sh"
source "$SCRIPT_DIR/../harness/mock-nas.sh"
require_root
setup_test_env
trap teardown_test_env EXIT
start_mock_nas
# Configure with a low upload limit and adaptive=true
gen_config \
"bandwidth.limit_up=5M" \
"bandwidth.adaptive=true" \
"writeback.write_back=1s"
start_warpgate
wait_for_mount 60
wait_for_rc_api 30
# Write several files to trigger write-back traffic
for i in $(seq 1 20); do
dd if=/dev/urandom of="$TEST_MOUNT/adaptive-test-$i.bin" bs=512K count=1 2>/dev/null
done
# Give the supervisor enough cycles for the adaptive window to fill:
# ADAPTIVE_WINDOW_SIZE=6 samples × POLL_INTERVAL=2s = ~12s minimum + margin
sleep 20
# Check for adaptive log line
if grep -q "Adaptive bwlimit adjusted" "$TEST_DIR/warpgate.log" 2>/dev/null; then
echo "# Adaptive adjustment logged"
else
# Even if the limit wasn't adjusted (traffic may be 0 without real NAS
# write-back happening), the supervisor must not have crashed.
if ! kill -0 "$WARPGATE_PID" 2>/dev/null; then
echo "FAIL: warpgate crashed during adaptive bandwidth test"
exit 1
fi
echo "# No adaptive adjustment this run (traffic level may have been stable)"
fi
# Confirm the supervisor is still alive
if ! kill -0 "$WARPGATE_PID" 2>/dev/null; then
echo "FAIL: warpgate is not running after adaptive bandwidth test"
exit 1
fi
# Confirm no panic in logs
if grep -q "panicked at\|thread.*panicked" "$TEST_DIR/warpgate.log" 2>/dev/null; then
echo "FAIL: panic detected in warpgate log"
grep "panicked" "$TEST_DIR/warpgate.log" | head -5
exit 1
fi
echo "PASS: $(basename "$0" .sh)"