feat(mail): send service identity mail through a leased worker
This commit is contained in:
1 parent
204c6bae62
commit
d21fd49a42
58 files changed
+7577
-164
No files matched your search
@@ -0,0 +1,146 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/internal/adapter/secondary/mail/disabled"
|
||||
"lincodex/backend/internal/adapter/secondary/mail/resend"
|
||||
"lincodex/backend/internal/adapter/secondary/prometheus"
|
||||
)
|
||||
|
||||
// resendSenderConfig is a Resend setup with a database that does not answer.
|
||||
func resendSenderConfig(t *testing.T) config.Config {
|
||||
t.Helper()
|
||||
settings := consumerTestConfig(t)
|
||||
settings.EmailProvider = config.EmailProviderResend
|
||||
settings.EmailAccount = "primary"
|
||||
settings.EmailTimeout = 10 * time.Second
|
||||
settings.EmailQuotaDaily = 100
|
||||
settings.EmailQuotaMonthly = 3000
|
||||
return settings
|
||||
}
|
||||
|
||||
func TestNewMailSenderWorkerSkipsWithoutWork(t *testing.T) {
|
||||
noDatabase := resendSenderConfig(t)
|
||||
noDatabase.DatabaseURL = ""
|
||||
tests := []struct {
|
||||
name string
|
||||
settings config.Config
|
||||
log string
|
||||
}{
|
||||
// consumerTestConfig selects the disabled transport.
|
||||
{name: "transport disabled", settings: consumerTestConfig(t), log: "EMAIL_PROVIDER is disabled"},
|
||||
{name: "database not configured", settings: noDatabase, log: "DATABASE_URL is not set"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var logs strings.Builder
|
||||
logger := slog.New(slog.NewTextHandler(&logs, nil))
|
||||
metrics := prometheus.New()
|
||||
worker, cleanup, err := newMailSenderWorker(t.Context(), test.settings, logger, metrics, disabled.NewSender())
|
||||
if err != nil {
|
||||
t.Fatalf("newMailSenderWorker() error = %v", err)
|
||||
}
|
||||
cleanup()
|
||||
if worker != nil {
|
||||
t.Fatal("newMailSenderWorker() built a worker without work to do")
|
||||
}
|
||||
if !strings.Contains(logs.String(), test.log) {
|
||||
t.Fatalf("logs = %q, want %q", logs.String(), test.log)
|
||||
}
|
||||
// Without a loop the gauge says so, so the overdue alert stays
|
||||
// silent.
|
||||
if body := scrape(metrics); !strings.Contains(body, "lincodex_mail_sender_running 0\n") {
|
||||
t.Fatalf("metrics lack lincodex_mail_sender_running 0:\n%s", body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMailIdempotencyWindowFollowsTheTransport(t *testing.T) {
|
||||
if got := mailIdempotencyWindow(config.Config{EmailProvider: config.EmailProviderResend}); got != resend.IdempotencyWindow {
|
||||
t.Fatalf("mailIdempotencyWindow(resend) = %s, want %s", got, resend.IdempotencyWindow)
|
||||
}
|
||||
// A relay has no provider-side deduplication, so every retry counts again.
|
||||
if got := mailIdempotencyWindow(config.Config{EmailProvider: config.EmailProviderSMTP}); got != 0 {
|
||||
t.Fatalf("mailIdempotencyWindow(smtp) = %s, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicationRunsAndStopsTheMailSenderWorker starts the loop against a
|
||||
// database that does not answer: it backs off in the background, reports
|
||||
// that it runs, and the application still stops within its shutdown budget
|
||||
// with the gauge back at 0.
|
||||
func TestApplicationRunsAndStopsTheMailSenderWorker(t *testing.T) {
|
||||
settings := resendSenderConfig(t)
|
||||
settings.HTTPAddr = "127.0.0.1:0"
|
||||
settings.MetricsAddr = "127.0.0.1:0"
|
||||
settings.ShutdownTimeout = time.Second
|
||||
metrics := prometheus.New()
|
||||
logger := slog.New(slog.DiscardHandler)
|
||||
worker, cleanup, err := newMailSenderWorker(t.Context(), settings, logger, metrics, disabled.NewSender())
|
||||
if err != nil {
|
||||
t.Fatalf("newMailSenderWorker() error = %v", err)
|
||||
}
|
||||
defer cleanup()
|
||||
if worker == nil {
|
||||
t.Fatal("newMailSenderWorker() built no worker for an active transport")
|
||||
}
|
||||
application := NewApplication(settings, http.NotFoundHandler(), metrics, logger, nil, nil, nil, nil, worker, nil)
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
finished := make(chan error, 1)
|
||||
go func() { finished <- application.run(ctx) }()
|
||||
deadline := time.Now().Add(relayLifecycleTimeout)
|
||||
for !strings.Contains(scrape(metrics), "lincodex_mail_sender_running 1\n") {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("the loop never reported that it runs")
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case err := <-finished:
|
||||
if err != nil {
|
||||
t.Fatalf("run() error = %v", err)
|
||||
}
|
||||
case <-time.After(relayLifecycleTimeout):
|
||||
t.Fatal("run() did not return after cancellation")
|
||||
}
|
||||
if body := scrape(metrics); !strings.Contains(body, "lincodex_mail_sender_running 0\n") {
|
||||
t.Fatalf("the gauge did not return to 0 after the loop stopped:\n%s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func scrape(metrics *prometheus.Metrics) string {
|
||||
recorder := httptest.NewRecorder()
|
||||
metrics.Handler().ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/metrics", nil))
|
||||
return recorder.Body.String()
|
||||
}
|
||||
|
||||
func TestShutdownSettleBudgetFitsTheShutdownTimeout(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
shutdown time.Duration
|
||||
want time.Duration
|
||||
}{
|
||||
{30 * time.Second, mailSenderMaxShutdownSettle},
|
||||
{10 * time.Second, mailSenderMaxShutdownSettle}, // the default SHUTDOWN_TIMEOUT
|
||||
{6 * time.Second, 3 * time.Second},
|
||||
{time.Second, time.Second},
|
||||
} {
|
||||
got := shutdownSettleBudget(test.shutdown)
|
||||
if got != test.want {
|
||||
t.Fatalf("shutdownSettleBudget(%s) = %s, want %s", test.shutdown, got, test.want)
|
||||
}
|
||||
if test.shutdown >= 4*time.Second && mailSenderShutdownGrace+got > test.shutdown {
|
||||
t.Fatalf("grace plus settle budget = %s, over SHUTDOWN_TIMEOUT %s", mailSenderShutdownGrace+got, test.shutdown)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user