lua test fixes
effil as lua library
This commit is contained in:
parent
852557ed37
commit
10e7bd7131
15
src/lua/effil.lua
Normal file
15
src/lua/effil.lua
Normal file
@ -0,0 +1,15 @@
|
||||
local function detect_native_lib_ext()
|
||||
local home = os.getenv("HOME")
|
||||
if not home then return "dll" end
|
||||
if string.find(home, "/Users/") then return "dylib" end
|
||||
if string.find(home, "/home/") then return "so" end
|
||||
-- TODO: unable to detect os
|
||||
-- how to reportabout error
|
||||
return "so"
|
||||
end
|
||||
|
||||
package.cpath = package.cpath .. ";./?." .. detect_native_lib_ext()
|
||||
|
||||
local api = require 'libeffil'
|
||||
|
||||
return api
|
||||
@ -2,55 +2,6 @@
|
||||
|
||||
-- TODO: remove hardcode
|
||||
package.path = package.path .. ";../libs/luaunit/?.lua;../tests/lua/?.lua"
|
||||
package.cpath = package.cpath .. ";./?.so;./?.dylib"
|
||||
|
||||
test = require "luaunit"
|
||||
|
||||
function log(...)
|
||||
local msg = '@\t\t' .. os.date('%Y-%m-%d %H:%M:%S ',os.time())
|
||||
for _, val in ipairs({...}) do
|
||||
msg = msg .. tostring(val) .. ' '
|
||||
end
|
||||
io.write(msg .. '\n')
|
||||
io.flush()
|
||||
end
|
||||
|
||||
function wait(timeInSec, condition, silent)
|
||||
if not silent then
|
||||
log("Start waiting for " .. tostring(timeInSec) .. "sec...")
|
||||
end
|
||||
local result = false
|
||||
local startTime = os.time()
|
||||
while ( (os.time() - startTime) <= timeInSec) do
|
||||
if condition ~= nil then
|
||||
if type(condition) == 'function' then
|
||||
if condition() then
|
||||
result = true
|
||||
break
|
||||
end
|
||||
else
|
||||
if condition then
|
||||
result = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if not silent then
|
||||
log "Give up"
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function sleep(timeInSec, silent)
|
||||
if not silent then
|
||||
log("Start sleep for " .. tostring(timeInSec) .. "sec...")
|
||||
end
|
||||
wait(timeInSec, nil, true)
|
||||
if not silent then
|
||||
log "Wake up"
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
-- Hack input arguments to make tests verbose by default
|
||||
@ -71,7 +22,9 @@ end
|
||||
-- TESTS --
|
||||
-----------
|
||||
|
||||
require("smoke_test")
|
||||
test = require "luaunit"
|
||||
require 'test_utils'
|
||||
require 'thread'
|
||||
require 'shared_table'
|
||||
|
||||
-----------
|
||||
os.exit( test.LuaUnit.run() )
|
||||
49
tests/lua/shared_table.lua
Normal file
49
tests/lua/shared_table.lua
Normal file
@ -0,0 +1,49 @@
|
||||
TestSharedTable = {tearDown = tearDown}
|
||||
|
||||
function TestSharedTable:testPairs()
|
||||
local effil = require 'effil'
|
||||
local share = effil.table()
|
||||
local data = { 0, 0, 0, ["key1"] = 0, ["key2"] = 0, ["key3"] = 0 }
|
||||
|
||||
for k, _ in pairs(data) do
|
||||
share[k] = k .. "-value"
|
||||
end
|
||||
|
||||
for k,v in pairs(share) do
|
||||
test.assertEquals(data[k], 0)
|
||||
data[k] = 1
|
||||
test.assertEquals(v, k .. "-value")
|
||||
end
|
||||
|
||||
for k,v in pairs(data) do
|
||||
log("Check: " .. k)
|
||||
test.assertEquals(v, 1)
|
||||
end
|
||||
|
||||
for k,v in ipairs(share) do
|
||||
test.assertEquals(data[k], 1)
|
||||
data[k] = 2
|
||||
test.assertEquals(v, k .. "-value")
|
||||
end
|
||||
|
||||
for k,v in ipairs(data) do
|
||||
log("Check: " .. k)
|
||||
test.assertEquals(v, 2)
|
||||
end
|
||||
end
|
||||
|
||||
function TestSharedTable:testLength()
|
||||
local effil = require 'effil'
|
||||
local share = effil.table()
|
||||
share[1] = 10
|
||||
share[2] = 20
|
||||
share[3] = 30
|
||||
share[4] = 40
|
||||
share["str"] = 50
|
||||
log "Check values"
|
||||
test.assertEquals(#share, 4)
|
||||
share[3] = nil
|
||||
test.assertEquals(#share, 2)
|
||||
share[1] = nil
|
||||
test.assertEquals(#share, 0)
|
||||
end
|
||||
50
tests/lua/test_utils.lua
Normal file
50
tests/lua/test_utils.lua
Normal file
@ -0,0 +1,50 @@
|
||||
function log(...)
|
||||
local msg = '@\t\t' .. os.date('%Y-%m-%d %H:%M:%S ',os.time())
|
||||
for _, val in ipairs({...}) do
|
||||
msg = msg .. tostring(val) .. ' '
|
||||
end
|
||||
io.write(msg .. '\n')
|
||||
io.flush()
|
||||
end
|
||||
|
||||
function wait(timeInSec, condition, silent)
|
||||
if not silent then
|
||||
log("Start waiting for " .. tostring(timeInSec) .. "sec...")
|
||||
end
|
||||
local result = false
|
||||
local startTime = os.time()
|
||||
while ( (os.time() - startTime) <= timeInSec) do
|
||||
if condition ~= nil then
|
||||
if type(condition) == 'function' then
|
||||
if condition() then
|
||||
result = true
|
||||
break
|
||||
end
|
||||
else
|
||||
if condition then
|
||||
result = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if not silent then
|
||||
log "Give up"
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function sleep(timeInSec, silent)
|
||||
if not silent then
|
||||
log("Start sleep for " .. tostring(timeInSec) .. "sec...")
|
||||
end
|
||||
wait(timeInSec, nil, true)
|
||||
if not silent then
|
||||
log "Wake up"
|
||||
end
|
||||
end
|
||||
|
||||
function tearDown()
|
||||
log "TearDown() collect garbage"
|
||||
collectgarbage()
|
||||
end
|
||||
@ -1,39 +1,7 @@
|
||||
TestSmoke = {}
|
||||
TestThread = {tearDown = tearDown }
|
||||
|
||||
function TestSmoke:tearDown()
|
||||
log "TearDown() collect garbage"
|
||||
collectgarbage()
|
||||
end
|
||||
|
||||
function TestSmoke:testSharedTableTypes()
|
||||
local effil = require('libeffil')
|
||||
local share = effil.table()
|
||||
|
||||
share["number"] = 100500
|
||||
share["string"] = "string value"
|
||||
share["bool"] = true
|
||||
share["function"] = function(left, right) return left + right end
|
||||
|
||||
local thread_factory = effil.thread(
|
||||
function(share)
|
||||
share["child.number"] = share["number"]
|
||||
share["child.string"] = share["string"]
|
||||
share["child.bool"] = share["bool"]
|
||||
share["child.function"] = share["function"](11,45)
|
||||
end
|
||||
)
|
||||
local thread = thread_factory(share)
|
||||
thread:wait()
|
||||
|
||||
log "Check values"
|
||||
test.assertEquals(share["child.number"], share["number"])
|
||||
test.assertEquals(share["child.string"], share["string"])
|
||||
test.assertEquals(share["child.bool"], share["bool"])
|
||||
test.assertEquals(share["child.function"], share["function"](11,45))
|
||||
end
|
||||
|
||||
function TestSmoke:testThreadCancel()
|
||||
local effil = require('libeffil')
|
||||
function TestThread:testThreadCancel()
|
||||
local effil = require 'effil'
|
||||
local thread_runner = effil.thread(
|
||||
function()
|
||||
local startTime = os.time()
|
||||
@ -49,8 +17,8 @@ function TestSmoke:testThreadCancel()
|
||||
test.assertEquals(thread:status(), 'canceled')
|
||||
end
|
||||
|
||||
function TestSmoke:testThreadPauseAndResume()
|
||||
local effil = require('libeffil')
|
||||
function TestThread:testThreadPauseAndResume()
|
||||
local effil = require 'effil'
|
||||
local data = effil.table()
|
||||
data.value = 0
|
||||
local thread_runner = effil.thread(
|
||||
@ -76,8 +44,8 @@ function TestSmoke:testThreadPauseAndResume()
|
||||
thread:wait()
|
||||
end
|
||||
|
||||
function TestSmoke:testThreadPauseAndStop()
|
||||
local effil = require('libeffil')
|
||||
function TestThread:testThreadPauseAndStop()
|
||||
local effil = require 'effil'
|
||||
log "Create thread"
|
||||
local data = effil.table()
|
||||
data.value = 0
|
||||
@ -103,8 +71,8 @@ function TestSmoke:testThreadPauseAndStop()
|
||||
thread:wait()
|
||||
end
|
||||
|
||||
function TestSmoke:testThreadPauseAndStop()
|
||||
local effil = require('libeffil')
|
||||
function TestThread:testThreadPauseAndStop()
|
||||
local effil = require 'effil'
|
||||
log "Create thread"
|
||||
local data = effil.table()
|
||||
data.value = 0
|
||||
@ -130,64 +98,8 @@ function TestSmoke:testThreadPauseAndStop()
|
||||
thread:wait()
|
||||
end
|
||||
|
||||
function TestSmoke:testRecursiveTables()
|
||||
local effil = require('libeffil')
|
||||
local share = effil.table()
|
||||
|
||||
local magic_number = 42
|
||||
share["subtable1"] = effil.table()
|
||||
share["subtable1"]["subtable1"] = effil.table()
|
||||
share["subtable1"]["subtable2"] = share["subtable1"]["subtable1"]
|
||||
share["subtable2"] = share["subtable1"]["subtable1"]
|
||||
share["magic_number"] = magic_number
|
||||
|
||||
local thread_factory = effil.thread(
|
||||
function(share)
|
||||
share["subtable1"]["subtable1"]["magic_number"] = share["magic_number"]
|
||||
share["magic_number"] = nil
|
||||
end
|
||||
)
|
||||
local thread = thread_factory(share)
|
||||
thread:wait()
|
||||
|
||||
log "Check values"
|
||||
test.assertEquals(share["subtable1"]["subtable1"]["magic_number"], magic_number)
|
||||
test.assertEquals(share["subtable1"]["subtable2"]["magic_number"], magic_number)
|
||||
test.assertEquals(share["subtable2"]["magic_number"], magic_number)
|
||||
test.assertEquals(share["magic_number"], nil)
|
||||
end
|
||||
|
||||
function TestSmoke:testThisThreadFunctions()
|
||||
local effil = require('libeffil')
|
||||
local share = effil.table()
|
||||
|
||||
local thread_factory = effil.thread(
|
||||
function(share)
|
||||
share["child.id"] = require('libeffil').thread_id()
|
||||
end
|
||||
)
|
||||
local thread = thread_factory(share)
|
||||
thread:wait()
|
||||
|
||||
log "Check values"
|
||||
test.assertString(share["child.id"])
|
||||
test.assertNumber(tonumber(share["child.id"]))
|
||||
test.assertNotEquals(share["child.id"], effil.thread_id())
|
||||
effil.yield() -- just call it
|
||||
|
||||
local function check_time(real_time, use_time, metric)
|
||||
local start_time = os.time()
|
||||
effil.sleep(use_time, metric)
|
||||
test.assertAlmostEquals(os.time(), start_time + real_time, 1)
|
||||
end
|
||||
check_time(4, 4, nil) -- seconds by default
|
||||
check_time(4, 4, 's')
|
||||
check_time(4, 4000, 'ms')
|
||||
check_time(60, 1, 'm')
|
||||
end
|
||||
|
||||
function TestSmoke:testCheckThreadReturns()
|
||||
local effil = require('libeffil')
|
||||
function TestThread:testCheckThreadReturns()
|
||||
local effil = require 'effil'
|
||||
local share = effil.table()
|
||||
share.value = "some value"
|
||||
|
||||
@ -218,50 +130,88 @@ function TestSmoke:testCheckThreadReturns()
|
||||
test.assertEquals(returns[5](11, 89), 100)
|
||||
end
|
||||
|
||||
function TestSmoke:testCheckPairsInterating()
|
||||
local effil = require('libeffil')
|
||||
|
||||
TestThreadWithTable = {tearDown = tearDown }
|
||||
|
||||
function TestThreadWithTable:testSharedTableTypes()
|
||||
local effil = require 'effil'
|
||||
local share = effil.table()
|
||||
local data = { 0, 0, 0, ["key1"] = 0, ["key2"] = 0, ["key3"] = 0 }
|
||||
|
||||
for k, _ in pairs(data) do
|
||||
share[k] = k .. "-value"
|
||||
end
|
||||
share["number"] = 100500
|
||||
share["string"] = "string value"
|
||||
share["bool"] = true
|
||||
share["function"] = function(left, right) return left + right end
|
||||
|
||||
for k,v in pairs(share) do
|
||||
test.assertEquals(data[k], 0)
|
||||
data[k] = 1
|
||||
test.assertEquals(v, k .. "-value")
|
||||
local thread_factory = effil.thread(
|
||||
function(share)
|
||||
share["child.number"] = share["number"]
|
||||
share["child.string"] = share["string"]
|
||||
share["child.bool"] = share["bool"]
|
||||
share["child.function"] = share["function"](11,45)
|
||||
end
|
||||
)
|
||||
local thread = thread_factory(share)
|
||||
thread:wait()
|
||||
|
||||
for k,v in pairs(data) do
|
||||
log("Check: " .. k)
|
||||
test.assertEquals(v, 1)
|
||||
end
|
||||
|
||||
for k,v in ipairs(share) do
|
||||
test.assertEquals(data[k], 1)
|
||||
data[k] = 2
|
||||
test.assertEquals(v, k .. "-value")
|
||||
end
|
||||
|
||||
for k,v in ipairs(data) do
|
||||
log("Check: " .. k)
|
||||
test.assertEquals(v, 2)
|
||||
end
|
||||
end
|
||||
|
||||
function TestSmoke:testCheckLengthOperator()
|
||||
local effil = require('libeffil')
|
||||
local share = effil.table()
|
||||
share[1] = 10
|
||||
share[2] = 20
|
||||
share[3] = 30
|
||||
share[4] = 40
|
||||
share["str"] = 50
|
||||
log "Check values"
|
||||
test.assertEquals(#share, 4)
|
||||
share[3] = nil
|
||||
test.assertEquals(#share, 2)
|
||||
share[1] = nil
|
||||
test.assertEquals(#share, 0)
|
||||
test.assertEquals(share["child.number"], share["number"])
|
||||
test.assertEquals(share["child.string"], share["string"])
|
||||
test.assertEquals(share["child.bool"], share["bool"])
|
||||
test.assertEquals(share["child.function"], share["function"](11,45))
|
||||
end
|
||||
|
||||
function TestThreadWithTable:testRecursiveTables()
|
||||
local effil = require 'effil'
|
||||
local share = effil.table()
|
||||
|
||||
local magic_number = 42
|
||||
share["subtable1"] = effil.table()
|
||||
share["subtable1"]["subtable1"] = effil.table()
|
||||
share["subtable1"]["subtable2"] = share["subtable1"]["subtable1"]
|
||||
share["subtable2"] = share["subtable1"]["subtable1"]
|
||||
share["magic_number"] = magic_number
|
||||
|
||||
local thread_factory = effil.thread(
|
||||
function(share)
|
||||
share["subtable1"]["subtable1"]["magic_number"] = share["magic_number"]
|
||||
share["magic_number"] = nil
|
||||
end
|
||||
)
|
||||
local thread = thread_factory(share)
|
||||
thread:wait()
|
||||
|
||||
log "Check values"
|
||||
test.assertEquals(share["subtable1"]["subtable1"]["magic_number"], magic_number)
|
||||
test.assertEquals(share["subtable1"]["subtable2"]["magic_number"], magic_number)
|
||||
test.assertEquals(share["subtable2"]["magic_number"], magic_number)
|
||||
test.assertEquals(share["magic_number"], nil)
|
||||
end
|
||||
|
||||
function TestThreadWithTable:testThisThreadFunctions()
|
||||
local effil = require 'effil'
|
||||
local share = effil.table()
|
||||
|
||||
local thread_factory = effil.thread(
|
||||
function(share)
|
||||
share["child.id"] = require('libeffil').thread_id()
|
||||
end
|
||||
)
|
||||
local thread = thread_factory(share)
|
||||
thread:wait()
|
||||
|
||||
log "Check values"
|
||||
test.assertString(share["child.id"])
|
||||
test.assertNumber(tonumber(share["child.id"]))
|
||||
test.assertNotEquals(share["child.id"], effil.thread_id())
|
||||
effil.yield() -- just call it
|
||||
|
||||
local function check_time(real_time, use_time, metric)
|
||||
local start_time = os.time()
|
||||
effil.sleep(use_time, metric)
|
||||
test.assertAlmostEquals(os.time(), start_time + real_time, 1)
|
||||
end
|
||||
check_time(4, 4, nil) -- seconds by default
|
||||
check_time(4, 4, 's')
|
||||
check_time(4, 4000, 'ms')
|
||||
check_time(60, 1, 'm')
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user