diff --git a/internal/gui/update.go b/internal/gui/update.go index ac32a6711..19fecdcab 100644 --- a/internal/gui/update.go +++ b/internal/gui/update.go @@ -68,19 +68,13 @@ type updater struct { fetch bool // keep: the check under way leaves a version downloaded and waiting // as it is, as the clock's does (freshen) - keep bool - // forRestart: the check under way is a restart's (recheck); what it - // downloads is staged at restartStaged, and a restart up to - // freshAfter later goes into it without asking the feed again - // (elis_pina on X) - forRestart bool - restartStaged time.Time - 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 - step string // downloading: the step under way (update.StepDownload, …) - rate []progMark // downloading: the last few seconds' progress, for its speed - onReady func(version string) + 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 + step string // downloading: the step under way (update.StepDownload, …) + rate []progMark // downloading: the last few seconds' progress, for its speed + onReady func(version string) // the waiting update's notes follow the pages' language (freecss on // Discord): the one they last asked in, else the app's lang string // the pages' language, "" before one asked @@ -331,31 +325,28 @@ func (u *updater) download() bool { return true } -// recheck asks the feed again before a restart into what was downloaded, -// 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). +// recheck asks the feed before a restart that has no version downloaded +// and waiting to go into (what another magpie put in, say). It waits for +// the feed a little while (recheckTicks of recheckTick); a newer version +// is then downloading. asked is a click, which downloads what is found +// whatever Settings' NoAutoDownload says (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. -// -// A version this asking itself downloaded is restarted into as it is too, -// for freshAfter. The header's Update restarts once its download is in, -// and that restart asked the feed again; magpie had released again while -// a slow download came in, so it downloaded once more, every time, and -// never restarted (elis_pina on X, on Windows). +// A version downloaded and waiting is restarted into as it is, and the +// feed isn't asked, however it came to be downloaded: by itself (the +// default), by a click on Download update, or by a restart's own asking. +// magpie releases many times a day, so a click that asked found a newer +// one nearly every time and downloaded it whole while the user waited: +// the download done ahead of time was no use (canlee886 on X), the +// header's Update downloaded again and again and never restarted +// (elis_pina on X, on Windows), and Restart after Download update went +// off downloading again (sakamotokurome on X, with downloads on a click: +// "重启能不能跟更新分开"). The new version's first check, seconds after it +// starts, finds whatever is newer by then. func (u *updater) recheck(asked bool) { - if u.keepsStaged(settings.Load()) { + if u.keepsStaged() { return } - u.askFor(asked, false, true) + u.ask(asked, false) } // ask asks the feed and waits for it a little while (recheckTicks of @@ -363,13 +354,10 @@ func (u *updater) recheck(asked bool) { // 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) { u.askFor(fetch, keep, false) } - -// askFor is ask; forRestart, a restart's (recheck). -func (u *updater) askFor(fetch, keep, forRestart bool) { +func (u *updater) ask(fetch, keep bool) { if u.begin() { u.mu.Lock() - u.fetch, u.keep, u.forRestart = fetch, keep, forRestart + u.fetch, u.keep = fetch, keep u.mu.Unlock() go u.run() } @@ -382,16 +370,11 @@ func (u *updater) askFor(fetch, keep, forRestart bool) { } // keepsStaged is whether a restart goes into the version downloaded and -// waiting without asking the feed again: one is ready, and downloads are -// automatic or a restart's asking downloaded it under freshAfter ago -// (recheck). -func (u *updater) keepsStaged(s settings.Settings) bool { +// waiting without asking the feed again: whenever one is (recheck). +func (u *updater) keepsStaged() bool { u.mu.Lock() defer u.mu.Unlock() - if u.state != "ready" || u.staged == "" { - return false - } - return !s.NoAutoDownload || !u.restartStaged.IsZero() && time.Since(u.restartStaged) < freshAfter + return u.state == "ready" && u.staged != "" } // askedOver is whether the feed was last asked over d ago. @@ -421,19 +404,19 @@ func (u *updater) asking() bool { // that shows the update before it is put in. 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). With -// downloads automatic recheck leaves the one waiting, which is what the -// dialog then names and the restart goes into (canlee886 on X). +// freshen asks the feed again when a version downloaded waits and the +// feed was last asked over freshAfter ago (#910), for the notes. recheck +// leaves the one waiting, which is what the dialog then names and the +// restart goes into (canlee886, sakamotokurome 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 { - // 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.Shared().NoAutoDownload) + // the dialog asks for nothing to be downloaded; it names the one + // waiting, which the restart goes into, and takes the notes the + // feed has for it now (#649) + u.ask(false, true) } } @@ -476,7 +459,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, u.keep, u.forRestart = "checking", "", false, "", false, time.Now(), false, false, 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 } @@ -545,9 +528,9 @@ func (u *updater) run() { // MB) in place of the one waiting, every check until a restart, on // 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. + // fetches anything newer (recheck, keep, canlee886 on X), and so with + // them on a click (sakamotokurome on X). 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 @@ -604,8 +587,7 @@ func (u *updater) run() { } // an older download is replaced by this one: until it is in, there is // nothing to install - u.state, u.done, u.total, u.staged, u.step, u.rate, u.restartStaged = "downloading", 0, 0, "", "", nil, time.Time{} - forRestart := u.forRestart + u.state, u.done, u.total, u.staged, u.step, u.rate = "downloading", 0, 0, "", "", nil u.mu.Unlock() ctx = update.WithProgress(ctx, func(done, total int64) { u.mu.Lock() @@ -631,9 +613,6 @@ func (u *updater) run() { return } u.state, u.staged = "ready", staged - if forRestart { - u.restartStaged = time.Now() - } if u.onReady != nil { go u.onReady(rel.Version) } diff --git a/internal/gui/update_fresh_test.go b/internal/gui/update_fresh_test.go index 2c133326a..b8369da91 100644 --- a/internal/gui/update_fresh_test.go +++ b/internal/gui/update_fresh_test.go @@ -14,7 +14,6 @@ import ( "testing" "time" - "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/settings" "github.com/yetone/magpie/internal/update" ) @@ -148,9 +147,10 @@ func postInstall(t *testing.T, w Windows) updateJSON { return j } -// With Settings' Download updates automatically off, 0.1.11 downloaded on -// a click and 0.1.12 out since: Restart to update downloads 0.1.12 and -// restarts into it, rather than into 0.1.11. +// With Settings' Download updates automatically off, 0.1.11 found by a +// check and 0.1.12 out since: a click on Download update downloads +// 0.1.12, not the version the last check found, and Restart to update +// then goes into it with nothing downloaded again. func TestRestartClickTakesTheNewest(t *testing.T) { f := newFreshFeed(t) downloadsOnClick(t) @@ -159,39 +159,29 @@ func TestRestartClickTakesTheNewest(t *testing.T) { if j := u.json(); j.State != "found" || j.Latest != "0.1.11" { t.Fatalf("the check: %+v", j) } - u.download() - if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.11" || f.downloads.Load() != 1 { + f.latest.Store("0.1.12") + postInstall(t, &quitCount{}) + if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 1 { t.Fatalf("Download update: %+v, %d downloads", j, f.downloads.Load()) } - f.latest.Store("0.1.12") - j := postInstall(t, &quitCount{}) - if j.State == "ready" && j.Latest == "0.1.11" { - t.Fatalf("Restart to update went into the version the last check found: %+v", j) - } - if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 2 { - t.Fatalf("after the click: %+v, %d downloads", j, f.downloads.Load()) - } - // nothing newer: the click restarts into what is downloaded, not - // downloading it again - if j := postInstall(t, &quitCount{}); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 2 { + if j := postInstall(t, &quitCount{}); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 1 { t.Fatalf("the newest downloaded: %+v, %d downloads", j, f.downloads.Load()) } } -// 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. +// A restart with nothing downloaded waiting (another magpie put the +// update in) asks the feed first; one slower than the restart's wait for +// it has the route say it is checking, and the answer, once in, is +// downloaded. func TestRestartWaitsForTheFeed(t *testing.T) { f := newFreshFeed(t) u := freshUpdater(t, true) oldTicks, oldTick := recheckTicks, recheckTick recheckTicks, recheckTick = 3, time.Millisecond t.Cleanup(func() { recheckTicks, recheckTick = oldTicks, oldTick }) - u.check() - if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" { - t.Fatalf("the check: %+v", j) - } + u.mu.Lock() + u.state, u.latest = "ready", &update.Release{Version: "0.1.11"} + u.mu.Unlock() downloadsOnClick(t) f.latest.Store("0.1.12") hold := make(chan struct{}) @@ -206,52 +196,11 @@ func TestRestartWaitsForTheFeed(t *testing.T) { if j.State != "checking" { t.Fatalf("restarted before the feed answered: %+v", j) } - if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 2 { + if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 1 { t.Fatalf("the answer: %+v, %d downloads", j, f.downloads.Load()) } } -// The tray's Restart to update asks the feed as the page's does: with -// 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) - quick(t, busy, u) - 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) - } - f.latest.Store("0.1.12") - into := make(chan string, 4) - u.restartAsked(func() bool { - u.mu.Lock() - into <- u.latest.Version - u.mu.Unlock() - return true - }) - select { - case v := <-into: - if v != "0.1.12" { - t.Fatalf("the tray restarted into %s with 0.1.12 out", v) - } - case <-time.After(5 * time.Second): - t.Fatalf("never restarted: %+v", u.json()) - } - if n := f.downloads.Load(); n != 2 { - t.Fatalf("%d downloads", n) - } - // the gateway busy: the restart waits, as before (#577) - busy.set(gateway.Busy{Requests: 1}) - u.restartAsked(func() bool { into <- "busy"; return true }) - if w, _ := u.waitingFor(); !w { - t.Fatal("restarted with a request in flight") - } -} - // A magpie that can't replace itself sends a click to the release page: // the newest's, not the one an old check found. func TestAvailableOpensTheNewestPage(t *testing.T) { @@ -365,8 +314,8 @@ func TestRestartTakesTheDownloadWaiting(t *testing.T) { // download is in; that restart asked the feed, magpie had released again // while the download came in, so it downloaded the newer one, and the // restart after that asked again. Here each restart finds a newer release -// out: the restart after the one its own asking downloaded goes into that -// download, and the feed is asked again only once it is freshAfter old. +// out: every one goes into the download the click made, and the feed is +// not asked. func TestRestartAfterItsDownloadDoesNotAskAgain(t *testing.T) { f := newFreshFeed(t) downloadsOnClick(t) @@ -376,27 +325,92 @@ func TestRestartAfterItsDownloadDoesNotAskAgain(t *testing.T) { if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.11" || f.downloads.Load() != 1 { t.Fatalf("Download update: %+v, %d downloads", j, f.downloads.Load()) } - // released while the download came in: the restart takes the newer - f.latest.Store("0.1.12") - postInstall(t, &quitCount{}) - if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 2 { - t.Fatalf("the first restart: %+v, %d downloads", j, f.downloads.Load()) - } - // released again while that one came in; the pill restarts again - for i, v := range []string{"0.1.13", "0.1.14", "0.1.15"} { + // released while the download came in, and again after each restart + for i, v := range []string{"0.1.12", "0.1.13", "0.1.14"} { f.latest.Store(v) asked := f.asked.Load() j := postInstall(t, &quitCount{}) - if j.State != "ready" || j.Latest != "0.1.12" || f.downloads.Load() != 2 || f.asked.Load() != asked { - t.Fatalf("restart %d, %s out: %+v, %d downloads, the feed asked %d times", i+2, v, j, f.downloads.Load(), f.asked.Load()-asked) + if j.State != "ready" || j.Latest != "0.1.11" || f.downloads.Load() != 1 || f.asked.Load() != asked { + t.Fatalf("restart %d, %s out: %+v, %d downloads, the feed asked %d times", i+1, v, j, f.downloads.Load(), f.asked.Load()-asked) } } - // a restart freshAfter later asks for the newest again (coeo91) +} + +// sakamotokurome on X: "重启能不能跟更新分开,我本来下载一个新版本了,点击restart, +// 结果它又跑去下载了". With Settings' Download updates automatically off, +// 0.1.11 was downloaded on a click; by the time Restart to update was +// clicked 0.1.12 was out, and the click downloaded 0.1.12 whole instead of +// putting in what was on disk. A version downloaded and waiting is what +// every restart goes into, on a click's download too: the page's Restart +// to update, the tray's and the wait for an idle gateway, with the feed +// not asked and nothing downloaded again. The new version's first check +// is what finds 0.1.12. +func TestRestartPutsInTheClickedDownload(t *testing.T) { + busy := &fakeBusy{} + u := freshUpdater(t, true) + quick(t, busy, u) + waitRecheck = (*updater).recheck + f := newFreshFeed(t) + downloadsOnClick(t) + u.check() + if j := u.json(); j.State != "found" || j.Latest != "0.1.11" { + t.Fatalf("the check: %+v", j) + } + u.download() + if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.11" || f.downloads.Load() != 1 { + t.Fatalf("Download update: %+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 := settled(t, u); j.State != "ready" || j.Latest != "0.1.11" || f.downloads.Load() != 1 || u.staged == "" || f.asked.Load() != asked { + t.Fatalf("%s: %+v, %d downloads, the feed asked %d times", what, j, f.downloads.Load(), f.asked.Load()-asked) + } + } + + // the page's Restart to update restarts at once into 0.1.11 + 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") + + // the tray's + 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") + + // a click's restart that waited for the gateway to be idle + u.waitIdle(restart, true) + select { + case v := <-into: + if v != "0.1.11" { + t.Fatalf("the wait restarted into %s", v) + } + case <-time.After(5 * time.Second): + t.Fatalf("the wait never restarted: %+v", u.json()) + } + unchanged("the wait") + + // the dialog that shows the update, its feed stale, names 0.1.11 still u.mu.Lock() - u.restartStaged = time.Now().Add(-freshAfter) + u.asked = time.Now().Add(-time.Hour) u.mu.Unlock() - postInstall(t, &quitCount{}) - if j := settled(t, u); j.State != "ready" || j.Latest != "0.1.15" || f.downloads.Load() != 3 { - t.Fatalf("a restart later: %+v, %d downloads", j, f.downloads.Load()) - } + u.freshen() + asked = f.asked.Load() + unchanged("the dialog") } diff --git a/internal/gui/update_test.go b/internal/gui/update_test.go index 428d84ff6..023ddd3c7 100644 --- a/internal/gui/update_test.go +++ b/internal/gui/update_test.go @@ -11,13 +11,14 @@ import ( "testing" "time" + "github.com/yetone/magpie/internal/settings" "github.com/yetone/magpie/internal/update" ) // A version downloaded and waiting for a restart gives way to a newer one -// 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). +// out since when a check is asked for (Settings' Check). A restart doesn't +// ask, with downloads on a click too: it goes into the one waiting +// (TestRestartPutsInTheClickedDownload). func TestUpdateStagedGivesWayToNewer(t *testing.T) { sandboxHome(t) body := []byte("new magpie") @@ -64,10 +65,21 @@ func TestUpdateStagedGivesWayToNewer(t *testing.T) { t.Fatalf("%+v", j) } t.Setenv("MAGPIE_UPDATE_FEED", srv.URL) - // 0.1.13 out: that is the one it restarts into + // 0.1.13 out: the restart's recheck leaves 0.1.11, with downloads on + // a click too; a check asked for, downloads automatic, takes 0.1.13 latest.Store("0.1.13") + u.recheck(true) downloadsOnClick(t) u.recheck(true) + if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 || u.staged == "" { + t.Fatalf("the restart's recheck: %+v, %d downloads", j, downloads.Load()) + } + s := settings.Load() + s.NoAutoDownload = false + if err := settings.Save(s); err != nil { + t.Fatal(err) + } + u.check() for u.json().State == "downloading" || u.json().State == "checking" { time.Sleep(10 * time.Millisecond) } diff --git a/internal/gui/update_wait.go b/internal/gui/update_wait.go index 264d70553..5dcd20770 100644 --- a/internal/gui/update_wait.go +++ b/internal/gui/update_wait.go @@ -80,8 +80,8 @@ var gatewayBusy = func() gateway.Busy { return gateway.Busy{} } -// waitRecheck asks the feed again before the restart (updater.recheck), -// asked when a click armed the wait. +// waitRecheck is updater.recheck before the restart, asked when a click +// armed the wait: the feed is asked only with nothing downloaded waiting. var waitRecheck = (*updater).recheck // idleAt is whether b leaves nothing for a restart at now to cut short. @@ -203,8 +203,9 @@ func (u *updater) waitFor(gen int) { switch st := u.json().State; { case in: case st == "ready": - // with Settings' NoAutoDownload, a newer version out since is - // downloaded first, and waited for (recheck) + // the version downloaded and waiting is gone into as it is; + // with none (another magpie put it in), the feed is asked + // (recheck) waitRecheck(u, asked) if u.asking() || u.json().State != "ready" { continue @@ -256,11 +257,9 @@ func (u *updater) autoTick(s settings.Settings) { // restartAsked is the tray's Restart to update, restart doing it. With the // 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): 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). +// waits restarts at once. It goes through recheck as the page's restart +// does: the version downloaded and waiting is restarted into as it is, +// and only with none is the feed asked first (coeo91 on Discord). 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 d24e84a72..4e7f8e15c 100644 --- a/internal/gui/update_wait_test.go +++ b/internal/gui/update_wait_test.go @@ -162,11 +162,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, -// 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) { +// Versions out while a click's restart waits for the gateway don't change +// what it goes into: the one downloaded and waiting, with downloads on a +// click too, and nothing downloaded again (sakamotokurome on X). The new +// version's first check finds the newer ones. +func TestRestartWhenIdleIntoTheWaiting(t *testing.T) { sandboxHome(t) busy := &fakeBusy{} u := &updater{} @@ -216,13 +216,13 @@ func TestRestartWhenIdleIntoTheNewest(t *testing.T) { busy.set(gateway.Busy{}) select { case v := <-into: - if v != "0.1.13" { + if v != "0.1.11" { t.Fatalf("restarted into %s", v) } case <-time.After(5 * time.Second): t.Fatalf("never restarted: %+v", u.json()) } - if j := u.json(); j.State != "ready" || j.Latest != "0.1.13" { + if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || u.staged == "" { t.Fatalf("%+v", j) } }