-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconn_test.go
219 lines (193 loc) · 5.5 KB
/
conn_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
package memcache
import (
"net"
"os"
"strings"
"testing"
"time"
)
func setup(t *testing.T) *Conn {
addr := os.Getenv("MC_ADDRESS")
conn, err := net.Dial("tcp", addr)
if err != nil {
t.Error(err)
}
return NewConn(conn)
}
func TestConn(t *testing.T) {
c := setup(t)
testWithClient(t, c)
}
func TestPing(t *testing.T) {
c := setup(t)
if err := c.ping(); err != nil {
t.Error(err)
}
}
func mustSetF(t *testing.T, c *Conn) func(*Item) {
return func(it *Item) {
if err := c.Set(it); err != nil {
t.Fatalf("failed to Set %#v: %v", *it, err)
}
}
}
func testWithClient(t *testing.T, c *Conn) {
if err := c.FlushAll(); err != nil {
t.Error(err)
}
checkErr := func(err error, format string, args ...interface{}) {
if err != nil {
t.Fatalf(format, args...)
}
}
mustSet := mustSetF(t, c)
// Set
foo := &Item{Key: "foo", Value: []byte("fooval"), Flags: 123}
err := c.Set(foo)
checkErr(err, "first set(foo): %v", err)
err = c.Set(foo)
checkErr(err, "second set(foo): %v", err)
// Get
it, err := c.Get("foo")
checkErr(err, "get(foo): %v", err)
if it.Key != "foo" {
t.Errorf("get(foo) Key = %q, want foo", it.Key)
}
if string(it.Value) != "fooval" {
t.Errorf("get(foo) Value = %q, want fooval", string(it.Value))
}
if it.Flags != 123 {
t.Errorf("get(foo) Flags = %v, want 123", it.Flags)
}
// Get and set a unicode key
quxKey := "Hello_世界"
qux := &Item{Key: quxKey, Value: []byte("hello world")}
err = c.Set(qux)
checkErr(err, "first set(Hello_世界): %v", err)
it, err = c.Get(quxKey)
checkErr(err, "get(Hello_世界): %v", err)
if it.Key != quxKey {
t.Errorf("get(Hello_世界) Key = %q, want Hello_世界", it.Key)
}
if string(it.Value) != "hello world" {
t.Errorf("get(Hello_世界) Value = %q, want hello world", string(it.Value))
}
// Set malformed keys
malFormed := &Item{Key: "foo bar", Value: []byte("foobarval")}
err = c.Set(malFormed)
if err != ErrMalformedKey {
t.Errorf("set(foo bar) should return ErrMalformedKey instead of %v", err)
}
malFormed = &Item{Key: "foo" + string(rune(0x7f)), Value: []byte("foobarval")}
err = c.Set(malFormed)
if err != ErrMalformedKey {
t.Errorf("set(foo<0x7f>) should return ErrMalformedKey instead of %v", err)
}
// Add
bar := &Item{Key: "bar", Value: []byte("barval")}
err = c.Add(bar)
checkErr(err, "first add(foo): %v", err)
if err := c.Add(bar); err != ErrNotStored {
t.Fatalf("second add(foo) want ErrNotStored, got %v", err)
}
// Replace
baz := &Item{Key: "baz", Value: []byte("bazvalue")}
if err := c.Replace(baz); err != ErrNotStored {
t.Fatalf("expected replace(baz) to return ErrNotStored, got %v", err)
}
err = c.Replace(bar)
checkErr(err, "replaced(foo): %v", err)
// GetMulti
m, err := c.GetMulti([]string{"foo", "bar"})
checkErr(err, "GetMulti: %v", err)
if g, e := len(m), 2; g != e {
t.Errorf("GetMulti: got len(map) = %d, want = %d", g, e)
}
if _, ok := m["foo"]; !ok {
t.Fatalf("GetMulti: didn't get key 'foo'")
}
if _, ok := m["bar"]; !ok {
t.Fatalf("GetMulti: didn't get key 'bar'")
}
if g, e := string(m["foo"].Value), "fooval"; g != e {
t.Errorf("GetMulti: foo: got %q, want %q", g, e)
}
if g, e := string(m["bar"].Value), "barval"; g != e {
t.Errorf("GetMulti: bar: got %q, want %q", g, e)
}
// Delete
err = c.Delete("foo")
checkErr(err, "Delete: %v", err)
it, err = c.Get("foo")
if err != ErrCacheMiss {
t.Errorf("post-Delete want ErrCacheMiss, got %v", err)
}
// Incr/Decr
mustSet(&Item{Key: "num", Value: []byte("42")})
n, err := c.Increment("num", 8)
checkErr(err, "Increment num + 8: %v", err)
if n != 50 {
t.Fatalf("Increment num + 8: want=50, got=%d", n)
}
n, err = c.Decrement("num", 49)
checkErr(err, "Decrement: %v", err)
if n != 1 {
t.Fatalf("Decrement 49: want=1, got=%d", n)
}
err = c.Delete("num")
checkErr(err, "delete num: %v", err)
n, err = c.Increment("num", 1)
if err != ErrCacheMiss {
t.Fatalf("increment post-delete: want ErrCacheMiss, got %v", err)
}
mustSet(&Item{Key: "num", Value: []byte("not-numeric")})
n, err = c.Increment("num", 1)
if err == nil || !strings.Contains(err.Error(), "client error") {
t.Fatalf("increment non-number: want client error, got %v", err)
}
if os.Getenv("TEST_MC_TOUCH") != "" {
testTouch(t, c)
}
// Test Delete All
err = c.FlushAll()
checkErr(err, "DeleteAll: %v", err)
it, err = c.Get("bar")
if err != ErrCacheMiss {
t.Errorf("post-DeleteAll want ErrCacheMiss, got %v", err)
}
}
func testTouch(t *testing.T, c *Conn) {
const secondsToExpiry = int32(2)
// We will set foo and bar to expire in 2 seconds, then we'll keep touching
// foo every second
// After 3 seconds, we expect foo to be available, and bar to be expired
foo := &Item{Key: "foo", Value: []byte("fooval"), Expiration: secondsToExpiry}
bar := &Item{Key: "bar", Value: []byte("barval"), Expiration: secondsToExpiry}
setTime := time.Now()
mustSet := mustSetF(t, c)
mustSet(foo)
mustSet(bar)
for s := 0; s < 3; s++ {
time.Sleep(time.Duration(1 * time.Second))
err := c.Touch(foo.Key, secondsToExpiry)
if nil != err {
t.Errorf("error touching foo: %v", err.Error())
}
}
_, err := c.Get("foo")
if err != nil {
if err == ErrCacheMiss {
t.Fatalf("touching failed to keep item foo alive")
} else {
t.Fatalf("unexpected error retrieving foo after touching: %v", err.Error())
}
}
_, err = c.Get("bar")
if nil == err {
t.Fatalf("item bar did not expire within %v seconds", time.Now().Sub(setTime).Seconds())
} else {
if err != ErrCacheMiss {
t.Fatalf("unexpected error retrieving bar: %v", err.Error())
}
}
}