From 973c3108b71063ea932aed6c67e16e393b5e6c41 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 7 May 2026 00:03:30 +0300 Subject: task 81: replace test sleeps with deterministic sync --- cmd/player/main.go | 35 +++++++++++++++++++++-------------- cmd/player/main_test.go | 13 +++++++------ 2 files changed, 28 insertions(+), 20 deletions(-) (limited to 'cmd') diff --git a/cmd/player/main.go b/cmd/player/main.go index 66d2889..ea3de1b 100644 --- a/cmd/player/main.go +++ b/cmd/player/main.go @@ -35,20 +35,21 @@ func run(args []string) error { // appDeps bundles all wired service-layer dependencies. type appDeps struct { - store repository.Store - hasher auth.Hasher - sm *auth.SessionManager - cfg *internal.Config - clk clock.Clock - mediaSvc service.MediaService - adminSvc service.AdminService - progressSvc service.ProgressService - authSvc service.AuthService - podcastSvc service.PodcastEpisodeService - scanner scanner.Scanner - gcWorker *service.GCWorker - logger *slog.Logger - appCtx context.Context + store repository.Store + hasher auth.Hasher + sm *auth.SessionManager + cfg *internal.Config + clk clock.Clock + mediaSvc service.MediaService + adminSvc service.AdminService + progressSvc service.ProgressService + authSvc service.AuthService + podcastSvc service.PodcastEpisodeService + scanner scanner.Scanner + gcWorker *service.GCWorker + logger *slog.Logger + appCtx context.Context + workersStarted chan<- struct{} } // parseVersionFlag parses CLI flags and returns whether --version was requested. @@ -137,6 +138,12 @@ func startBackgroundWorkers(deps *appDeps) { } } }() + if deps.workersStarted != nil { + select { + case deps.workersStarted <- struct{}{}: + default: + } + } } // ensureSignalChannel returns the provided channel or creates a new one wired diff --git a/cmd/player/main_test.go b/cmd/player/main_test.go index 907e320..21b6ff0 100644 --- a/cmd/player/main_test.go +++ b/cmd/player/main_test.go @@ -106,9 +106,6 @@ func TestRunWithSignal_NormalShutdown(t *testing.T) { errCh <- runWithSignal([]string{}, sigCh) }() - // Give the server a moment to start listening. - time.Sleep(500 * time.Millisecond) - // Send a synthetic signal to trigger shutdown. sigCh <- syscall.SIGINT @@ -137,7 +134,6 @@ func TestRunWithSignal_LogLevels(t *testing.T) { go func() { errCh <- runWithSignal([]string{}, sigCh) }() - time.Sleep(200 * time.Millisecond) sigCh <- syscall.SIGTERM select { @@ -257,10 +253,15 @@ func TestStartBackgroundWorkers_StartsAndStops(t *testing.T) { defer cancel() deps := wireDeps(cfg, store, logger, ctx) + workersStarted := make(chan struct{}, 1) + deps.workersStarted = workersStarted startBackgroundWorkers(deps) - // Give the goroutines a moment to start. - time.Sleep(50 * time.Millisecond) + select { + case <-workersStarted: + case <-time.After(time.Second): + t.Fatal("timeout waiting for background workers to start") + } // Cancel the app context; workers should exit. cancel() -- cgit v1.2.3