forked from stretchr/hoard
/
shared_hoard_test.go
150 lines (105 loc) · 3.18 KB
/
shared_hoard_test.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package hoard
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestHoard_Shared(t *testing.T) {
h := Shared()
assert.NotNil(t, h)
h2 := Shared()
assert.Equal(t, h, h2)
}
func TestShared_Get(t *testing.T) {
firstCalled := false
secondCalled := false
result := Get("key", func() (interface{}, *Expiration) {
firstCalled = true
return "first", ExpiresNever
})
assert.Equal(t, result, "first")
assert.True(t, firstCalled)
result = Get("key", func() (interface{}, *Expiration) {
secondCalled = true
return "second", ExpiresNever
})
assert.NotEqual(t, result, "second")
assert.False(t, secondCalled)
}
func TestShared_GetWithError(t *testing.T) {
result, err := GetWithError("key", func() (interface{}, error, *Expiration) {
return "first", nil, ExpiresNever
})
assert.Equal(t, result, "first")
assert.Nil(t, err)
result, err = GetWithError("key2", func() (interface{}, error, *Expiration) {
return "second", errors.New("EXTERMINATE!!!"), ExpiresNever
})
assert.Equal(t, "second", result)
assert.NotNil(t, err)
}
func TestShared_Remove(t *testing.T) {
Get("something", func() (interface{}, *Expiration) {
return 1, nil
})
assert.Equal(t, 1, Get("something"))
Remove("something")
assert.Equal(t, 2, Get("something", func() (interface{}, *Expiration) { return 2, nil }))
}
func TestShared_SetExpires(t *testing.T) {
date := time.Now()
Get("key", func() (interface{}, *Expiration) {
return "first", ExpiresNever
})
assert.Equal(t, ExpiresNever, Shared().cache["key"].expiration)
SetExpires("key", Expires().OnDate(date))
item, _ := Shared().cacheGet("key")
if assert.NotNil(t, &item) {
if assert.NotNil(t, item.expiration, "Expiration should be set") {
assert.Equal(t, date, item.expiration.date)
}
}
// the expiratoin cache item should have its absolute expiration set to the date value as well
expirationItem := Shared().expirationCache["key"]
if assert.NotNil(t, &expirationItem) {
if assert.NotNil(t, expirationItem.expiration, "Expiration should be set") {
assert.Equal(t, date, expirationItem.expiration.absolute)
}
}
}
func TestShared_SetExpires_Panics(t *testing.T) {
sharedHoard = Make(nil)
assert.False(t, SetExpires("key", Expires().OnDate(time.Now())))
}
func TestShared_ExpirationSetting(t *testing.T) {
result := Get("key2", func() (interface{}, *Expiration) {
expiration := new(Expiration)
expiration.AfterSecondsIdle(10)
expiration.AfterSeconds(10)
expiration.OnCondition(func() bool {
return true
})
return "second", expiration
})
assert.Equal(t, result, "second")
assert.NotEqual(t, 0, Shared().cache["key2"].expiration.idle)
assert.NotEqual(t, 0, Shared().cache["key2"].expiration.duration)
assert.Condition(t, func() bool {
return Shared().cache["key2"].expiration.condition != nil
})
assert.Condition(t, func() bool {
return !Shared().cache["key2"].expiration.absolute.IsZero()
})
}
func TestShared_Set(t *testing.T) {
Set("key", 1)
assert.Equal(t, 1, Shared().Get("key"))
assert.Equal(t, 1, Get("key"))
}
func TestShared_Has(t *testing.T) {
_ = Get("key", func() (interface{}, *Expiration) {
return "first", ExpiresNever
})
assert.True(t, Has("key"))
}