From a2ab7c368566397f3a0bc7dd8d4877d8a5afa094 Mon Sep 17 00:00:00 2001 From: fm Date: Fri, 16 Mar 2012 09:57:35 +0100 Subject: [PATCH] Lua test coded, not yet running .. --- Makefile | 9 +++++--- luevent.c | 10 ++++---- test.c | 2 +- test.lua | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 test.lua diff --git a/Makefile b/Makefile index 1da79fa..5609241 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ CC=$(CROSS_COMPILE)gcc AR=$(CROSS_COMPILE)ar INCLUDE += -I /usr/include/lua5.1/ -LDFLAGS=-s +LDFLAGS=-g CFLAGS=-fPIC -g -Os $(INCLUDE) MAJOR = 1 @@ -23,7 +23,6 @@ lua: luevent.so libuevent.a: libuevent.o $(AR) r $@ $^ - $(CROSS_COMPILE)strip --strip-unneeded $@ libuevent.so.$(MAJOR): libuevent.o $(CC) $(LDFLAGS) -shared -o $@ $^ -Wl,-soname=libuevent @@ -31,6 +30,10 @@ libuevent.so.$(MAJOR): libuevent.o luevent.so: luevent.o all $(CC) $(LDFLAGS) -shared -o $@ luevent.o -L . -l uevent -Wl,-soname=luevent +stripped: all lua + $(CROSS_COMPILE)strip --strip-unneeded libuevent.a libuevent.so.$(MAJOR) luevent.so + + test: all $(CC) -g -o test -D NO_DEBUG test.c libuevent.c && ./test @@ -38,7 +41,7 @@ luatest: lua lua test.lua clean: - rm -f libuevent.o luauevent.o libuevent.so.$(MAJOR) luevent.so test + rm -f libuevent.o luevent.o libuevent.so.$(MAJOR) luevent.so test .PHONY: all test clean luatest lua diff --git a/luevent.c b/luevent.c index dacce00..d8f4823 100644 --- a/luevent.c +++ b/luevent.c @@ -393,23 +393,25 @@ int luaopen_luevent(lua_State * L) assert(lueContext); luaL_register(L, lua_tostring(L, 1), lueventlib); // calls - lua_setmetatable(L, -1); // Call GC at module cleanup. Pops it too + lua_pushvalue(L, -1); // Dup + lua_setmetatable(L, -2); // Call GC at module cleanup. l_newType(L, LUA_LTIMER_MT); // create metatable for timers luaL_register(L, NULL, ltimermeta); // methods - lua_pop(L, 1); + lua_setfield(L, -2, "_meta_timer"); // easy access from test code l_newType(L, LUA_LFILEH_MT); // create metatable for file handlers luaL_register(L, NULL, lfilehmeta); // methods - lua_pop(L, 1); + lua_setfield(L, -2, "_meta_fileh"); // easy access from test code lua_newtable(L); // Table[obj] -> Callback-closure lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, LUA_LUEOBJ); lua_pushlightuserdata(L, lueContext);// Table[1] = lueContext lua_rawseti(L, -2, 1); - lua_pop(L, 1); + lua_setfield(L, -2, "_lue_objects");// easy access from test code + lua_pop(L, 1); // pop module return 0; } diff --git a/test.c b/test.c index 9d43bee..5106f98 100644 --- a/test.c +++ b/test.c @@ -19,7 +19,7 @@ static timeOutHdl(struct LUECtxt *ctxt, struct LUETimerH *handle, void *data) timeOut = NULL; printf("Timeout is exceeded %p\n", data); lueRemFd(ctxt, readFd); - lueExit(ctxt); + lueTerminate(ctxt); } static appTimeout(struct LUECtxt *ctxt, struct LUETimerH *handle, void *data) diff --git a/test.lua b/test.lua new file mode 100644 index 0000000..f63c2fc --- /dev/null +++ b/test.lua @@ -0,0 +1,69 @@ +#!/usr/bin/env lua +--/usr/bin/env luajit +--- +-- @file +-- Micro event library libuevent. +-- Test & showcase of libuevent +-- +-- (C) Copyright 2012 flemming.madsen at madsensoft.dk. See libuevent.h +--- +require "socket" +require "luevent" + +local ADDR = "127.0.0.1" +local PORT = 42555 +local timer +local period + +local function timeOut() + print("Timeout is exceeded") + luevent.terminate() +end + +local function writeThread(sck, cnt) + sck:send(("TESTING"):sub(cnt)) + period = cnt + 5 + luevent.addTimer(100 * period, function() writeThread(sck, cnt + 1) end) +end + +local function main() + luevent.addTimer(5000, function() + print("Lifetime is exceeded") + os.exit(1); + end) + + local isrv = assert(socket.bind(ADDR, PORT)) + luevent.addFileIO(isrv:getfd(), function(self) + local isock = isrv:accept() + luevent.addFileIO(isock:getfd(), function(self) + -- Pipe readable + if timer then + timer:clear() + timer = luevent.addTimer(950, timeOut) + end + local str = isock:receive(50) + io.write(str.."."..#str) io.flush() + end) + + self:clear() -- This is a oneshot + end) + timer = luevent.addTimer(950, timeOut) + + local osck = socket.tcp() osck:settimeout(0) osck:connect(ADDR, PORT) + luevent.addFileIO(osck:getfd(), function(self) + writeThread(osck, 1) + self:clear() -- This is a oneshot + end, "w") + + luevent.run() + print "DONE" + for k,v in pairs(luevent._lue_objects) do print(k,v) end + + package.loaded.luevent = nil + collectgarbage("collect") + print(("Period is %d must be 10: %s"):format(period, 10 == period and "OK" or "ERROR!")) + os.exit(period == 10 and 0 or 1) +end + +main() +-- vim: set sw=3 sts=3 et nu: