analytics: pin the day salt in TestMeterCountsAndDrains
With a random salt the test's two visitors share one of the 2,048 HLL registers about once in 2,048 runs, and the small-range estimate reads 1.00 instead of 2 (CI flake, discussion 663). The test now writes an all-zero salt, which puts them in different registers, and a guard fails loudly if a future change makes them collide.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+18
−1
internal/analytics/meter_test.go+18 −1
CI failed — run details
Intent
Attributed to this commit: 40.6 g CO₂e (5,273,923 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on fix-meter-visitors-flake, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
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") |