diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-07 00:03:30 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-07 00:03:30 +0300 |
| commit | 973c3108b71063ea932aed6c67e16e393b5e6c41 (patch) | |
| tree | 0fdfc73724b1478aa6993fc4ef2e6dd6c2e4cc19 /cmd | |
| parent | 1f162963c0950cdf08858940cf43e21e5741937d (diff) | |
task 81: replace test sleeps with deterministic sync
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/player/main.go | 35 | ||||
| -rw-r--r-- | cmd/player/main_test.go | 13 |
2 files changed, 28 insertions, 20 deletions
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() |
