summary refs log tree commit diff stats
path: root/svc/cache_test.go
blob: 29b92b83a30060ba4693082cc4f3fab031ed11dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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()
	prevtime := confObj.LastCache
	confObj.Mu.RUnlock()

	t.Run("Cache Time Check", func(t *testing.T) {
		refreshCache()
		confObj.Mu.RLock()
		newtime := confObj.LastCache
		confObj.Mu.RUnlock()

		if !newtime.After(prevtime) || newtime == prevtime {
			t.Errorf("Cache time did not update, check refreshCache() logic\n")
		}
	})
}

func Benchmark_refreshCache(b *testing.B) {
	initTestConf()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		refreshCache()
	}
}

func Benchmark_pingAssets(b *testing.B) {
	initTestConf()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		pingAssets()
	}
}