test(mail): claim in a stable order in the sender test store

This commit is contained in:
shira committed 2026-10-06 23:57:10 +03:00
1 parent 57174dffba
commit 5e202ac972
1 file changed
+15 -1
@@ -8,6 +8,7 @@ import (
"io" "io"
"log/slog" "log/slog"
"net" "net"
"slices"
"strings" "strings"
"testing" "testing"
"time" "time"
@@ -37,6 +38,9 @@ type leasedRow struct {
// when none did. failures counts failures of the worker on the row. // when none did. failures counts failures of the worker on the row.
firstSend time.Time firstSend time.Time
failures int32 failures int32
// seq is the insertion order, so the fake claims in a stable order like
// the store, which orders by the due time and then by identifier.
seq int
} }
// memoryDeliveries stores deliveries with the version rule and the lease of // memoryDeliveries stores deliveries with the version rule and the lease of
@@ -69,7 +73,17 @@ func (store *memoryDeliveries) Get(_ context.Context, id uuid.UUID) (domain.Deli
} }
func (store *memoryDeliveries) ClaimDue(_ context.Context, claim outbound.DeliveryClaim) (outbound.ClaimedDelivery, error) { func (store *memoryDeliveries) ClaimDue(_ context.Context, claim outbound.DeliveryClaim) (outbound.ClaimedDelivery, error) {
ordered := make([]*leasedRow, 0, len(store.rows))
for _, row := range store.rows { for _, row := range store.rows {
ordered = append(ordered, row)
}
slices.SortFunc(ordered, func(a, b *leasedRow) int {
if byDue := a.delivery.NextAttemptAt().Compare(b.delivery.NextAttemptAt()); byDue != 0 {
return byDue
}
return a.seq - b.seq
})
for _, row := range ordered {
delivery := row.delivery delivery := row.delivery
due := !delivery.NextAttemptAt().IsZero() && !delivery.NextAttemptAt().After(claim.Now) due := !delivery.NextAttemptAt().IsZero() && !delivery.NextAttemptAt().After(claim.Now)
pinned := delivery.ProviderAccount() == claim.Account || pinned := delivery.ProviderAccount() == claim.Account ||
@@ -336,7 +350,7 @@ func (h *senderTestHarness) addDelivery(t *testing.T, account domain.MailProvide
if err != nil { if err != nil {
t.Fatalf("NewDelivery() error = %v", err) t.Fatalf("NewDelivery() error = %v", err)
} }
h.deliveries.rows[delivery.ID()] = &leasedRow{delivery: delivery} h.deliveries.rows[delivery.ID()] = &leasedRow{delivery: delivery, seq: len(h.deliveries.rows)}
h.deliveries.state = domain.ServiceMailState{ h.deliveries.state = domain.ServiceMailState{
TokenFound: true, TokenFound: true,
TokenPurpose: domain.EmailTokenPurposeEmailVerification, TokenPurpose: domain.EmailTokenPurposeEmailVerification,