Skip to content

Commit

Permalink
Fix fs_read leak and make fs leak tests better
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Oct 31, 2014
1 parent 42a17b8 commit f19c3d9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/lreq.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static luv_req_t* luv_setup_req(lua_State* L, int callback_ref) {
lua_pushvalue(L, -1);
data->req_ref = luaL_ref(L, LUA_REGISTRYINDEX);
data->callback_ref = callback_ref;
data->data = NULL;

return data;
}
Expand All @@ -63,5 +64,6 @@ static void luv_fulfill_req(lua_State* L, luv_req_t* data, int nargs) {
static void luv_cleanup_req(lua_State* L, luv_req_t* data) {
luaL_unref(L, LUA_REGISTRYINDEX, data->req_ref);
luaL_unref(L, LUA_REGISTRYINDEX, data->callback_ref);
free(data->data);
free(data);
}
16 changes: 16 additions & 0 deletions src/schema.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2014 The Luvit Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
37 changes: 29 additions & 8 deletions tests/test-leaks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,45 @@ return require('lib/tap')(function (test)
end)
end)

test("reading valid file", function (print, p, expect, uv)
test("reading file async", function (print, p, expect, uv)
local mode = tonumber("644", 8)
bench(uv, p, 0x500, function ()
local req = assert(uv.fs_open("README.md", "r", mode, expect(function (err, fd)
local onOpen, onStat, onRead, onClose
local fd, stat

onOpen = expect(function (err, result)
assert(not err, err)
fd = result
uv.fs_fstat(fd, onStat)
end)

onStat = expect(function (err, result)
assert(not err, err)
stat = result
uv.fs_read(fd, stat.size, 0, onRead)
end)

onRead = expect(function (err, data)
assert(not err, err)
assert(#data == stat.size)
uv.fs_close(fd, onClose)
end)

onClose = expect(function (err)
assert(not err, err)
local stat = assert(uv.fs_fstat(fd))
assert(uv.fs_read(fd, stat.size, 0))
assert(uv.fs_close(fd, expect(function (err)
assert(not err, err)
end)))
end)))
end)

assert(uv.fs_open("README.md", "r", mode, onOpen))
end)
end)

test("reading file sync", function (print, p, expect, uv)
local mode = tonumber("644", 8)
bench(uv, p, 0x2000, function ()
local fd = assert(uv.fs_open("README.md", "r", mode))
local stat = assert(uv.fs_fstat(fd))
local data = assert(uv.fs_read(fd, stat.size, 0))
assert(#data == stat.size)
assert(uv.fs_close(fd))
end)
end)
Expand Down

0 comments on commit f19c3d9

Please sign in to comment.