gateway: TestHeldClock passes each zone in instead of writing time.Local (#2949)

CI run 39242954115 (b749cffbb, ubuntu Race) went red on a data race:
TestHeldClock assigned time.Local per zone while a goroutine left by an
earlier gateway test (restAfterMarked -> narrowWhenRead) called time.Now,
which reads time.Local. heldClock now delegates to heldClockIn(t, now,
loc), and the test calls heldClockIn with the zone, so nothing writes
time.Local. Callers are unchanged.

Run: go test -race -run 'TestHeldClock|TestZhipu' -count=3
./internal/gateway/ ok; GOOS=linux go vet -tags nogui and GOOS=windows
go vet of internal/gateway ok; gofmt -l clean. The race itself was not
reproduced locally (it needs the stray goroutine's timing); the test no
longer writes the global the race trace names.
This commit is contained in:
yetone
2026-10-19 19:55:11 +08:00
parent b749cffbb1
commit b641868eb3
2 changed files with 9 additions and 7 deletions
+5 -2
View File
@@ -602,8 +602,11 @@ var zhipuResets = regexp.MustCompile(`(?:\bYour limit will reset at|您的限额
// offset goes with it: "20:12 UTC+8" today, else "Oct 16 09:00 UTC+8".
// Without it a magpie on UTC+8 said "not asked again before 17:12" to a
// user who couldn't tell whose 17:12 it was (Player on Discord).
func heldClock(t, now time.Time) string {
t, now = t.Local(), now.Local()
func heldClock(t, now time.Time) string { return heldClockIn(t, now, time.Local) }
// heldClockIn is heldClock on the clock of loc.
func heldClockIn(t, now time.Time, loc *time.Location) string {
t, now = t.In(loc), now.In(loc)
s := t.Format("Jan 2 15:04")
if y, m, d := t.Date(); now.Year() == y && now.Month() == m && now.Day() == d {
s = t.Format("15:04")
+4 -5
View File
@@ -74,7 +74,9 @@ func TestResetsZhipu(t *testing.T) {
}
// heldClock gives the time with its offset, so a reader on another clock
// can tell whose it is.
// can tell whose it is. Each zone is passed in, never set on time.Local:
// time.Now reads time.Local, and a goroutine an earlier test left running
// read it while this test wrote it, a race on ubuntu CI (run 39242954115).
func TestHeldClock(t *testing.T) {
cst := time.FixedZone("CST", 8*3600)
now := time.Date(2026, 10, 15, 16, 25, 0, 0, cst)
@@ -88,10 +90,7 @@ func TestHeldClock(t *testing.T) {
{time.UTC, time.Date(2026, 10, 15, 20, 12, 13, 0, cst), "12:12 UTC+0"},
{time.FixedZone("", -(3*3600 + 30*60)), time.Date(2026, 10, 15, 20, 12, 13, 0, cst), "08:42 UTC-3:30"},
} {
was := time.Local
time.Local = c.zone
got := heldClock(c.at, now)
time.Local = was
got := heldClockIn(c.at, now, c.zone)
if got != c.want {
t.Errorf("%v in %v: %q, want %q", c.at, c.zone, got, c.want)
}