fix(go): guard realtime mysql against stale events

This commit is contained in:
lingniu
2026-07-03 09:03:46 +08:00
parent c2e4c2b955
commit a16043c032
2 changed files with 41 additions and 17 deletions

View File

@@ -182,6 +182,30 @@ func TestSnapshotWriterUpsertsRealtimeLocationWhenCoordinatesExist(t *testing.T)
}
}
func TestRealtimeUpsertsDoNotOverwriteWithOlderEventTime(t *testing.T) {
for _, tc := range []struct {
name string
table string
query string
}{
{name: "snapshot", table: "vehicle_realtime_snapshot", query: upsertRealtimeSnapshotSQL},
{name: "location", table: "vehicle_realtime_location", query: upsertRealtimeLocationSQL},
} {
t.Run(tc.name, func(t *testing.T) {
guard := "VALUES(event_time) IS NOT NULL AND (" + tc.table + ".event_time IS NULL OR VALUES(event_time) >= " + tc.table + ".event_time)"
if !strings.Contains(tc.query, guard) {
t.Fatalf("upsert should guard realtime state by event_time, missing %q in:\n%s", guard, tc.query)
}
if strings.Contains(tc.query, "event_time = VALUES(event_time)") {
t.Fatalf("upsert should not unconditionally replace event_time:\n%s", tc.query)
}
if strings.Contains(tc.query, "event_id = VALUES(event_id)") {
t.Fatalf("upsert should not unconditionally replace event_id:\n%s", tc.query)
}
})
}
}
func TestSnapshotWriterBackfillsPlateFromBindingByVIN(t *testing.T) {
exec := &recordingSnapshotExec{}
resolver := &recordingPlateResolver{plate: "沪A12345"}