package queue import ( "context" "errors" "testing" "github.com/emersion/go-smtp" "git.kmalloc.org/squid/parsimony/internal/outbound" "git.kmalloc.org/squid/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:", []byte("Message-ID: \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) } }