Files
magpie/internal/gui/update_progress_test.go
T
yetone 80a47abba8 update: a download says its speed and each step after it, and a silent connection is tried again (esir_tech)
esir_tech on Discord: after clicking Update the progress sat on 17% and
looked stuck. Cause: nothing watched a download that stopped sending. A
connection GitHub's asset host keeps open but silent (seen from some
networks) held the percent still until the client's 10-minute timeout,
then failed. The waits between retries showed the same still percent, and
after the bytes the Mac's unpack and codesign check showed a stale 100%.

- internal/update: fetch cancels a body that sends nothing for 30s
  (stallAfter) as a network error, so download's existing retry resumes it
  with Range. A step callback (update.WithStep) names download, retry,
  verify, unpack and signature.
- internal/gui: /api/update's downloading answer carries step, and speed
  (bytes/s over the last ~5s; it falls to 0 when bytes stop).
- app.js: the pill says "Downloading… 50% · 1.5 MB/s", "Reconnecting…",
  then "Verifying…", "Unpacking…", "Checking signature…"; its tooltip
  carries the text where the tight header hides it. Settings' Version row
  says the same after its MB count. i18n in zh, zh-TW, ja, de. Comments
  cut to stay under the core script budget (not raised).

Run on mote (mote-run):
- go test ./internal/update ./internal/gui -run 'TestDownload|TestUpdate|TestStage|Budget': ok
- new: TestDownloadStallIsTriedAgain, TestDownloadStallEveryTimeSaysSo,
  TestUpdateDownloadSaysStepAndSpeed, TestUpdateSpeedFallsWhenStalled
- GUI, chromium and webkit: update-download-steps (new: en, zh, zh-TW, ja,
  de at 900 and 420px), update-download-percent, update-download,
  update-row-steady, update-stuck, update-network, update-mirror: 178 pass
- i18n-en-no-cjk, header-relang: pass. i18n-keys "a plugin's words are
  its own" is red on origin/main 0d70f335e too (plugins/library,
  routing, usage i18n.js vs Go strings), untouched here.
- gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...: clean

Broken by hand: with the idle timer a no-op, TestDownloadStallIsTriedAgain
and TestDownloadStallEveryTimeSaysSo fail at their 20s bound (context
deadline exceeded); with speed left out of the answer
TestUpdateDownloadSaysStepAndSpeed fails ("never said how fast"); on the
old app.js/i18n.js update-download-steps fails in every case.

Not tried: a real update on a stalling network, a real Mac unpack and
codesign step (tests cover download/verify only), Windows/Linux runs.
The 10-minute total cap (client Timeout, GUI run ctx, CLI update) is
unchanged, so a moving download slower than ~140KB/s for the 83MB
Windows exe still fails at 10 minutes.

Reported-by: esir_tech
2026-11-07 05:02:02 +08:00

128 lines
3.7 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))
}
}