-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathleak_test.lua
243 lines (220 loc) · 6.3 KB
/
leak_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
240
241
242
243
-- sanity checks for memory leak(eg: leaving dead references alive in the
-- registry)
local mpack = require('mpack')
local packed, unpacked
function fhex(s)
local rv = {}
for h in s:gmatch('%x+') do
rv[#rv + 1] = string.char(tonumber(h, 16))
end
return table.concat(rv)
end
local Type = {}
Type.__index = Type
function Type.new(a, b, c)
return setmetatable({a = a, b = b, c = c}, Type)
end
function Type:get_a() return self.a end
function Type:get_b() return self.b end
function Type:get_c() return self.c end
local ext_unpackers = {
[5] = function(code, str)
local data = mpack.unpack(str)
return Type.new(data._a, data._b, data._c)
end
}
local ext_packers = {
[Type] = function(obj)
local data = {_a = obj.a, _b = obj.b, _c = obj.c}
return 5, mpack.pack(data)
end
}
function simple_unpack()
local blob = io.open('cases.mpac'):read('*a') .. io.open('cases_compact.mpac'):read('*a')
-- simple unpack
local unpack = mpack.Unpacker()
local pos = 1
unpacked = {}
while pos <= #blob do
local item
item, pos = unpack(blob, pos)
unpacked[#unpacked + 1] = item
end
end
function simple_pack()
local items = {
false, true, mpack.NIL, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1,
127, 127, 255, 65535, 4294967295, -32, -32, -128, -32768, -2147483648,
0, -0, 1, -1, "a", "a", "a", "", "", "", { 0 }, { 0 }, { 0 }, {}, {},
{}, {}, {}, {}, { a = 97 }, { a = 97 }, { a = 97 }, { {} }, { { "a" } },
false, true, mpack.NIL, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1,
127, 127, 255, 65535, 4294967295, -32, -32, -128, -32768, -2147483648,
0, -0, 1, -1, "a", "a", "a", "", "", "", { 0 }, { 0 }, { 0 }, {}, {},
{}, {}, {}, {}, { a = 97 }, { a = 97 }, { a = 97 }, { {} }, { { "a" } }
}
local pack = mpack.Packer()
local chunks = {}
for i = 1, #items do
chunks[#chunks + 1] = pack(items[i])
end
packed = pack(items)
assert(table.concat(chunks, '') == packed:sub(4))
end
function unpack_chunks()
local unpack = mpack.Unpacker()
local blob = io.open('cases.mpac'):read('*a') .. io.open('cases_compact.mpac'):read('*a')
while #blob > 1 do
unpacked = unpack(blob:sub(1, 1))
blob = blob:sub(2)
end
unpacked = unpack(blob)
end
function pack_unpack_big_table()
local arr = {}
for i = 1, 10000 do
table.insert(arr, ('item%d'):format(i))
end
local unpack = mpack.Unpacker()
local pack = mpack.Packer()
packed = pack(arr)
unpacked = unpack(packed)
end
function pack_unpack_ext()
local unpacker = mpack.Unpacker({ext = ext_unpackers})
local packer = mpack.Packer({ext = ext_packers})
local t1 = Type.new('field a', 2, {1, 2})
local t2 = unpacker(packer({k={1,t1}})).k[2]
assert(t1:get_a() == t2:get_a())
assert(t1:get_b() == t2:get_b())
end
function cyclic_ref()
local tbl1 = {1}
table.insert(tbl1, tbl1)
table.insert(tbl1, 2)
local tbl2 = {1, 2, 3}
local tbl3 = {4, 5, tbl2, 6}
local tbl4 = {7, tbl3}
table.insert(tbl2, tbl4)
local unpack = mpack.Unpacker()
local pack = mpack.Packer()
unpack(pack(tbl1))
unpack(pack(tbl2))
end
function rpc()
local session = mpack.Session()
session:request('req1')
session:request('req2')
session:request('req3')
session:notify()
session:receive('\148\000\000')
session:receive('\148\000\001')
session:receive('\147\002')
session:receive('\148\001\002')
session:receive('\148\001\000')
session:receive('\148\001\001')
session:receive('\148')
session:receive('\000')
session:receive('\007')
session:request('req4')
session:receive('\148')
session:receive('\001')
session:receive('\003')
session:receive('\147')
session:receive('\002')
local failed = true
pcall(function()
session:receive('\148\001\009')
failed = false
end)
assert(failed)
end
function rpc_unpack()
local session = mpack.Session({unpack = mpack.Unpacker()})
session:receive(fhex('94 00 00 a6 6d 65 74 68 6f 64 92 01 a1 61'))
session:request('req1')
session:receive(fhex('94 01 00 c0 a6 72 65 73 75 6c 74'))
session:receive(fhex('94'))
session:receive(fhex('00'))
session:receive(fhex('01'))
session:receive(fhex('a6'))
session:receive(fhex('6d'))
session:receive(fhex('65'))
session:receive(fhex('74'))
session:receive(fhex('68'))
session:receive(fhex('6f'))
session:receive(fhex('64'))
session:receive(fhex('92'))
session:receive(fhex('01'))
session:receive(fhex('a1'))
session:receive(fhex('61'))
session:request('req2')
session:receive(fhex('94'))
session:receive(fhex('01'))
session:receive(fhex('01'))
session:receive(fhex('c0'))
session:receive(fhex('a6'))
session:receive(fhex('72'))
session:receive(fhex('65'))
session:receive(fhex('73'))
session:receive(fhex('75'))
session:receive(fhex('6c'))
session:receive(fhex('74'))
session:receive(fhex('93 02 a6 6d 65 74 68 6f 64 92 01 a1 61'))
session:receive(fhex('93'))
session:receive(fhex('02'))
session:receive(fhex('a6'))
session:receive(fhex('6d'))
session:receive(fhex('65'))
session:receive(fhex('74'))
session:receive(fhex('68'))
session:receive(fhex('6f'))
session:receive(fhex('64'))
session:receive(fhex('92'))
session:receive(fhex('01'))
session:receive(fhex('a1'))
session:receive(fhex('61'))
end
-- create a session out of the function scope to make sure the
-- collect call can capture the leak
local session = mpack.Session({unpack = mpack.Unpacker()})
function callback(err, result)
end
function test_request_cb_leak()
local pack = mpack.Packer()
-- create request
local request = session:request(callback) .. pack('method') .. pack('args')
-- receive the request
local _, id = session:receive(request, 1)
local response = session:reply(id) .. pack(mpack.NIL) .. pack('response')
-- handle the response
local _, cb = session:receive(response, 1)
end
function run()
simple_unpack()
simple_pack()
unpack_chunks()
pack_unpack_big_table()
pack_unpack_ext()
cyclic_ref()
rpc()
rpc_unpack()
test_request_cb_leak()
end
function collect()
-- run multiple times to ensure lua has a chance to reclaim all unused memomry
for i = 1, 10 do
collectgarbage()
end
return collectgarbage('count')
end
run() -- run once to "initialize" the state
before = collect()
run()
after = collect()
if before ~= after then
print('fail .. leaked '..((after - before) * 1024)..' kilobytes')
os.exit(1)
else
print('ok')
os.exit(0)
end