-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_test.go
55 lines (48 loc) · 1.2 KB
/
command_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
package redisgo
import (
"bytes"
"strconv"
"strings"
"testing"
)
func tstr(s string) string {
b := make([]byte, 0, 30+len(s))
b = append(b, '$')
b = strconv.AppendInt(b, int64(len(s)), 10)
b = append(b, CRLF...)
b = append(b, s...)
b = append(b, CRLF...)
return string(b)
}
func tcmd(aa ...string) string {
s := strings.Join(aa, "")
b := make([]byte, 0, 30+len(s))
b = append(b, '*')
b = strconv.AppendInt(b, int64(len(aa)), 10)
b = append(b, CRLF...)
b = append(b, s...)
return string(b)
}
func TestCommand(t *testing.T) {
buf := new(bytes.Buffer)
c := commandPool.Get().(*command)
c.Reset("CMD").Args("a0", []byte("a1")).Args(uint(1)).Dump(buf)
if s := tcmd(tstr("CMD"), tstr("a0"), tstr("a1"), tstr("1")); s != buf.String() {
t.Fatal("expect:\n", s, "get:\n", buf.String())
}
buf.Reset()
c.Reset("CMD").Args([]string{"a0", "a1"}).Args(uint(1)).Dump(buf)
if s := tcmd(tstr("CMD"), tstr("a0"), tstr("a1"), tstr("1")); s != buf.String() {
t.Fatal("expect:\n", s, "get:\n", buf.String())
}
}
func BenchmarkCommand(b *testing.B) {
b.ReportAllocs()
c := commandPool.Get().(*command)
for i := 0; i < b.N; i++ {
cmd := "GET"
a1 := "key"
a2 := 1
c.Reset(cmd).Args(a1).Args(a2)
}
}