-
Notifications
You must be signed in to change notification settings - Fork 18
/
gomemcache_test.go
292 lines (263 loc) · 7.01 KB
/
gomemcache_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
/**
* Run two following commands on the background before this test.
* $ memcached
* $ memcached -s /tmp/memcached.sock -a 0755
*/
package gomemcache
import (
"bufio"
"strconv"
"strings"
"testing"
)
const (
KEY_1 string = "test-key-1"
KEY_2 string = "test-key-2"
VALUE_1 string = "test-value-1"
VALUE_2 string = "test-value-2"
FLAGS int = 1
)
var memc *Memcache
func TestDial_TCP(t *testing.T) {
c, err := Dial("tcp", "127.0.0.1:11211")
assertNoError(t, err)
err = c.Close()
assertNoError(t, err)
}
func TestDial_UNIX(t *testing.T) {
c, err := Dial("unix", "/tmp/memcached.sock")
assertNoError(t, err)
err = c.Close()
assertNoError(t, err)
}
func testConnect_TCP(t *testing.T) {
c, err := Connect("127.0.0.1", 11211)
assertNoError(t, err)
err = c.Close()
assertNoError(t, err)
}
func testConnect_UNIX(t *testing.T) {
c, err := Connect("/tmp/memcached.sock", 0)
assertNoError(t, err)
err = c.Close()
assertNoError(t, err)
}
func TestSet(t *testing.T) {
connect(t)
err := memc.Set(KEY_1, []uint8(VALUE_1), FLAGS, 0)
assertNoError(t, err)
assertGet(t, KEY_1, VALUE_1)
cleanUp()
}
func TestAdd(t *testing.T) {
connect(t)
err := memc.Add(KEY_1, []uint8(VALUE_1), FLAGS, 0)
assertNoError(t, err)
assertGet(t, KEY_1, VALUE_1)
cleanUp()
}
func TestGetMulti(t *testing.T) {
// given
connect(t)
memc.Add(KEY_1, []uint8(VALUE_1), FLAGS, 0)
memc.Add(KEY_2, []uint8(VALUE_2), FLAGS, 0)
// when
receivedValues, err := memc.GetMulti(KEY_1, KEY_2)
// then
assertNoError(t, err)
assertEqual(t, VALUE_1, string(receivedValues[KEY_1].Value))
assertEqual(t, VALUE_2, string(receivedValues[KEY_2].Value))
assertEqualInt(t, FLAGS, receivedValues[KEY_1].Flags)
assertEqualInt(t, FLAGS, receivedValues[KEY_2].Flags)
cleanUp()
}
func TestGetMultiForMissingKeys(t *testing.T) {
// given
connect(t)
// when
receivedValues, err := memc.GetMulti(KEY_1, KEY_2)
// then
assertNoError(t, err)
if len(receivedValues) != 0 {
t.Error("Expected emtpy result")
}
cleanUp()
}
func TestGetMultiForMissingOneKey(t *testing.T) {
// given
connect(t)
memc.Add(KEY_2, []uint8(VALUE_2), FLAGS, 0)
// when
receivedValues, err := memc.GetMulti(KEY_1, KEY_2)
// then
assertNoError(t, err)
assertEqual(t, "", string(receivedValues[KEY_1].Value))
assertEqual(t, VALUE_2, string(receivedValues[KEY_2].Value))
assertEqualInt(t, 0, receivedValues[KEY_1].Flags)
assertEqualInt(t, FLAGS, receivedValues[KEY_2].Flags)
cleanUp()
}
func TestGetForMissingKey(t *testing.T) {
// given
connect(t)
// when
receivedValue, _, err := memc.Get(KEY_1)
// then
if err != NotFoundError {
t.Error("Expected NotFoundError. receivedValue:", string(receivedValue))
}
cleanUp()
}
func TestAddingPresentKEY_1(t *testing.T) {
connect(t)
err := memc.Set(KEY_1, []uint8(VALUE_1), FLAGS, 0)
assertNoError(t, err)
assertGet(t, KEY_1, VALUE_1)
err = memc.Add(KEY_1, []uint8(VALUE_1), FLAGS, 0)
if err == nil {
t.Error("Adding should fail because KEY_1 is already present")
}
cleanUp()
}
func TestReplace(t *testing.T) {
connect(t)
err := memc.Add(KEY_1, []uint8(VALUE_1), FLAGS, 0)
assertNoError(t, err)
newValue := "new value"
err = memc.Replace(KEY_1, []uint8(newValue), FLAGS, 0)
assertNoError(t, err)
assertGet(t, KEY_1, newValue)
cleanUp()
}
func TestPrepend(t *testing.T) {
connect(t)
err := memc.Add(KEY_1, []uint8(VALUE_1), FLAGS, 0)
assertNoError(t, err)
prefix := "prefix"
err = memc.Prepend(KEY_1, []uint8(prefix), FLAGS, 0)
assertNoError(t, err)
assertGet(t, KEY_1, prefix+VALUE_1)
cleanUp()
}
func TestAppend(t *testing.T) {
connect(t)
err := memc.Add(KEY_1, []uint8(VALUE_1), FLAGS, 0)
assertNoError(t, err)
suffix := "suffix"
err = memc.Append(KEY_1, []uint8(suffix), FLAGS, 0)
assertNoError(t, err)
assertGet(t, KEY_1, VALUE_1+suffix)
cleanUp()
}
func TestDelete(t *testing.T) {
connect(t)
err := memc.Add(KEY_1, []uint8(VALUE_1), FLAGS, 0)
assertNoError(t, err)
err = memc.Delete(KEY_1)
assertNoError(t, err)
_, _, err = memc.Get(KEY_1)
if err == nil {
t.Error("Data not removed from memcache")
}
cleanUp()
}
func TestFlushAll(t *testing.T) {
connect(t)
err := memc.Add(KEY_1, []uint8(VALUE_1), FLAGS, 0)
assertNoError(t, err)
err = memc.FlushAll()
assertNoError(t, err)
_, _, err = memc.Get(KEY_1)
if err == nil {
t.Error("Data not removed from memcache")
}
cleanUp()
}
func TestIncr(t *testing.T) {
connect(t)
err := memc.Add(KEY_1, []uint8("1234"), FLAGS, 0)
assertNoError(t, err)
i, err := memc.Incr(KEY_1, 9)
assertNoError(t, err)
if i != 1243 {
t.Error("Value expected: 1243\nValue received: " + strconv.FormatUint(i, 10))
}
cleanUp()
}
func TestDecr(t *testing.T) {
connect(t)
err := memc.Add(KEY_1, []uint8("1243"), FLAGS, 0)
assertNoError(t, err)
i, err := memc.Decr(KEY_1, 9)
assertNoError(t, err)
if i != 1234 {
t.Error("Value expected: 1234\nValue received: " + strconv.FormatUint(i, 10))
}
cleanUp()
}
func TestParseResponseWithOneValue(t *testing.T) {
// given
responseString := "VALUE " + KEY_1 + " " + strconv.Itoa(FLAGS) + " " + strconv.Itoa(len(VALUE_1)) + "\r\n" +
VALUE_1 + "\r\n" +
"END\r\n"
stringReader := strings.NewReader(responseString)
reader := bufio.NewReader(stringReader)
// when
value, receivedFlags, err := memc.readValue(reader, KEY_1)
// then
assertNoError(t, err)
assertEqual(t, VALUE_1, string(value))
assertEqualInt(t, FLAGS, receivedFlags)
}
func TestParseResponseWithTwoValues(t *testing.T) {
// given
responseString := "VALUE " + KEY_1 + " " + strconv.Itoa(FLAGS) + " " + strconv.Itoa(len(VALUE_1)) + "\r\n" +
VALUE_1 + "\r\n" +
"VALUE " + KEY_2 + " " + strconv.Itoa(FLAGS) + " " + strconv.Itoa(len(VALUE_2)) + "\r\n" +
VALUE_2 + "\r\n" +
"END\r\n"
stringReader := strings.NewReader(responseString)
reader := bufio.NewReader(stringReader)
// when called first time
value, receivedFlags, err := memc.readValue(reader, KEY_1)
// then
assertNoError(t, err)
assertEqual(t, VALUE_1, string(value))
assertEqualInt(t, FLAGS, receivedFlags)
// when called second time
value, receivedFlags, err = memc.readValue(reader, KEY_2)
// then
assertNoError(t, err)
assertEqual(t, VALUE_2, string(value))
assertEqualInt(t, FLAGS, receivedFlags)
}
func assertGet(t *testing.T, key string, expectedValue string) {
receivedValue, receivedFlags, err := memc.Get(key)
assertNoError(t, err)
assertEqual(t, expectedValue, string(receivedValue))
assertEqualInt(t, FLAGS, receivedFlags)
}
func connect(t *testing.T) {
connection, err := Connect("127.0.0.1", 11211)
assertNoError(t, err)
memc = connection
}
func cleanUp() {
memc.Delete(KEY_1)
memc.Delete(KEY_2)
}
func assertNoError(t *testing.T, err error) {
if err != nil {
t.Error(err)
}
}
func assertEqualInt(t *testing.T, expectedValue int, receivedValue int) {
if receivedValue != expectedValue {
t.Error("Value expected: " + strconv.Itoa(expectedValue) + "\nValue received: " + strconv.Itoa(receivedValue))
}
}
func assertEqual(t *testing.T, expectedValue string, receivedValue string) {
if receivedValue != expectedValue {
t.Error("Value expected: " + expectedValue + "\nValue received: " + receivedValue)
}
}