diff options
Diffstat (limited to 'test/utils.lua')
| -rw-r--r-- | test/utils.lua | 63 |
1 files changed, 63 insertions, 0 deletions
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 @@ | |||
| 1 | local lua_version_t | ||
| 2 | local function lua_version() | ||
| 3 | if not lua_version_t then | ||
| 4 | local version = rawget(_G,"_VERSION") | ||
| 5 | local maj,min = version:match("^Lua (%d+)%.(%d+)$") | ||
| 6 | if maj then lua_version_t = {tonumber(maj),tonumber(min)} | ||
| 7 | elseif not math.mod then lua_version_t = {5,2} | ||
| 8 | elseif table.pack and not pack then lua_version_t = {5,2} | ||
| 9 | else lua_version_t = {5,2} end | ||
| 10 | end | ||
| 11 | return lua_version_t[1], lua_version_t[2] | ||
| 12 | end | ||
| 13 | |||
| 14 | local LUA_MAJOR, LUA_MINOR = lua_version() | ||
| 15 | local IS_LUA_51 = (LUA_MAJOR == 5) and (LUA_MINOR == 1) | ||
| 16 | local IS_LUA_52 = (LUA_MAJOR == 5) and (LUA_MINOR == 2) | ||
| 17 | |||
| 18 | local LUA_INIT = "LUA_INIT" | ||
| 19 | local LUA_INIT_VER | ||
| 20 | if not IS_LUA_51 then | ||
| 21 | LUA_INIT_VER = LUA_INIT .. "_" .. LUA_MAJOR .. "_" .. LUA_MINOR | ||
| 22 | end | ||
| 23 | |||
| 24 | LUA_INIT = LUA_INIT_VER and os.getenv( LUA_INIT_VER ) or os.getenv( LUA_INIT ) or "" | ||
| 25 | |||
| 26 | LUA_INIT = [[do | ||
| 27 | local lua_init = ]] .. ("%q"):format(LUA_INIT) .. [[ | ||
| 28 | if lua_init and #lua_init > 0 then | ||
| 29 | if lua_init:sub(1,1) == '@' then | ||
| 30 | dofile(lua_init:sub(2)) | ||
| 31 | else | ||
| 32 | assert((loadstring or load)(lua_init))() | ||
| 33 | end | ||
| 34 | end | ||
| 35 | end;]] | ||
| 36 | |||
| 37 | local sleep | ||
| 38 | local status, socket = pcall(require,"socket") | ||
| 39 | if status then | ||
| 40 | sleep = function(secs) | ||
| 41 | return socket.sleep(secs) | ||
| 42 | end | ||
| 43 | end | ||
| 44 | |||
| 45 | if not sleep then | ||
| 46 | local status, ztimer = pcall(require, "lzmq.timer") | ||
| 47 | if status then | ||
| 48 | sleep = function(secs) | ||
| 49 | ztimer.sleep(secs * 1000) | ||
| 50 | end | ||
| 51 | end | ||
| 52 | end | ||
| 53 | |||
| 54 | if not sleep then | ||
| 55 | sleep = function(secs) | ||
| 56 | os.execute("sleep " .. tonumber(secs)) | ||
| 57 | end | ||
| 58 | end | ||
| 59 | |||
| 60 | return { | ||
| 61 | thread_init = LUA_INIT, | ||
| 62 | sleep = sleep, | ||
| 63 | } | ||
