Files
magpie/internal/tui/main_test.go
T
yetone 1caea63804 tui tests: the package runs in UTC from TestMain, so no test swaps time.Local under the gateway's loops
TestSessionsPage raced about 1 in 20-40 runs under -race: sessionsHome set
time.Local = time.UTC (and back in its cleanup) while goroutines an earlier
test left running read it. gateway.ListenAndServe starts its Keep* loops
(check-ins, credits, history, quotas) on its ctx and returns once the
server is shut down, without waiting for them, and those loops call
.Local() and time.Date(..., time.Local). TestMain now sets time.Local to
UTC once, before any test starts a goroutine, and sessionsHome no longer
touches it; the sessions fixtures' days are counted in UTC as before.

A probe test with a goroutine calling time.Now().Local() while
sessionsHome ran reported DATA RACE at sessions_test.go:30 on the old code
and passes -race -count=3 on the new one (probe not committed). The
reported race itself didn't reproduce here before the change (-race
-count=40 of the package, -count=150 of TestTUI*|TestSessionsPage).
go test -race -count=40 -run TestSessionsPage ./internal/tui and
go test -race -count=3 ./internal/tui pass; go vet and gofmt are clean.
2026-10-07 00:32:49 +08:00

18 lines
471 B
Go

package tui
import (
"testing"
"time"
"github.com/yetone/magpie/internal/testenv"
)
// The tests run in a home of their own, never the user's real files, and in
// UTC, the zone the sessions fixtures' days are counted in. The zone is set
// here, before any test starts a goroutine: a test that swapped it would race
// the gateway's loops an earlier test left running, which read time.Local.
func TestMain(m *testing.M) {
time.Local = time.UTC
testenv.Main(m)
}