test: pin the duplicate guard and the graceful shutdown

- router: an approved post is remembered, so the MTA's copy is refused
- store: a post with no recipients stores no message body
- app: an API request in flight during Close gets its answer
- app: finished LMTP sessions leave the connection tracker
- router: drop a TrimSpace on the Message-ID that net/mail already does
This commit is contained in:
tgiachi 2026-11-10 11:58:07 +01:00
commit dd0096093b
4 changed files with 85 additions and 1 deletions

View file

@ -1,6 +1,7 @@
package app
import (
"bufio"
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
@ -12,6 +13,7 @@ import (
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"io"
"log"
"math/big"
@ -562,6 +564,66 @@ func TestCloseCutsAHungLMTPClientAfterTheGracePeriod(t *testing.T) {
}
}
func TestCloseLetsAnAPIRequestInFlightFinish(t *testing.T) {
relayAddr, _ := startSink(t)
a, err := Start(testConfig(t, relayAddr), "tok")
if err != nil {
t.Fatal(err)
}
conn, err := net.Dial("tcp", a.APIAddr)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
body := `{"name":"dev","domain":"example.org"}`
fmt.Fprintf(conn, "POST /api/v1/lists HTTP/1.1\r\nHost: x\r\nAuthorization: Bearer tok\r\nContent-Type: application/json\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", len(body), body[:10])
time.Sleep(100 * time.Millisecond) // the handler is now reading the body
closed := make(chan struct{})
go func() { a.Close(); close(closed) }()
waitRefused(t, a.APIAddr)
io.WriteString(conn, body[10:])
conn.SetReadDeadline(time.Now().Add(5 * time.Second))
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
t.Fatalf("the request in flight must get its answer: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
t.Fatalf("status = %d", resp.StatusCode)
}
select {
case <-closed:
case <-time.After(5 * time.Second):
t.Fatal("Close must return once the request is over")
}
}
func TestFinishedLMTPSessionsAreForgotten(t *testing.T) {
relayAddr, _ := startSink(t)
a, err := Start(testConfig(t, relayAddr), "tok")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { a.Close() })
for i := 0; i < 3; i++ {
lmtpSend(t, a.LMTPAddr, "alice@example.com", "nope@example.org", "Subject: x\r\n\r\nx\r\n")
}
deadline := time.Now().Add(5 * time.Second)
for {
a.lmtpOpen.mu.Lock()
n := len(a.lmtpOpen.conns)
a.lmtpOpen.mu.Unlock()
if n == 0 {
break
}
if time.Now().After(deadline) {
t.Fatalf("%d closed sessions are still tracked", n)
}
time.Sleep(5 * time.Millisecond)
}
}
func TestQueuedMailSurvivesARestartAndIsRetriedWhenTheRelayReturns(t *testing.T) {
var skew atomic.Int64
nowFunc = func() time.Time { return time.Now().Add(time.Duration(skew.Load())) }

View file

@ -106,3 +106,21 @@ func TestAnApprovedPostIsDeliveredEvenIfItWasSeen(t *testing.T) {
t.Fatalf("a copy after the approval: want ErrDuplicate, got %v", err)
}
}
func TestAnApprovedPostIsRemembered(t *testing.T) {
e := newEnv(t)
msg := "From: alice@example.com\r\nSubject: hi\r\nMessage-ID: <held@x>\r\n\r\nbody\r\n"
id, err := e.s.HoldMessage(e.list.ID, "alice@example.com", []byte(msg), "r")
if err != nil {
t.Fatal(err)
}
if err := e.r.Release(id); err != nil {
t.Fatalf("release: %v", err)
}
if err := e.post(t, msg); !errors.Is(err, ErrDuplicate) {
t.Fatalf("the MTA's copy of an approved post: want ErrDuplicate, got %v", err)
}
if n := pending(t, e); n != 2 {
t.Fatalf("pending = %d", n)
}
}

View file

@ -155,7 +155,7 @@ func (r *Router) Deliver(res Result) error {
// message when it has none.
func postID(data []byte) string {
if m, err := mail.ReadMessage(bytes.NewReader(data)); err == nil {
if id := strings.TrimSpace(m.Header.Get("Message-ID")); id != "" {
if id := m.Header.Get("Message-ID"); id != "" {
return "mid:" + id
}
}

View file

@ -106,6 +106,10 @@ func TestEnqueuePostRecordsAPostWithNoRecipients(t *testing.T) {
if err := s.EnqueuePost(l.ID, "mid:<a@x>", []byte("m"), nil); !errors.Is(err, ErrDuplicate) {
t.Fatalf("want ErrDuplicate, got %v", err)
}
var n int
if err := s.db.QueryRow(`SELECT COUNT(*) FROM outbox_msgs`).Scan(&n); err != nil || n != 0 {
t.Fatalf("stored messages = %d (%v), want none without recipients", n, err)
}
}
func TestReleasingAHeldPostDeliversItEvenIfSeenAndRecordsIt(t *testing.T) {