feat(queue): count sent, failed and dropped deliveries of a post

- a delivery the relay accepted completes as sent, one given up as
  failed, one to a missing mailbox (or with a missing body) is dropped
  through store.DropOutbox; a retry changes nothing
- tests: each outcome of a post, giving up after MaxAge, a missing body,
  and discussion mail sent before a bigger newsletter queued earlier
This commit is contained in:
tgiachi 2026-11-10 12:38:32 +01:00
commit 51e2bf683e
2 changed files with 123 additions and 2 deletions

View file

@ -0,0 +1,114 @@
package queue
import (
"context"
"errors"
"testing"
"github.com/emersion/go-smtp"
"codeberg.org/squidd/parsimony/internal/outbound"
"codeberg.org/squidd/parsimony/internal/store"
)
// post queues a post of n recipients (a@x.com, b@x.com...) to a new list and returns the list.
func (e *env) post(t *testing.T, n int) store.List {
t.Helper()
l, err := e.s.CreateList("dev", "example.org", "Dev")
if err != nil {
t.Fatal(err)
}
var items []store.NewOutbound
for i := 0; i < n; i++ {
items = append(items, store.NewOutbound{From: "bounce@x.org", Rcpt: string(rune('a'+i)) + "@x.com"})
}
if err := e.s.EnqueuePost(l.ID, "mid:<p@x>", []byte("Message-ID: <p@x>\r\nSubject: hi\r\n\r\nbody\r\n"), items); err != nil {
t.Fatal(err)
}
return l
}
func (e *env) onlyPost(t *testing.T, l store.List) store.Post {
t.Helper()
posts, err := e.s.ListPosts(l.ID, 0)
if err != nil || len(posts) != 1 {
t.Fatalf("posts: %+v %v", posts, err)
}
return posts[0]
}
func TestTheWorkerCountsEachOutcomeOfAPost(t *testing.T) {
e := setup(t)
l := e.post(t, 4)
e.snd.fn = func(_ context.Context, _ string, to []string) error {
switch to[0] {
case "b@x.com":
return errors.New("connection refused")
case "c@x.com":
return &smtp.SMTPError{Code: 554, EnhancedCode: smtp.EnhancedCode{5, 7, 1}, Message: "policy"}
case "d@x.com":
return outbound.AtRcpt(&smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 1, 1}, Message: "no such user"})
}
return nil
}
st, err := e.w.RunOnce(context.Background())
if err != nil || st.Sent != 1 || st.Retried != 1 || st.Failed != 1 || st.Dropped != 1 {
t.Fatalf("run: %+v %v", st, err)
}
p := e.onlyPost(t, l)
if p.Recipients != 4 || p.Sent != 1 || p.Failed != 1 || p.Dropped != 1 || p.Pending != 1 {
t.Fatalf("post: %+v", p)
}
// The retried delivery goes out later.
e.snd.fn = nil
e.clk.set(t0.Add(Backoff(1)))
e.w.RunOnce(context.Background())
if p := e.onlyPost(t, l); p.Sent != 2 || p.Pending != 0 {
t.Fatalf("post: %+v", p)
}
}
func TestGivingUpAfterMaxAgeCountsAsFailed(t *testing.T) {
e := setup(t)
l := e.post(t, 1)
e.snd.fn = func(context.Context, string, []string) error { return errors.New("down") }
e.clk.set(t0.Add(MaxAge + 1))
if st, _ := e.w.RunOnce(context.Background()); st.Failed != 1 {
t.Fatalf("stats: %+v", st)
}
if p := e.onlyPost(t, l); p.Failed != 1 || p.Pending != 0 {
t.Fatalf("post: %+v", p)
}
}
func TestAMissingBodyCountsAsDropped(t *testing.T) {
e := setup(t)
e.w.readBody = func(int64) ([]byte, error) { return nil, store.ErrNotFound }
l := e.post(t, 2)
e.w.RunOnce(context.Background())
if p := e.onlyPost(t, l); p.Dropped != 2 || p.Sent != 0 || p.Pending != 0 {
t.Fatalf("post: %+v", p)
}
}
func TestDiscussionMailIsSentBeforeABigNewsletterQueuedEarlier(t *testing.T) {
e := setup(t)
e.w.Concurrency = 1
var news []store.NewOutbound
for i := 0; i < 120; i++ {
news = append(news, store.NewOutbound{From: "b@x.org", Rcpt: "reader@x.com", Priority: 1})
}
if err := e.s.Enqueue([]byte("Subject: newsletter\r\n\r\nx"), news); err != nil {
t.Fatal(err)
}
e.clk.set(t0.Add(1))
e.s.Enqueue([]byte("Subject: talk\r\n\r\nx"), []store.NewOutbound{
{From: "b@x.org", Rcpt: "first@x.com"}, {From: "b@x.org", Rcpt: "second@x.com"},
})
if _, err := e.w.RunOnce(context.Background()); err != nil {
t.Fatal(err)
}
if e.snd.sent[0].To[0] != "first@x.com" || e.snd.sent[1].To[0] != "second@x.com" || e.snd.sent[2].To[0] != "reader@x.com" {
t.Fatalf("order: %v %v %v", e.snd.sent[0].To, e.snd.sent[1].To, e.snd.sent[2].To)
}
}

View file

@ -219,7 +219,7 @@ func (w *Worker) RunOnce(ctx context.Context) (Stats, error) {
func (w *Worker) deliver(ctx context.Context, cache *bodies, it store.OutboxItem) Stats {
data, err := cache.get(it.MsgID)
if errors.Is(err, store.ErrNotFound) {
w.complete(it)
w.drop(it)
return Stats{Dropped: 1}
}
if err != nil {
@ -259,7 +259,7 @@ func (w *Worker) deliver(ctx context.Context, cache *bodies, it store.OutboxItem
}
}
log.Printf("queue: drop %s: %v", it.Rcpt, err)
w.complete(it)
w.drop(it)
return out
case outbound.IsPermanent(err):
// The relay refused the message itself (policy, size, line length...): nobody gets it, so
@ -292,3 +292,10 @@ func (w *Worker) complete(it store.OutboxItem) {
log.Printf("queue: remove %d: %v", it.ID, err)
}
}
// drop removes a delivery that will never be made; a delivery of a post counts as dropped.
func (w *Worker) drop(it store.OutboxItem) {
if err := w.Store.DropOutbox(it.ID); err != nil && !errors.Is(err, store.ErrNotFound) {
log.Printf("queue: drop %d: %v", it.ID, err)
}
}