devbox/internal/daemon/pause_test.go
shoemoney 03469958d3
Some checks are pending
CI / vet-build-test (push) Waiting to run
chore: prep for public release — MIT license + github.com module path 🌍⚖️
- LICENSE: MIT (enables the adoption push; public repo was all-rights-reserved).
- go.mod + all imports: git.shoemoney.ai/shoemoney/devbox -> github.com/shoemoney/devbox,
  so outsiders can 'go install github.com/shoemoney/devbox/cmd/devbox@latest'. Forgejo
  stays the self-hosted home; the registry (devbox-hub) + public dist (devbox-dist) are
  unchanged. vet + build + go test -race all green after the rename.
- README: document the go install path.

Security: full git-history scan (manual blob sweep + gitleaks, 84 commits) found
ZERO secrets — Forgejo token never committed, no keys/passwords/private keys.
2026-06-23 19:19:09 -05:00

90 lines
2.7 KiB
Go

package daemon
import (
"context"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/shoemoney/devbox/internal/config"
"github.com/shoemoney/devbox/internal/hub"
"github.com/shoemoney/devbox/internal/hub/blobstore"
"github.com/shoemoney/devbox/internal/hub/meta"
"github.com/shoemoney/devbox/internal/transport"
)
// absent reports whether path stays absent for the whole window. It's the
// inverse of waitFor: it proves a change did NOT propagate.
func absent(path string, d time.Duration) bool {
deadline := time.Now().Add(d)
for time.Now().Before(deadline) {
if _, err := os.Stat(path); err == nil {
return false
}
time.Sleep(50 * time.Millisecond)
}
return true
}
// TestPauseGatesSyncing is the load-bearing pause test: while paused, a local
// edit must NOT reach the peer; after resume it must. If Pause() were wired but
// runMount still synced through the trigger, the "absent while paused" assertion
// would fail — so this test fails loudly if pause doesn't gate syncing.
func TestPauseGatesSyncing(t *testing.T) {
db, err := meta.Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
store, err := blobstore.NewDisk(t.TempDir())
if err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(hub.NewServer(db, store).Handler())
defer srv.Close()
bearerA := join(t, db, srv.URL, "alice")
bearerB := join(t, db, srv.URL, "bob")
ca := transport.New(srv.URL)
ca.SetBearer(bearerA)
if err := ca.Publish("s"); err != nil {
t.Fatal(err)
}
rootA, rootB := t.TempDir(), t.TempDir()
dirA := t.TempDir()
dirB := t.TempDir()
cfgA := config.Daemon{Hub: srv.URL, Bearer: bearerA, Mounts: []config.Mount{{Share: "s", Local: rootA, Hub: srv.URL}}}
cfgB := config.Daemon{Hub: srv.URL, Bearer: bearerB, Mounts: []config.Mount{{Share: "s", Local: rootB, Hub: srv.URL}}}
dA, err := New(dirA, cfgA, "alice", t.Logf)
if err != nil {
t.Fatal(err)
}
dB, err := New(dirB, cfgB, "bob", t.Logf)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go dA.Run(ctx)
go dB.Run(ctx)
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.
dA.Pause()
writeF(t, rootA, "while-paused.txt", "should not propagate yet\n")
peerPath := filepath.Join(rootB, "while-paused.txt")
if !absent(peerPath, 2*time.Second) {
t.Fatal("edit propagated while paused — Pause() did not gate syncing")
}
// Resume Alice: the buffered edit must now converge to Bob.
dA.Resume()
if !waitFor(peerPath, "should not propagate yet\n", 10*time.Second) {
t.Fatal("edit did not propagate after resume — Resume() did not catch up")
}
}