-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
67 lines (59 loc) · 1.53 KB
/
cache_test.go
File metadata and controls
67 lines (59 loc) · 1.53 KB
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
package onecache
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
c := New()
if c.Timestamp.After(time.Now()) {
t.Error("Wrong timestamp generated")
}
}
func TestCacheSet(t *testing.T) {
c := New()
c.Set("sample-key", "sample-value", 1000)
r, ok := c.recordMap["sample-key"]
assert.Equal(t, true, ok)
assert.Equal(t, "sample-value", r.value)
assert.Equal(t, int64(1000), r.expiresIn)
}
func TestCacheFind(t *testing.T) {
c := New()
t.Log("When the record does not exist")
_, found := c.Find("invalid-cache")
assert.Equal(t, false, found)
t.Log("When the record exists but has expired")
c.recordMap["key1"] = record{
expiresIn: 10,
timestamp: time.Now().Add(time.Second * time.Duration(11) * -1),
}
_, found = c.Find("key1")
assert.Equal(t, false, found)
t.Log("When the record exists but has expired")
c.recordMap["key2"] = record{
expiresIn: 1000,
timestamp: time.Now().Add(time.Second * time.Duration(11) * -1),
value: "some-value",
}
v, found := c.Find("key2")
assert.Equal(t, true, found)
assert.Equal(t, "some-value", v)
}
func TestCache_clean(t *testing.T) {
c := New()
c.recordMap["key1"] = record{
expiresIn: 10,
timestamp: time.Now().Add(time.Second * time.Duration(11) * -1),
}
c.recordMap["key2"] = record{
expiresIn: 1000,
timestamp: time.Now().Add(time.Second * time.Duration(11) * -1),
value: "some-value",
}
c.clean()
_, ok := c.recordMap["key1"]
assert.Equal(t, false, ok)
_, ok = c.recordMap["key2"]
assert.Equal(t, true, ok)
}