Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

More update checker tests #31

Merged
merged 6 commits into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions update_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ func newUpdateChecker(updater *Updater, ctx Context, log logging.Logger, tickDur
}
}

// Check checks for an update.
func (u *UpdateChecker) Check() {
func (u *UpdateChecker) check() error {
u.count++
_, err := u.updater.Update(u.ctx)
if err != nil {
return err
}

// Check checks for an update.
func (u *UpdateChecker) Check() {
if err := u.check(); err != nil {
u.log.Errorf("Error in update: %s", err)
}
}

// Start starts the update checker
func (u *UpdateChecker) Start() {
// Start starts the update checker. Returns false if we are already running.
func (u *UpdateChecker) Start() bool {
if u.ticker != nil {
return
return false
}
u.ticker = time.NewTicker(u.tickDuration)
go func() {
Expand All @@ -55,6 +59,7 @@ func (u *UpdateChecker) Start() {
u.Check()
}
}()
return true
}

// Stop stops the update checker
Expand Down
25 changes: 22 additions & 3 deletions update_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
package updater

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestUpdateChecker(t *testing.T) {
Expand All @@ -18,7 +20,10 @@ func TestUpdateChecker(t *testing.T) {

checker := newUpdateChecker(updater, testUpdateCheckUI{promptDelay: 10 * time.Millisecond}, log, time.Millisecond)
defer checker.Stop()
checker.Start()
started := checker.Start()
require.True(t, started)
started = checker.Start()
require.False(t, started)

time.Sleep(11 * time.Millisecond)

Expand All @@ -27,10 +32,13 @@ func TestUpdateChecker(t *testing.T) {

type testUpdateCheckUI struct {
promptDelay time.Duration
verifyError error
}

func (u testUpdateCheckUI) UpdatePrompt(_ Update, _ UpdateOptions, _ UpdatePromptOptions) (*UpdatePromptResponse, error) {
time.Sleep(u.promptDelay)
if u.promptDelay > 0 {
time.Sleep(u.promptDelay)
}
return &UpdatePromptResponse{Action: UpdateActionApply}, nil
}

Expand All @@ -47,7 +55,7 @@ func (u testUpdateCheckUI) GetUpdateUI() (UpdateUI, error) {
}

func (u testUpdateCheckUI) Verify(update Update) error {
return nil
return u.verifyError
}

func (u testUpdateCheckUI) Restart() error {
Expand All @@ -57,3 +65,14 @@ func (u testUpdateCheckUI) Restart() error {
func (u testUpdateCheckUI) UpdateOptions() UpdateOptions {
return newDefaultTestUpdateOptions()
}

func TestUpdateCheckerError(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
updater, err := newTestUpdaterWithServer(t, testServer)
assert.NoError(t, err)

checker := NewUpdateChecker(updater, testUpdateCheckUI{verifyError: fmt.Errorf("Test verify error")}, log)
err = checker.check()
require.Error(t, err)
}