-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcache_test.go
238 lines (203 loc) · 5.48 KB
/
memcache_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package memcache
import (
"encoding/gob"
"errors"
"fmt"
"io/ioutil"
"testing"
"time"
"aahframe.work/cache"
"aahframe.work/config"
"aahframe.work/log"
"github.com/stretchr/testify/assert"
)
func TestMemcache(t *testing.T) {
mgr := createCacheMgr(t, "memcache1", `
cache {
memcache1 {
provider = "memcache"
addresses = ["localhost:11211"]
max_idle_conns = 4
timeout = "5s"
}
}
`)
e := mgr.CreateCache(&cache.Config{Name: "cache1", ProviderName: "memcache1"})
assert.Nil(t, e, "unable to create cache")
c := mgr.Cache("cache1")
type sample struct {
Name string
Present bool
Value string
}
testcases := []struct {
label string
key string
value interface{}
}{
{
label: "Memcache integer",
key: "key1",
value: 342348347,
},
{
label: "Memcache float",
key: "key2",
value: 0.78346374,
},
{
label: "Memcache string",
key: "key3",
value: "This is mt cache string",
},
{
label: "Memcache map",
key: "key4",
value: map[string]interface{}{"key1": 343434, "key2": "kjdhdsjkdhjs", "key3": 87235.3465},
},
{
label: "Memcache struct",
key: "key5",
value: sample{Name: "Jeeva", Present: true, Value: "memcache provider"},
},
}
err := c.Put("pre-test-key1", sample{Name: "Jeeva", Present: true, Value: "memcache provider"}, 3*time.Second)
assert.Equal(t, errors.New("aah/cache/cache1: gob: type not registered for interface: memcache.sample"), err)
_, _ = c.GetOrPut("pre-test-key1", sample{Name: "Jeeva", Present: true, Value: "memcache provider"}, 3*time.Second)
gob.Register(map[string]interface{}{})
gob.Register(sample{})
for _, tc := range testcases {
t.Run(tc.label, func(t *testing.T) {
assert.False(t, c.Exists(tc.key))
assert.Nil(t, c.Get(tc.key))
err := c.Put(tc.key, tc.value, 3*time.Second)
assert.Nil(t, err)
v := c.Get(tc.key)
assert.Equal(t, tc.value, v)
assert.True(t, c.Exists(tc.key))
assert.Nil(t, c.Delete(tc.key))
v, err = c.GetOrPut(tc.key, tc.value, 3*time.Second)
assert.Nil(t, err)
assert.Equal(t, tc.value, v)
})
}
c.Flush()
}
func TestMemcacheAddAndGet(t *testing.T) {
c := createTestCache(t, "memcache1", `
cache {
memcache1 {
provider = "memcache"
addresses = ["localhost:11211"]
}
}
`, &cache.Config{Name: "addgetcache", ProviderName: "memcache1"})
for i := 0; i < 20; i++ {
c.Put(fmt.Sprintf("key_%v", i), i, 3*time.Second)
}
for i := 5; i < 10; i++ {
v := c.Get(fmt.Sprintf("key_%v", i))
assert.Equal(t, i, v)
}
assert.Equal(t, "addgetcache", c.Name())
}
func TestMemcacheMultipleCache(t *testing.T) {
mgr := createCacheMgr(t, "memcache1", `
cache {
memcache1 {
provider = "memcache"
addresses = ["localhost:11211"]
}
}
`)
names := []string{"testcache1", "testcache2", "testcache3"}
for _, name := range names {
err := mgr.CreateCache(&cache.Config{Name: name, ProviderName: "memcache1"})
assert.Nil(t, err, "unable to create cache")
c := mgr.Cache(name)
assert.NotNil(t, c)
assert.Equal(t, name, c.Name())
for i := 0; i < 20; i++ {
c.Put(fmt.Sprintf("key_%v", i), i, 3*time.Second)
}
for i := 5; i < 10; i++ {
v := c.Get(fmt.Sprintf("key_%v", i))
assert.Equal(t, i, v)
}
c.Flush()
p := mgr.Provider("memcache1").(*Provider)
assert.NotNil(t, p.Client())
}
}
func TestRedisSlideEvictionMode(t *testing.T) {
c := createTestCache(t, "memcache1", `
cache {
memcache1 {
provider = "memcache"
# addresses = ["localhost:11211"]
}
}
`, &cache.Config{Name: "addgetcache", ProviderName: "memcache1", EvictionMode: cache.EvictionModeSlide})
for i := 0; i < 20; i++ {
c.Put(fmt.Sprintf("key_%v", i), i, 3*time.Second)
}
for i := 5; i < 10; i++ {
v, err := c.GetOrPut(fmt.Sprintf("key_%v", i), i, 3*time.Second)
assert.Nil(t, err)
assert.Equal(t, i, v)
}
assert.Equal(t, "addgetcache", c.Name())
}
func TestMemcacheInvalidProviderName(t *testing.T) {
mgr := cache.NewManager()
mgr.AddProvider("memcache1", new(Provider))
cfg, _ := config.ParseString(`
cache {
memcache1 {
provider = "mymemcache"
addresses = ["localhost:11211"]
}
}
`)
l, _ := log.New(config.NewEmpty())
err := mgr.InitProviders(cfg, l)
assert.Equal(t, errors.New("aah/cache: not a vaild provider name, expected 'memcache'"), err)
}
func TestMemcacheInvalidAddress(t *testing.T) {
mgr := cache.NewManager()
mgr.AddProvider("memcache1", new(Provider))
cfg, _ := config.ParseString(`
cache {
memcache1 {
provider = "memcache"
addresses = ["localhost:1123211"]
}
}
`)
l, _ := log.New(config.NewEmpty())
err := mgr.InitProviders(cfg, l)
assert.Equal(t, errors.New("aah/cache/memcache1: memcache: no servers configured or available"), err)
}
func TestParseTimeDuration(t *testing.T) {
d := parseDuration("", "1m")
assert.Equal(t, float64(1), d.Minutes())
}
func createCacheMgr(t *testing.T, name, appCfgStr string) *cache.Manager {
mgr := cache.NewManager()
mgr.AddProvider(name, new(Provider))
cfg, _ := config.ParseString(appCfgStr)
l, _ := log.New(config.NewEmpty())
l.SetWriter(ioutil.Discard)
err := mgr.InitProviders(cfg, l)
assert.Nil(t, err, "unexpected")
return mgr
}
func createTestCache(t *testing.T, name, appCfgStr string, cacheCfg *cache.Config) cache.Cache {
mgr := createCacheMgr(t, name, appCfgStr)
e := mgr.CreateCache(cacheCfg)
assert.Nil(t, e, "unable to create cache")
return mgr.Cache(cacheCfg.Name)
}