-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_xconf_adapter_test.go
254 lines (224 loc) · 7.25 KB
/
memory_xconf_adapter_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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xcache/blob/main/LICENSE.
package xcache_test
import (
"context"
"errors"
"fmt"
"os"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/actforgood/xcache"
"github.com/actforgood/xconf"
)
func TestMemory_withXConf(t *testing.T) {
t.Parallel()
t.Run("expected config is changed", testMemoryWithXConfConfigIsChanged)
t.Run("expected config is not changed", testMemoryWithXConfConfigIsNotChanged)
}
func testMemoryWithXConfConfigIsChanged(t *testing.T) {
t.Parallel()
// arrange
var (
reloadConfig uint32
memSize1 int64 = freecacheMinMem // 512 Kb
initialConfig = map[string]any{
xcache.MemoryCfgKeyMemorySize: memSize1,
}
memSize2 int64 = 1024 * 1024 // 1 Mb
configReloaded = map[string]any{
xcache.MemoryCfgKeyMemorySize: memSize2,
}
configLoader = xconf.LoaderFunc(func() (map[string]any, error) {
if atomic.LoadUint32(&reloadConfig) == 1 {
return configReloaded, nil
}
return initialConfig, nil
})
config, _ = xconf.NewDefaultConfig(
configLoader,
xconf.DefaultConfigWithReloadInterval(time.Second),
)
subject = xcache.NewMemoryWithConfig(config)
keyPrefix = "test-xconf-key-"
value = []byte("test value")
ctx = context.Background()
)
defer config.Close()
// save some keys
for i := 0; i < 10; i++ {
key := keyPrefix + strconv.FormatInt(int64(i), 10)
err := subject.Save(ctx, key, value, xcache.NoExpire)
requireNil(t, err)
}
for i := 10; i < 20; i++ {
key := keyPrefix + strconv.FormatInt(int64(i), 10)
err := subject.Save(ctx, key, value, 5*time.Second)
requireNil(t, err)
}
for i := 20; i < 30; i++ { // keys that will expire
key := keyPrefix + strconv.FormatInt(int64(i), 10)
err := subject.Save(ctx, key, value, time.Second)
requireNil(t, err)
}
// act
stats1, _ := subject.Stats(ctx)
time.Sleep(1300 * time.Millisecond) // let the keys with 1s expiration to expire
atomic.AddUint32(&reloadConfig, 1)
time.Sleep(1300 * time.Millisecond) // let xconf reload the configuration
stats2, _ := subject.Stats(ctx)
// assert
assertEqual(t, memSize1, stats1.MaxMemory)
assertEqual(t, int64(30), stats1.Keys)
assertEqual(t, memSize2, stats2.MaxMemory)
assertEqual(t, int64(20), stats2.Keys) // 10 expired
for i := 0; i < 20; i++ {
key := keyPrefix + strconv.FormatInt(int64(i), 10)
_, err := subject.Load(ctx, key)
assertNil(t, err)
}
for i := 20; i < 30; i++ {
key := keyPrefix + strconv.FormatInt(int64(i), 10)
_, err := subject.Load(ctx, key)
assertTrue(t, errors.Is(err, xcache.ErrNotFound))
}
}
func testMemoryWithXConfConfigIsNotChanged(t *testing.T) {
t.Parallel()
// arrange
var (
reloadConfig uint32
memSize int64 = freecacheMinMem // 512 Kb
initialConfig = map[string]any{
xcache.MemoryCfgKeyMemorySize: memSize,
"some_other_config": "some value",
}
configReloaded = map[string]any{
xcache.MemoryCfgKeyMemorySize: memSize,
"some_other_config": "some other value",
}
configLoader = xconf.LoaderFunc(func() (map[string]any, error) {
if atomic.LoadUint32(&reloadConfig) == 1 {
return configReloaded, nil
}
return initialConfig, nil
})
config, _ = xconf.NewDefaultConfig(
configLoader,
xconf.DefaultConfigWithReloadInterval(time.Second),
)
subject = xcache.NewMemoryWithConfig(config)
keyPrefix = "test-xconf-key-"
value = []byte("test value")
ctx = context.Background()
)
defer config.Close()
// save some keys
for i := 0; i < 10; i++ {
key := keyPrefix + strconv.FormatInt(int64(i), 10)
err := subject.Save(ctx, key, value, xcache.NoExpire)
requireNil(t, err)
}
for i := 10; i < 20; i++ {
key := keyPrefix + strconv.FormatInt(int64(i), 10)
err := subject.Save(ctx, key, value, 15*time.Second)
requireNil(t, err)
}
for i := 20; i < 30; i++ { // keys that will expire
key := keyPrefix + strconv.FormatInt(int64(i), 10)
err := subject.Save(ctx, key, value, time.Second)
requireNil(t, err)
}
// act
stats1, _ := subject.Stats(ctx)
time.Sleep(1300 * time.Millisecond) // let the keys with 1s expiration to expire
atomic.AddUint32(&reloadConfig, 1)
time.Sleep(1300 * time.Millisecond) // let xconf reload the configuration
stats2, _ := subject.Stats(ctx)
// assert
assertEqual(t, memSize, stats1.MaxMemory)
assertEqual(t, int64(30), stats1.Keys)
assertEqual(t, memSize, stats2.MaxMemory)
assertEqual(t, int64(30), stats2.Keys) // 10 expired, but freecache does not deletes them until load
for i := 0; i < 20; i++ {
key := keyPrefix + strconv.FormatInt(int64(i), 10)
_, err := subject.Load(ctx, key)
assertNil(t, err)
}
for i := 20; i < 30; i++ {
key := keyPrefix + strconv.FormatInt(int64(i), 10)
_, err := subject.Load(ctx, key)
assertTrue(t, errors.Is(err, xcache.ErrNotFound))
}
}
func TestMemory_withXConf_concurrency(t *testing.T) {
t.Parallel()
var (
memSize = freecacheMinMem
configLoader = xconf.LoaderFunc(func() (map[string]any, error) {
if time.Now().Unix()%2 == 0 {
memSize++
}
return map[string]any{
xcache.MemoryCfgKeyMemorySize: memSize,
}, nil
})
config, _ = xconf.NewDefaultConfig(
configLoader,
xconf.DefaultConfigWithReloadInterval(time.Second),
)
subject = xcache.NewMemoryWithConfig(config)
)
testCacheWithXConfConcurrency(subject)(t)
_ = config.Close()
t.Logf("config changed %d times during test", memSize-freecacheMinMem)
}
func ExampleMemory_withXConf() {
// Setup an env (assuming your application configuration comes from env,
// it's not mandatory to be env, you can use any source loader you want)
_ = os.Setenv("MY_APP_CACHE_MEM_SIZE", "1048576")
defer os.Unsetenv("MY_APP_CACHE_MEM_SIZE")
// Initialize config, we set an alias, as example, as our config key is custom ("MY_APP_CACHE_MEM_SIZE").
config, err := xconf.NewDefaultConfig(
xconf.AliasLoader(
xconf.EnvLoader(),
xcache.MemoryCfgKeyMemorySize, "MY_APP_CACHE_MEM_SIZE",
),
xconf.DefaultConfigWithReloadInterval(2*time.Second),
)
if err != nil {
panic(err)
}
defer config.Close()
// Initialize the cache our application will use.
cache := xcache.NewMemoryWithConfig(config)
// From this point forward you can use the cache object however you want.
// Let's print some stats to see the memory size.
stats, err := cache.Stats(context.Background())
if err != nil {
fmt.Println(err)
} else {
fmt.Println(stats)
}
// Let's assume we monitor our cache - see StatsWatcher for that,
// and we notice existing keys count is kind of constant and an increase in evictions,
// meaning our memory cache is probably full.
// We decide to increase the memory size.
_ = os.Setenv("MY_APP_CACHE_MEM_SIZE", "5242880")
time.Sleep(2500 * time.Millisecond) // wait for config to reload
// Print again the stats, we can see that memory was changed,
// without the need of restarting/redeploying our application.
stats, err = cache.Stats(context.Background())
if err != nil {
fmt.Println(err)
} else {
fmt.Println(stats)
}
// Output:
// mem=1M maxMem=1M memUsage=100.00% hits=0 misses=0 hitRate=100.00% keys=0 expired=0 evicted=0
// mem=5M maxMem=5M memUsage=100.00% hits=0 misses=0 hitRate=100.00% keys=0 expired=0 evicted=0
}