Add internal/control: an HTTP/1.1 control plane the daemon hosts on a 0600 Unix socket (control.sock) in the config dir — GET /state, GET /events (SSE), POST /pause|/resume — plus a stdlib net/http client (DialState/Pause/Resume) that returns a typed ErrNotRunning when the daemon is down so callers can fall back. Wire into the daemon: paused atomic.Bool gates runMount's trigger loop (drains while paused, no-ops the initial nudge), a per-mount nudge registry lets Resume catch every mount up, and StateSnapshot feeds /state. The control server starts in Run and is non-fatal — sync never depends on it. Windows gets a build-tagged stub (named-pipe upgrade path noted). CLI: devbox pause/resume call the client (friendly 'daemon not running' when absent); status prefers the live socket but ONLY on a TTY, so piped output stays byte-identical to v1 for scripts.
90 lines
2.7 KiB
Go
90 lines
2.7 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.shoemoney.ai/shoemoney/devbox/internal/config"
|
|
"git.shoemoney.ai/shoemoney/devbox/internal/hub"
|
|
"git.shoemoney.ai/shoemoney/devbox/internal/hub/blobstore"
|
|
"git.shoemoney.ai/shoemoney/devbox/internal/hub/meta"
|
|
"git.shoemoney.ai/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")
|
|
}
|
|
}
|