forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
197 lines (190 loc) · 5.5 KB
/
client_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
package mock
import (
"context"
"errors"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/redis/rueidis"
)
func TestNewClient(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()
client := NewClient(ctrl)
{
client.EXPECT().Do(ctx, Match("GET", "a")).Return(Result(RedisNil()))
if err := client.Do(ctx, client.B().Get().Key("a").Build()).Error(); !rueidis.IsRedisNil(err) {
t.Fatalf("unexpected err %v", err)
}
}
{
client.EXPECT().DoCache(ctx, Match("GET", "b"), time.Second).Return(Result(RedisNil()))
if err := client.DoCache(ctx, client.B().Get().Key("b").Cache(), time.Second).Error(); !rueidis.IsRedisNil(err) {
t.Fatalf("unexpected err %v", err)
}
}
{
client.EXPECT().DoMulti(ctx,
Match("GET", "c"),
Match("GET", "d")).Return([]rueidis.RedisResult{
Result(RedisNil()),
Result(RedisNil())})
for _, resp := range client.DoMulti(ctx,
client.B().Get().Key("c").Build(),
client.B().Get().Key("d").Build()) {
if err := resp.Error(); !rueidis.IsRedisNil(err) {
t.Fatalf("unexpected err %v", err)
}
}
}
{
client.EXPECT().DoMultiCache(ctx,
Match("GET", "e"),
Match("GET", "f")).Return([]rueidis.RedisResult{
Result(RedisNil()),
Result(RedisNil())})
for _, resp := range client.DoMultiCache(ctx,
rueidis.CT(client.B().Get().Key("e").Cache(), time.Second),
rueidis.CT(client.B().Get().Key("f").Cache(), time.Second)) {
if err := resp.Error(); !rueidis.IsRedisNil(err) {
t.Fatalf("unexpected err %v", err)
}
}
}
{
client.EXPECT().Nodes().Return(map[string]rueidis.Client{"addr": client})
if nodes := client.Nodes(); nodes["addr"] != client {
t.Fatalf("unexpected val %v", nodes)
}
}
{
ch := make(chan struct{})
client.EXPECT().Close().Do(func() { close(ch) })
client.Close()
<-ch
}
{
client.EXPECT().Receive(ctx, Match("SUBSCRIBE", "a"), gomock.Any()).DoAndReturn(func(ctx context.Context, cmd any, fn func(msg rueidis.PubSubMessage)) error {
fn(rueidis.PubSubMessage{
Channel: "s",
Message: "s",
})
return errors.New("any")
})
if err := client.Receive(ctx, client.B().Subscribe().Channel("a").Build(), func(msg rueidis.PubSubMessage) {
if msg.Message != "s" && msg.Channel != "s" {
t.Fatalf("unexpected val %v", msg)
}
}); err.Error() != "any" {
t.Fatalf("unexpected err %v", err)
}
}
{
dc := NewDedicatedClient(ctrl)
client.EXPECT().Dedicate().Return(dc, func() {})
if c, _ := client.Dedicate(); c != dc {
t.Fatalf("unexpected val %v", c)
}
}
{
dc := NewDedicatedClient(ctrl)
cb := func(c rueidis.DedicatedClient) error {
if c != dc {
t.Fatalf("unexpected val %v", c)
}
return errors.New("any")
}
client.EXPECT().Dedicated(gomock.Any()).DoAndReturn(func(fn func(c rueidis.DedicatedClient) error) error {
return fn(dc)
})
if err := client.Dedicated(cb); err.Error() != "any" {
t.Fatalf("unexpected err %v", err)
}
}
}
func TestNewDedicatedClient(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()
client := NewDedicatedClient(ctrl)
{
client.EXPECT().Do(ctx, Match("GET", "a")).Return(Result(RedisNil()))
if err := client.Do(ctx, client.B().Get().Key("a").Build()).Error(); !rueidis.IsRedisNil(err) {
t.Fatalf("unexpected err %v", err)
}
}
{
client.EXPECT().DoMulti(ctx,
Match("GET", "c"),
Match("GET", "d")).Return([]rueidis.RedisResult{
Result(RedisNil()),
Result(RedisNil())})
for _, resp := range client.DoMulti(ctx,
client.B().Get().Key("c").Build(),
client.B().Get().Key("d").Build()) {
if err := resp.Error(); !rueidis.IsRedisNil(err) {
t.Fatalf("unexpected err %v", err)
}
}
}
{
ch := make(chan struct{})
client.EXPECT().Close().Do(func() { close(ch) })
client.Close()
<-ch
}
{
client.EXPECT().Receive(ctx, Match("SUBSCRIBE", "a"), gomock.Any()).DoAndReturn(func(ctx context.Context, cmd any, fn func(msg rueidis.PubSubMessage)) error {
fn(rueidis.PubSubMessage{
Channel: "s",
Message: "s",
})
return errors.New("any")
})
if err := client.Receive(ctx, client.B().Subscribe().Channel("a").Build(), func(msg rueidis.PubSubMessage) {
if msg.Message != "s" && msg.Channel != "s" {
t.Fatalf("unexpected val %v", msg)
}
}); err.Error() != "any" {
t.Fatalf("unexpected err %v", err)
}
}
{
client.EXPECT().SetPubSubHooks(gomock.Any()).Do(func(hooks rueidis.PubSubHooks) {
if hooks.OnMessage == nil || hooks.OnSubscription == nil {
t.Fatalf("unexpected val %v", hooks)
}
})
client.SetPubSubHooks(rueidis.PubSubHooks{
OnMessage: func(m rueidis.PubSubMessage) {},
OnSubscription: func(s rueidis.PubSubSubscription) {},
})
}
}
func TestNewClientAcrossSlot(t *testing.T) {
t.Run("Default", func(t *testing.T) {
cc := NewClient(nil)
cc.B().Del().Key("{1}", "{2}").Build() // should not panic
dc := NewDedicatedClient(nil)
dc.B().Del().Key("{1}", "{2}").Build() // should not panic
})
t.Run("Enable Slot Check", func(t *testing.T) {
defer func() {
if msg := recover().(string); msg != "multi key command with different key slots are not allowed" {
t.Fail()
}
}()
cc := NewClient(nil, WithSlotCheck())
cc.B().Del().Key("{1}", "{2}").Build() // should panic
})
t.Run("Enable Slot Check", func(t *testing.T) {
defer func() {
if msg := recover().(string); msg != "multi key command with different key slots are not allowed" {
t.Fail()
}
}()
cc := NewDedicatedClient(nil, WithSlotCheck())
cc.B().Del().Key("{1}", "{2}").Build() // should panic
})
}