test: join live sync daemons before fixture teardown

This commit is contained in:
shoemoney 2026-11-28 00:26:20 -06:00
parent 45fb7a365a
commit 8e06d83693
No known key found for this signature in database
2 changed files with 26 additions and 9 deletions

View file

@ -8,6 +8,7 @@ import (
"net/http/httptest" "net/http/httptest"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"testing" "testing"
"time" "time"
@ -18,6 +19,27 @@ import (
"github.com/shoemoney/devbox/internal/transport" "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 { func join(t *testing.T, db *meta.DB, url, name string) string {
t.Helper() t.Helper()
tok := name + "-tok" tok := name + "-tok"
@ -140,10 +162,8 @@ func TestLiveTwoWaySyncViaSSE(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ctx, cancel := context.WithCancel(context.Background()) stop := startDaemons(t, dA, dB)
defer cancel() defer stop()
go dA.Run(ctx)
go dB.Run(ctx)
time.Sleep(200 * time.Millisecond) // let both subscribe before the first edit time.Sleep(200 * time.Millisecond) // let both subscribe before the first edit
// Alice creates a file -> it should reach Bob automatically. // Alice creates a file -> it should reach Bob automatically.

View file

@ -1,7 +1,6 @@
package daemon package daemon
import ( import (
"context"
"net/http/httptest" "net/http/httptest"
"os" "os"
"path/filepath" "path/filepath"
@ -98,10 +97,8 @@ func TestPauseGatesSyncing(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ctx, cancel := context.WithCancel(context.Background()) stop := startDaemons(t, dA, dB)
defer cancel() defer stop()
go dA.Run(ctx)
go dB.Run(ctx)
time.Sleep(300 * time.Millisecond) // let both subscribe + do initial sync 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. // Pause Alice, then make a local edit. It must NOT reach Bob while paused.