Files
magpie/internal/gui/update_progress_test.go
T
yetone e484b4ff26 update: a slow download still moving is never ended by a total time; Bun, Claude Code and pf-serve too
#5572: since 80a47abba a silent download is broken off after 30s and
tried again, but the update's download still had a 10-minute total
(the update client's Timeout, the GUI run's ctx, `magpie update`'s ctx).
A download under ~140 KB/s, still receiving bytes, failed the 83 MB
Windows exe at 10 minutes part way. The stall timer is what ends a dead
one now; nothing ends one that is still moving.

- internal/update: fetch downloads through fileClient, the client without
  its Timeout. The feed and notes keep the 10-minute client. fetch's stall
  timer runs from the ask to the last byte, so a server that never answers
  still ends at stallAfter (30s), and download's retries stay bounded
  (len(retryWaits)+1 = 6 tries), so a connection that keeps going silent
  ends in about 4 minutes.
- internal/gui (Mac Stage, Windows/Linux StageBinary): run's 10 minutes
  (checkWait, a var for tests) bound only LatestIn; the download runs
  under a ctx with no deadline.
- internal/cli `magpie update` (Mac Stage, Windows/Linux ReplaceBinary):
  the same split.
- Siblings with the same fixed cap:
  - new internal/stall: Start(ctx, after) gives a ctx that ends with
    ErrStalled once `after` goes by with no byte read through w.Reader.
  - Bun (internal/plugin getURLOnce): watched with a 60s stall;
    KeepBunUpdated's 10-minute ctx around CheckBun is gone.
  - Claude Code (internal/claudecode downloadOnce, ~200 MB): watched with
    a 60s stall; keepOnce's 30-minute ctx around install is gone.
  - pf-serve (internal/pii upgrade): its 10-minute ctx is gone; get
    already had a 60s stall and the client a 60s header timeout, as the
    model's download (no total) does.
- Left as they are: plugin installs and npm updates (KeepUpdated's 10
  minutes) run `bun install`, whose network is Bun's own, small tarballs;
  rtk (5 min client), skills (2 min client), skilltree and market reads
  are small files; Grok CLI install has no cap of its own (its caller's
  ctx). None of them carries a large download behind a wall-clock total.

Run on mote (mote-run), at 49f207c78 + this change; the targeted
set again after rebasing on d4a221f79 (ok):
- go test ./internal/stall ./internal/update ./internal/claudecode
  ./internal/pii -run 'Stall|Slow|Silen|TestDownload|TestInstall|TestKeep';
  go test ./internal/plugin -run Bun; go test ./internal/gui -run
  TestUpdate: ok
- new: TestSlowButMovingIsReadWhole, TestSilenceIsAStall (no answer, part
  way), TestDownloadSlowButMovingOutlastsTheClientTimeout (client Timeout
  300ms, body ~1.2s: one try, no retry), TestUpdateSlowDownloadOutlastsTheCheckBound
  (checkWait 200ms, body ~600ms: ready), TestDownloadThatGoesSilentIsAStall
  (claudecode), TestBunDownloadThatGoesSilentIsAStall. The silent server
  after its retries stays covered by TestDownloadStallEveryTimeSaysSo
  (6 tries, errStalled).
- go test -race -count=3 ./internal/stall ./internal/update -run
  'Stall|Slow|Silen': ok
- win-run (Windows 11): ./internal/stall ./internal/update
  ./internal/claudecode ./internal/plugin with the same -run, and
  ./internal/gui -run 'TestUpdateSlowDownload|TestUpdateDownloadSaysStepAndSpeed': ok
- wsl-run (Ubuntu 24.04), -tags nogui: the same packages and ./internal/gui
  -run TestUpdate: ok
- gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; go build; GOOS=linux go build -tags nogui;
  GOOS=windows go build: clean
- Full suite: run after the push under the suite lock.

Broken by hand: fetch back on `client` fails
TestDownloadSlowButMovingOutlastsTheClientTimeout ("broke off ...
Client.Timeout or context cancellation while reading body"); the GUI's
download back under the check's ctx fails
TestUpdateSlowDownloadOutlastsTheCheckBound (state error, "context
deadline exceeded"); stall's reader not putting the timer off fails
TestSlowButMovingIsReadWhole (50 of 120 bytes); Bun's and Claude Code's
downloads unwatched fail their silent tests at the 20s bound ("context
deadline exceeded").

Not tried: a real update over a slow or throttled link, on any OS; a real
Bun or Claude Code download over one. The Mac's unpack and signature
check now run with no deadline (they had the 10 minutes before).

Fixes #5572
2026-11-07 06:09:42 +08:00

191 lines
5.5 KiB
Go

package gui
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/update"
)
// esir_tech on Discord: after Update, the pill said 17% and sat there.
// While a download goes on, /api/update says its step and how fast the
// bytes come, so the pill and Settings' row show it moving.
func TestUpdateDownloadSaysStepAndSpeed(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
const chunk, chunks = 8 << 10, 40
body := make([]byte, chunk*chunks)
for i := range body {
body[i] = byte(i * 7)
}
sum := sha256.Sum256(body)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/asset" {
for i := 0; i < chunks; i++ {
if _, err := w.Write(body[i*chunk : (i+1)*chunk]); err != nil {
return
}
w.(http.Flusher).Flush()
time.Sleep(60 * time.Millisecond)
}
return
}
json.NewEncoder(w).Encode(update.Release{Version: "0.1.11", Assets: map[string]update.Asset{
update.BinaryAsset(): {URL: "http://" + r.Host + "/asset", Size: int64(len(body)), SHA256: hex.EncodeToString(sum[:])},
}})
}))
defer srv.Close()
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
oldV := Version
Version = "0.1.10"
defer func() { Version = oldV }()
exe, err := update.Executable()
if err != nil {
t.Fatal(err)
}
u := &updater{exe: exe}
u.self, _ = os.Stat(exe)
defer func() {
u.mu.Lock()
staged := u.staged
u.mu.Unlock()
if staged != "" {
os.Remove(staged)
}
}()
if !u.begin() {
t.Fatal("no check began")
}
ran := make(chan struct{})
go func() { u.run(); close(ran) }()
var moving []updateJSON
deadline := time.After(time.Minute)
for watching := true; watching; {
select {
case <-ran:
watching = false
case <-deadline:
t.Fatalf("still downloading: %+v", u.json())
case <-time.After(20 * time.Millisecond):
if j := u.json(); j.State == "downloading" && j.Step == update.StepDownload && j.Speed > 0 {
moving = append(moving, j)
}
}
}
if j := u.json(); j.State != "ready" {
t.Fatalf("after the download: %+v", j)
}
if len(moving) == 0 {
t.Fatal("the download never said how fast it went")
}
for _, j := range moving {
// at most every byte in the shortest span speedOf takes, a second
if j.Done <= 0 || j.Total != int64(len(body)) || j.Speed > j.Total {
t.Fatalf("while downloading: %+v", j)
}
}
}
// A download's speed is over its last seconds: none until a second is
// known, and falling to 0 while no bytes come.
func TestUpdateSpeedFallsWhenStalled(t *testing.T) {
t0 := time.Unix(1_800_000_000, 0)
var marks []progMark
for i := range 41 { // 100 KB every 100ms for 4s
marks = marked(marks, int64(i)*100_000, t0.Add(time.Duration(i)*100*time.Millisecond))
}
at := t0.Add(4 * time.Second)
if s := speedOf(marks, 4_000_000, at); s < 900_000 || s > 1_100_000 {
t.Fatalf("speed = %d, want about 1 MB/s", s)
}
if s := speedOf(marked(nil, 5, t0), 5, t0.Add(500*time.Millisecond)); s != 0 {
t.Fatalf("speed after half a second = %d, want none yet", s)
}
// stalled: the bytes stay at 4 MB
if s := speedOf(marks, 4_000_000, at.Add(3*time.Second)); s <= 0 || s >= 900_000 {
t.Fatalf("speed 3s into a stall = %d, want falling", s)
}
if s := speedOf(marks, 4_000_000, at.Add(10*time.Second)); s != 0 {
t.Fatalf("speed 10s into a stall = %d, want 0", s)
}
// a host that sent the file again from its start: the marks before go
if m := marked(marks, 1000, at.Add(time.Second)); len(m) != 1 {
t.Fatalf("marks after the bytes went back = %d, want 1", len(m))
}
}
// The update's check is bounded (checkWait), its download isn't: one
// still moving past that bound finishes (#5572, an 83 MB download under
// ~140 KB/s failed at 10 minutes part way). Here the bound is 200ms and
// the bytes take ~600ms.
func TestUpdateSlowDownloadOutlastsTheCheckBound(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
old := checkWait
checkWait = 200 * time.Millisecond
t.Cleanup(func() { checkWait = old })
const chunk, chunks = 4 << 10, 10
body := make([]byte, chunk*chunks)
for i := range body {
body[i] = byte(i * 5)
}
sum := sha256.Sum256(body)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/asset" {
for i := 0; i < chunks; i++ {
if _, err := w.Write(body[i*chunk : (i+1)*chunk]); err != nil {
return
}
w.(http.Flusher).Flush()
time.Sleep(60 * time.Millisecond)
}
return
}
json.NewEncoder(w).Encode(update.Release{Version: "0.1.11", Assets: map[string]update.Asset{
update.BinaryAsset(): {URL: "http://" + r.Host + "/asset", Size: int64(len(body)), SHA256: hex.EncodeToString(sum[:])},
}})
}))
defer srv.Close()
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
oldV := Version
Version = "0.1.10"
defer func() { Version = oldV }()
exe, err := update.Executable()
if err != nil {
t.Fatal(err)
}
u := &updater{exe: exe}
u.self, _ = os.Stat(exe)
defer func() {
u.mu.Lock()
staged := u.staged
u.mu.Unlock()
if staged != "" {
os.Remove(staged)
}
}()
if !u.begin() {
t.Fatal("no check began")
}
u.run()
if j := u.json(); j.State != "ready" {
t.Fatalf("after the download: %+v", j)
}
}