-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_test.go
84 lines (72 loc) · 1.97 KB
/
manager_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
package rediser
import (
"context"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var ctx = context.Background()
func TestRediser(t *testing.T) {
m := New(&Config{
Default: "db1",
Connections: map[string]Configable{
"db1": &redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
},
"db2": &redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 1, // use default DB
},
"db3": &redis.ClusterOptions{
Addrs: []string{"localhost:6379", "localhost:6379"},
},
},
}, WithInstance)
// use default(db1)
assert.Nil(t, m.Connect().Set(ctx, "aaa", "1", time.Second*2).Err())
assert.Equal(t, "1", m.Connect().Get(ctx, "aaa").Val())
// db1
assert.Equal(t, "1", m.Connect("db1").Get(ctx, "aaa").Val())
assert.Error(t, m.Connect("db2").Get(ctx, "aaa").Err())
// db2
assert.Nil(t, m.Connect("db2").Set(ctx, "bbb", "1", time.Second*2).Err())
assert.Equal(t, "1", m.Connect("db2").Get(ctx, "bbb").Val())
// use instance
assert.Nil(t, Connect().Set(ctx, "ccc", "1", time.Second*2).Err())
assert.Equal(t, "1", Connect().Get(ctx, "ccc").Val())
// test sleep
time.Sleep(time.Second * 3)
assert.Equal(t, "", Connect().Get(ctx, "aaa").Val())
// not found
assert.Panics(t, func() {
m.Connect("not-found").Get(ctx, "aaa").Val()
})
// type
assert.Equal(t, "localhost:6379", m.Connect("db1").(*redis.Client).Options().Addr)
}
func BenchmarkMap(b *testing.B) {
m := New(&Config{
Default: "db1",
Connections: map[string]Configable{
"db1": &redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
},
"db2": &redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 1, // use default DB
},
},
})
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
go m.Connect().Get(ctx, "default").Val()
}
})
}