#!/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)"