summary refs log tree commit diff stats
path: root/svc
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-09 03:30:36 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-09 03:30:36 -0400
commit7f76158b9565d804ada682c1a9aa83c22cb8de71 (patch)
treece387d25941bb9a13fb495ddd439309568fffbe9 /svc
parent5f06e58a09c21e8016197fc8eefc38a86e9eb3e7 (diff)
downloadgetwtxt-7f76158b9565d804ada682c1a9aa83c22cb8de71.tar.gz
cache-related tests
Diffstat (limited to 'svc')
-rw-r--r--svc/cache_test.go53
-rw-r--r--svc/init_test.go6
2 files changed, 55 insertions, 4 deletions
diff --git a/svc/cache_test.go b/svc/cache_test.go
index f298508..29b92b8 100644
--- a/svc/cache_test.go
+++ b/svc/cache_test.go
@@ -2,8 +2,52 @@ package svc // import "github.com/getwtxt/getwtxt/svc"
 
 import (
 	"testing"
+	"time"
 )
 
+func Test_cacheTimer(t *testing.T) {
+	initTestConf()
+	dur, _ := time.ParseDuration("5m")
+	back30, _ := time.ParseDuration("-30m")
+
+	cases := []struct {
+		name      string
+		lastCache time.Time
+		interval  time.Duration
+		expect    bool
+	}{
+		{
+			name:      "Past Interval",
+			lastCache: time.Now().Add(back30),
+			interval:  dur,
+			expect:    true,
+		},
+		{
+			name:      "Before Interval",
+			lastCache: time.Now(),
+			interval:  dur,
+			expect:    false,
+		},
+	}
+
+	for _, tt := range cases {
+		t.Run(tt.name, func(t *testing.T) {
+
+			confObj.Mu.Lock()
+			confObj.LastCache = tt.lastCache
+			confObj.CacheInterval = tt.interval
+			confObj.Mu.Unlock()
+
+			res := cacheTimer()
+
+			if res != tt.expect {
+				t.Errorf("Got %v, expected %v\n", res, tt.expect)
+			}
+		})
+	}
+
+}
+
 func Test_refreshCache(t *testing.T) {
 	initTestConf()
 	confObj.Mu.RLock()
@@ -30,3 +74,12 @@ func Benchmark_refreshCache(b *testing.B) {
 		refreshCache()
 	}
 }
+
+func Benchmark_pingAssets(b *testing.B) {
+	initTestConf()
+	b.ResetTimer()
+
+	for i := 0; i < b.N; i++ {
+		pingAssets()
+	}
+}
diff --git a/svc/init_test.go b/svc/init_test.go
index cac2fe6..11a1593 100644
--- a/svc/init_test.go
+++ b/svc/init_test.go
@@ -25,8 +25,8 @@ func initTestConf() {
 		tmpls = testTemplates()
 
 		confObj.Mu.RLock()
+		defer confObj.Mu.RUnlock()
 		testport = fmt.Sprintf(":%v", confObj.Port)
-		confObj.Mu.RUnlock()
 
 		logToNull()
 	})
@@ -69,6 +69,7 @@ func testConfig() {
 	viper.SetDefault("Instance.Description", "A fast, resilient twtxt registry server written in Go!")
 
 	confObj.Mu.Lock()
+	defer confObj.Mu.Unlock()
 
 	confObj.Port = viper.GetInt("ListenPort")
 	confObj.AssetsDir = "../" + viper.GetString("AssetsDirectory")
@@ -92,7 +93,4 @@ func testConfig() {
 	confObj.Instance.Owner = viper.GetString("Instance.OwnerName")
 	confObj.Instance.Mail = viper.GetString("Instance.Email")
 	confObj.Instance.Desc = viper.GetString("Instance.Description")
-
-	confObj.Mu.Unlock()
-
 }