From cceb35740b358f6efa89f4843d2f3774f7a42572 Mon Sep 17 00:00:00 2001 From: dborup Date: Sat, 28 Nov 2026 13:07:47 +0100 Subject: [PATCH] test(ingestor): seed tx_last_seen backfill fixtures in one transaction seedTxWithObs inserted the transmission and each observation as separate autocommit statements. If the tx_last_seen_backfill_v1 goroutine that OpenStore schedules ran between them, it stamped last_seen from a partial set of observations (100 instead of 300), and the test's own backfill call then skipped the row because it only selects last_seen = 0. newTestStore and WaitForAsyncMigrations already drain that goroutine before seeding, so current master is not exposed. The failures on dborup/CoreScope#1012 came from a merge commit (5072050d, base 4af4177c) whose test code predates those waits. Seeding in one transaction closes the window regardless: a backfill running at any point sees the row with none or all of its observations. seedTxNoObs now reuses the same helper. Relates to dborup/CoreScope#1012 Co-Authored-By: Claude Opus 5.5 --- cmd/ingestor/tx_last_seen_backfill_test.go | 37 ++++++++++++++++------ 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/cmd/ingestor/tx_last_seen_backfill_test.go b/cmd/ingestor/tx_last_seen_backfill_test.go index f88b33f31..dd1325e2e 100644 --- a/cmd/ingestor/tx_last_seen_backfill_test.go +++ b/cmd/ingestor/tx_last_seen_backfill_test.go @@ -15,7 +15,29 @@ import ( // observations. hash must be unique. func seedTxNoObs(t *testing.T, s *Store, hash string) int64 { t.Helper() - res, err := s.db.Exec(`INSERT INTO transmissions (raw_hex, hash, first_seen, payload_type, last_seen) VALUES ('aabb', ?, '2026-01-01T00:00:00Z', 1, 0)`, hash) + return seedTxWithObs(t, s, hash) +} + +// seedTxWithObs inserts a transmission row with last_seen=0 plus one or +// more observations at the given unix timestamps. +// +// Race this guards against: OpenStore schedules tx_last_seen_backfill_v1 +// in a goroutine. If that goroutine runs between two of the seed INSERTs +// below, it sees only the first observation, stamps last_seen from it +// (e.g. 100 instead of 300), and the test's own backfillTxLastSeen call +// then skips the row because it only selects last_seen = 0. newTestStore +// and WaitForAsyncMigrations already drain the goroutine before seeding; +// seeding in one transaction also makes the row and all of its +// observations appear at once, so a backfill running at any point sees +// either none of them or all of them. +func seedTxWithObs(t *testing.T, s *Store, hash string, obsTimestamps ...int64) int64 { + t.Helper() + tx, err := s.db.Begin() + if err != nil { + t.Fatalf("begin seed tx for %s: %v", hash, err) + } + defer tx.Rollback() // no-op after Commit + res, err := tx.Exec(`INSERT INTO transmissions (raw_hex, hash, first_seen, payload_type, last_seen) VALUES ('aabb', ?, '2026-01-01T00:00:00Z', 1, 0)`, hash) if err != nil { t.Fatalf("seed transmission %s: %v", hash, err) } @@ -23,19 +45,14 @@ func seedTxNoObs(t *testing.T, s *Store, hash string) int64 { if err != nil { t.Fatal(err) } - return id -} - -// seedTxWithObs inserts a transmission row with last_seen=0 plus one or -// more observations at the given unix timestamps. -func seedTxWithObs(t *testing.T, s *Store, hash string, obsTimestamps ...int64) int64 { - t.Helper() - id := seedTxNoObs(t, s, hash) for _, ts := range obsTimestamps { - if _, err := s.db.Exec(`INSERT INTO observations (transmission_id, timestamp) VALUES (?, ?)`, id, ts); err != nil { + if _, err := tx.Exec(`INSERT INTO observations (transmission_id, timestamp) VALUES (?, ?)`, id, ts); err != nil { t.Fatalf("seed observation for %s: %v", hash, err) } } + if err := tx.Commit(); err != nil { + t.Fatalf("commit seed tx for %s: %v", hash, err) + } return id }