diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go index eeb1cbc..26ddfc7 100644 --- a/internal/daemon/daemon_test.go +++ b/internal/daemon/daemon_test.go @@ -8,6 +8,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "sync" "testing" "time" @@ -18,6 +19,27 @@ import ( "github.com/shoemoney/devbox/internal/transport" ) +// startDaemons returns a shutdown function that must be deferred after the hub +// and database close defers, so Run finishes before its dependencies or t expire. +func startDaemons(t *testing.T, daemons ...*Daemon) func() { + t.Helper() + ctx, cancel := context.WithCancel(context.Background()) + var wg sync.WaitGroup + for _, d := range daemons { + wg.Add(1) + go func() { + defer wg.Done() + if err := d.Run(ctx); err != nil { + t.Errorf("daemon Run: %v", err) + } + }() + } + return func() { + cancel() + wg.Wait() + } +} + func join(t *testing.T, db *meta.DB, url, name string) string { t.Helper() tok := name + "-tok" @@ -140,10 +162,8 @@ func TestLiveTwoWaySyncViaSSE(t *testing.T) { if err != nil { t.Fatal(err) } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - go dA.Run(ctx) - go dB.Run(ctx) + stop := startDaemons(t, dA, dB) + defer stop() time.Sleep(200 * time.Millisecond) // let both subscribe before the first edit // Alice creates a file -> it should reach Bob automatically. diff --git a/internal/daemon/pause_test.go b/internal/daemon/pause_test.go index 066359d..7a22119 100644 --- a/internal/daemon/pause_test.go +++ b/internal/daemon/pause_test.go @@ -1,7 +1,6 @@ package daemon import ( - "context" "net/http/httptest" "os" "path/filepath" @@ -98,10 +97,8 @@ func TestPauseGatesSyncing(t *testing.T) { if err != nil { t.Fatal(err) } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - go dA.Run(ctx) - go dB.Run(ctx) + stop := startDaemons(t, dA, dB) + defer stop() time.Sleep(300 * time.Millisecond) // let both subscribe + do initial sync // Pause Alice, then make a local edit. It must NOT reach Bob while paused.