-
Notifications
You must be signed in to change notification settings - Fork 4
/
redis_test.go
67 lines (63 loc) · 1.57 KB
/
redis_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
package goutils
import (
"context"
"testing"
"github.com/go-redis/redis/v8"
"github.com/spf13/viper"
)
func TestNewRedisClient(t *testing.T) {
rdb, err := NewRedisClient(&redis.Options{})
if err != nil {
t.Fatal(err)
}
if rdb == nil {
t.Fatal("new a nil redis client")
}
rdb.Close()
}
func TestRedisClient(t *testing.T) {
defer viper.Reset()
viper.Set("redis.localhost.addr", "127.0.0.1:6379")
viper.Set("redis.localhost.password", "")
viper.Set("redis.localhost.db", 0)
viper.Set("redis.localhost.dial_timeout", 5)
viper.Set("redis.localhost.read_timeout", 3)
viper.Set("redis.localhost.write_timeout", 3)
viper.Set("redis.localhost.pool_size", 0)
rdb, err := RedisClient("localhost")
if err != nil {
t.Error(err)
}
if rdb == nil {
t.Fatal("get a nil redis client")
}
defer CloseRedisInstances()
if _, err := rdb.Ping(context.TODO()).Result(); err != nil {
t.Error(err)
}
if _, err := RedisClient("localhost"); err != nil {
t.Error(err)
}
viper.Set("redis.unittest.addr", "127.0.0.1:6379")
viper.Set("redis.unittest.password", "")
viper.Set("redis.unittest.db", 0)
viper.Set("redis.unittest.dial_timeout", 5)
viper.Set("redis.unittest.read_timeout", 3)
viper.Set("redis.unittest.write_timeout", 3)
viper.Set("redis.unittest.pool_size", 0)
rdb, err = RedisClient("unittest")
if err != nil {
t.Error(err)
}
if rdb == nil {
t.Fatal("get a nil redis client")
}
instanceCount := 0
RedisInstances.Range(func(k, v interface{}) bool {
instanceCount++
return true
})
if instanceCount != 2 {
t.Error("instanceCount != 2, ", instanceCount)
}
}