82 lines
2.8 KiB
Go
82 lines
2.8 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"lincodex/backend/config"
|
|
"lincodex/backend/internal/adapter/secondary/prometheus"
|
|
)
|
|
|
|
func TestNewDeliveryMetricsSkipsWithoutADatabase(t *testing.T) {
|
|
sampler, cleanup, err := newDeliveryMetrics(t.Context(), config.Config{EmailProvider: config.EmailProviderDisabled},
|
|
slog.New(slog.DiscardHandler), prometheus.New())
|
|
if err != nil {
|
|
t.Fatalf("newDeliveryMetrics() error = %v", err)
|
|
}
|
|
cleanup()
|
|
if sampler != nil {
|
|
t.Fatal("newDeliveryMetrics() built a sampler without DATABASE_URL")
|
|
}
|
|
}
|
|
|
|
func TestDeliveryMetricsQuotaFollowsTheTransport(t *testing.T) {
|
|
quota, err := deliveryMetricsQuota(config.Config{EmailProvider: config.EmailProviderDisabled})
|
|
if err != nil || quota != nil {
|
|
t.Fatalf("deliveryMetricsQuota(disabled) = %v, %v; want no quota", quota, err)
|
|
}
|
|
quota, err = deliveryMetricsQuota(config.Config{
|
|
EmailProvider: config.EmailProviderResend,
|
|
EmailAccount: "primary",
|
|
EmailQuotaDaily: 100,
|
|
EmailQuotaMonthly: 3000,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("deliveryMetricsQuota(resend) error = %v", err)
|
|
}
|
|
if quota.Account.Provider() != "resend" || quota.Account.Account() != "primary" || quota.Limits.Daily() != 100 || quota.Limits.Monthly() != 3000 {
|
|
t.Fatalf("deliveryMetricsQuota(resend) = %+v", quota)
|
|
}
|
|
if _, err := deliveryMetricsQuota(config.Config{EmailProvider: config.EmailProviderSMTP, EmailAccount: "Not A Label", EmailQuotaDaily: 1, EmailQuotaMonthly: 1}); err == nil {
|
|
t.Fatal("deliveryMetricsQuota() accepted an invalid account label")
|
|
}
|
|
}
|
|
|
|
// TestApplicationRunsAndStopsTheDeliveryMetricsSampler starts the sampler
|
|
// against a database that does not answer: it logs stale gauges, and the
|
|
// application still stops within its shutdown budget.
|
|
func TestApplicationRunsAndStopsTheDeliveryMetricsSampler(t *testing.T) {
|
|
settings := consumerTestConfig(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)
|
|
sampler, cleanup, err := newDeliveryMetrics(t.Context(), settings, logger, metrics)
|
|
if err != nil {
|
|
t.Fatalf("newDeliveryMetrics() error = %v", err)
|
|
}
|
|
defer cleanup()
|
|
if sampler == nil {
|
|
t.Fatal("newDeliveryMetrics() built no sampler with DATABASE_URL set")
|
|
}
|
|
application := NewApplication(settings, http.NotFoundHandler(), metrics, logger, nil, nil, nil, nil, nil, sampler)
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
finished := make(chan error, 1)
|
|
go func() { finished <- application.run(ctx) }()
|
|
time.Sleep(50 * 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")
|
|
}
|
|
}
|