feat(notifications): send product notifications with a rights check right before each send
This commit is contained in:
1 parent
a856066316
commit
623d41b969
59 files changed
+4125
-1126
No files matched your search
@@ -0,0 +1,93 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/internal/adapter/secondary/mail/disabled"
|
||||
"lincodex/backend/internal/adapter/secondary/prometheus"
|
||||
)
|
||||
|
||||
func TestNewNotificationSenderWorkerSkipsWithoutWork(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 := newNotificationSenderWorker(t.Context(), test.settings, logger, metrics, disabled.NewSender())
|
||||
if err != nil {
|
||||
t.Fatalf("newNotificationSenderWorker() error = %v", err)
|
||||
}
|
||||
cleanup()
|
||||
if worker != nil {
|
||||
t.Fatal("newNotificationSenderWorker() built a worker without work to do")
|
||||
}
|
||||
if !strings.Contains(logs.String(), test.log) {
|
||||
t.Fatalf("logs = %q, want %q", logs.String(), test.log)
|
||||
}
|
||||
// The product loop has its own gauge: without it the gauge reads 0,
|
||||
// and the service loop's gauge is untouched.
|
||||
body := scrape(metrics)
|
||||
if !strings.Contains(body, "lincodex_notification_sender_running 0\n") {
|
||||
t.Fatalf("metrics lack lincodex_notification_sender_running 0:\n%s", body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplicationRunsAndStopsTheNotificationSenderWorker(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 := newNotificationSenderWorker(t.Context(), settings, logger, metrics, disabled.NewSender())
|
||||
if err != nil {
|
||||
t.Fatalf("newNotificationSenderWorker() error = %v", err)
|
||||
}
|
||||
defer cleanup()
|
||||
if worker == nil {
|
||||
t.Fatal("newNotificationSenderWorker() built no worker for an active transport")
|
||||
}
|
||||
application := NewApplication(settings, http.NotFoundHandler(), metrics, logger, nil, 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_notification_sender_running 1\n") {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("the product 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_notification_sender_running 0\n") {
|
||||
t.Fatalf("the gauge did not return to 0 after the loop stopped:\n%s", body)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user