diff --git a/internal/gui/update.go b/internal/gui/update.go index e3677013d..debeb88d3 100644 --- a/internal/gui/update.go +++ b/internal/gui/update.go @@ -65,7 +65,10 @@ type updater struct { // fetch: the check under way is a click on Download update, which // downloads what it finds whatever Settings' NoAutoDownload says; with // it on, any other check stops at found (#2296) - fetch bool + fetch bool + // keep: the check under way leaves a version downloaded and waiting + // as it is, as the clock's does (freshen) + keep bool asked time.Time // when the feed was last asked, by the clock or a click done int64 // downloading: bytes so far, of total (0 when unknown) total int64 @@ -323,16 +326,35 @@ func (u *updater) download() bool { } // recheck asks the feed again before a restart into what was downloaded, -// so a newer version out since is the one restarted into. It waits for -// the feed a little while (recheckTicks of recheckTick); a newer version -// is then downloading. asked is a click on Restart to update, which asks -// for the newest: with Settings' NoAutoDownload on too, a newer version -// out since is downloaded and restarted into, not the one the last check +// with Settings' NoAutoDownload on, so a newer version out since is the +// one restarted into. It waits for the feed a little while (recheckTicks +// of recheckTick); a newer version is then downloading. asked is a click +// on Restart to update, which asks for the newest: a newer version out +// since is downloaded and restarted into, not the one the last check // found (coeo91 on Discord). +// +// With downloads automatic (the default) a version downloaded and waiting +// is restarted into as it is, and the feed isn't asked: magpie releases +// many times a day, so the click found a newer one nearly every time and +// downloaded it whole while the user waited, which made the download done +// ahead of time no use (canlee886 on X). The new version's first check, +// seconds after it starts, downloads whatever is newer by then. func (u *updater) recheck(asked bool) { + if u.keepsStaged(settings.Load()) { + return + } + u.ask(asked, false) +} + +// ask asks the feed and waits for it a little while (recheckTicks of +// recheckTick). fetch is a click's: what is found is downloaded whatever +// Settings' NoAutoDownload says. keep leaves a version downloaded and +// waiting as it is, as the clock's check does: a newer one found is not +// downloaded in its place. +func (u *updater) ask(fetch, keep bool) { if u.begin() { u.mu.Lock() - u.fetch = asked + u.fetch, u.keep = fetch, keep u.mu.Unlock() go u.run() } @@ -344,6 +366,15 @@ func (u *updater) recheck(asked bool) { } } +// keepsStaged is whether a restart goes into the version downloaded and +// waiting without asking the feed again: downloads are automatic and one +// is ready (recheck). +func (u *updater) keepsStaged(s settings.Settings) bool { + u.mu.Lock() + defer u.mu.Unlock() + return !s.NoAutoDownload && u.state == "ready" && u.staged != "" +} + // askedOver is whether the feed was last asked over d ago. func (u *updater) askedOver(d time.Duration) bool { u.mu.Lock() @@ -372,13 +403,18 @@ func (u *updater) asking() bool { const freshAfter = 5 * time.Minute // freshen asks the feed again, as recheck does, when a version downloaded -// waits and the feed was last asked over freshAfter ago (#910). +// waits and the feed was last asked over freshAfter ago (#910). With +// downloads automatic recheck leaves the one waiting, which is what the +// dialog then names and the restart goes into (canlee886 on X). func (u *updater) freshen() { u.mu.Lock() stale := u.state == "ready" && u.staged != "" && time.Since(u.asked) >= freshAfter u.mu.Unlock() if stale { - u.recheck(false) // the dialog asks for nothing to be downloaded + // the dialog asks for nothing to be downloaded; with downloads + // automatic it names the one waiting, which the restart goes + // into, and takes the notes the feed has for it now (#649) + u.ask(false, !settings.Load().NoAutoDownload) } } @@ -421,7 +457,7 @@ func (u *updater) begin() bool { if u.state == "checking" || u.state == "downloading" { return false } - u.state, u.err, u.retry, u.mirror, u.network, u.asked, u.byClock, u.fetch = "checking", "", false, "", false, time.Now(), false, false + u.state, u.err, u.retry, u.mirror, u.network, u.asked, u.byClock, u.fetch, u.keep = "checking", "", false, "", false, time.Now(), false, false, false return true } @@ -487,10 +523,12 @@ func (u *updater) run() { // 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) { + // the user's proxy too (iiiiiimone on X). With downloads automatic a + // restart goes into the one waiting and the new version's first check + // fetches anything newer (recheck, keep, canlee886 on X); with them on + // a click, a restart asks the feed first 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.keep) { 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_fresh_test.go b/internal/gui/update_fresh_test.go index 984e16dc8..9ba193683 100644 --- a/internal/gui/update_fresh_test.go +++ b/internal/gui/update_fresh_test.go @@ -31,6 +31,7 @@ type freshFeed struct { srv *httptest.Server latest atomic.Value downloads atomic.Int32 + asked atomic.Int32 // the feed's answers mu sync.Mutex hold chan struct{} } @@ -58,6 +59,7 @@ func newFreshFeed(t *testing.T) *freshFeed { if hold != nil { <-hold } + f.asked.Add(1) v := f.latest.Load().(string) json.NewEncoder(w).Encode(update.Release{Version: v, URL: "https://example.invalid/v" + v, Assets: map[string]update.Asset{ update.BinaryAsset(): {URL: "http://" + r.Host + "/asset", Size: int64(len(body)), SHA256: hex.EncodeToString(sum[:])}, @@ -95,6 +97,17 @@ func freshUpdater(t *testing.T, exe bool) *updater { return u } +// downloadsOnClick turns Settings' Download updates automatically off, +// in a HOME of the test's own (newFreshFeed's, or sandboxHome's). +func downloadsOnClick(t *testing.T) { + t.Helper() + s := settings.Load() + s.NoAutoDownload = true + if err := settings.Save(s); err != nil { + t.Fatal(err) + } +} + // settled waits for u's check or download to end. func settled(t *testing.T, u *updater) updateJSON { t.Helper() @@ -140,11 +153,7 @@ func postInstall(t *testing.T, w Windows) updateJSON { // restarts into it, rather than into 0.1.11. func TestRestartClickTakesTheNewest(t *testing.T) { f := newFreshFeed(t) - s := settings.Load() - s.NoAutoDownload = true - if err := settings.Save(s); err != nil { - t.Fatal(err) - } + downloadsOnClick(t) u := freshUpdater(t, true) u.check() if j := u.json(); j.State != "found" || j.Latest != "0.1.11" { @@ -169,9 +178,10 @@ func TestRestartClickTakesTheNewest(t *testing.T) { } } -// A feed slower than the restart's wait for it: the restart doesn't go -// into the download it has, which the answer may have made old; it says -// it is checking, and the answer, once in, is downloaded. +// A feed slower than the restart's wait for it, with downloads on a +// click: the restart doesn't go into the download it has, which the +// answer may have made old; it says it is checking, and the answer, once +// in, is downloaded. func TestRestartWaitsForTheFeed(t *testing.T) { f := newFreshFeed(t) u := freshUpdater(t, true) @@ -182,6 +192,7 @@ func TestRestartWaitsForTheFeed(t *testing.T) { if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" { t.Fatalf("the check: %+v", j) } + downloadsOnClick(t) f.latest.Store("0.1.12") hold := make(chan struct{}) f.mu.Lock() @@ -201,7 +212,8 @@ func TestRestartWaitsForTheFeed(t *testing.T) { } // The tray's Restart to update asks the feed as the page's does: with -// 0.1.12 out since, it is downloaded and restarted into, once. +// downloads on a click and 0.1.12 out since, it is downloaded and +// restarted into, once. func TestTrayRestartTakesTheNewest(t *testing.T) { busy := &fakeBusy{} u := freshUpdater(t, true) @@ -209,6 +221,7 @@ func TestTrayRestartTakesTheNewest(t *testing.T) { waitRecheck = (*updater).recheck f := newFreshFeed(t) u.check() + downloadsOnClick(t) if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" { t.Fatalf("the check: %+v", j) } @@ -262,3 +275,87 @@ func TestAvailableOpensTheNewestPage(t *testing.T) { t.Fatalf("opened %q, want %q", w.urls, want) } } + +// canlee886 on X: with downloads automatic, the version downloaded ahead +// of time is what Restart to update goes into, at once: magpie releases +// many times a day, so asking the feed at the click found a newer one +// nearly every time and downloaded it whole while the user waited. The +// page's click, the tray's, the restart Settings' AutoRestart waits for +// and the dialog that shows the update all leave the one waiting, with +// nothing downloaded again; the clock's next check is what takes the +// newer one (here, the new version's first). +func TestRestartTakesTheDownloadWaiting(t *testing.T) { + busy := &fakeBusy{} + u := freshUpdater(t, true) + quick(t, busy, u) + waitRecheck = (*updater).recheck + f := newFreshFeed(t) + u.checkByClock() + if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || f.downloads.Load() != 1 { + t.Fatalf("the clock's check: %+v, %d downloads", j, f.downloads.Load()) + } + f.latest.Store("0.1.12") + asked := f.asked.Load() + unchanged := func(what string) { + t.Helper() + if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || f.downloads.Load() != 1 || u.staged == "" { + t.Fatalf("%s: %+v, %d downloads", what, j, f.downloads.Load()) + } + } + + // the page's Restart to update: no wait on the feed, nothing downloaded + if j := postInstall(t, &quitCount{}); j.State != "ready" || j.Latest != "0.1.11" { + t.Fatalf("the page's click: %+v", j) + } + unchanged("the page's click") + if n := f.asked.Load(); n != asked { + t.Fatalf("the click asked the feed %d times", n-asked) + } + + // the tray's, at once into the one waiting + into := make(chan string, 4) + restart := func() bool { + u.mu.Lock() + into <- u.latest.Version + u.mu.Unlock() + return true + } + u.restartAsked(restart) + select { + case v := <-into: + if v != "0.1.11" { + t.Fatalf("the tray restarted into %s", v) + } + default: + t.Fatalf("the tray didn't restart at once: %+v", u.json()) + } + unchanged("the tray's click") + + // AutoRestart's wait, once the gateway is idle + u.waitIdle(restart, false) + select { + case v := <-into: + if v != "0.1.11" { + t.Fatalf("AutoRestart restarted into %s", v) + } + case <-time.After(5 * time.Second): + t.Fatalf("AutoRestart never restarted: %+v", u.json()) + } + unchanged("AutoRestart") + + // the dialog, its feed stale: asked again, the one waiting named + u.mu.Lock() + u.asked = time.Now().Add(-time.Hour) + u.mu.Unlock() + u.freshen() + if n := f.asked.Load(); n == asked { + t.Fatal("the dialog didn't ask the stale feed again") + } + unchanged("the dialog") + + // a check asked for in Settings still takes the newest + u.check() + if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 2 { + t.Fatalf("Settings' Check: %+v, %d downloads", j, f.downloads.Load()) + } +} diff --git a/internal/gui/update_notes_test.go b/internal/gui/update_notes_test.go index 07fb91341..b31bbd102 100644 --- a/internal/gui/update_notes_test.go +++ b/internal/gui/update_notes_test.go @@ -77,9 +77,12 @@ func TestUpdateNotesRoute(t *testing.T) { // The dialog asks the feed again when it was last asked a while ago (#910, // Moody-Sin: it stopped at v0.1.1020, downloaded hours before, with -// v0.1.1024 out): the newest release is the one it names, with the notes -// of every release up to it. +// v0.1.1024 out). With downloads automatic the version downloaded is what +// the restart goes into at once (canlee886 on X), so that is the one it +// names, with the notes of every release up to it; the new version's +// first check takes v0.1.1024. func TestUpdateNotesAskTheFeedAgain(t *testing.T) { + sandboxHome(t) feedAsked := 0 feed := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { feedAsked++ @@ -100,8 +103,7 @@ func TestUpdateNotesAskTheFeedAgain(t *testing.T) { updates.mu.Lock() oldState, oldStaged, oldIn, oldLatest, oldAsked, oldV := updates.state, updates.staged, updates.notesIn, updates.latest, updates.asked, Version Version = "0.1.1010" - // downloaded hours ago; this test's updater can't replace the app, so - // the newer release is left "available" rather than downloaded + // downloaded hours ago updates.state, updates.staged, updates.notesIn = "ready", filepath.Join(t.TempDir(), "staged"), "en" updates.latest, updates.asked = &update.Release{Version: "0.1.1020", Notes: "- twenty"}, time.Now().Add(-3*time.Hour) updates.mu.Unlock() @@ -121,8 +123,8 @@ func TestUpdateNotesAskTheFeedAgain(t *testing.T) { json.Unmarshal(rw.Body.Bytes(), &out) return out } - if j := get(); j.Latest != "0.1.1024" || len(j.Releases) != 2 || j.Releases[0].Version != "0.1.1024" { - t.Fatalf("stale: %+v", j) + if j := get(); j.Latest != "0.1.1020" || feedAsked != 1 || len(j.Releases) != 1 || j.Releases[0].Version != "0.1.1020" { + t.Fatalf("stale: %+v, feed asked %d", j, feedAsked) } // asked a moment ago: not again @@ -162,14 +164,15 @@ func TestUpdateRecheckTakesNotesLate(t *testing.T) { updates.latest = &update.Release{Version: "0.1.1224", Notes: held, URL: "https://example.invalid/v0.1.1224"} updates.mu.Unlock() } + // the dialog's look at a stale feed, the version downloaded the newest hold("") - updates.recheck(false) + updates.freshen() if j := updates.json(); j.State != "ready" || j.Latest != "0.1.1224" || !strings.Contains(j.Notes, "twenty-four") || j.URL == "" { t.Fatalf("empty notes not taken from the recheck: %+v", j) } hold("- held") - updates.recheck(false) + updates.freshen() if j := updates.json(); j.Notes != "- held" { t.Fatalf("held notes replaced: %+v", j) } diff --git a/internal/gui/update_test.go b/internal/gui/update_test.go index 7bb6170b0..428d84ff6 100644 --- a/internal/gui/update_test.go +++ b/internal/gui/update_test.go @@ -15,8 +15,11 @@ import ( ) // A version downloaded and waiting for a restart gives way to a newer one -// out since, so the restart goes straight to the latest. +// out since, so the restart goes straight to the latest, with downloads on +// a click (with them automatic it is restarted into as it is: +// TestRestartTakesTheDownloadWaiting). func TestUpdateStagedGivesWayToNewer(t *testing.T) { + sandboxHome(t) body := []byte("new magpie") sum := sha256.Sum256(body) var latest atomic.Value @@ -63,7 +66,8 @@ func TestUpdateStagedGivesWayToNewer(t *testing.T) { t.Setenv("MAGPIE_UPDATE_FEED", srv.URL) // 0.1.13 out: that is the one it restarts into latest.Store("0.1.13") - u.recheck(false) + downloadsOnClick(t) + u.recheck(true) for u.json().State == "downloading" || u.json().State == "checking" { time.Sleep(10 * time.Millisecond) } @@ -75,9 +79,11 @@ func TestUpdateStagedGivesWayToNewer(t *testing.T) { // 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. +// (iiiiiimone on X). Nor does the feed asked again before a restart, with +// downloads automatic (canlee886 on X). A check asked for still fetches +// the newest. func TestUpdateClockLeavesWaitingDownload(t *testing.T) { + sandboxHome(t) body := []byte("new magpie") sum := sha256.Sum256(body) var latest atomic.Value @@ -119,18 +125,15 @@ func TestUpdateClockLeavesWaitingDownload(t *testing.T) { 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 { + // the restart's recheck: the one waiting is what it goes into + u.recheck(true) + if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 { t.Fatalf("recheck: %+v, %d downloads", j, downloads.Load()) } - // and so is it on a check asked for + // a check asked for takes the newest latest.Store("0.1.15") u.check() - if j := u.json(); j.State != "ready" || j.Latest != "0.1.15" || downloads.Load() != 3 { + if j := u.json(); j.State != "ready" || j.Latest != "0.1.15" || downloads.Load() != 2 { t.Fatalf("check: %+v, %d downloads", j, downloads.Load()) } } diff --git a/internal/gui/update_wait.go b/internal/gui/update_wait.go index 96a0ed7d9..264d70553 100644 --- a/internal/gui/update_wait.go +++ b/internal/gui/update_wait.go @@ -203,7 +203,8 @@ func (u *updater) waitFor(gen int) { switch st := u.json().State; { case in: case st == "ready": - // a newer version out since is downloaded first, and waited for + // with Settings' NoAutoDownload, a newer version out since is + // downloaded first, and waited for (recheck) waitRecheck(u, asked) if u.asking() || u.json().State != "ready" { continue @@ -257,8 +258,9 @@ func (u *updater) autoTick(s settings.Settings) { // gateway busy it waits for it to be idle (#577), and a click while it // waits restarts at once. The feed is asked first, as the page's restart // asks it (coeo91 on Discord: the tray restarted into the version the -// last check found): a newer version out since is downloaded, and the -// restart waits for it. +// last check found): with Settings' NoAutoDownload a newer version out +// since is downloaded, and the restart waits for it; with downloads +// automatic the one waiting is restarted into (recheck). func (u *updater) restartAsked(restart func() bool) { if waiting, _ := u.waitingFor(); !waiting && !u.idle() { u.waitIdle(restart, true) diff --git a/internal/gui/update_wait_test.go b/internal/gui/update_wait_test.go index 6fd615002..d24e84a72 100644 --- a/internal/gui/update_wait_test.go +++ b/internal/gui/update_wait_test.go @@ -163,8 +163,11 @@ func TestRestartWhenIdle(t *testing.T) { } // Versions out while a restart waits collapse into the newest: the restart -// is into the last one, downloaded once the gateway is idle, not into each. +// is into the last one, downloaded once the gateway is idle, not into each, +// for a click's restart with downloads on a click (with them automatic the +// one waiting is restarted into: TestRestartTakesTheDownloadWaiting). func TestRestartWhenIdleIntoTheNewest(t *testing.T) { + sandboxHome(t) busy := &fakeBusy{} u := &updater{} quick(t, busy, u) @@ -198,6 +201,7 @@ func TestRestartWhenIdleIntoTheNewest(t *testing.T) { if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" { t.Fatalf("%+v", j) } + downloadsOnClick(t) busy.set(gateway.Busy{Requests: 1}) into := make(chan string, 4) u.waitIdle(func() bool { @@ -205,7 +209,7 @@ func TestRestartWhenIdleIntoTheNewest(t *testing.T) { into <- u.latest.Version u.mu.Unlock() return true - }, false) + }, true) latest.Store("0.1.12") time.Sleep(30 * time.Millisecond) latest.Store("0.1.13")