Files
magpie/internal/gui/daemon_nudge_test.go
T
yetone 0af73f42b4 gateway: an app update asks its own background service to restart, and the page no longer calls it another program (Norethcy on X)
After an app update the service (`magpie daemon`, ~/.local/bin/magpie)
ran on as the old version until its own look at the disk found the gateway
idle, and `magpie daemon restart` by hand showed the app "Port 3425 is in
use by ~/.local/bin/magpie (pid N): quit it" while the new service came up.

- PortHeld.Own: a holder that is a magpie running this install's program
  (same file, symlinks followed) is marked; the Gateway card then says
  "Starting the gateway…" instead of the port-in-use error.
- The app asks an older service of its own install (the pid the service
  manager reports, program from the unit/plist: daemon.Program) to restart,
  once per process, through gateway.RestartPath, as `magpie update` does:
  it restarts into the new version once idle, bounded by its drain, under
  the same service manager.

Run on mote: go test of the new tests in internal/gui, internal/daemon,
internal/gateway; go vet darwin, linux nogui, windows; gofmt; gateway-*
and i18n GUI files in chromium and webkit. Broken by hand: dropping the
Older check makes TestNudgeAsksOwnOlderDaemonOnce red.
Not tried: a real app update over a real launchd service. The restart is
an in-place re-exec of the service, not a socket handover, so the port is
closed for the service's start-up.

GUI (mote, chromium+webkit): gateway-port-holder, older, skew, takeover, port, i18n-keys, i18n-en-no-cjk 36 of 36 ok. All gateway-* files: 30 reds, all in gateway-card (shares on the network, #6267) and gateway-key-arrange (420px, #6268); the same 30 with client.js reverted to origin/main, so not from this change.
Not tried: a real app update over a real launchd service; the daemon re-execs in place, it is not a socket handover.
2026-11-12 12:50:57 +08:00

55 lines
1.6 KiB
Go

package gui
import (
"testing"
"time"
"github.com/yetone/magpie/internal/gateway"
)
// Norethcy on X: the app updated and the background service stayed on the
// version before, until `magpie daemon restart` by hand. The app now asks
// its own service, once, to restart into the version on disk.
func TestNudgeAsksOwnOlderDaemonOnce(t *testing.T) {
was := gateway.Version
gateway.Version = "0.1.2000"
defer func() { gateway.Version = was }()
wasOwn, wasAsk := ownDaemon, askRestart
defer func() { ownDaemon, askRestart = wasOwn, wasAsk }()
nudgedMu.Lock()
nudged = map[int]bool{}
nudgedMu.Unlock()
asked := make(chan string, 4)
askRestart = func(v string) (gateway.RestartReply, error) {
asked <- v
return gateway.RestartReply{State: "restarting"}, nil
}
own := true
ownDaemon = func(gateway.Served) bool { return own }
older := gateway.Served{Running: true, Version: "0.1.1990", Pid: 4321, Serves: "serve"}
nudgeDaemon(older, skewOf(older))
select {
case v := <-asked:
if v != "0.1.2000" {
t.Errorf("asked to restart into %q", v)
}
case <-time.After(3 * time.Second):
t.Fatal("an older service of this install wasn't asked to restart")
}
// the same process is asked once
nudgeDaemon(older, skewOf(older))
// a newer one, and one that isn't this install's service, never
newer := gateway.Served{Running: true, Version: "0.1.2010", Pid: 4322, Serves: "serve"}
nudgeDaemon(newer, skewOf(newer))
own = false
other := gateway.Served{Running: true, Version: "0.1.1990", Pid: 4323, Serves: "serve"}
nudgeDaemon(other, skewOf(other))
select {
case v := <-asked:
t.Fatalf("asked again: %q", v)
case <-time.After(300 * time.Millisecond):
}
}