diff --git a/internal/gui/update.go b/internal/gui/update.go index c6f2ae1b2..e3677013d 100644 --- a/internal/gui/update.go +++ b/internal/gui/update.go @@ -483,8 +483,14 @@ func (u *updater) run() { } } // a version downloaded on a click stays the one restarted into while - // downloads wait for a click: a newer one out since isn't fetched - if u.staged != "" && u.latest != nil && (err != nil || !update.Newer(rel.Version, u.latest.Version) || hold) { + // downloads wait for a click: a newer one out since isn't fetched. + // Nor is one by the clock's check: magpie releases many times a day, + // so each check found a newer one and downloaded it whole (40 to 95 + // MB) in place of the one waiting, every check until a restart, on + // the user's proxy too (iiiiiimone on X). Whatever restarts into it + // asks the feed first (recheck, waitRecheck), and fetches the newest + // then; quitting puts in the one waiting. + if u.staged != "" && u.latest != nil && (err != nil || !update.Newer(rel.Version, u.latest.Version) || hold || u.byClock) { u.state = "ready" // what was downloaded is still the latest // the feed found it before it had its notes (the site, kept from // GitHub's API, reads them from the releases feed, which can lag diff --git a/internal/gui/update_test.go b/internal/gui/update_test.go index ea973f764..7bb6170b0 100644 --- a/internal/gui/update_test.go +++ b/internal/gui/update_test.go @@ -71,3 +71,66 @@ func TestUpdateStagedGivesWayToNewer(t *testing.T) { t.Fatalf("%+v, %d downloads", j, downloads.Load()) } } + +// The clock's check, with a version downloaded and waiting, doesn't +// download a newer one out since: magpie releases many times a day, and +// each check downloaded the whole app again on the user's proxy +// (iiiiiimone on X). A check asked for, and the feed asked again before a +// restart, still fetch the newest. +func TestUpdateClockLeavesWaitingDownload(t *testing.T) { + body := []byte("new magpie") + sum := sha256.Sum256(body) + var latest atomic.Value + latest.Store("0.1.11") + var downloads atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/asset" { + downloads.Add(1) + w.Write(body) + return + } + json.NewEncoder(w).Encode(update.Release{Version: latest.Load().(string), Assets: map[string]update.Asset{ + update.BinaryAsset(): {URL: "http://" + r.Host + "/asset", SHA256: hex.EncodeToString(sum[:])}, + }}) + })) + defer srv.Close() + t.Setenv("MAGPIE_UPDATE_FEED", srv.URL) + old := Version + Version = "0.1.10" + defer func() { Version = old }() + exe, err := update.Executable() + if err != nil { + t.Fatal(err) + } + u := &updater{exe: exe} + u.self, _ = os.Stat(exe) + defer os.Remove(exe + ".new") + + // nothing waiting: the clock's check downloads what it finds + u.checkByClock() + if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 { + t.Fatalf("%+v, %d downloads", j, downloads.Load()) + } + // newer ones out, check after check: the one waiting stays + for _, v := range []string{"0.1.12", "0.1.13", "0.1.14"} { + latest.Store(v) + u.checkByClock() + if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 || u.staged == "" { + t.Fatalf("%s out: %+v, %d downloads", v, j, downloads.Load()) + } + } + // the feed asked again before a restart: the newest is fetched + u.recheck(false) + for u.json().State == "downloading" || u.json().State == "checking" { + time.Sleep(10 * time.Millisecond) + } + if j := u.json(); j.State != "ready" || j.Latest != "0.1.14" || downloads.Load() != 2 { + t.Fatalf("recheck: %+v, %d downloads", j, downloads.Load()) + } + // and so is it on a check asked for + latest.Store("0.1.15") + u.check() + if j := u.json(); j.State != "ready" || j.Latest != "0.1.15" || downloads.Load() != 3 { + t.Fatalf("check: %+v, %d downloads", j, downloads.Load()) + } +}