From 08e2c1c3777a89b482f6b841c7cf83ec02c93309 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Mon, 13 Mar 2023 10:48:38 +1030 Subject: [PATCH] Use constants and constructors in the test --- download/download.go | 28 ++++++++--- download/download_test.go | 98 ++++++++++++++++++++++++++------------- web/popup.html | 2 +- 3 files changed, 88 insertions(+), 40 deletions(-) diff --git a/download/download.go b/download/download.go index 407176a..ef8060b 100644 --- a/download/download.go +++ b/download/download.go @@ -47,16 +47,30 @@ type Manager struct { Lock sync.Mutex } +func (m *Manager) String() string { + m.Lock.Lock() + defer m.Lock.Unlock() + out := fmt.Sprintf("Max per domain: %d, downloads: %d\n", m.MaxPerDomain, len(m.Downloads)) + + for _, dl := range m.Downloads { + out = out + fmt.Sprintf("%3d: (%10s) %30s\n", dl.Id, dl.State, dl.Url) + } + + return out + +} + type State string const ( STATE_PREPARING State = "Preparing to start" - STATE_QUEUED = "Queued" - STATE_DOWNLOADING = "Downloading" - STATE_DOWNLOADING_METADATA = "Downloading metadata" - STATE_FAILED = "Failed" - STATE_COMPLETE = "Complete" - STATE_MOVED = "Moved" + STATE_CHOOSE_PROFILE State = "Choose Profile" + STATE_QUEUED State = "Queued" + STATE_DOWNLOADING State = "Downloading" + STATE_DOWNLOADING_METADATA State = "Downloading metadata" + STATE_FAILED State = "Failed" + STATE_COMPLETE State = "Complete" + STATE_MOVED State = "Moved" ) var CanStopDownload = false @@ -204,7 +218,7 @@ func NewDownload(url string, conf *config.Config) *Download { Id: int(downloadId), Url: url, PopupUrl: fmt.Sprintf("/fetch/%d", int(downloadId)), - State: "choose profile", + State: STATE_CHOOSE_PROFILE, Files: []string{}, Log: []string{}, Config: conf, diff --git a/download/download_test.go b/download/download_test.go index d3995c4..5e6ead1 100644 --- a/download/download_test.go +++ b/download/download_test.go @@ -85,10 +85,20 @@ func TestQueue(t *testing.T) { cs.LoadTestConfig() conf := cs.Config - new1 := Download{Id: 1, Url: "http://sub.example.org/foo1", State: "queued", DownloadProfile: conf.DownloadProfiles[0], Config: conf} - new2 := Download{Id: 2, Url: "http://sub.example.org/foo2", State: "queued", DownloadProfile: conf.DownloadProfiles[0], Config: conf} - new3 := Download{Id: 3, Url: "http://sub.example.org/foo3", State: "queued", DownloadProfile: conf.DownloadProfiles[0], Config: conf} - new4 := Download{Id: 4, Url: "http://example.org/", State: "queued", DownloadProfile: conf.DownloadProfiles[0], Config: conf} + new1 := NewDownload("http://sub.example.org/foo1", conf) + new2 := NewDownload("http://sub.example.org/foo2", conf) + new3 := NewDownload("http://sub.example.org/foo3", conf) + new4 := NewDownload("http://example.org/", conf) + + // pretend the user chose a profile for each + new1.DownloadProfile = *conf.ProfileCalled("test profile") + new2.DownloadProfile = *conf.ProfileCalled("test profile") + new3.DownloadProfile = *conf.ProfileCalled("test profile") + new4.DownloadProfile = *conf.ProfileCalled("test profile") + new1.State = STATE_QUEUED + new2.State = STATE_QUEUED + new3.State = STATE_QUEUED + new4.State = STATE_QUEUED q := Manager{ Downloads: []*Download{}, @@ -96,105 +106,129 @@ func TestQueue(t *testing.T) { Lock: sync.Mutex{}, } - q.AddDownload(&new1) - q.AddDownload(&new2) - q.AddDownload(&new3) - q.AddDownload(&new4) + q.AddDownload(new1) + q.AddDownload(new2) + q.AddDownload(new3) + q.AddDownload(new4) q.startQueued(1) // two should start, one from each of the two domains time.Sleep(time.Millisecond * 100) - if q.Downloads[0].State != "downloading" { + if q.Downloads[0].State != STATE_DOWNLOADING { t.Errorf("#1 was not downloading - %s instead ", q.Downloads[0].State) + t.Log(q.String()) } - if q.Downloads[1].State != "queued" { + if q.Downloads[1].State != STATE_QUEUED { t.Errorf("#2 is not queued - %s instead", q.Downloads[1].State) + t.Log(q.String()) } - if q.Downloads[2].State != "queued" { + if q.Downloads[2].State != STATE_QUEUED { t.Errorf("#3 is not queued - %s instead", q.Downloads[2].State) + t.Log(q.String()) } - if q.Downloads[3].State != "downloading" { + if q.Downloads[3].State != STATE_DOWNLOADING { t.Errorf("#4 is not downloading - %s instead", q.Downloads[3].State) + t.Log(q.String()) } // this should start no more, as one is still going q.startQueued(1) time.Sleep(time.Millisecond * 100) - if q.Downloads[0].State != "downloading" { + if q.Downloads[0].State != STATE_DOWNLOADING { t.Errorf("#1 was not downloading - %s instead ", q.Downloads[0].State) + t.Log(q.String()) } - if q.Downloads[1].State != "queued" { + if q.Downloads[1].State != STATE_QUEUED { t.Errorf("#2 is not queued - %s instead", q.Downloads[1].State) + t.Log(q.String()) } - if q.Downloads[2].State != "queued" { + if q.Downloads[2].State != STATE_QUEUED { t.Errorf("#3 is not queued - %s instead", q.Downloads[2].State) + t.Log(q.String()) } - if q.Downloads[3].State != "downloading" { + if q.Downloads[3].State != STATE_DOWNLOADING { t.Errorf("#4 is not downloading - %s instead", q.Downloads[3].State) + t.Log(q.String()) } // wait until the two finish, check time.Sleep(time.Second * 5.0) - if q.Downloads[0].State != "complete" { + if q.Downloads[0].State != STATE_COMPLETE { t.Errorf("#1 was not complete - %s instead ", q.Downloads[0].State) + t.Log(q.String()) } - if q.Downloads[1].State != "queued" { + if q.Downloads[1].State != STATE_QUEUED { t.Errorf("#2 is not queued - %s instead", q.Downloads[1].State) + t.Log(q.String()) } - if q.Downloads[2].State != "queued" { + if q.Downloads[2].State != STATE_QUEUED { t.Errorf("#3 is not queued - %s instead", q.Downloads[2].State) + t.Log(q.String()) } - if q.Downloads[3].State != "complete" { + if q.Downloads[3].State != STATE_COMPLETE { t.Errorf("#4 is not complete - %s instead", q.Downloads[3].State) + t.Log(q.String()) } // this should start one more, as one is still going q.startQueued(1) time.Sleep(time.Millisecond * 100) - if q.Downloads[0].State != "complete" { + if q.Downloads[0].State != STATE_COMPLETE { t.Errorf("#1 was not complete - %s instead ", q.Downloads[0].State) + t.Log(q.String()) } - if q.Downloads[1].State != "downloading" { + if q.Downloads[1].State != STATE_DOWNLOADING { t.Errorf("#2 is not downloading - %s instead", q.Downloads[1].State) + t.Log(q.String()) } - if q.Downloads[2].State != "queued" { + if q.Downloads[2].State != STATE_QUEUED { t.Errorf("#3 is not queued - %s instead", q.Downloads[2].State) + t.Log(q.String()) } - if q.Downloads[3].State != "complete" { + if q.Downloads[3].State != STATE_COMPLETE { t.Errorf("#4 is not complete - %s instead", q.Downloads[3].State) + t.Log(q.String()) } // this should start no more, as one is still going q.startQueued(1) time.Sleep(time.Millisecond * 100) - if q.Downloads[0].State != "complete" { + if q.Downloads[0].State != STATE_COMPLETE { t.Errorf("#1 was not complete - %s instead ", q.Downloads[0].State) + t.Log(q.String()) } - if q.Downloads[1].State != "downloading" { + if q.Downloads[1].State != STATE_DOWNLOADING { t.Errorf("#2 is not downloading - %s instead", q.Downloads[1].State) + t.Log(q.String()) } - if q.Downloads[2].State != "queued" { + if q.Downloads[2].State != STATE_QUEUED { t.Errorf("#3 is not queued - %s instead", q.Downloads[2].State) + t.Log(q.String()) } - if q.Downloads[3].State != "complete" { + if q.Downloads[3].State != STATE_COMPLETE { t.Errorf("#4 is not complete - %s instead", q.Downloads[3].State) + t.Log(q.String()) } // but if we allow two per domain, the other queued one will start q.startQueued(2) time.Sleep(time.Millisecond * 100) - if q.Downloads[0].State != "complete" { + if q.Downloads[0].State != STATE_COMPLETE { t.Errorf("#1 was not complete - %s instead ", q.Downloads[0].State) + t.Log(q.String()) } - if q.Downloads[1].State != "downloading" { + if q.Downloads[1].State != STATE_DOWNLOADING { t.Errorf("#2 is not downloading - %s instead", q.Downloads[1].State) + t.Log(q.String()) } - if q.Downloads[2].State != "downloading" { + if q.Downloads[2].State != STATE_DOWNLOADING { t.Errorf("#3 is not downloading - %s instead", q.Downloads[2].State) + t.Log(q.String()) } - if q.Downloads[3].State != "complete" { + if q.Downloads[3].State != STATE_COMPLETE { t.Errorf("#4 is not complete - %s instead", q.Downloads[3].State) + t.Log(q.String()) } } diff --git a/web/popup.html b/web/popup.html index bcaceb7..f3fa0db 100644 --- a/web/popup.html +++ b/web/popup.html @@ -105,7 +105,7 @@ if (info.destination) { this.destination_chosen = info.destination.name; } - if (this.state != 'choose profile') { + if (this.state != 'Choose Profile') { this.profile_chosen = true; } this.finished = info.finished;