Skip to content
Permalink
Browse files

clean up and add tests for updates cache

- removes the parameter for newVersionCheck as was not being used
- changes newUpdatesCache to take expiry parameter for possible future
configuration option
- adds basic test quite to verify all cache fucntions work as expected
  • Loading branch information
robjloranger committed Sep 9, 2019
1 parent 2a7a829 commit 908f009248ec540576f3dd02ecf6d35edb0c5dce
Showing with 89 additions and 6 deletions.
  1. +7 −6 updates.go
  2. +82 −0 updates_test.go
@@ -20,7 +20,7 @@ import (

// updatesCacheTime is the default interval between cache updates for new
// software versions
const updatesCacheTime = 12 * time.Hour
const defaultUpdatesCacheTime = 12 * time.Hour

// updatesCache holds data about current and new releases of the writefreely
// software
@@ -38,7 +38,7 @@ type updatesCache struct {
func (uc *updatesCache) CheckNow() error {
uc.mu.Lock()
defer uc.mu.Unlock()
latestRemote, err := newVersionCheck(uc.currentVersion)
latestRemote, err := newVersionCheck()
if err != nil {
return err
}
@@ -74,23 +74,24 @@ func (uc updatesCache) ReleaseURL() string {
}

// newUpdatesCache returns an initialized updates cache
func newUpdatesCache() *updatesCache {
func newUpdatesCache(expiry time.Duration) *updatesCache {
cache := updatesCache{
frequency: updatesCacheTime,
frequency: expiry,
currentVersion: "v" + softwareVer,
}
cache.CheckNow()
return &cache
}

// InitUpdates initializes the updates cache, if the config value is set
// It uses the defaultUpdatesCacheTime for the cache expiry
func (app *App) InitUpdates() {
if app.cfg.App.UpdateChecks {
app.updates = newUpdatesCache()
app.updates = newUpdatesCache(defaultUpdatesCacheTime)
}
}

func newVersionCheck(serverVersion string) (string, error) {
func newVersionCheck() (string, error) {
res, err := http.Get("https://version.writefreely.org")
if err == nil && res.StatusCode == http.StatusOK {
defer res.Body.Close()
@@ -0,0 +1,82 @@
package writefreely

import (
"regexp"
"testing"
"time"
)

func TestUpdatesRoundTrip(t *testing.T) {
cache := newUpdatesCache(defaultUpdatesCacheTime)
t.Run("New Updates Cache", func(t *testing.T) {

if cache == nil {
t.Fatal("Returned nil cache")
}

if cache.frequency != defaultUpdatesCacheTime {
t.Fatalf("Got cache expiry frequency: %s but expected: %s", cache.frequency, defaultUpdatesCacheTime)
}

if cache.currentVersion != "v"+softwareVer {
t.Fatalf("Got current version: %s but expected: %s", cache.currentVersion, "v"+softwareVer)
}
})

t.Run("Release URL", func(t *testing.T) {
url := cache.ReleaseURL()

reg, err := regexp.Compile(`^https:\/\/blog.writefreely.org\/version(-\d+){1,}$`)
if err != nil {
t.Fatalf("Test Case Error: Failed to compile regex: %v", err)
}
match := reg.MatchString(url)

if !match {
t.Fatalf("Malformed Release URL: %s", url)
}
})

t.Run("Check Now", func(t *testing.T) {
// ensure time between init and next check
time.Sleep(1 * time.Second)

prevLastCheck := cache.lastCheck

// force to known older version for latest and current
prevLatestVer := "v0.8.1"
cache.latestVersion = prevLatestVer
cache.currentVersion = "v0.8.0"

err := cache.CheckNow()
if err != nil {
t.Fatalf("Error should be nil, got: %v", err)
}

if prevLastCheck == cache.lastCheck {
t.Fatal("Expected lastCheck to update")
}

if cache.lastCheck.Before(prevLastCheck) {
t.Fatal("Last check should be newer than previous")
}

if prevLatestVer == cache.latestVersion {
t.Fatal("expected latestVersion to update")
}

})

t.Run("Are Available", func(t *testing.T) {
if !cache.AreAvailable() {
t.Fatalf("Cache reports not updates but Current is %s and Latest is %s", cache.currentVersion, cache.latestVersion)
}
})

t.Run("Latest Version", func(t *testing.T) {
gotLatest := cache.LatestVersion()
if gotLatest != cache.latestVersion {
t.Fatalf("Malformed latest version. Expected: %s but got: %s", cache.latestVersion, gotLatest)
}
})
}

0 comments on commit 908f009

Please sign in to comment.
You can’t perform that action at this time.