package store import ( "sync" "sync/atomic" "testing" "time" ) func TestANoticeIsClaimedByOneOfThoseAskingAtOnce(t *testing.T) { db := capacityStore(t) at := time.Date(2026, 10, 10, 9, 0, 0, 0, time.UTC) var yes atomic.Int64 var wg sync.WaitGroup for range 8 { wg.Go(func() { ok, err := db.ClaimNotice(t.Context(), "budget_spent\x00team:t1", at, time.Hour) if err != nil { t.Error(err) } if ok { yes.Add(1) } }) } wg.Wait() if yes.Load() != 1 { t.Fatalf("%d of eight claimed the same notice", yes.Load()) } if ok, _ := db.ClaimNotice(t.Context(), "budget_spent\x00team:t1", at.Add(59*time.Minute), time.Hour); ok { t.Error("claimed again within the hour") } if ok, _ := db.ClaimNotice(t.Context(), "budget_spent\x00team:t2", at, time.Hour); !ok { t.Error("another subject could not be claimed") } if ok, _ := db.ClaimNotice(t.Context(), "budget_spent\x00team:t1", at.Add(61*time.Minute), time.Hour); !ok { t.Error("not claimable once the hour had passed") } }