char1es_lu on X: Restart to update put in a version downloaded a while ago, and right after the restart magpie offered an update again. Sinceb05a525a3(0.1.4051) a restart never asked the feed while a copy was downloaded and waiting (keepsStaged). The clock's check runs every half hour or more and magpie releases many times a day, so that copy was often a release or more behind. Every restart now asks the feed first (recheck, a check marked restarting). This covers the page's Restart to update, the tray's, and the restart AutoRestart or a click waits for. If a newer version is out it is downloaded in place of the waiting copy, and the restart goes into it once it is in. A restart waiting for an idle gateway takes it too: run's waiting rule doesn't apply to the restart's own check. The clock's check during that wait still keeps the waiting copy. The feed is still not asked in two cases: - The copy was downloaded by a restart's own check, or that check found it to be the newest (restartGot). The header's Update restarts once a click's download is in, and asking again there was elis_pina's loop of downloads that never restarted. This also stops a feed slower than recheck's wait from asking on every poll. - Downloads wait for a click (NoAutoDownload). The copy clicked for is what goes in (sakamotokurome, iiiiiimone on X). The update dialog's freshen still names the waiting copy (keep). Trade-off: a click may now wait for a download (canlee886's earlier complaint). The header's Update and the tray restart by themselves once it is in. Settings' Version card shows the download and needs a second click, which restarts without asking again. Making the card follow by itself needs app.js bytes, and its budget has no room (tried: 337 over). Tests: - TestRestartTakesTheNewestOut replaces TestRestartTakesTheDownloadWaiting. With a fake feed it steps 0.1.11 waiting, 0.1.12 out, through the page's click, the restart once it is in (0.1.13 out, feed not asked), the tray (0.1.14), AutoRestart's wait (0.1.16) and the dialog. - TestUpdateStagedGivesWayToNewer now checks that a restart's recheck takes 0.1.13 with downloads automatic, and keeps 0.1.11 on a click. - The recheck lines in TestUpdateClockFetchesNewerThanWaiting asserted the old rule and are gone. Run (mote-run, at9c4e87736+ this change): - go test ./internal/gui ./internal/update -count=1 -timeout 45m: ok, ok - gofmt -l internal/: clean - On the old update.go, TestRestartTakesTheNewestOut and TestUpdateStagedGivesWayToNewer fail: "the page's click ... Latest:0.1.11, 1 downloads, want 0.1.12 after 2". - Broken by hand: - Without restartGot set: "the restart once it is in: Latest:0.1.13, the feed asked 1 times". - With run's old waiting rule: "AutoRestart restarted into 0.1.15, want 0.1.16". Docs: gui-shell.md "When an update downloads" and reference.md. Not tried: a real release feed and real app restart; the fake feed covers the updater only.
296 lines
9.6 KiB
Go
296 lines
9.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"sync/atomic"
|
|
"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 when a check is asked for (Settings' Check), and when a
|
|
// restart asks (char1es_lu on X). With downloads on a click a restart
|
|
// goes into the one waiting (TestRestartPutsInTheClickedDownload).
|
|
func TestUpdateStagedGivesWayToNewer(t *testing.T) {
|
|
sandboxHome(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")
|
|
|
|
u.check()
|
|
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 {
|
|
t.Fatalf("%+v, %d downloads", j, downloads.Load())
|
|
}
|
|
// nothing newer: it stays as it is, not downloaded again
|
|
u.check()
|
|
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 {
|
|
t.Fatalf("%+v, %d downloads", j, downloads.Load())
|
|
}
|
|
// the feed down: still ready to restart into what it has
|
|
t.Setenv("MAGPIE_UPDATE_FEED", "http://127.0.0.1:1/")
|
|
u.check()
|
|
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" {
|
|
t.Fatalf("%+v", j)
|
|
}
|
|
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
|
|
// 0.1.13 out: the restart's recheck leaves 0.1.11 with downloads on a
|
|
// click; with them automatic it takes 0.1.13, as a check asked for does
|
|
latest.Store("0.1.13")
|
|
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.recheck(true)
|
|
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.13" || downloads.Load() != 2 || u.staged == "" {
|
|
t.Fatalf("%+v, %d downloads", j, downloads.Load())
|
|
}
|
|
}
|
|
|
|
// The clock's check, with a version downloaded and waiting, downloads a
|
|
// newer one out since in its place: with downloads automatic, a version
|
|
// magpie knows is out is downloaded at once, not when Update is clicked
|
|
// (canlee886 on X). With downloads on a click the clock fetches nothing
|
|
// more (iiiiiimone on X).
|
|
func TestUpdateClockFetchesNewerThanWaiting(t *testing.T) {
|
|
sandboxHome(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())
|
|
}
|
|
// the same version again: nothing more is downloaded
|
|
u.checkByClock()
|
|
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 {
|
|
t.Fatalf("same version: %+v, %d downloads", j, downloads.Load())
|
|
}
|
|
// newer ones out, check after check: each is downloaded as it is found
|
|
for i, v := range []string{"0.1.12", "0.1.13"} {
|
|
latest.Store(v)
|
|
u.checkByClock()
|
|
if j := u.json(); j.State != "ready" || j.Latest != v || downloads.Load() != int32(i+2) || u.staged == "" {
|
|
t.Fatalf("%s out: %+v, %d downloads", v, j, downloads.Load())
|
|
}
|
|
}
|
|
// downloads on a click: the clock leaves the one waiting
|
|
latest.Store("0.1.14")
|
|
downloadsOnClick(t)
|
|
u.checkByClock()
|
|
if j := u.json(); j.State != "ready" || j.Latest != "0.1.13" || downloads.Load() != 3 || u.staged == "" {
|
|
t.Fatalf("on a click: %+v, %d downloads", j, downloads.Load())
|
|
}
|
|
}
|
|
|
|
// A version found and not downloaded is fetched by the minute loop, not
|
|
// left for a click: once downloads are automatic again (found), and half an
|
|
// hour after a download failed (error with retry), not six hours later.
|
|
func TestUpdateFetchDue(t *testing.T) {
|
|
now := time.Now()
|
|
auto := settings.Settings{}
|
|
for _, c := range []struct {
|
|
name string
|
|
s settings.Settings
|
|
state string
|
|
retry bool
|
|
last time.Time
|
|
want bool
|
|
}{
|
|
{"found", auto, "found", false, now, true},
|
|
{"failed just now", auto, "error", true, now, false},
|
|
{"failed half an hour ago", auto, "error", true, now.Add(-retryAfter), true},
|
|
{"error with no retry", auto, "error", false, now.Add(-time.Hour), false},
|
|
{"ready", auto, "ready", false, now.Add(-time.Hour), false},
|
|
{"latest", auto, "latest", false, now.Add(-time.Hour), false},
|
|
{"downloads on a click", settings.Settings{NoAutoDownload: true}, "found", false, now, false},
|
|
{"never asks", settings.Settings{NoAutoUpdate: true}, "found", false, now, false},
|
|
} {
|
|
if got := fetchDue(c.s, c.state, c.retry, c.last, now); got != c.want {
|
|
t.Errorf("%s: %v, want %v", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A check that comes while a download is under way doesn't start a second
|
|
// download beside it; once that one is in, the feed is asked again and a
|
|
// newer version found then is downloaded in its place.
|
|
func TestUpdateCheckDuringDownloadTakesNewer(t *testing.T) {
|
|
sandboxHome(t)
|
|
body := []byte("new magpie")
|
|
sum := sha256.Sum256(body)
|
|
var latest atomic.Value
|
|
latest.Store("0.1.11")
|
|
var downloads, inFlight, most atomic.Int32
|
|
hold := make(chan struct{})
|
|
started := make(chan struct{}, 4)
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/asset" {
|
|
n := inFlight.Add(1)
|
|
defer inFlight.Add(-1)
|
|
for m := most.Load(); n > m && !most.CompareAndSwap(m, n); m = most.Load() {
|
|
}
|
|
if downloads.Add(1) == 1 {
|
|
started <- struct{}{}
|
|
<-hold
|
|
}
|
|
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")
|
|
|
|
go u.checkByClock()
|
|
<-started
|
|
if j := u.json(); j.State != "downloading" {
|
|
t.Fatalf("first download: %+v", j)
|
|
}
|
|
// 0.1.12 comes out mid-download, and a check finds it
|
|
latest.Store("0.1.12")
|
|
u.check()
|
|
if j := u.json(); j.State != "downloading" || downloads.Load() != 1 {
|
|
t.Fatalf("check mid-download: %+v, %d downloads", j, downloads.Load())
|
|
}
|
|
close(hold)
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
if j := u.json(); j.State == "ready" && j.Latest == "0.1.12" {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("never took 0.1.12: %+v, %d downloads", u.json(), downloads.Load())
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
settled(t, u)
|
|
if downloads.Load() != 2 || most.Load() != 1 {
|
|
t.Fatalf("%d downloads, %d at once", downloads.Load(), most.Load())
|
|
}
|
|
}
|
|
|
|
// A restart waiting for the gateway to go idle goes into the copy it is
|
|
// waiting with: a check meanwhile doesn't swap it for a download.
|
|
func TestUpdateWaitingRestartKeepsItsDownload(t *testing.T) {
|
|
sandboxHome(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")
|
|
|
|
u.checkByClock()
|
|
if j := u.json(); j.State != "ready" || downloads.Load() != 1 {
|
|
t.Fatalf("%+v, %d downloads", j, downloads.Load())
|
|
}
|
|
u.mu.Lock()
|
|
u.waiting = true
|
|
u.mu.Unlock()
|
|
latest.Store("0.1.12")
|
|
u.checkByClock()
|
|
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 || u.staged == "" {
|
|
t.Fatalf("waiting: %+v, %d downloads", j, downloads.Load())
|
|
}
|
|
}
|