-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskytable_test.go
308 lines (248 loc) · 7.61 KB
/
skytable_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package skytable_test
import (
"bytes"
"context"
"errors"
"net"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/satvik007/skytable-go"
)
type skytableHookError struct {
skytable.Hook
}
var _ skytable.Hook = skytableHookError{}
func (skytableHookError) BeforeProcess(ctx context.Context, cmd skytable.Cmder) (context.Context, error) {
return ctx, nil
}
func (skytableHookError) AfterProcess(ctx context.Context, cmd skytable.Cmder) error {
return errors.New("hook error")
}
func TestHookError(t *testing.T) {
rdb := skytable.NewClient(&skytable.Options{
Addr: "localhost:2003",
})
rdb.AddHook(skytableHookError{})
err := rdb.Heya(ctx, "").Err()
if err == nil {
t.Fatalf("got nil, expected an error")
}
wanted := "hook error"
if err.Error() != wanted {
t.Fatalf(`got %q, wanted %q`, err, wanted)
}
}
// ------------------------------------------------------------------------------
var _ = Describe("Client", func() {
var client *skytable.Client
BeforeEach(func() {
client = skytable.NewClient(skytableOptions())
Expect(client.FlushDB(ctx, "").Err()).NotTo(HaveOccurred())
})
AfterEach(func() {
client.Close()
})
It("should Stringer", func() {
Expect(client.String()).To(Equal("Skytable<localhost:2003 table:default:test15>"))
})
It("supports context", func() {
ctx, cancel := context.WithCancel(ctx)
cancel()
err := client.Heya(ctx, "").Err()
Expect(err).To(MatchError("context canceled"))
})
It("should ping", func() {
val, err := client.Heya(ctx, "").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("HEY!"))
})
It("should return pool stats", func() {
Expect(client.PoolStats()).To(BeAssignableToTypeOf(&skytable.PoolStats{}))
})
It("should support custom dialers", func() {
custom := skytable.NewClient(&skytable.Options{
Network: "tcp",
Addr: skytableAddr,
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, network, addr)
},
})
val, err := custom.Heya(ctx, "").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("HEY!"))
Expect(custom.Close()).NotTo(HaveOccurred())
})
It("should close", func() {
Expect(client.Close()).NotTo(HaveOccurred())
err := client.Heya(ctx, "").Err()
Expect(err).To(MatchError("skytable: client is closed"))
})
It("should select DB", func() {
db2 := skytable.NewClient(&skytable.Options{
Addr: skytableAddr,
Table: "test2",
})
Expect(db2.FlushDB(ctx, "").Err()).NotTo(HaveOccurred())
Expect(db2.Get(ctx, "db").Err()).To(Equal(skytable.Nil))
Expect(db2.Set(ctx, "db", 2).Err()).NotTo(HaveOccurred())
n, err := db2.Get(ctx, "db").Int64()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
Expect(client.Get(ctx, "db").Err()).To(Equal(skytable.Nil))
Expect(db2.FlushDB(ctx, "").Err()).NotTo(HaveOccurred())
Expect(db2.Close()).NotTo(HaveOccurred())
})
It("processes custom commands", func() {
cmd := skytable.NewCmd(ctx, "HEYA")
_ = client.Process(ctx, cmd)
// Flush buffers.
Expect(client.Heya(ctx, "hello").Err()).NotTo(HaveOccurred())
Expect(cmd.Err()).NotTo(HaveOccurred())
Expect(cmd.Val()).To(Equal("HEY!"))
})
It("should retry command on network error", func() {
Expect(client.Close()).NotTo(HaveOccurred())
client = skytable.NewClient(&skytable.Options{
Addr: skytableAddr,
MaxRetries: 1,
})
// Put bad connection in the pool.
cn, err := client.Pool().Get(ctx)
Expect(err).NotTo(HaveOccurred())
cn.SetNetConn(&badConn{})
client.Pool().Put(ctx, cn)
err = client.Heya(ctx, "").Err()
Expect(err).NotTo(HaveOccurred())
})
It("should retry with backoff", func() {
clientNoRetry := skytable.NewClient(&skytable.Options{
Addr: ":1234",
MaxRetries: -1,
})
defer clientNoRetry.Close()
clientRetry := skytable.NewClient(&skytable.Options{
Addr: ":1234",
MaxRetries: 5,
MaxRetryBackoff: 128 * time.Millisecond,
})
defer clientRetry.Close()
startNoRetry := time.Now()
err := clientNoRetry.Heya(ctx, "").Err()
Expect(err).To(HaveOccurred())
elapseNoRetry := time.Since(startNoRetry)
startRetry := time.Now()
err = clientRetry.Heya(ctx, "").Err()
Expect(err).To(HaveOccurred())
elapseRetry := time.Since(startRetry)
Expect(elapseRetry).To(BeNumerically(">", elapseNoRetry, 10*time.Millisecond))
})
It("should update conn.UsedAt on read/write", func() {
cn, err := client.Pool().Get(context.Background())
Expect(err).NotTo(HaveOccurred())
Expect(cn.UsedAt).NotTo(BeZero())
// set cn.SetUsedAt(time) or time.Sleep(>1*time.Second)
// simulate the last time Conn was used
// time.Sleep() is not the standard sleep time
// link: https://go-review.googlesource.com/c/go/+/232298
cn.SetUsedAt(time.Now().Add(-1 * time.Second))
createdAt := cn.UsedAt()
client.Pool().Put(ctx, cn)
Expect(cn.UsedAt().Equal(createdAt)).To(BeTrue())
err = client.Heya(ctx, "").Err()
Expect(err).NotTo(HaveOccurred())
cn, err = client.Pool().Get(context.Background())
Expect(err).NotTo(HaveOccurred())
Expect(cn).NotTo(BeNil())
Expect(cn.UsedAt().After(createdAt)).To(BeTrue())
})
It("should process command with special chars", func() {
set := client.Set(ctx, "key", "hello1\r\nhello2\r\n")
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal(int64(0)))
get := client.Get(ctx, "key")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello1\r\nhello2\r\n"))
})
It("should handle big vals", func() {
bigVal := bytes.Repeat([]byte{'*'}, 2e6)
err := client.Set(ctx, "key", bigVal).Err()
Expect(err).NotTo(HaveOccurred())
// Reconnect to get new connection.
Expect(client.Close()).NotTo(HaveOccurred())
client = skytable.NewClient(skytableOptions())
got, err := client.Get(ctx, "key").Bytes()
Expect(err).NotTo(HaveOccurred())
Expect(got).To(Equal(bigVal))
})
It("should Conn", func() {
err := client.Conn().Get(ctx, "this-key-does-not-exist").Err()
Expect(err).To(Equal(skytable.Nil))
})
})
var _ = Describe("Client timeout", func() {
var opt *skytable.Options
var client *skytable.Client
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
testTimeout := func() {
It("Ping timeouts", func() {
err := client.Heya(ctx, "").Err()
Expect(err).To(HaveOccurred())
Expect(err.(net.Error).Timeout()).To(BeTrue())
})
It("Pipeline timeouts", func() {
_, err := client.Pipelined(ctx, func(pipe skytable.Pipeliner) error {
pipe.Heya(ctx, "")
return nil
})
Expect(err).To(HaveOccurred())
Expect(err.(net.Error).Timeout()).To(BeTrue())
})
}
Context("read timeout", func() {
BeforeEach(func() {
opt = skytableOptions()
opt.ReadTimeout = time.Nanosecond
opt.WriteTimeout = -1
client = skytable.NewClient(opt)
})
testTimeout()
})
Context("write timeout", func() {
BeforeEach(func() {
opt = skytableOptions()
opt.ReadTimeout = -1
opt.WriteTimeout = time.Nanosecond
client = skytable.NewClient(opt)
})
testTimeout()
})
})
// var _ = Describe("Client context cancellation", func() {
// var opt *skytable.Options
// var client *skytable.Client
//
// BeforeEach(func() {
// opt = skytableOptions()
// opt.ReadTimeout = -1
// opt.WriteTimeout = -1
// client = skytable.NewClient(opt)
// })
//
// AfterEach(func() {
// Expect(client.Close()).NotTo(HaveOccurred())
// })
//
// It("Blocking operation cancelation", func() {
// ctx, cancel := context.WithCancel(ctx)
// cancel()
//
// err := client.BLPop(ctx, 1*time.Second, "test").Err()
// Expect(err).To(HaveOccurred())
// Expect(err).To(BeIdenticalTo(context.Canceled))
// })
// })