carlos / platform Public

test: give the freshest-stamp fold pin teeth in both sort orders

TestAliasReadingsPrefersTheFreshestStamp previously relied on the
alphabetically-later box also carrying the newer stamp, so DirStore's
sorted List order made a naive last-write-wins fold pass by accident.
Split into two subtests so the fresher box sorts both first and last —
only a genuine DNSCheckedAt comparison passes both.
Paul Campbell pushed by paul@keymail.dev 1035e879d94d375698ebb592e03169ce02f8f3fb parent 98f7fa8
1 file changed, +36 −10
  • internal/console/apidomains_test.go +36 −10
diff --git a/internal/console/apidomains_test.go b/internal/console/apidomains_test.go
index 4557e600c..195f27b05 100644
--- a/internal/console/apidomains_test.go
+++ b/internal/console/apidomains_test.go
@@ -1484,16 +1484,42 @@ func writeBoxWithAlias(t *testing.T, s release.Store, label string, now time.Tim
// Two boxes can both carry an alias (an everywhere-class primary). The fold
// must take the FRESHEST stamp, not whichever box the map happened to yield
// last — otherwise a frozen reading deterministically overwrites a live one.
+//
+// DirStore.List returns keys sorted, and WriteBox keys off the box label, so
+// LoadBoxes iterates boxes in label order regardless of write order. A test
+// with only one label ordering can't tell a genuine freshness comparison
+// apart from a last-write-wins bug that happens to agree with it for that
+// ordering — so this asserts BOTH directions: the fresher box sorting
+// alphabetically first (where a last-write-wins fold would wrongly keep the
+// stale, later-sorted box's reading) and sorting last (where naive
+// last-write-wins would coincidentally get it right too). Only a real
+// DNSCheckedAt comparison passes both.
func TestAliasReadingsPrefersTheFreshestStamp(t *testing.T) {
- s := readingsTestStore(t)
- now := time.Unix(1756300060, 0)
- writeBoxWithAlias(t, s, "annie", now, status.AliasReading{
- Route: "app.example.com", DNSStatus: "elsewhere", DNSCheckedAt: 1756200000})
- writeBoxWithAlias(t, s, "betty", now, status.AliasReading{
- Route: "app.example.com", DNSStatus: "pointing", DNSCheckedAt: 1756300000})
+ t.Run("fresher box sorts first", func(t *testing.T) {
+ s := readingsTestStore(t)
+ now := time.Unix(1756300060, 0)
+ writeBoxWithAlias(t, s, "annie", now, status.AliasReading{
+ Route: "app.example.com", DNSStatus: "pointing", DNSCheckedAt: 1756300000})
+ writeBoxWithAlias(t, s, "betty", now, status.AliasReading{
+ Route: "app.example.com", DNSStatus: "elsewhere", DNSCheckedAt: 1756200000})
+
+ got := aliasReadings(context.Background(), s, now)
+ if got["shop.customer.com"].DNSStatus != "pointing" {
+ t.Fatalf("reading = %+v, want the freshest box's stamp (annie, which sorts first)", got["shop.customer.com"])
+ }
+ })
- got := aliasReadings(context.Background(), s, now)
- if got["shop.customer.com"].DNSStatus != "pointing" {
- t.Fatalf("reading = %+v, want the freshest box's stamp", got["shop.customer.com"])
- }
+ t.Run("fresher box sorts last", func(t *testing.T) {
+ s := readingsTestStore(t)
+ now := time.Unix(1756300060, 0)
+ writeBoxWithAlias(t, s, "annie", now, status.AliasReading{
+ Route: "app.example.com", DNSStatus: "elsewhere", DNSCheckedAt: 1756200000})
+ writeBoxWithAlias(t, s, "betty", now, status.AliasReading{
+ Route: "app.example.com", DNSStatus: "pointing", DNSCheckedAt: 1756300000})
+
+ got := aliasReadings(context.Background(), s, now)
+ if got["shop.customer.com"].DNSStatus != "pointing" {
+ t.Fatalf("reading = %+v, want the freshest box's stamp (betty, which sorts last)", got["shop.customer.com"])
+ }
+ })
}