test: join live sync daemons before fixture teardown
This commit is contained in:
parent
45fb7a365a
commit
8e06d83693
2 changed files with 26 additions and 9 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue