-
Notifications
You must be signed in to change notification settings - Fork 4
/
memcache_req_test.go
85 lines (76 loc) · 1.68 KB
/
memcache_req_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
package mc
import (
"bufio"
"reflect"
"strings"
"testing"
)
func testReq(in string, t *testing.T) (ret *Request, err error) {
r := strings.NewReader(in)
return ReadRequest(bufio.NewReader(r))
}
func TestSet(t *testing.T) {
ret, err := testReq("set KEY 0 0 10\r\n1234567890\r\n", t)
if err != nil {
t.Fatalf("ReadRequest %+v", err)
}
if ret.Command != "set" {
t.Errorf("Command %s", ret.Command)
}
if ret.Key != "KEY" {
t.Errorf("Key %s", ret.Key)
}
if ret.Flags != "0" {
t.Errorf("Flags %s", ret.Flags)
}
if ret.Exptime != 0 {
t.Errorf("Exptime %d", ret.Exptime)
}
if string(ret.Data) != "1234567890" {
t.Errorf("Data %s", ret.Data)
}
}
func TestGet(t *testing.T) {
ret, err := testReq("get a bb c\r\n", t)
if err != nil {
t.Fatalf("ReadRequest %+v", err)
}
if ret.Command != "get" {
t.Errorf("Command %s", ret.Command)
}
if !reflect.DeepEqual(ret.Keys, []string{"a", "bb", "c"}) {
t.Errorf("Keys %v", ret.Keys)
}
}
func TestCas(t *testing.T) {
ret, err := testReq("cas KEY 0 0 10 UNIQ\r\n1234567890\r\n", t)
if err != nil {
t.Fatalf("ReadRequest %+v", err)
}
if ret.Command != "cas" {
t.Errorf("Command %s", ret.Command)
}
if ret.Key != "KEY" {
t.Errorf("Key %s", ret.Key)
}
if ret.Flags != "0" {
t.Errorf("Flags %s", ret.Flags)
}
if ret.Exptime != 0 {
t.Errorf("Exptime %d", ret.Exptime)
}
if ret.Cas != "UNIQ" {
t.Errorf("Cas %d", ret.Exptime)
}
if string(ret.Data) != "1234567890" {
t.Errorf("Data %s", ret.Data)
}
}
func TestError(t *testing.T) {
_, err := testReq("xxx KEY 0 0 10\r\n1234567890\r\n", t)
if perr, ok := err.(Error); ok {
t.Logf("Good error: %v", perr)
return
}
t.Fatalf("ReadRequest did not return error")
}