From 1b81ddc1a8d19ba09d7e61d0d9d841993d7187fb Mon Sep 17 00:00:00 2001 From: moteus Date: Fri, 27 Dec 2013 12:36:24 +0400 Subject: Add. logger test --- .travis.yml | 1 + src/llthread.c | 34 ++++++++++++++++------------------ test/test_logger.lua | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 test/test_logger.lua diff --git a/.travis.yml b/.travis.yml index 96cab16..7cadb6a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,6 +51,7 @@ script: - lua$LUA_SFX test_join_timeout.lua - lua$LUA_SFX test_join_detach.lua - lua$LUA_SFX test_register_ffi.lua + - lua$LUA_SFX test_logger.lua notifications: email: diff --git a/src/llthread.c b/src/llthread.c index 492766e..0ff39d7 100644 --- a/src/llthread.c +++ b/src/llthread.c @@ -2,20 +2,21 @@ # define USE_PTHREAD #endif -#include -#include -#include -#include - #ifndef USE_PTHREAD # include -# include # include #else # include -# include #endif +#include +#include +#include +#include +#include +#include +#include "l52util.h" + /*export*/ #ifdef _WIN32 # define LLTHREADS_EXPORT_API __declspec(dllexport) @@ -40,7 +41,7 @@ #endif #ifndef USE_PTHREAD -# define OS_THREAD_RETURT unsigned int __stdcall +# define OS_THREAD_RETURN unsigned int __stdcall # define INVALID_THREAD INVALID_HANDLE_VALUE # define INFINITE_JOIN_TIMEOUT INFINITE # define JOIN_OK 0 @@ -49,8 +50,7 @@ typedef DWORD join_timeout_t; typedef HANDLE os_thread_t; #else -# define OS_THREAD_RETURT void * -# define INVALID_THREAD 0 +# define OS_THREAD_RETURN void * # define INFINITE_JOIN_TIMEOUT -1 # define JOIN_OK 0 # define JOIN_ETIMEDOUT ETIMEDOUT @@ -58,8 +58,6 @@ typedef int join_timeout_t; typedef pthread_t os_thread_t; #endif -#include "l52util.h" -#include LLTHREADS_EXPORT_API int luaopen_llthreads(lua_State *L); @@ -389,10 +387,8 @@ static llthread_child_t *llthread_child_new() { /* create new lua_State for the thread. */ /* open standard libraries. */ - /* push traceback function as first value on stack. */ this->L = luaL_newstate(); open_thread_libs(this->L); - lua_pushcfunction(this->L, traceback); return this; } @@ -402,10 +398,14 @@ static void llthread_child_destroy(llthread_child_t *this) { FREE_STRUCT(this); } -static OS_THREAD_RETURT llthread_child_thread_run(void *arg) { +static OS_THREAD_RETURN llthread_child_thread_run(void *arg) { llthread_child_t *this = (llthread_child_t *)arg; lua_State *L = this->L; - int nargs = lua_gettop(L) - 2; + int nargs = lua_gettop(L) - 1; + + /* push traceback function as first value on stack. */ + lua_pushcfunction(this->L, traceback); + lua_insert(L, 1); this->status = lua_pcall(L, nargs, LUA_MULTRET, 1); @@ -485,8 +485,6 @@ static int llthread_push_results(lua_State *L, llthread_child_t *child, int idx, static int llthread_detach(llthread_t *this){ int rc = 0; this->child = NULL; - FLAG_SET(this, TSTATE_DETACHED); - FLAG_UNSET(this, FLAG_JOIN_LUA); #ifdef USE_PTHREAD rc = pthread_detach(this->thread); #endif diff --git a/test/test_logger.lua b/test/test_logger.lua new file mode 100644 index 0000000..66c190f --- /dev/null +++ b/test/test_logger.lua @@ -0,0 +1,22 @@ +require "llthreads".new([[ +require "string" + +require "llthreads".set_logger(function(msg) + if type(msg) ~= 'string' then + print("ERROR! Invalid error message: ", msg) + os.exit(-2) + end + if not msg:find("SOME ERROR", nil, true) then + print("ERROR! Invalid error message: ", msg) + os.exit(-1) + end + print("Done!") + os.exit(0) +end) + +error("SOME ERROR") +]]):start():join() + +print("ERROR! Logger has not been call!") +os.exit(-1) + -- cgit v1.2.3-55-g6feb