elis_pina on X (Windows): a click on the header's Update downloaded to 100%, then downloaded again, over and over; Settings > About did update. The pill restarts by itself once the download a click started is in (pulling in app.js). That restart asks the feed first (recheck, coeo91's "take the newest"), and magpie releases every 5 to 10 minutes, so with a download slower than that (80 MB on Windows, from China) a newer version was out each time: it was downloaded in place of the one just in, and the restart after it asked again. Settings' button only restarts on a click, so its loop stopped whenever the user clicked before the next release.b05a525a3(v0.1.4051) already ends this with downloads automatic: a ready download is restarted into without asking. With Settings' "Download updates automatically" off the loop was still there. Now a version a restart's own check downloaded (forRestart, restartStaged) is restarted into without asking the feed again for freshAfter (5 min): at most one newer download per click, and coeo91's case, a restart long after the download, still asks for the newest. Test: TestRestartAfterItsDownloadDoesNotAskAgain downloads 0.1.11 on a click, publishes 0.1.12, restarts (0.1.12 downloaded), then publishes 0.1.13 to 0.1.15 before each of three more restarts: each goes into 0.1.12 with the feed not asked; with restartStaged freshAfter old the restart takes 0.1.15. Run: - Old update.go: the test is red (restart 2, 0.1.13 out, went to 0.1.13 with 3 downloads). - mote-run go test -count=1 -run 'Update|Restart|Tray|Available|Freshen|Wait|Auto' ./internal/gui/: ok. - win-run go test -count=1 -run 'TestRestart|TestTrayRestart|TestUpdate|TestAvailable' ./internal/gui/: all ok but TestUpdateThatWontStart ("relaunched 0 times"), red the same on base48e394146, filed as #6059. - mica-run go test -count=1 -timeout 45m ./...: exit 0, 99 packages ok (internal/gui 168.5s), at this change before a comment-only rewording of recheck's doc comment. - go vet ./internal/gui/, GOOS=linux -tags nogui, GOOS=windows: ok. gofmt -l: clean. No GUI file changed. - Rebased on origin/main9e3b269c5(3 commits, none in internal/gui or internal/update): mica-run go test -count=1 ./internal/gui/ ok (195.9s). Not tried: a real packaged Windows restart across live releases; the reporter's version is unknown.
403 lines
13 KiB
Go
403 lines
13 KiB
Go
package gui
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/update"
|
|
)
|
|
|
|
// coeo91 on Discord: "更新行为不对,他会在点击更新按钮后下载上一次或最近一次获取到的
|
|
// 最新版本,而不是实际的最新版本". A click that updates acts on what the feed
|
|
// says at the click, not on what the last check found: these tests run a
|
|
// check that finds 0.1.11, publish 0.1.12, and click.
|
|
|
|
// freshFeed is a fake update feed naming latest, whose asset counts its
|
|
// downloads. hold, while set, keeps the feed's answers back until it is
|
|
// closed.
|
|
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{}
|
|
}
|
|
|
|
func newFreshFeed(t *testing.T) *freshFeed {
|
|
t.Helper()
|
|
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"))
|
|
f := &freshFeed{}
|
|
f.latest.Store("0.1.11")
|
|
body := []byte("new magpie")
|
|
sum := sha256.Sum256(body)
|
|
f.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/asset" {
|
|
f.downloads.Add(1)
|
|
w.Write(body)
|
|
return
|
|
}
|
|
f.mu.Lock()
|
|
hold := f.hold
|
|
f.mu.Unlock()
|
|
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[:])},
|
|
}})
|
|
}))
|
|
t.Cleanup(f.srv.Close)
|
|
t.Setenv("MAGPIE_UPDATE_FEED", f.srv.URL)
|
|
old := Version
|
|
Version = "0.1.10"
|
|
t.Cleanup(func() { Version = old })
|
|
return f
|
|
}
|
|
|
|
// freshUpdater is updates for the test: replacing the test's binary when
|
|
// exe is set, and never restarting (a handover stands in as under way).
|
|
func freshUpdater(t *testing.T, exe bool) *updater {
|
|
t.Helper()
|
|
u := &updater{}
|
|
if exe {
|
|
p, err := update.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
u.exe = p
|
|
u.self, _ = os.Stat(p)
|
|
t.Cleanup(func() { os.Remove(p + ".new") })
|
|
}
|
|
old := updates
|
|
updates = u
|
|
t.Cleanup(func() { updates = old })
|
|
if pending.Load() == nil {
|
|
pending.Store(&handover{})
|
|
t.Cleanup(func() { pending.Store(nil) })
|
|
}
|
|
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()
|
|
for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(5 * time.Millisecond) {
|
|
if !u.asking() {
|
|
if j := u.json(); j.State != "downloading" {
|
|
return j
|
|
}
|
|
}
|
|
}
|
|
t.Fatalf("still %+v", u.json())
|
|
return updateJSON{}
|
|
}
|
|
|
|
type openedURLs struct {
|
|
quitCount
|
|
mu sync.Mutex
|
|
urls []string
|
|
}
|
|
|
|
func (o *openedURLs) OpenURL(u string) error {
|
|
o.mu.Lock()
|
|
o.urls = append(o.urls, u)
|
|
o.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
func postInstall(t *testing.T, w Windows) updateJSON {
|
|
t.Helper()
|
|
mux := http.NewServeMux()
|
|
updateRoutes(mux, w)
|
|
rec := httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, httptest.NewRequest("POST", "/api/update/install", strings.NewReader("{}")))
|
|
var j updateJSON
|
|
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &j) != nil {
|
|
t.Fatalf("install: %d %s", rec.Code, rec.Body)
|
|
}
|
|
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.
|
|
func TestRestartClickTakesTheNewest(t *testing.T) {
|
|
f := newFreshFeed(t)
|
|
downloadsOnClick(t)
|
|
u := freshUpdater(t, true)
|
|
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")
|
|
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 {
|
|
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.
|
|
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)
|
|
}
|
|
downloadsOnClick(t)
|
|
f.latest.Store("0.1.12")
|
|
hold := make(chan struct{})
|
|
f.mu.Lock()
|
|
f.hold = hold
|
|
f.mu.Unlock()
|
|
j := postInstall(t, &quitCount{})
|
|
f.mu.Lock()
|
|
f.hold = nil
|
|
f.mu.Unlock()
|
|
close(hold)
|
|
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 {
|
|
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) {
|
|
f := newFreshFeed(t)
|
|
u := freshUpdater(t, false)
|
|
u.check()
|
|
if j := u.json(); j.State != "available" || j.Latest != "0.1.11" {
|
|
t.Fatalf("the check: %+v", j)
|
|
}
|
|
f.latest.Store("0.1.12")
|
|
w := &openedURLs{}
|
|
// a check a moment ago: the page it found is opened, the feed not asked
|
|
postInstall(t, w)
|
|
u.mu.Lock()
|
|
u.asked = time.Now().Add(-2 * time.Minute)
|
|
u.mu.Unlock()
|
|
postInstall(t, w)
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
if want := []string{"https://example.invalid/v0.1.11", "https://example.invalid/v0.1.12"}; strings.Join(w.urls, " ") != strings.Join(want, " ") {
|
|
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())
|
|
}
|
|
}
|
|
|
|
// elis_pina on X, on Windows: the header's Update downloaded to 100% and
|
|
// then downloaded again, over and over. The pill restarts once a click's
|
|
// 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.
|
|
func TestRestartAfterItsDownloadDoesNotAskAgain(t *testing.T) {
|
|
f := newFreshFeed(t)
|
|
downloadsOnClick(t)
|
|
u := freshUpdater(t, true)
|
|
u.check()
|
|
u.download() // the pill's "found": a click downloads, then restarts
|
|
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"} {
|
|
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)
|
|
}
|
|
}
|
|
// a restart freshAfter later asks for the newest again (coeo91)
|
|
u.mu.Lock()
|
|
u.restartStaged = time.Now().Add(-freshAfter)
|
|
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())
|
|
}
|
|
}
|