package server_test import ( "context" "io" "log/slog" "net" "net/http" "testing" "time" "github.com/gin-gonic/gin" "messenger/internal/health" "messenger/internal/platform/httpx" "messenger/internal/server" ) // newHandler builds the smallest handler with probes; the lifecycle does not // depend on the business modules or their stores. func newHandler(log *slog.Logger) (*gin.Engine, *health.Handler) { e := httpx.NewEngine(log, 1024, "test") probes := health.New() probes.Register(e) return e, probes } func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) { log := slog.New(slog.NewTextHandler(io.Discard, nil)) h, probes := newHandler(log) ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) go func() { done <- server.Run(ctx, log, ln, h, probes, 0, 5*time.Second) }() base := "http://" + ln.Addr().String() waitFor(t, base+"/health/ready", http.StatusOK) cancel() select { case err := <-done: if err != nil { t.Fatalf("Run returned error: %v", err) } case <-time.After(10 * time.Second): t.Fatal("Run did not stop within 10s after cancel") } if _, err := http.Get(base + "/health/live"); err == nil { t.Fatal("listener must be closed after shutdown") } } func TestReadinessDropsBeforeListenerCloses(t *testing.T) { log := slog.New(slog.NewTextHandler(io.Discard, nil)) h, probes := newHandler(log) ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) go func() { done <- server.Run(ctx, log, ln, h, probes, 0, 5*time.Second) }() base := "http://" + ln.Addr().String() waitFor(t, base+"/health/ready", http.StatusOK) // Hold one request open so shutdown has something to drain. slow := make(chan struct{}) go func() { resp, err := http.Get(base + "/health/live") if err == nil { resp.Body.Close() } close(slow) }() cancel() <-done <-slow if !probes.IsReady() { return } t.Fatal("readiness must be switched off during shutdown") } // During the drain delay the balancer is still learning that the replica is // going away: readiness already fails, yet new connections are accepted and // served. Only after the delay does the listener close. func TestDrainKeepsServingWhileNotReady(t *testing.T) { log := slog.New(slog.NewTextHandler(io.Discard, nil)) h, probes := newHandler(log) ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) const drain = 700 * time.Millisecond go func() { done <- server.Run(ctx, log, ln, h, probes, drain, 5*time.Second) }() base := "http://" + ln.Addr().String() waitFor(t, base+"/health/ready", http.StatusOK) cancel() waitFor(t, base+"/health/ready", http.StatusServiceUnavailable) // A fresh connection, as the balancer opens for a new request. fresh := &http.Client{Transport: &http.Transport{DisableKeepAlives: true}} resp, err := fresh.Get(base + "/health/live") if err != nil { t.Fatalf("new connection during the drain delay: %v", err) } resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatalf("liveness during drain = %d, want 200", resp.StatusCode) } select { case err := <-done: if err != nil { t.Fatalf("Run returned error: %v", err) } case <-time.After(10 * time.Second): t.Fatal("Run did not stop after the drain delay") } if _, err := fresh.Get(base + "/health/live"); err == nil { t.Fatal("listener must be closed after the drain delay") } } func waitFor(t *testing.T, url string, status int) { t.Helper() deadline := time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { resp, err := http.Get(url) if err == nil { resp.Body.Close() if resp.StatusCode == status { return } } time.Sleep(20 * time.Millisecond) } t.Fatalf("%s did not return %d in time", url, status) } // The debug listener has no drain: it serves the benchmark's metrics scraper // only and stops with the process. func TestRunDebugServesUntilCancelled(t *testing.T) { log := slog.New(slog.NewTextHandler(io.Discard, nil)) ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusTeapot) }) ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) go func() { done <- server.RunDebug(ctx, log, ln, h) }() waitFor(t, "http://"+ln.Addr().String()+"/debug/vars", http.StatusTeapot) cancel() select { case err := <-done: if err != nil { t.Fatalf("RunDebug returned %v", err) } case <-time.After(2 * time.Second): t.Fatal("RunDebug did not stop after cancel") } }