Files
llmgw/internal/store/notify_test.go
T
Ilker ManapandClaude Opus 5.5 39bb8ee557 Two control planes send a notification once
Each kept its own hour of quiet per event, so a budget spent on a team
both control planes served was announced by each. An event is now claimed
in the store before it is sent (notify_sent, migration 0043; one statement,
so two asking at once cannot both win), and only the claimer sends it. A
store that cannot be asked sends it anyway.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-10 13:36:08 +02:00

40 lines
1016 B
Go

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")
}
}