devbox/internal/daemon/backoff_test.go
shoemoney 38766fdcd5 feat(M7): SSE reconnect backoff + share-name validation 🛡️
- daemon: exponential backoff (1s→30s) + ±25% jitter on SSE reconnect so a fleet
  that drops together doesn't reconnect in lockstep; a long-lived stream resets it
- hub: ValidateShareName rejects path separators, traversal, control/non-ASCII
  chars, reserved Windows device names (CON/NUL/COM1…), and case-clashes with an
  existing share (Foo vs foo would collide on case-insensitive filesystems)
- tests for both
2026-06-23 01:26:04 -05:00

39 lines
989 B
Go

package daemon
import (
"testing"
"time"
)
func TestNextBackoffDoublesAndCaps(t *testing.T) {
// Starts at base, doubles, then saturates at the ceiling.
want := []time.Duration{
1 * time.Second, 2 * time.Second, 4 * time.Second, 8 * time.Second,
16 * time.Second, 30 * time.Second, 30 * time.Second,
}
got := time.Duration(0)
for i, w := range want {
got = nextBackoff(got)
if got != w {
t.Fatalf("step %d: nextBackoff = %s, want %s", i, got, w)
}
}
// A non-positive input restarts at base (used to reset after a healthy stream).
if d := nextBackoff(0); d != backoffBase {
t.Fatalf("reset: nextBackoff(0) = %s, want %s", d, backoffBase)
}
}
func TestJitteredStaysWithinBand(t *testing.T) {
const d = 8 * time.Second
lo, hi := d-d/4, d+d/4 // ±25%
for i := 0; i < 1000; i++ {
j := jittered(d)
if j < lo || j >= hi {
t.Fatalf("jittered(%s) = %s, want in [%s,%s)", d, j, lo, hi)
}
}
if jittered(0) != 0 {
t.Fatal("jittered(0) should be 0")
}
}