-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacheablemanager_test.go
195 lines (152 loc) · 5.28 KB
/
cacheablemanager_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package gocacheable
import (
"errors"
"os"
"testing"
"time"
gcCacheModule "github.com/josemiguelmelo/gocacheable/cachemodule"
bcProvider "github.com/josemiguelmelo/gocacheable/providers/bigcache"
"github.com/stretchr/testify/assert"
)
const (
identifier = "testing_manager"
moduleName = "testing_module"
)
var cacheableManager CacheableManager
func CreateCacheableManager() CacheableManager {
return NewCacheableManager(identifier)
}
func setup() {
cacheableManager = CreateCacheableManager()
}
func TestMain(m *testing.M) {
setup()
code := m.Run()
os.Exit(code)
}
func TestManagerCreatedSuccessfully(t *testing.T) {
// Check manager does not contain any module
assert.Equal(t, 0, cacheableManager.ModulesCount())
assert.Equal(t, identifier, cacheableManager.Identifier)
}
func TestAddModuleToManager(t *testing.T) {
storageProvider := &bcProvider.BigCacheProvider{
Lifetime: 2,
}
err := cacheableManager.AddModule(moduleName, storageProvider)
assert.Nil(t, err)
assert.Equal(t, 1, cacheableManager.ModulesCount())
}
func TestAddAlreadyExistingModuleToManager(t *testing.T) {
storageProvider := &bcProvider.BigCacheProvider{
Lifetime: 2,
}
err := cacheableManager.AddModule(moduleName, storageProvider)
assert.NotNil(t, err)
assert.Equal(t, "Module already exists", err.Error())
assert.Equal(t, 1, cacheableManager.ModulesCount())
}
func TestManagerFindModule(t *testing.T) {
// Find a module that exists
module, err := cacheableManager.FindModule(moduleName)
assert.Nil(t, err)
assert.Equal(t, moduleName, module.Identifier)
// Check if module exists
containsModule := cacheableManager.ContainsModule(*module)
assert.Equal(t, true, containsModule)
// Find a module that does not exist
module, err = cacheableManager.FindModule("not_found")
assert.NotNil(t, err)
assert.Equal(t, "", module.Identifier)
// Check if a non existent module is present
containsModule = cacheableManager.ContainsModule(gcCacheModule.CacheModule{})
assert.Equal(t, false, containsModule)
}
func methodToCache() int {
return 9
}
func secondMethodToCache() int {
panic(errors.New("Should not be here"))
}
func TestCacheableMethod(t *testing.T) {
var outValue int
err := cacheableManager.Cacheable(moduleName, "test", func() (interface{}, error) {
return methodToCache(), nil
}, &outValue, 100*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 9, outValue)
err = cacheableManager.Cacheable(moduleName, "test", func() (interface{}, error) {
return secondMethodToCache(), nil
}, &outValue, 100*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 9, outValue)
err = cacheableManager.Cacheable("moduleName", "test", func() (interface{}, error) {
return secondMethodToCache(), nil
}, &outValue, 100*time.Millisecond)
assert.NotNil(t, err)
assert.Equal(t, "Module not found", err.Error())
}
type ExampleObj struct {
St string
notShown string
}
func TestCacheableMethodWithObject(t *testing.T) {
var outValue ExampleObj
err := cacheableManager.Cacheable(moduleName, "test_object", func() (interface{}, error) {
return ExampleObj{St: "yes", notShown: "here"}, nil
}, &outValue, 100*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, "yes", outValue.St)
// Is not serializable as it is private, so is not cached.
assert.Equal(t, "", outValue.notShown)
err = cacheableManager.Cacheable(moduleName, "test_object", func() (interface{}, error) {
return ExampleObj{St: "no", notShown: "here"}, nil
}, &outValue, 100*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, "yes", outValue.St)
// Is not serializable as it is private, so is not cached.
assert.Equal(t, "", outValue.notShown)
}
func TestCacheableDeleteKey(t *testing.T) {
var outVal ExampleObj
err := cacheableManager.Get(moduleName, "test_object", &outVal)
assert.Nil(t, err)
err = cacheableManager.DeleteKey(moduleName, "test_object")
assert.Nil(t, err)
err = cacheableManager.Get(moduleName, "test_object", &outVal)
assert.NotNil(t, err)
}
func TestCacheableReset(t *testing.T) {
var outVal int
err := cacheableManager.Get(moduleName, "test", &outVal)
assert.Nil(t, err)
err = cacheableManager.Reset(moduleName)
assert.Nil(t, err)
err = cacheableManager.Get(moduleName, "test", &outVal)
assert.NotNil(t, err)
assert.Equal(t, "Entry not found", err.Error())
}
func TestCacheableTimeToLive(t *testing.T) {
var outValue ExampleObj
err := cacheableManager.Cacheable(moduleName, "time_to_live", func() (interface{}, error) {
return ExampleObj{St: "yes", notShown: "here"}, nil
}, &outValue, 100*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, "yes", outValue.St)
// Is not serializable as it is private, so is not cached.
assert.Equal(t, "", outValue.notShown)
err = cacheableManager.Cacheable(moduleName, "time_to_live", func() (interface{}, error) {
return ExampleObj{St: "no", notShown: "here"}, nil
}, &outValue, 100*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, "yes", outValue.St)
// Is not serializable as it is private, so is not cached.
assert.Equal(t, "", outValue.notShown)
time.Sleep(200 * time.Millisecond)
err = cacheableManager.Cacheable(moduleName, "time_to_live", func() (interface{}, error) {
return ExampleObj{St: "no", notShown: "here"}, nil
}, &outValue, 100*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, "no", outValue.St)
assert.Equal(t, "", outValue.notShown)
}