summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-07-27 19:47:47 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2018-08-02 16:50:49 +0200
commitf9b8a3e6323ef2f4ee5255b16044d966d73a7446 (patch)
treecee5dae52215b5be8cd7d7283fc9cac7267f120d
parent9de89469c4aaba827617a8775c656a4a9e7ac973 (diff)
Asserting the reception of event group:
Change the way to assert the reception of a group of event. To be able to know the attended event count it is better to define it directly when you pass the table of event group. The event name as the key and the number of event that you are waiting for. Also returning the total of received events instead of 1 then it could be asserted to the expected total count. Fix: segfault when manipulating data history values: There was a segfault and random unexpected behavior when attempting to access old event's data. Only the last one was available and trying to access older values lead to memory access violation as the JSON representing the old data has been released since. So it completely mess up the memory and causes unexpected situations. This is simply fix by deep copying the data into another memory space which could be safely accessed afterwards. Changes the way to receive events from monitor api: The old method isn't accurate because it tolds monitor to trace all api request and all push_after event from that api. But we only want event from the api not the others messages coming from the binder which aren't event. Change-Id: Id5aa4a0c79d2adba050ef03d58c45a3db72ec2a2 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--conf.d/controller/lua.d/aft.lua99
-rw-r--r--conf.d/controller/lua.d/mapi_tests.lua2
2 files changed, 57 insertions, 44 deletions
diff --git a/conf.d/controller/lua.d/aft.lua b/conf.d/controller/lua.d/aft.lua
index 1e0109b..d661233 100644
--- a/conf.d/controller/lua.d/aft.lua
+++ b/conf.d/controller/lua.d/aft.lua
@@ -94,15 +94,16 @@ function _AFT.incrementCount(dict)
end
function _AFT.registerData(dict, eventData)
+ local dataCpy = deep_copy(eventData)
if dict.data and type(dict.data) == 'table' then
if _AFT.event_history == true then
- table.insert(dict.data, 1, eventData)
+ table.insert(dict.data, dataCpy)
else
- dict.data[1] = eventData
+ dict.data[1] = dataCpy
end
else
dict.data = {}
- table.insert(dict.data, eventData)
+ table.insert(dict.data, dataCpy)
end
end
@@ -183,18 +184,32 @@ function _AFT.lockWaitGroup(eventGroup, timeout)
print("Error: wrong argument given to wait a group of events. 1st argument should be a table")
return 0
end
- local eventGroupCpy = {table.unpack(eventGroup)}
+ -- Copy and compute the expected as it may have already received events
+ -- you should add the expected count to the actual received counter to be
+ -- accurate.
+ local eventGroupCpy = {}
+ for event,expectedCount in pairs(eventGroup) do
+ eventGroupCpy[event] = expectedCount + _AFT.monitored_events[event].receivedCount
+ end
+
+ local total = 0
+ local matched = nil
while timeout > 0 do
timeout = AFB:lockwait(_AFT.context, timeout)
AFB:lockwait(_AFT.context, 0) --without it _evt_catcher_ cannot received event
- for key,event in pairs(eventGroupCpy) do
- if _AFT.monitored_events[event.name].receivedCount == event.receivedCount + 1 then
- eventGroupCpy[key] = nil
+ for name,expectedCount in pairs(eventGroupCpy) do
+ if _AFT.monitored_events[name].receivedCount >= expectedCount then
+ total = total + _AFT.monitored_events[name].receivedCount
+ matched = name
end
end
- if table_size(eventGroupCpy) == 0 then return 1 end
+ if matched then
+ eventGroupCpy[matched] = nil
+ matched = nil
+ end
+ if table_size(eventGroupCpy) == 0 then return total end
end
return 0
end
@@ -205,67 +220,53 @@ end
function _AFT.assertEvtGrpNotReceived(eventGroup, timeout)
local count = 0
+ local expected = 0
local eventName = ""
- for key,event in pairs(eventGroup) do
- eventGroup[key] = {name = event, receivedCount = _AFT.monitored_events[event].receivedCount}
- end
if timeout then
count = _AFT.lockWaitGroup(eventGroup, timeout)
else
- for _,v in pairs(eventGroup) do
- count = count + v.count
+ for event in pairs(eventGroup) do
+ count = count + _AFT.monitored_events[event].receivedCount
end
end
- for _,event in pairs(eventGroup) do
- eventName = eventName .. " " .. event.name
+ for event,expectedCount in pairs(eventGroup) do
+ eventName = eventName .. " " .. event
+ expected = expected + expectedCount
end
- _AFT.assertIsTrue(count == 0, "One of the following events has been received: '".. eventName .."' but it shouldn't")
+ _AFT.assertIsTrue(count <= expected, "One of the following events has been received: '".. eventName .."' but it shouldn't")
- for _,event in pairs(eventGroup) do
- _AFT.triggerEvtCallback(event.name)
+ for event in pairs(eventGroup) do
+ _AFT.triggerEvtCallback(event)
end
end
function _AFT.assertEvtGrpReceived(eventGroup, timeout)
local count = 0
+ local expected = 0
local eventName = ""
- for key,event in pairs(eventGroup) do
- eventGroup[key] = {name = event, receivedCount = _AFT.monitored_events[event].receivedCount}
- end
if timeout then
count = _AFT.lockWaitGroup(eventGroup, timeout)
else
- for _,v in pairs(eventGroup) do
- count = count + v.receivedCount
+ for event in pairs(eventGroup) do
+ count = count + _AFT.monitored_events[event].receivedCount
end
end
- for _,event in pairs(eventGroup) do
- eventName = eventName .. " " .. event.name
- end
- _AFT.assertIsTrue(count >= table_size(eventGroup), "None or one of the following events: '".. eventName .."' has not been received")
-
- for _,event in pairs(eventGroup) do
- _AFT.triggerEvtCallback(event.name)
+ for event,expectedCount in pairs(eventGroup) do
+ eventName = eventName .. " " .. event
+ expected = expected + expectedCount
end
-end
-function _AFT.testEvtGrpReceived(testName, eventGroup, timeout, setUp, tearDown)
- _AFT.describe(testName, function()
- _AFT.assertEvtGrpReceived(eventGroup, timeout)
- end, setUp, tearDown)
-end
+ _AFT.assertIsTrue(count >= expected, "None or one of the following events: '".. eventName .."' has not been received")
-function _AFT.testEvtGrpNotReceived(testName, eventGroup, timeout, setUp, tearDown)
- _AFT.describe(testName, function()
- _AFT.assertEvtGrpNotReceived(eventGroup, timeout)
- end, setUp, tearDown)
+ for event in pairs(eventGroup) do
+ _AFT.triggerEvtCallback(event)
+ end
end
-
function _AFT.assertEvtNotReceived(eventName, timeout)
local count = _AFT.monitored_events[eventName].receivedCount
if timeout then
@@ -304,6 +305,18 @@ function _AFT.testEvtReceived(testName, eventName, timeout, setUp, tearDown)
end})
end
+function _AFT.testEvtGrpReceived(testName, eventGroup, timeout, setUp, tearDown)
+ _AFT.describe(testName, function()
+ _AFT.assertEvtGrpReceived(eventGroup, timeout)
+ end, setUp, tearDown)
+end
+
+function _AFT.testEvtGrpNotReceived(testName, eventGroup, timeout, setUp, tearDown)
+ _AFT.describe(testName, function()
+ _AFT.assertEvtGrpNotReceived(eventGroup, timeout)
+ end, setUp, tearDown)
+end
+
--[[
Assert function meant to tests API Verbs calls
]]
@@ -580,11 +593,11 @@ function _launch_test(context, args)
-- lua test files to execute in the Framework.
AFB:servsync(_AFT.context, "monitor", "set", { verbosity = "debug" })
if type(args.trace) == "string" then
- AFB:servsync(_AFT.context, "monitor", "trace", { add = { api = args.trace, request = "vverbose", event = "push_after" }})
+ AFB:servsync(_AFT.context, "monitor", "trace", { add = { request = "vverbose", event = "push_after", pattern = args.trace.."/*" }})
elseif type(args.trace) == "table" then
for _,v in pairs(args.trace) do
if type(v) == "string" then
- AFB:servsync(_AFT.context, "monitor", "trace", { add = { api = v, request = "vverbose", event = "push_after" }})
+ AFB:servsync(_AFT.context, "monitor", "trace", { add = { request = "vverbose", event = "push_after", pattern = v.."/*" }})
end
end
end
diff --git a/conf.d/controller/lua.d/mapi_tests.lua b/conf.d/controller/lua.d/mapi_tests.lua
index abd65cb..ced9ea0 100644
--- a/conf.d/controller/lua.d/mapi_tests.lua
+++ b/conf.d/controller/lua.d/mapi_tests.lua
@@ -29,5 +29,5 @@ _AFT.describe("Test_turning_on", function()
_AFT.assertVerb("low-can", "get", {})
- _AFT.assertEvtGrpReceived({evt1, evt2})
+ _AFT.assertEvtGrpReceived({[evt1] = 1, [evt2] = 1})
end)