-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
150 lines (134 loc) · 3.34 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
package redists
import (
"context"
"fmt"
goredis "github.com/go-redis/redis/v8"
redigo "github.com/gomodule/redigo/redis"
redispipe "github.com/joomcode/redispipe/redis"
"github.com/joomcode/redispipe/redisconn"
"github.com/mediocregopher/radix/v4"
"github.com/mediocregopher/radix/v4/resp"
"io"
"testing"
"time"
)
type goredisDoer struct {
goredis.UniversalClient
}
func (f goredisDoer) Do(ctx context.Context, cmd string, args ...interface{}) (interface{}, error) {
args = append([]interface{}{cmd}, args...)
return f.UniversalClient.Do(ctx, args...).Result()
}
type redigoDoer struct {
redigo.Conn
}
func (f redigoDoer) Do(ctx context.Context, cmd string, args ...interface{}) (interface{}, error) {
deadline, ok := ctx.Deadline()
if ok {
return redigo.DoWithTimeout(f.Conn, deadline.Sub(time.Now()), cmd, args...)
}
return f.Conn.Do(cmd, args...)
}
type radixDoer struct {
radix.Client
}
func (f radixDoer) Do(ctx context.Context, cmd string, args ...interface{}) (interface{}, error) {
var val interface{}
err := f.Client.Do(ctx, radix.FlatCmd(&val, cmd, args...))
if e, ok := val.(error); err == nil && ok {
return nil, e
}
return val, err
}
type redispipeDoer struct {
redispipe.Sender
}
func (f redispipeDoer) Do(ctx context.Context, cmd string, args ...interface{}) (interface{}, error) {
res := redispipe.SyncCtx{f.Sender}.Do(ctx, cmd, args...)
if err := redispipe.AsError(res); err != nil {
return nil, err
}
return res, nil
}
func (f redispipeDoer) Close() error {
f.Sender.Close()
return nil
}
type doCloser interface {
Doer
io.Closer
}
var doerTests = []struct {
name string
doer func(ctx context.Context) (doCloser, error)
}{
{
name: "goredis",
doer: func(ctx context.Context) (doCloser, error) {
client := goredis.NewClient(&goredis.Options{Addr: "localhost:6379"})
if err := client.Ping(ctx).Err(); err != nil {
return nil, err
}
return goredisDoer{client}, nil
},
},
{
name: "redigo",
doer: func(ctx context.Context) (doCloser, error) {
conn, err := redigo.DialContext(ctx, "tcp", "localhost:6379")
if err != nil {
return nil, err
}
return redigoDoer{conn}, nil
},
},
{
name: "radix",
doer: func(ctx context.Context) (doCloser, error) {
d := radix.Dialer{
NewRespOpts: func() *resp.Opts {
opts := resp.NewOpts()
opts.DisableErrorBubbling = true
return opts
},
}
client, err := (radix.PoolConfig{Dialer: d}).New(ctx, "tcp", "localhost:6379")
if err != nil {
return nil, err
}
return radixDoer{client}, err
},
},
{
name: "redispipe",
doer: func(ctx context.Context) (doCloser, error) {
sender, err := redisconn.Connect(ctx, "localhost:6379", redisconn.Opts{})
if err != nil {
return nil, err
}
return redispipeDoer{sender}, nil
},
},
}
func TestNewClient(t *testing.T) {
if testing.Short() {
t.Skip("skip client test")
}
for _, tt := range doerTests {
t.Run(tt.name, func(t *testing.T) {
key := fmt.Sprintf("example:%s", t.Name())
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
doer, err := tt.doer(ctx)
if err != nil {
t.Fatal(err)
}
defer doer.Close()
defer doer.Do(context.Background(), "DEL", key)
tsclient := NewClient(doer)
if err := tsclient.Create(ctx, key); err != nil {
t.Errorf("Create() error = %v", err)
}
})
}
}