-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.lua
239 lines (177 loc) · 4.83 KB
/
test.lua
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
require "memcached"
require "socket"
require "lunatest"
pcall(require, "luacov")
local test_async = true
local def_ct = 0
if test_async then
dh = function()
def_ct = def_ct + 1
end
end
function setup()
m = assert(memcached.connect("localhost", 11211, dh),
"Is the server running?")
assert_true(m:flush_all(), "Flush failed")
end
function teardown()
m:quit()
if m:is_async() then
--print(string.format("-- Deferred %d times", def_ct))
end
def_ct = 0
end
function test_version()
assert_match("^VERSION", m:version())
end
function test_get_unavailable()
local v = m:get("foo")
assert_nil(v, "Shouldn't be defined yet")
end
function test_set()
local ok, res = m:set("foo", "bar")
assert_true(ok, res)
assert_true(res == "STORED")
end
function test_set_get()
local ok, res = m:set("foo", "bar")
assert_true(ok, res)
local val = m:get("foo")
assert_true(val == "bar")
end
function test_set_get_multi()
assert_true(m:set("foo", "bar"))
assert_true(m:set("bar", "baz"))
local val = m:get {"foo", "bar" }
assert_true(val.foo)
assert_true(val.bar)
assert_true(val.foo.data == "bar")
assert_true(val.bar.data == "baz")
end
function test_set_gets_multi_flags()
assert_true(m:set("foo", "bar", nil, 31))
assert_true(m:set("bar", "baz", nil, 87))
local val = m:gets {"foo", "bar" }
assert_true(val.foo)
assert_true(val.foo.data == "bar")
assert_true(val.foo.flags == 31)
assert_true(val.foo.cas)
assert_true(val.bar)
assert_true(val.bar.data == "baz")
assert_true(val.bar.flags == 87)
assert_true(val.bar.cas)
end
function test_set_get_large()
local big = ("a"):rep(500 * 1024) -- 500kb of 'a's.
local ok, err = m:set("foo", big)
local v, err = m:get("foo")
assert_true(big == m:get("foo"), err)
end
function test_set_gets()
local ok, res = m:set("foo", "bar")
assert_true(ok, res)
local v, flags, cas = m:gets("foo")
assert_true(v == "bar", v)
end
function test_set_get_flag()
local ok, res = m:set("foo", "bar", nil, 41)
assert_true(ok, res)
local v, flag = m:get("foo")
assert_true(v == "bar", v)
assert_true(flag == 41, flag)
end
local function sleep(secs)
if dh then
local now = socket.gettime
local t = now()
while now() - t < 2 do dh() end
else
socket.select(nil, nil, 2)
end
end
function test_set_get_expire()
local ok, res = m:set("foo", "bar", 1)
assert_true(ok, res)
local v, flag = m:get("foo")
assert_true(v == "bar")
print "\nSleeping for two seconds (key should expire)"
sleep(2)
local v, flag = m:get("foo")
assert_nil(v, "Should have expired")
end
function test_stats()
local stats = m:stats()
assert_true(stats, stats)
assert_true(stats.version, "No version field in stats table.")
--for k,v in pairs(stats) do print(k,v) end
end
function test_incr()
assert_true(m:set("foo", 10))
assert_equal("10", (m:get("foo")))
assert_equal(51, m:incr("foo", 41))
assert_equal("51", m:get("foo"))
end
function test_incr_decr()
assert_true(m:set("foo", 10))
assert_equal(110, m:incr("foo", 100))
assert_true(m:incr("foo", 15, true))
assert_equal(112, m:decr("foo", 13))
local res, err = m:get("foo")
assert_equal(112, tonumber(res))
end
function test_add()
local ok, res = m:add("foo", "bar")
assert_true(ok, res)
assert_equal("bar", m:get("foo"))
ok, res = m:add("foo", "blah")
assert_false(ok)
assert_equal("NOT_STORED", res)
end
function test_replace()
local ok, res = m:add("foo", "bar")
assert_true(ok, res)
assert_equal("bar", m:get("foo"))
ok, res = m:replace("bar", "blah")
assert_false(ok)
assert_equal("NOT_STORED", res)
ok, res = m:replace("foo", "blah")
assert_true(ok)
assert_equal("blah", m:get("foo"))
end
function test_append()
local ok, res = m:add("foo", "bar")
assert_true(ok, res)
assert_equal("bar", m:get("foo"))
ok, res = m:append("foo", "baz")
assert_equal("barbaz", m:get("foo"))
end
function test_prepend()
local ok, res = m:add("foo", "bar")
assert_true(ok, res)
assert_equal("bar", m:get("foo"))
ok, res = m:prepend("foo", "foo")
assert_equal("foobar", m:get("foo"))
end
function test_cas()
local ok, res = m:add("foo", "bar")
local val, flags, cas = m:gets("foo")
ok, res = m:cas("foo", "blaff", cas)
assert_true(ok, res)
assert_equal("STORED", res)
end
function test_cas_altered()
local ok, res = m:add("foo", "bar")
local val, flags, cas = m:gets("foo")
--change it since last CAS id
m:replace("foo", "mutation")
ok, res = m:cas("foo", "blaff", cas)
assert_false(ok)
assert_match("EXISTS", res)
end
function test_delete()
local ok, res = m:add("foo", "bar")
assert_equal("bar", m:gets("foo"))
m:delete("foo")
assert_nil(m:get("foo"))
end
lunatest.run(true)