forked from Imgur/incus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_store_test.go
293 lines (236 loc) · 7.11 KB
/
redis_store_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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package incus
import (
"log"
"os"
"testing"
"time"
)
var (
REDISHOST = "127.0.0.1"
REDISPORT = 6379
)
func newTestRedisStore() *RedisStore {
stats := &DiscardStats{}
store := newRedisStore(REDISHOST, REDISPORT, "", redisTLSOption{enabled: false}, 5, 3, stats)
store.presenceDuration = 10
return store
}
func TestMain(m *testing.M) {
DEBUG = true
store := newTestRedisStore()
_, err := store.GetConn()
if err != nil {
log.Printf("Failed to connect to redis at %s:%d. Skipping redis tests.\n", REDISHOST, REDISPORT)
} else {
os.Exit(m.Run())
}
}
//var redisStore = makeTestStore()
// func makeTestStore() RedisStore {
// var redisStore = initStore(nil).redis
// redisStore.clientsKey = "TestClientKey"
// redisStore.pool.maxIdle = 1
// return redisStore
// }
func TestPooltestConn(t *testing.T) {
// conn := redis.New()
// err := conn.Connect("localhost", 6379)
// err = redisStore.pool.testConn(conn)
// if err != nil {
// t.Fatalf("testConn test failed: %s", err.Error())
// }
// conn.Quit()
// err = redisStore.pool.testConn(conn)
// if err == nil {
// t.Fatalf("testConn test failed: expected error got nil")
// }
return
}
func TestPoolClose(t *testing.T) {
// conn := redis.New()
// err := conn.Connect("localhost", 6379)
// if err != nil {
// t.Errorf("pool.Close test failed: could not get connection")
// }
// if len(redisStore.pool.connections) != 0 {
// t.Errorf("pool.Close test failed: connection pool not empty")
// }
// redisStore.pool.Close(conn)
// if len(redisStore.pool.connections) != 1 {
// t.Errorf("pool.Close test failed: connection pool length expected to be 1 got %v", len(redisStore.pool.connections))
// }
// conn = redis.New()
// err = conn.Connect("localhost", 6379)
// if err != nil {
// t.Errorf("pool.Close test failed: could not get connection")
// }
// redisStore.pool.Close(conn)
// if len(redisStore.pool.connections) != 1 { //testing maxIdle; maxIdle = 1
// t.Errorf("pool.Close test failed: connection pool length expected to be 1 got %s", len(redisStore.pool.connections))
// }
return
}
func TestPoolGet(t *testing.T) {
// if len(redisStore.pool.connections) != 1 {
// t.Fatalf("pool.Get test failed: connection pool length expected to be 1 got %s", len(redisStore.pool.connections))
// }
// client, ok := redisStore.pool.Get() // retrieve the connection that we created in the previous test
// if !ok {
// t.Fatalf("pool.Close test failed: could not get connection")
// }
// if len(redisStore.pool.connections) != 0 {
// t.Errorf("pool.Get test failed: connection pool length expected to be 0 got %s", len(redisStore.pool.connections))
// }
// client.Quit()
// client, ok = redisStore.pool.Get() // create a new connection using connFn
// if !ok {
// t.Fatalf("pool.Close test failed: could not get connection")
// }
// if _, err := client.Ping(); err != nil {
// t.Errorf("pool.Close test failed: could not ping new connection")
// }
// client.Quit()
}
func TestSubscribeAndPublish(t *testing.T) {
// rec := make(chan []string)
// conn, err := redisStore.Subscribe(rec, "incusTesting")
// if err != nil {
// t.Fatalf("Could not subscribe: %s", err.Error())
// }
// defer conn.Quit()
// go func() {
// time.Sleep(20 * time.Millisecond)
// redisStore.Publish("incusTesting", "TEST")
// }()
// <-rec // throwaway subscribe message
// ms := <-rec
// if ms[2] != "TEST" {
// t.Fatalf("Subscribe and Publish test failed, got %s", ms[2])
// }
}
func TestRSave(t *testing.T) {
// redisStore.Save("TEST")
// redisStore.Save("TEST1")
// redisStore.Save("TEST2")
// redisStore.Save("TEST3")
// client, err := redisStore.GetConn()
// if err != nil {
// t.Fatal("Save test failed couldn't get redis connection")
// }
// defer client.Quit()
// arr, _ := client.SMembers(redisStore.clientsKey)
// if len(arr) != 4 {
// t.Fatal("Save test failed")
// }
return
}
func TestRRemove(t *testing.T) {
// redisStore.Remove("TEST")
// redisStore.Remove("TEST1")
// client, err := redisStore.GetConn()
// if err != nil {
// t.Fatal("Remove test failed couldn't get redis connection")
// }
// defer client.Quit()
// arr, _ := client.SMembers(redisStore.clientsKey)
// if len(arr) != 2 {
// t.Fatal("Remove test failed")
// }
return
}
func TestRClients(t *testing.T) {
// arr, err := redisStore.Clients()
// if err != nil {
// t.Fatal("Clients test failed couldn't get redis connection")
// }
// if len(arr) != 2 {
// t.Fatal("Clients test failed")
// }
return
}
func TestRCount(t *testing.T) {
// num, err := redisStore.Count()
// if err != nil {
// t.Fatal("Clients test failed couldn't get redis connection")
// }
// if num != 2 {
// t.Fatal("Clients test failed")
// }
return
}
func TestUserPresenceExpiresFromRedis(t *testing.T) {
store := newTestRedisStore()
// Assert precondition, so that prior test runs don't mess up this one.
// It might also make sense in a separate test to white-box it and call DEL on what the key is supposed to be
store.MarkInactive("foobar", "sock1")
active, err := store.QueryIsUserActive("foobar", time.Now().Unix())
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if active {
t.Fatalf("Expected 'foobar' to be inactive")
}
store.MarkActive("foobar", "sock1", time.Now().Unix())
active, err = store.QueryIsUserActive("foobar", time.Now().Unix())
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if !active {
t.Fatalf("Expected 'foobar' to be active")
}
// wait a bit more than the presence duration for the redis key to expire
time.Sleep(15 * time.Second)
active, err = store.QueryIsUserActive("foobar", time.Now().Unix())
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if active {
t.Fatalf("Expected 'foobar' to be inactive")
}
}
func TestUserPresenceIsImmediatelyRemovedUponMarkingInactive(t *testing.T) {
store := newTestRedisStore()
store.MarkActive("bazbar", "sock1", time.Now().Unix())
active, err := store.QueryIsUserActive("bazbar", time.Now().Unix())
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if !active {
t.Fatalf("Expected precondition that 'bazbar' to be active")
}
store.MarkInactive("bazbar", "sock1")
active, err = store.QueryIsUserActive("bazbar", time.Now().Unix())
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if active {
t.Fatalf("Expected 'bazbar' to be inactive after affirmatively marking as inactive")
}
}
func TestKillswitch(t *testing.T) {
store := newTestRedisStore()
store.DeactivateLongpollKillswitch()
active, err := store.GetIsLongpollKillswitchActive()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if active {
t.Fatalf("Expected precondition that killswitch is inactive")
}
store.ActivateLongpollKillswitch(3)
active, err = store.GetIsLongpollKillswitchActive()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if !active {
t.Fatalf("Expected killswitch to be active")
}
time.Sleep(4 * time.Second)
active, err = store.GetIsLongpollKillswitchActive()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if active {
t.Fatalf("Expected killswitch to be inactive")
}
}