-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis7_xconf_adapter_integration_test.go
185 lines (162 loc) · 5.66 KB
/
redis7_xconf_adapter_integration_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
//go:build integration
// 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"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/actforgood/xcache"
"github.com/actforgood/xconf"
)
func TestRedis7_withXConf_integration(t *testing.T) {
t.Parallel()
if redis7ConfigIntegration.IsCluster() {
t.Skip("skip as tests rely on db, and db does not matter in cluster setup")
}
t.Run("expected config is changed", testRedis7WithXConfConfigIsChanged)
t.Run("expected config is not changed", testRedis7WithXConfConfigIsNotChanged)
}
func testRedis7WithXConfConfigIsChanged(t *testing.T) {
t.Parallel()
// arrange
var (
reloadConfig uint32
initialConfig = map[string]any{
xcache.RedisCfgKeyAddrs: redis7ConfigIntegration.Addrs,
xcache.RedisCfgKeyFailoverMasterName: redis7ConfigIntegration.MasterName,
xcache.RedisCfgKeyDB: 0,
xcache.RedisCfgKeyDialTimeout: 10 * time.Second,
xcache.RedisCfgKeyReadTimeout: 10 * time.Second,
xcache.RedisCfgKeyWriteTimeout: 15 * time.Second,
}
configReloaded = map[string]any{
xcache.RedisCfgKeyAddrs: redis7ConfigIntegration.Addrs,
xcache.RedisCfgKeyFailoverMasterName: redis7ConfigIntegration.MasterName,
xcache.RedisCfgKeyDB: 1,
xcache.RedisCfgKeyDialTimeout: 9 * time.Second,
xcache.RedisCfgKeyReadTimeout: 9 * time.Second,
xcache.RedisCfgKeyWriteTimeout: 14 * time.Second,
}
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.NewRedis7WithConfig(config)
keyPrefix = "test-xconf-withconfigchange-key-"
value = []byte("test value")
ctx = context.Background()
)
defer config.Close()
defer subject.Close()
// save some keys
for i := 0; i < 10; i++ {
key := keyPrefix + strconv.FormatInt(int64(i), 10)
err := subject.Save(ctx, key, value, 5*time.Minute)
requireNil(t, err)
}
// act
time.Sleep(200 * time.Millisecond) // let the config reload goroutine to start
atomic.AddUint32(&reloadConfig, 1)
time.Sleep(1200 * time.Millisecond) // let xconf reload the configuration
// assert
for i := 0; i < 10; i++ { // db was switched, so keys are expected not to be found
key := keyPrefix + strconv.FormatInt(int64(i), 10)
_, err := subject.Load(ctx, key)
assertTrue(t, errors.Is(err, xcache.ErrNotFound))
}
}
func testRedis7WithXConfConfigIsNotChanged(t *testing.T) {
t.Parallel()
// arrange
var (
reloadConfig uint32
initialConfig = map[string]any{
xcache.RedisCfgKeyAddrs: redis7ConfigIntegration.Addrs,
xcache.RedisCfgKeyFailoverMasterName: redis7ConfigIntegration.MasterName,
xcache.RedisCfgKeyDB: 0,
xcache.RedisCfgKeyDialTimeout: 10 * time.Second,
xcache.RedisCfgKeyReadTimeout: 10 * time.Second,
xcache.RedisCfgKeyWriteTimeout: 15 * time.Second,
"some_other_config": "some value",
}
configReloaded = map[string]any{
xcache.RedisCfgKeyAddrs: redis7ConfigIntegration.Addrs,
xcache.RedisCfgKeyFailoverMasterName: redis7ConfigIntegration.MasterName,
xcache.RedisCfgKeyDB: 0,
xcache.RedisCfgKeyDialTimeout: 10 * time.Second,
xcache.RedisCfgKeyReadTimeout: 10 * time.Second,
xcache.RedisCfgKeyWriteTimeout: 15 * time.Second,
"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.NewRedis7WithConfig(config)
keyPrefix = "test-xconf-withoutconfigchange-key-"
value = []byte("test value")
ctx = context.Background()
)
defer config.Close()
defer subject.Close()
// save some keys
for i := 0; i < 10; i++ {
key := keyPrefix + strconv.FormatInt(int64(i), 10)
err := subject.Save(ctx, key, value, 5*time.Minute)
requireNil(t, err)
}
// act
time.Sleep(200 * time.Millisecond) // let the config reload goroutine to start
atomic.AddUint32(&reloadConfig, 1)
time.Sleep(1200 * time.Millisecond) // let xconf reload the configuration
// assert
for i := 0; i < 10; i++ { // check that keys are there found
key := keyPrefix + strconv.FormatInt(int64(i), 10)
_, err := subject.Load(ctx, key)
assertNil(t, err)
}
}
func TestRedis7_withXConf_concurrency(t *testing.T) {
t.Parallel()
var (
readTimeout = 3 * time.Second
configLoader = xconf.LoaderFunc(func() (map[string]any, error) {
if time.Now().Unix()%2 == 0 {
readTimeout += time.Second
}
return map[string]any{
xcache.RedisCfgKeyAddrs: redis7ConfigIntegration.Addrs,
xcache.RedisCfgKeyFailoverMasterName: redis7ConfigIntegration.MasterName,
xcache.RedisCfgKeyReadTimeout: readTimeout,
}, nil
})
config, _ = xconf.NewDefaultConfig(
configLoader,
xconf.DefaultConfigWithReloadInterval(time.Second),
)
subject = xcache.NewRedis7WithConfig(config)
)
testCacheWithXConfConcurrency(subject)(t)
_ = config.Close()
err := subject.Close()
assertNil(t, err)
t.Logf("config changed %d times during test", (readTimeout-3*time.Second)/time.Second)
}