tests: memory's date test holds its own zone, not time.Local
TestMemorySearchDatesInLocalTime (4028687b8) set time.Local to +08:00. time.Local is read by every time.Now, so the swap raced an http conn goroutine an earlier test (serveMTLSGateway) left finishing: main's Test run 43194740198 at727e23637went red on macOS -race with a DATA RACE on time.Local, memory_mcp_test.go:262 against net/http setState. Now no test touches time.Local here: - memoryStamp writes in memoryZone(), time.Local by default, a variable the gateway test sets. - The half that checked a new save keeps its zone moved to memory's own TestSaveKeepsTheLocalZone, which holds memory's clock (now) at 04:30 +08:00 and reads "+08:00" back from memory.json. Run on mote: go vet ./internal/memory ./internal/gateway, linux nogui and windows vet of both; go test ./internal/memory ok; go test -race -count=3 -run ^TestMemory ./internal/gateway ok; gofmt -l empty. Broken by hand, each red: Save's now() as now().UTC() fails TestSaveKeepsTheLocalZone; memoryStamp in time.UTC fails TestMemorySearchDatesInLocalTime ("2026-11-10T20:30Z"). Not run: the full gateway package (the race needs a leftover goroutine from another test, so it isn't reproduced on demand; the swap that it raced on is gone). Same class still open: #2949 (TestHeldClock) and #4360 (testenv.Zone's cleanup), which swap time.Local too.
This commit is contained in:
@@ -469,4 +469,9 @@ func memoryForget(a memoryArgs) (string, error) {
|
||||
// put a memory on the day before or after the one the user lived
|
||||
// (Norethcy on X). Memories saved before this are stored in UTC ("…Z")
|
||||
// and are shown the same way.
|
||||
func memoryStamp(t time.Time) string { return t.In(time.Local).Format("2006-01-02T15:04Z07:00") }
|
||||
func memoryStamp(t time.Time) string { return t.In(memoryZone()).Format("2006-01-02T15:04Z07:00") }
|
||||
|
||||
// memoryZone is the zone memoryStamp writes in: time.Local, a variable for
|
||||
// tests, which must never swap time.Local itself (every goroutine still
|
||||
// running reads it, so the swap is a race).
|
||||
var memoryZone = func() *time.Location { return time.Local }
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -256,11 +255,13 @@ func TestRootDirs(t *testing.T) {
|
||||
// A memory is dated in the reader's own zone, with the time and its
|
||||
// offset: at 04:30 in Shanghai on the 11th, UTC is still the 10th
|
||||
// (Norethcy on X). One saved before, stored as "…Z", reads the same way.
|
||||
// That a new one is stored in the local zone is memory's
|
||||
// TestSaveKeepsTheLocalZone.
|
||||
func TestMemorySearchDatesInLocalTime(t *testing.T) {
|
||||
fresh(t)
|
||||
oldLocal := time.Local
|
||||
time.Local = time.FixedZone("CST", 8*3600)
|
||||
t.Cleanup(func() { time.Local = oldLocal })
|
||||
oldZone := memoryZone
|
||||
memoryZone = func() *time.Location { return time.FixedZone("CST", 8*3600) }
|
||||
t.Cleanup(func() { memoryZone = oldZone })
|
||||
|
||||
old := `{"memories":[{"id":"m1","text":"Heavy tests run on mote","kind":"fact","scope":"global","origin":"agent","status":"active","created":"2026-11-10T20:30:00Z","updated":"2026-11-10T20:30:00Z"}]}`
|
||||
if err := os.MkdirAll(filepath.Dir(memory.Path()), 0o700); err != nil {
|
||||
@@ -274,16 +275,4 @@ func TestMemorySearchDatesInLocalTime(t *testing.T) {
|
||||
if bad || !strings.Contains(said, ", 2026-11-11T04:30+08:00: Heavy tests run on mote") {
|
||||
t.Fatalf("an old UTC memory: %s", said)
|
||||
}
|
||||
|
||||
// a new one is kept with the zone it was saved in
|
||||
if _, bad := memPost(t, h, memorySaveTool, `{"text":"Answer in Chinese","kind":"preference"}`); bad {
|
||||
t.Fatal("save failed")
|
||||
}
|
||||
b, _ := os.ReadFile(memory.Path())
|
||||
if !strings.Contains(string(b), `"updated": "`) && !strings.Contains(string(b), `"updated":"`) {
|
||||
t.Fatalf("no updated in %s", b)
|
||||
}
|
||||
if !regexp.MustCompile(`"updated":\s*"[^"]*\+08:00"`).Match(b) {
|
||||
t.Fatalf("saved in UTC, not the local zone: %s", b)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,21 @@ func save(t *testing.T, text, kind, scope string, tags ...string) *Memory {
|
||||
return m
|
||||
}
|
||||
|
||||
// A memory is stored with the zone it was saved in, not in UTC: read back
|
||||
// as a day, UTC puts 04:30 in Shanghai on the day before (Norethcy on X).
|
||||
func TestSaveKeepsTheLocalZone(t *testing.T) {
|
||||
at := fresh(t)
|
||||
*at = time.Date(2026, 11, 11, 4, 30, 0, 0, time.FixedZone("CST", 8*3600))
|
||||
save(t, "Answer in Chinese", Preference, Global)
|
||||
b, err := os.ReadFile(Path())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(b), `"updated": "2026-11-11T04:30:00+08:00"`) && !strings.Contains(string(b), `"updated":"2026-11-11T04:30:00+08:00"`) {
|
||||
t.Fatalf("not saved in the local zone: %s", b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveKeepsOneOfTheSameText(t *testing.T) {
|
||||
fresh(t)
|
||||
a := save(t, "UI uses no gradients.", Preference, Global, "ui")
|
||||
|
||||
Reference in New Issue
Block a user