tests: fix two CI flakes (analytics visitors, demo spool log)
- TestMeterCountsAndDrains pins the day salt: with a random salt its two visitors shared one of 2,048 HLL registers ~1 run in 2,048 and the estimate read 1.00 (discussion 663). - TestDemoJobSpoolRecoveryInvalidPath checks only its own job's log lines for durability: another test's goroutine logged into the process-wide logger on CI. Test-only; census clean. Squashed from fix-meter-visitors-flake into main.
2 files changed,
+22
−2
internal/analytics/meter_test.go+18 −1internal/console/demo_spool_test.go+4 −1
CI passed — run details
diff --git a/internal/analytics/meter_test.go b/internal/analytics/meter_test.go| index a485da65e..e719a13ee 100644 |
| --- a/internal/analytics/meter_test.go |
| +++ b/internal/analytics/meter_test.go |
| @@ -1,6 +1,7 @@ |
| package analytics |
| import ( |
| + "os" |
| "path/filepath" |
| "testing" |
| "time" |
| @@ -42,7 +43,23 @@ func TestIsPageviewRule(t *testing.T) { |
| func TestMeterCountsAndDrains(t *testing.T) { |
| now := time.Date(2026, 9, 7, 9, 30, 0, 0, time.UTC) |
| - m := NewMeter(t.TempDir(), "box1", func() time.Time { return now }) |
| + dir := t.TempDir() |
| + // Pin the day salt. DaySalt makes a random one per directory, and with |
| + // 2^HLLPrecision registers two visitors share a register about once in |
| + // 2,048 runs; the small-range estimate then reads 1, not 2 (CI flake, |
| + // discussion 663). The all-zero salt puts this test's two visitors in |
| + // different registers, which the guard below checks. |
| + salt := make([]byte, 32) |
| + if err := os.WriteFile(filepath.Join(dir, saltPrefix+"2026-09-07"), salt, 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := func(ip string) uint64 { |
| + return mix64(VisitorHash(salt, "h1", ip, "Mozilla/5.0 Safari")) >> (64 - HLLPrecision) |
| + } |
| + if reg("203.0.113.5") == reg("198.51.100.9") { |
| + t.Fatal("the pinned salt puts both visitors in one register: pick another") |
| + } |
| + m := NewMeter(dir, "box1", func() time.Time { return now }) |
| m.Observe(obs("GET", 200, "text/html")) |
| m.Observe(obs("GET", 200, "text/html")) |
| o := obs("GET", 200, "text/html") |
diff --git a/internal/console/demo_spool_test.go b/internal/console/demo_spool_test.go| index 87c10b9f0..03c03cded 100644 |
| --- a/internal/console/demo_spool_test.go |
| +++ b/internal/console/demo_spool_test.go |
| @@ -71,7 +71,10 @@ func TestDemoJobSpoolRecoveryInvalidPath(t *testing.T) { |
| if err := s.ReconcileDemoJobs(t.Context(), true); err != nil { |
| t.Fatal(err) |
| } |
| - if logs.Len() != 0 { |
| + // Only this job's own lines count: the logger is process-wide, |
| + // and a goroutine left by another test (an edge tunnel ping, |
| + // CI 2026-11-08) can log into the buffer meanwhile. |
| + if strings.Contains(logs.String(), j.ID) { |
| t.Fatalf("recovery was not durable: %s", logs.String()) |
| } |
| }) |