From 5291caae5be68250a4d9aafa54fe2081ab2a6113 Mon Sep 17 00:00:00 2001 From: moteus Date: Thu, 26 Dec 2013 18:10:40 +0400 Subject: Update tests. --- README.md | 9 +++---- test/test_join_timeout.lua | 50 +++--------------------------------- test/test_llthreads.lua | 24 +----------------- test/test_threads.lua | 11 ++------ test/utils.lua | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 84 deletions(-) create mode 100644 test/utils.lua diff --git a/README.md b/README.md index 3721b90..b14d44d 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,6 @@ This is full dropin replacement for [llthreads](https://github.com/Neopallium/lu * thread:join() method support arbitrary timeout on Windows platform * set_logger function allow logging errors (crash Lua VM) in current llthread's threads -[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/moteus/lua-llthreads2/trend.png)](https://bitdeli.com/free "Bitdeli Badge") - - ##Usage ### Use custom logger @@ -32,9 +29,9 @@ local LOG = require"log".new( require "log.writer.net.zmq".new("tcp://127.0.0.1:5555") ) llthread.set_logger(function(msg) LOG.error(msg) end) - -... - -- This error with traceback will be passed to logger error("SOME ERROR") ``` + +[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/moteus/lua-llthreads2/trend.png)](https://bitdeli.com/free "Bitdeli Badge") + diff --git a/test/test_join_timeout.lua b/test/test_join_timeout.lua index 5beac2b..085497f 100644 --- a/test/test_join_timeout.lua +++ b/test/test_join_timeout.lua @@ -1,53 +1,11 @@ local llthreads = require"llthreads" - -local sleep -local status, socket = pcall(require,"socket") -if status then - sleep = function(secs) - return socket.sleep(secs) - end -end - -if not sleep then - local status, ztimer = pcall(require, "lzmq.timer") - if status then - sleep = function(secs) - ztimer.sleep(secs * 1000) - end - end -end - -if not sleep then - sleep = function(secs) - os.execute("sleep " .. tonumber(secs)) - end -end +local utils = require "utils" +local sleep = utils.sleep local include = [[ local llthreads = require"llthreads" - -local sleep -local status, socket = pcall(require,"socket") -if status then - sleep = function(secs) - return socket.sleep(secs) - end -end - -if not sleep then - local status, ztimer = pcall(require, "lzmq.timer") - if status then - sleep = function(secs) - ztimer.sleep(secs * 1000) - end - end -end - -if not sleep then - sleep = function(secs) - os.execute("sleep " .. tonumber(secs)) - end -end +]] .. utils.thread_init .. [[ +local sleep = require "utils".sleep ]] local thread = llthreads.new(include .. [[ diff --git a/test/test_llthreads.lua b/test/test_llthreads.lua index 3474b9b..df8c6a2 100644 --- a/test/test_llthreads.lua +++ b/test/test_llthreads.lua @@ -19,29 +19,7 @@ -- THE SOFTWARE. local llthreads = require"llthreads" - -local sleep -local status, socket = pcall(require,"socket") -if status then - sleep = function(secs) - return socket.sleep(secs) - end -end - -if not sleep then - local status, ztimer = pcall(require, "lzmq.timer") - if status then - sleep = function(secs) - ztimer.sleep(secs * 1000) - end - end -end - -if not sleep then - sleep = function(secs) - os.execute("sleep " .. tonumber(secs)) - end -end +local sleep = require"utils".sleep local function detached_thread(...) local thread = llthreads.new([[ print("print_detached_thread:", ...) ]], ...) diff --git a/test/test_threads.lua b/test/test_threads.lua index 467526e..459604d 100644 --- a/test/test_threads.lua +++ b/test/test_threads.lua @@ -23,19 +23,12 @@ -- luajit sub_threads.lua local llthreads = require"llthreads" +local utils = require "utils" local num_threads = tonumber(arg[1] or 1000) -- level 0 string literal enclosure [[ ]] of child execution code -local thread_code = [[ - local lua_init = os.getenv("lua_init") - if lua_init and #lua_init > 0 then - if lua_init:sub(1,1) == '@' then - dofile(lua_init:sub(2)) - else - assert((loadstring or load)(lua_init))() - end - end +local thread_code = utils.thread_init .. [[ local num_threads = ... print("CHILD: received from ROOT params:", ...) diff --git a/test/utils.lua b/test/utils.lua new file mode 100644 index 0000000..d9b5fe5 --- /dev/null +++ b/test/utils.lua @@ -0,0 +1,63 @@ +local lua_version_t +local function lua_version() + if not lua_version_t then + local version = rawget(_G,"_VERSION") + local maj,min = version:match("^Lua (%d+)%.(%d+)$") + if maj then lua_version_t = {tonumber(maj),tonumber(min)} + elseif not math.mod then lua_version_t = {5,2} + elseif table.pack and not pack then lua_version_t = {5,2} + else lua_version_t = {5,2} end + end + return lua_version_t[1], lua_version_t[2] +end + +local LUA_MAJOR, LUA_MINOR = lua_version() +local IS_LUA_51 = (LUA_MAJOR == 5) and (LUA_MINOR == 1) +local IS_LUA_52 = (LUA_MAJOR == 5) and (LUA_MINOR == 2) + +local LUA_INIT = "LUA_INIT" +local LUA_INIT_VER +if not IS_LUA_51 then + LUA_INIT_VER = LUA_INIT .. "_" .. LUA_MAJOR .. "_" .. LUA_MINOR +end + +LUA_INIT = LUA_INIT_VER and os.getenv( LUA_INIT_VER ) or os.getenv( LUA_INIT ) or "" + +LUA_INIT = [[do + local lua_init = ]] .. ("%q"):format(LUA_INIT) .. [[ + if lua_init and #lua_init > 0 then + if lua_init:sub(1,1) == '@' then + dofile(lua_init:sub(2)) + else + assert((loadstring or load)(lua_init))() + end + end +end;]] + +local sleep +local status, socket = pcall(require,"socket") +if status then + sleep = function(secs) + return socket.sleep(secs) + end +end + +if not sleep then + local status, ztimer = pcall(require, "lzmq.timer") + if status then + sleep = function(secs) + ztimer.sleep(secs * 1000) + end + end +end + +if not sleep then + sleep = function(secs) + os.execute("sleep " .. tonumber(secs)) + end +end + +return { + thread_init = LUA_INIT, + sleep = sleep, +} -- cgit v1.2.3-55-g6feb