From 912e4187c49a76ecfcca10b362c00c368563687a Mon Sep 17 00:00:00 2001 From: Thomas Harning Jr Date: Tue, 10 Apr 2012 23:04:58 -0400 Subject: [PATCH] base/lua/test: updates buffer management to be compatible with Lua 5.2 and latest libevent buffer handling --- CHANGELOG | 4 ++- include/buffer_event.h | 2 +- include/event_buffer.h | 2 +- include/event_callback.h | 2 +- src/buffer_event.c | 7 ++--- src/event_buffer.c | 9 ++++--- src/event_callback.c | 3 +-- src/luaevent.c | 11 ++++---- test/event_buffer-tests.lua | 66 ++++++++++++++++++++++----------------------- test/lunit.lua | 16 ++++++----- 10 files changed, 65 insertions(+), 57 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9fec9e5..566bbaa 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ 0.4.0 - WIP - * Makes compatible with Lua 5.2 + * Fixes buffer event tests to work with current bufferevent behavior + * Fixed buffer event and related tests to be compatible with Lua 5.2 + * Makes core segment compatible with Lua 5.2 * Remove use of 'module' in sources * Remove assumption that luaevent will set globals - Only 'core' will modify global table (creating luaevent.core) diff --git a/include/buffer_event.h b/include/buffer_event.h index ad8fc55..b98b26a 100644 --- a/include/buffer_event.h +++ b/include/buffer_event.h @@ -10,7 +10,7 @@ typedef struct { le_base* base; } le_bufferevent; -int buffer_event_register(lua_State* L); +void buffer_event_register(lua_State* L, int coreIndex); int is_buffer_event(lua_State* L, int idx); le_bufferevent* buffer_event_check(lua_State* L, int idx); diff --git a/include/event_buffer.h b/include/event_buffer.h index 0ee4cd2..33306f1 100644 --- a/include/event_buffer.h +++ b/include/event_buffer.h @@ -9,7 +9,7 @@ typedef struct { struct evbuffer* buffer; } le_buffer; -int event_buffer_register(lua_State* L); +void event_buffer_register(lua_State* L, int coreIndex); int is_event_buffer(lua_State* L, int idx); le_buffer* event_buffer_check(lua_State* L, int idx); int event_buffer_push(lua_State* L, struct evbuffer* buffer); diff --git a/include/event_callback.h b/include/event_callback.h index fcda8ce..7bb7c5e 100644 --- a/include/event_callback.h +++ b/include/event_callback.h @@ -12,7 +12,7 @@ typedef struct { struct timeval timeout; } le_callback; -int event_callback_register(lua_State* L); +void event_callback_register(lua_State* L); le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx); diff --git a/src/buffer_event.c b/src/buffer_event.c index 598230f..09410f0 100644 --- a/src/buffer_event.c +++ b/src/buffer_event.c @@ -213,7 +213,7 @@ static luaL_Reg funcs[] = { {NULL, NULL} }; -int buffer_event_register(lua_State* L) { +void buffer_event_register(lua_State* L, int coreIndex) { luaL_newmetatable(L, BUFFER_EVENT_MT); lua_pushcfunction(L, buffer_event_gc); lua_setfield(L, -2, "__gc"); @@ -222,6 +222,7 @@ int buffer_event_register(lua_State* L) { lua_setfield(L, -2, "__index"); lua_pop(L, 1); - luaL_register(L, "luaevent.core.bufferevent", funcs); - return 1; + lua_newtable(L); + luaL_register(L, NULL, funcs); + lua_setfield(L, coreIndex, "bufferevent"); } diff --git a/src/event_buffer.c b/src/event_buffer.c index a61c643..048b04a 100644 --- a/src/event_buffer.c +++ b/src/event_buffer.c @@ -243,7 +243,7 @@ static luaL_Reg funcs[] = { {NULL, NULL} }; -int event_buffer_register(lua_State* L) { +void event_buffer_register(lua_State* L, int coreIndex) { luaL_newmetatable(L, EVENT_BUFFER_MT); lua_pushcfunction(L, event_buffer_gc); lua_setfield(L, -2, "__gc"); @@ -255,7 +255,8 @@ int event_buffer_register(lua_State* L) { luaL_register(L, NULL, buffer_funcs); lua_setfield(L, -2, "__index"); lua_pop(L, 1); - - luaL_register(L, "luaevent.core.buffer", funcs); - return 1; + + lua_newtable(L); + luaL_register(L, NULL, funcs); + lua_setfield(L, coreIndex, "buffer"); } diff --git a/src/event_callback.c b/src/event_callback.c index 810c616..ace9cf2 100644 --- a/src/event_callback.c +++ b/src/event_callback.c @@ -80,7 +80,7 @@ le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx) { return cb; } -int event_callback_register(lua_State* L) { +void event_callback_register(lua_State* L) { luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT); lua_pushcfunction(L, luaevent_cb_gc); lua_setfield(L, -2, "__gc"); @@ -89,5 +89,4 @@ int event_callback_register(lua_State* L) { lua_setfield(L, -2, "close"); lua_setfield(L, -2, "__index"); lua_pop(L, 1); - return 0; } diff --git a/src/luaevent.c b/src/luaevent.c index 89c295a..c037872 100644 --- a/src/luaevent.c +++ b/src/luaevent.c @@ -167,11 +167,6 @@ int luaopen_luaevent_core(lua_State* L) { WSAStartup(wVersionRequested, &wsaData); #endif event_init( ); - /* Register external items */ - event_callback_register(L); - event_buffer_register(L); - buffer_event_register(L); - lua_settop(L, 0); /* Setup metatable */ luaL_newmetatable(L, EVENT_BASE_MT); lua_newtable(L); @@ -183,6 +178,12 @@ int luaopen_luaevent_core(lua_State* L) { luaL_register(L, "luaevent.core", funcs); setNamedIntegers(L, consts); + + /* Register external items */ + event_callback_register(L); + event_buffer_register(L, lua_gettop(L)); + buffer_event_register(L, lua_gettop(L)); + return 1; } diff --git a/test/event_buffer-tests.lua b/test/event_buffer-tests.lua index 468747e..6c2fced 100644 --- a/test/event_buffer-tests.lua +++ b/test/event_buffer-tests.lua @@ -1,11 +1,11 @@ -require("luaevent.core") -local buffer = luaevent.core.buffer +local core = require("luaevent.core") +local buffer = core.buffer require("lunit") -lunit.import("all") +--lunit.import("all") -bufferTests = TestCase("bufferTests") +bufferTests = lunit.TestCase("bufferTests") function bufferTests:setup() self.buffer = buffer.new() @@ -19,10 +19,10 @@ end local function testDataEqual(expected, actual, msg) msg = msg or '' - assert_equal(expected, actual:get_data(), "Buffer not the same: " .. msg) - assert_equal(#expected, actual:length(), "Buffer length not the same: " .. msg) - assert_equal(expected, tostring(actual), "Buffer (from tostring) not the same: " .. msg) - assert_equal(#expected, #actual, "Buffer length (from #) not zero: " .. msg) + lunit.assert_equal(expected, actual:get_data(), "Buffer not the same: " .. msg) + lunit.assert_equal(#expected, actual:length(), "Buffer length not the same: " .. msg) + lunit.assert_equal(expected, tostring(actual), "Buffer (from tostring) not the same: " .. msg) + lunit.assert_equal(#expected, #actual, "Buffer length (from #) not zero: " .. msg) end function bufferTests:test_empty() @@ -60,10 +60,10 @@ function bufferTests:test_addBuffer() self.buffer:add(self.buffer2) testDataEqual("Hello", self.buffer) testDataEqual("", self.buffer2) - assert_error("Cannot self-add buffers", function() + lunit.assert_error("Cannot self-add buffers", function() self.buffer:add(self.buffer) end) - assert_error("Cannot self-add buffers", function() + lunit.assert_error("Cannot self-add buffers", function() self.buffer2:add(self.buffer2) end) testDataEqual("Hello", self.buffer, "Failures should not affect data content") @@ -71,28 +71,28 @@ function bufferTests:test_addBuffer() end function bufferTests:test_addBadValues_fail() - assert_error("Should not be able to add no values", function() + lunit.assert_error("Should not be able to add no values", function() self.buffer:add() end) - assert_error("Should not be able to add boolean true", function() + lunit.assert_error("Should not be able to add boolean true", function() self.buffer:add(true) end) - assert_error("Should not be able to add boolean false", function() + lunit.assert_error("Should not be able to add boolean false", function() self.buffer:add(false) end) - assert_error("Should not be able to add functions", function() + lunit.assert_error("Should not be able to add functions", function() self.buffer:add(function() end) end) - assert_error("Should not be able to add threads", function() + lunit.assert_error("Should not be able to add threads", function() self.buffer:add(coroutine.create(function() end)) end) - assert_error("Should not be able to add non-buffer userdata", function() + lunit.assert_error("Should not be able to add non-buffer userdata", function() self.buffer:add(newproxy()) end) - assert_error("Should not be able to add nil values", function() + lunit.assert_error("Should not be able to add nil values", function() self.buffer:add(nil) end) - assert_error("Multiples including valid values should not affect failure results", function() + lunit.assert_error("Multiples including valid values should not affect failure results", function() self.buffer:add("Hello", 1, bufferb, true, false, function() end, newproxy(), nil) end) testDataEqual("", self.buffer, "Buffer not empty after failing additions") @@ -101,20 +101,20 @@ end function bufferTests:test_drain() self.buffer:add("123456789") testDataEqual("123456789", self.buffer) - assert_error("Cannot call drain w/ no args", function() + lunit.assert_error("Cannot call drain w/ no args", function() self.buffer:drain() end) self.buffer:drain(1) testDataEqual("23456789", self.buffer) self.buffer:drain(4) testDataEqual("6789", self.buffer) - assert_pass("Should be able to apply draining beyond actual buffer length", function() + lunit.assert_pass("Should be able to apply draining beyond actual buffer length", function() self.buffer:drain(5) end) testDataEqual("", self.buffer) self.buffer:add("123456789") testDataEqual("123456789", self.buffer) - assert_pass([[Should be able to apply negative draining to cause draining `all data` + lunit.assert_pass([[Should be able to apply negative draining to cause draining `all data` (see source comments for why)]], function() self.buffer:drain(-1) end) @@ -123,14 +123,14 @@ end function bufferTests:test_getPartial() self.buffer:add("123456789") - assert_equal("1234", self.buffer:get_data(4)) - assert_equal("1234", self.buffer:get_data(1,4)) - assert_equal("5678", self.buffer:get_data(5,4)) - assert_equal("5", self.buffer:get_data(5,1)) - assert_equal("56789", self.buffer:get_data(5,100000000), "Data length is capped at max obtainable") - assert_equal("56789", self.buffer:get_data(5,-100), "Negative sizes capture entire remaining string") - assert_equal("9", self.buffer:get_data(-1, 1, "Negative position causes wraparound")) - assert_equal("89", self.buffer:get_data(-2,2, "Negative wraparound does not cause length inversion")) + lunit.assert_equal("1234", self.buffer:get_data(4)) + lunit.assert_equal("1234", self.buffer:get_data(1,4)) + lunit.assert_equal("5678", self.buffer:get_data(5,4)) + lunit.assert_equal("5", self.buffer:get_data(5,1)) + lunit.assert_equal("56789", self.buffer:get_data(5,100000000), "Data length is capped at max obtainable") + lunit.assert_equal("56789", self.buffer:get_data(5,-100), "Negative sizes capture entire remaining string") + lunit.assert_equal("9", self.buffer:get_data(-1, 1, "Negative position causes wraparound")) + lunit.assert_equal("89", self.buffer:get_data(-2,2, "Negative wraparound does not cause length inversion")) end local lineData = [[1 @@ -141,13 +141,13 @@ local splitLineData = { } local mixedLineData = "1\r2\n3\r\n4\n\r5\r\r6\n\n7\r\n\r8\r\n\r9" local splitMixedLineData = { - "1","2","3","4","5","","6","","7","","8","", nil + "1","2","3","4","5","6","7","8", nil } function bufferTests:test_readline() self.buffer:add(lineData) testDataEqual(lineData, self.buffer) for _, data in ipairs(splitLineData) do - assert_equal(data, self.buffer:readline()) + lunit.assert_equal(data, self.buffer:readline()) end testDataEqual("3", self.buffer, "Failed readline doesn't affect buffer contents") self.buffer:drain(-1) @@ -155,9 +155,9 @@ function bufferTests:test_readline() self.buffer:add(mixedLineData) testDataEqual(mixedLineData, self.buffer) for _, data in ipairs(splitMixedLineData) do - assert_equal(data, self.buffer:readline()) + lunit.assert_equal(data, self.buffer:readline()) end testDataEqual("9", self.buffer) end -lunit.run() \ No newline at end of file +lunit.run() diff --git a/test/lunit.lua b/test/lunit.lua index 1ebec48..15ea06f 100644 --- a/test/lunit.lua +++ b/test/lunit.lua @@ -59,9 +59,12 @@ local setfenv = setfenv local tostring = tostring --- Start package scope -setfenv(1, P) - +if not setfenv then + _ENV = P +else + -- Start package scope + setfenv(1, P) +end @@ -452,10 +455,10 @@ function run() -- Count Test Cases and Tests -- -------------------------------- - stats.testcases = table.getn(testcases) + stats.testcases = #testcases -- table.getn(testcases) for _, tc in ipairs(testcases) do - stats_inc("tests" , table.getn(tc.__lunit_tests)) + stats_inc("tests" , #tc.__lunit_tests) --table.getn(tc.__lunit_tests)) end ------------------ @@ -575,7 +578,8 @@ function run_testcase(tc) --------------------------------- print() - print("#### Running '"..tc.__lunit_name.."' ("..table.getn(tc.__lunit_tests).." Tests)...") + --print("#### Running '"..tc.__lunit_name.."' ("..table.getn(tc.__lunit_tests).." Tests)...") + print("#### Running '"..tc.__lunit_name.."' ("..#tc.__lunit_tests.." Tests)...") for _, testname in ipairs(tc.__lunit_tests) do if setup(testname) then -- 2.11.4.GIT