test(server): set the local zone in a child process, not via time.Local (#2438)

## Summary

`TestNodeAnalyticsWindowIgnoresLocalZone` set the zone by assigning the
global `time.Local`. Every `time.Now()` reads that variable, so if a
timer left over from an earlier test fires during this test, the race
detector reports a data race. It counts as a failure of this test,
whichever test left the timer.

It showed up as an intermittent failure of the server race detector
(shard 3/4) on a PR run (#2436). Three local runs of the same shard
passed. The code under test is fine; only the test was racy.

## Change

The test now re-runs itself in a child process with `TZ=Etc/GMT-2`
(UTC+2) and writes nothing global:
- **Embedded zone data:** `time/tzdata` is embedded in the test binary,
so the zone resolves without the host's zoneinfo.
- **Zone check:** the child first checks that its local offset really is
+02:00.
- **Test name:** the child's `-test.run` pattern is built from
`t.Name()`.
- **Vacuous pass guarded:** the parent requires `--- PASS: <name>` in
the child's output, so a rename can't make it pass without running.
- **Windows:** the test is skipped there before the re-exec. Go on
Windows takes the local zone from the OS and ignores `TZ`, so the child
cannot be put in UTC+2.
- **Bounded child:** the child runs under a 60 s context, so a hung
child fails with its output.

This follows the existing re-exec pattern in
`distance_background_fill_test.go`. `TestMain` is just
`os.Exit(m.Run())`, so the child runs only this test. It works under the
sharded `-test.run` in the race job, and in coverage builds.

## Testing

- `go test -race -run TestNodeAnalyticsWindowIgnoresLocalZone`: pass.
gofmt and vet are clean.
- **Still guards the original bug:** with `time.Now().UTC()` changed
back to `time.Now()` in `GetNodeAnalytics`, it fails as before. The 7d
window holds 166 of 168 packets, and `From` is `...+02:00`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZachandClaude Opus 5.5 authored and GitHub committed 2026-11-21 22:26:05 +01:00
1 parent 49dd4ee773
commit 9a57b2fca6
1 file changed
+34 -3
+34 -3
View File
@@ -1,9 +1,16 @@
package main
import (
"context"
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"testing"
"time"
_ "time/tzdata" // TZ=Etc/GMT-2 in the child, without the host's zoneinfo
)
const coverageNodePK = "c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00"
@@ -130,10 +137,34 @@ func TestNodeAnalyticsCoverageEmptyStore(t *testing.T) {
// TestNodeAnalyticsWindowIgnoresLocalZone: the window bound is compared as a
// string with UTC FirstSeen values, so it must be formatted in UTC. With a
// +02:00 local zone the old code dropped the oldest two hours of a 7d range.
//
// The zone is set by running this test again in a child process with
// TZ=Etc/GMT-2 (UTC+2): assigning time.Local here would race with any timer
// still running in the test binary (time.Now reads it), which the race
// detector reports as a failure of whichever test is running.
func TestNodeAnalyticsWindowIgnoresLocalZone(t *testing.T) {
saved := time.Local
time.Local = time.FixedZone("UTC+2", 2*3600)
t.Cleanup(func() { time.Local = saved })
if os.Getenv("CS_TEST_LOCAL_ZONE_CHILD") != "1" {
if runtime.GOOS == "windows" {
t.Skip("Go takes the local zone from the OS on Windows and ignores TZ")
}
// A hung child fails here with its output, not at the parent's timeout.
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^"+regexp.QuoteMeta(t.Name())+"$", "-test.count=1", "-test.v")
cmd.Env = append(os.Environ(), "CS_TEST_LOCAL_ZONE_CHILD=1", "TZ=Etc/GMT-2")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("child run with TZ=Etc/GMT-2 failed: %v\n%s", err, out)
}
// A pattern that matched nothing would also exit 0.
if !strings.Contains(string(out), "--- PASS: "+t.Name()) {
t.Fatalf("child run did not run %s:\n%s", t.Name(), out)
}
return
}
if _, off := time.Now().Zone(); off != 2*3600 {
t.Fatalf("child local zone offset %d s, want 7200 (TZ=Etc/GMT-2)", off)
}
ps := setupCoverageStore(t, 10*24)
a, err := ps.GetNodeAnalytics(coverageNodePK, 7)