-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
174 lines (135 loc) · 3.77 KB
/
cache_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
package sync_cache
import (
"fmt"
"github.com/go-redis/redis"
uuid_gen "github.com/google/uuid"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
func TestMain(m *testing.M) {
m.Run()
os.Exit(0)
}
func TestRedis(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 10,
})
err := client.Set("key", "value1", 0).Err()
if err != nil {
panic(err)
}
val, err := client.Get("key").Result()
if err != nil {
panic(err)
}
fmt.Println("key", val)
val2, err := client.Get("key2").Result()
if err == redis.Nil {
fmt.Println("key2 does not exist")
} else if err != nil {
panic(err)
} else {
fmt.Println("key2", val2)
}
// Output: key value
// key2 does not exist
}
type testStruct struct {
Param1 string
Param2 int
}
const groupName = "testGroup"
func newTestObj() testStruct {
return testStruct{
Param1: "testStr",
Param2: 1000,
}
}
func newClientWithGroup() *SyncCacheClient {
cacheClient := NewSyncCacheClient(SyncCacheOpts{
Address: "localhost:6379",
Db: 10,
})
getterFunc := func(key string, setCacheFunc SetCacheFunc) error {
obj := newTestObj()
setCacheFunc(obj)
return nil
}
cacheClient.AddCacheGroup(groupName, getterFunc)
return cacheClient
}
func BenchmarkSyncCacheClient_Get(b *testing.B) {
b.ReportAllocs()
cacheClient := newClientWithGroup()
cacheClient.redis.Set("testGroup_testKey", "1", 0)
defer cacheClient.redis.FlushDB()
for i := 0; i < b.N; i++ {
if _, err := cacheClient.Get(groupName, "test"); err != nil {
b.Error(err)
}
}
}
// Если объект есть в Redis
// Если объекта нет в in-memory кэше
func TestSyncCacheClient_Get1(t *testing.T) {
cacheClient := newClientWithGroup()
cacheClient.redis.Set("testGroup_testKey", "redis_uuid", 0)
defer cacheClient.redis.FlushDB()
i, err := cacheClient.Get(groupName, "testKey")
assert.NoError(t, err)
obj, ok := i.(testStruct)
assert.True(t, ok)
assert.Equal(t, 1000, obj.Param2)
cacheEntity := cacheClient.cacheGroupManager.cacheGroups[groupName].get("testKey")
assert.Equal(t, "redis_uuid", cacheEntity.uuid)
}
// Key not in Redis
// Object not in cache
func TestSyncCacheClient_Get2(t *testing.T) {
cacheClient := newClientWithGroup()
defer cacheClient.redis.FlushDB()
i, err := cacheClient.Get(groupName, "testKey")
assert.NoError(t, err)
obj, ok := i.(testStruct)
assert.True(t, ok)
assert.Equal(t, 1000, obj.Param2)
cacheEntity := cacheClient.cacheGroupManager.cacheGroups[groupName].get("testKey")
assert.Equal(t, "", cacheEntity.uuid)
uuidFromRedis, err := cacheClient.redis.Get("testGroup_testKey").Result()
assert.NoError(t, err)
_, err = uuid_gen.Parse(uuidFromRedis)
assert.NoError(t, err)
}
// Key not in Redis
// Object in cache
func TestSyncCacheClient_Get3(t *testing.T) {
cacheClient := newClientWithGroup()
objOld := newTestObj()
objOld.Param2 = 111
cacheClient.cacheGroupManager.cacheGroups[groupName].set("testKey", objOld, "old_redis_uuid")
cacheClient.redis.Set("testGroup_testKey", "redis_uuid", 0)
defer cacheClient.redis.FlushDB()
i, err := cacheClient.Get(groupName, "testKey")
assert.NoError(t, err)
obj, ok := i.(testStruct)
assert.True(t, ok)
assert.Equal(t, 1000, obj.Param2)
cacheEntity := cacheClient.cacheGroupManager.cacheGroups[groupName].get("testKey")
assert.Equal(t, "redis_uuid", cacheEntity.uuid)
}
// Key not in Redis
// Object in cache
// Object not in db
func TestSyncCacheClient_Get4(t *testing.T) {
cacheClient := NewSyncCacheClient(SyncCacheOpts{})
defer cacheClient.redis.FlushDB()
getterFunc := func(key string, setCacheFunc SetCacheFunc) error {
return fmt.Errorf("test error")
}
cacheClient.AddCacheGroup(groupName, getterFunc)
_, err := cacheClient.Get(groupName, "testKey")
assert.Error(t, err)
}