This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
forked from Imgur/incus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_store_test.go
164 lines (133 loc) · 3.82 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
package main
import (
"testing"
"time"
"menteslibres.net/gosexy/redis"
)
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")
}
}
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))
}
}
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")
}
}
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")
}
}
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")
}
}
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")
}
}