From 99216ef808cd7439842bea9623e2af39f5f8be95 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Thu, 5 Nov 2026 14:41:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BA=D1=80=D1=8B=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20listener=20API,=20=D0=B5=D1=81=D0=BB=D0=B8=20?= =?UTF-8?q?=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80=20=D0=BD=D0=B5=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=80=D1=82=D0=BE=D0=B2=D0=B0=D0=BB=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=81=D0=BB=D0=B5=20=D0=B5=D0=B3=D0=BE=20=D0=BE=D1=82=D0=BA?= =?UTF-8?q?=D1=80=D1=8B=D1=82=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server/main.go | 1 + cmd/server/main_test.go | 32 ++++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 4947a381..9d4f4b94 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -64,6 +64,7 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string) if err != nil { return fmt.Errorf("listen on %s: %w", cfg.Addr(), err) } + defer ln.Close() log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(), "gomaxprocs", runtime.GOMAXPROCS(0), "db_max_conns", cfg.DBMaxConns, "drain_delay", cfg.DrainDelay.String(), "shutdown_timeout", cfg.ShutdownTimeout.String(), diff --git a/cmd/server/main_test.go b/cmd/server/main_test.go index 3885495a..296607aa 100644 --- a/cmd/server/main_test.go +++ b/cmd/server/main_test.go @@ -40,6 +40,31 @@ func TestMain(m *testing.M) { // A start-up failure after the background work began must still end the // process with an error, not leave it waiting for a signal. func TestRunFailsFastWhenTheDebugListenerCannotStart(t *testing.T) { + if err := runWithBusyDebugAddr(t, 0); err == nil { + t.Fatal("run succeeded on a busy debug address") + } +} + +func TestFailedStartReleasesTheAPIPort(t *testing.T) { + probe, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + port := probe.Addr().(*net.TCPAddr).Port + probe.Close() + + if err := runWithBusyDebugAddr(t, port); err == nil { + t.Fatal("run succeeded on a busy debug address") + } + ln, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port)) + if err != nil { + t.Fatalf("the API port is still taken after the failed start: %v", err) + } + ln.Close() +} + +func runWithBusyDebugAddr(t *testing.T, port int) error { + t.Helper() busy, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) @@ -47,7 +72,7 @@ func TestRunFailsFastWhenTheDebugListenerCannotStart(t *testing.T) { defer busy.Close() cfg := config.Config{ Host: "127.0.0.1", - Port: 0, + Port: port, ShutdownTimeout: time.Second, MaxBodyBytes: 1024, DatabaseURL: db.DSN, @@ -63,10 +88,9 @@ func TestRunFailsFastWhenTheDebugListenerCannotStart(t *testing.T) { }() select { case err := <-done: - if err == nil { - t.Fatal("run succeeded on a busy debug address") - } + return err case <-time.After(10 * time.Second): t.Fatal("run hung instead of returning the listen error") + return nil } }