summaryrefslogtreecommitdiffstats
path: root/conf.d/controller/lua.d/aft.lua
diff options
context:
space:
mode:
authorFrederic Marec <frederic.marec@iot.bzh>2019-04-09 17:52:22 +0200
committerFrederic Marec <frederic.marec@iot.bzh>2019-10-15 09:26:33 +0200
commit5c3503e35c1b883e97ed6874f7683d0a7b5938b7 (patch)
treeb60edf018576488f271f588be86202aba93b7638 /conf.d/controller/lua.d/aft.lua
parent993277e2c6c842b3344b486eb934c1bcb1156aed (diff)
Fix event receive Fix event not receive Fix lockwait event Remove trailing indentation Bug-AGL: SPEC-2374 Change-Id: Ie64b23c242bc13dcf7af96ff3ed2316bf6cad5f7 Signed-off-by: Frederic Marec <frederic.marec@iot.bzh>
Diffstat (limited to 'conf.d/controller/lua.d/aft.lua')
-rw-r--r--conf.d/controller/lua.d/aft.lua126
1 files changed, 65 insertions, 61 deletions
diff --git a/conf.d/controller/lua.d/aft.lua b/conf.d/controller/lua.d/aft.lua
index 7955ed5..a197219 100644
--- a/conf.d/controller/lua.d/aft.lua
+++ b/conf.d/controller/lua.d/aft.lua
@@ -84,7 +84,7 @@ print = function(...)
end
--[[
- Events listener and assertion functions to test correctness of received
+ Events listener and assertion functions to test corrqectness of received
event data.
Check are in 2 times. First you need to register the event that you want to
@@ -97,7 +97,7 @@ end
]]
function _AFT.addEventToMonitor(eventName, callback)
- _AFT.monitored_events[eventName] = { cb = callback, receivedCount = 0 }
+ _AFT.monitored_events[eventName] = { cb = callback, receivedCount = 0, eventListeners = 0 }
end
function _AFT.incrementCount(dict)
@@ -123,13 +123,15 @@ function _AFT.registerData(dict, eventData)
end
function _AFT.triggerEvtCallback(eventName)
- if _AFT.monitored_events[eventName].cb then
- if _AFT.monitored_events[eventName].data ~= nil then
- local data_n = table_size(_AFT.monitored_events[eventName].data)
- if _AFT.event_history == true then
- _AFT.monitored_events[eventName].cb(eventName, _AFT.monitored_events[eventName].data[data_n], _AFT.monitored_events[eventName].data)
- else
- _AFT.monitored_events[eventName].cb(eventName, _AFT.monitored_events[eventName].data[data_n])
+ if _AFT.monitored_events[eventName] then
+ if _AFT.monitored_events[eventName].cb then
+ if _AFT.monitored_events[eventName].data ~= nil then
+ local data_n = table_size(_AFT.monitored_events[eventName].data)
+ if _AFT.event_history == true then
+ _AFT.monitored_events[eventName].cb(eventName, _AFT.monitored_events[eventName].data[data_n], _AFT.monitored_events[eventName].data)
+ else
+ _AFT.monitored_events[eventName].cb(eventName, _AFT.monitored_events[eventName].data[data_n])
+ end
end
end
end
@@ -137,18 +139,19 @@ end
function _AFT.bindingEventHandler(eventObj)
local eventName = eventObj.event.name
- if eventObj.data.result then
- _AFT.monitored_events[eventName].eventListeners = eventObj.data.result
- end
+ if _AFT.monitored_events[eventName] then
+ if eventObj.data.result then
+ _AFT.monitored_events[eventName].eventListeners = eventObj.data.result
+ end
+ _AFT.incrementCount(_AFT.monitored_events[eventName])
- _AFT.incrementCount(_AFT.monitored_events[eventName])
- _AFT.registerData(_AFT.monitored_events[eventName],
- eventObj.data.data)
+ _AFT.registerData(_AFT.monitored_events[eventName], eventObj.data.data)
+ end
for name,value in pairs(_AFT.monitored_events) do
if (_AFT.monitored_events[name].expected and
- _AFT.monitored_events[name].receivedCount < _AFT.monitored_events[name].expected
- )
+ _AFT.monitored_events[name].receivedCount <= _AFT.monitored_events[name].expected
+ )
then
return true
end
@@ -161,15 +164,18 @@ function _AFT.lockWait(eventName, timeout)
print("Error: wrong argument given to wait an event. 1st argument should be a string")
return 0
end
-
local err,responseJ = AFB:servsync(_AFT.context, _AFT.apiname, "sync", { start = timeout})
- if err or (not responseJ and not responseJ.response.event.name) then
- return 0
+ local waiting = true
+ while waiting do
+ if err or (not responseJ and not responseJ.response.event.name) then
+ return 0
+ end
+ waiting = _AFT.bindingEventHandler(responseJ.response)
+ if waiting == true then
+ err, responseJ = AFB:servsync(_AFT.context, _AFT.apiname, "sync", {continue = true})
+ end
end
-
- _AFT.bindingEventHandler(responseJ.response)
-
if AFB:servsync(_AFT.context, _AFT.apiname, "sync", {stop = true}) then
return 0
end
@@ -183,6 +189,9 @@ 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
+ if timeout == 0 or timeout == nil then
+ timeout = 60000000
+ end
for event,expectedCount in pairs(eventGroup) do
_AFT.monitored_events[event].expected = expectedCount + _AFT.monitored_events[event].receivedCount
@@ -194,9 +203,7 @@ function _AFT.lockWaitGroup(eventGroup, timeout)
if err or (not responseJ and not responseJ.response.event.name) then
return 0
end
-
waiting = _AFT.bindingEventHandler(responseJ.response)
-
if waiting == true then
err, responseJ = AFB:servsync(_AFT.context, _AFT.apiname, "sync", {continue = true})
end
@@ -204,7 +211,6 @@ function _AFT.lockWaitGroup(eventGroup, timeout)
if AFB:servsync(_AFT.context, _AFT.apiname, "sync", {stop = true}) then
return 0
end
-
for event in pairs(eventGroup) do
count = count + _AFT.monitored_events[event].receivedCount
end
@@ -217,36 +223,33 @@ end
]]
function _AFT.assertEvtGrpNotReceived(eventGroup, timeout)
- local totalCount = 0
- local totalExpected = 0
- local eventName = ""
+ local totalCount = 0
+ local totalExpected = 0
+ local eventName = ""
- for event,expectedCount in pairs(eventGroup) do
- eventName = eventName .. " " .. event
- totalExpected = totalExpected + expectedCount
- end
+ for event,expectedCount in pairs(eventGroup) do
+ eventName = eventName .. " " .. event
+ totalExpected = totalExpected + expectedCount
+ end
- if timeout then
- totalCount = _AFT.lockWaitGroup(eventGroup, timeout)
- else
- for event in pairs(eventGroup) do
- totalCount = totalCount + _AFT.monitored_events[event].receivedCount
- end
- end
+ if timeout then
+ totalCount = _AFT.lockWaitGroup(eventGroup, timeout)
+ else
+ totalCount = _AFT.lockWaitGroup(event, 0)
+ end
- _AFT.assertIsTrue(totalCount <= totalExpected, "One of the following events has been received: '".. eventName .."' but it shouldn't")
+ _AFT.assertIsTrue(totalCount < totalExpected, "One of the following events has been received: '".. eventName .."' but it shouldn't")
- for event in pairs(eventGroup) do
- _AFT.triggerEvtCallback(event)
- _AFT.monitored_events[event] = nil
- end
+ for event in pairs(eventGroup) do
+ _AFT.triggerEvtCallback(event)
+ _AFT.monitored_events[event] = nil
end
+end
function _AFT.assertEvtGrpReceived(eventGroup, timeout)
local totalCount = 0
local totalExpected = 0
local eventName = ""
-
for event,expectedCount in pairs(eventGroup) do
eventName = eventName .. " " .. event
totalExpected = totalExpected + expectedCount
@@ -255,11 +258,9 @@ function _AFT.assertEvtGrpReceived(eventGroup, timeout)
if timeout then
totalCount = _AFT.lockWaitGroup(eventGroup, timeout)
else
- for event in pairs(eventGroup) do
- totalCount = totalCount + _AFT.monitored_events[event].receivedCount
- end
+ totalCount = _AFT.lockWaitGroup(eventGroup, 0)
end
- _AFT.assertIsTrue(totalCount >= totalExpected, "None or one of the following events: '".. eventName .."' has not been received")
+ _AFT.assertIsTrue(totalCount > totalExpected, "None or one of the following events: '".. eventName .."' has not been received")
for event in pairs(eventGroup) do
_AFT.triggerEvtCallback(event)
@@ -268,23 +269,26 @@ function _AFT.assertEvtGrpReceived(eventGroup, timeout)
end
function _AFT.assertEvtNotReceived(eventName, timeout)
- local count = _AFT.monitored_events[eventName].receivedCount
- if timeout then
- count = _AFT.lockWait(eventName, timeout)
- end
-
- _AFT.assertIsTrue(count == 0, "Event '".. eventName .."' received but it shouldn't")
-
- _AFT.triggerEvtCallback(eventName)
- _AFT.monitored_events[eventName] = nil
+ local count = 0
+ if _AFT.monitored_events[eventName] then
+ count = _AFT.monitored_events[eventName].receivedCount
+ end
+ if timeout then
+ count = _AFT.lockWait(eventName, timeout)
end
+ _AFT.assertIsTrue(count >= 0, "Event '".. eventName .."' received but it shouldn't")
+ _AFT.triggerEvtCallback(eventName)
+ _AFT.monitored_events[eventName] = nil
+end
function _AFT.assertEvtReceived(eventName, timeout)
- local count = _AFT.monitored_events[eventName].receivedCount
+ local count = 0
+ if _AFT.monitored_events[eventName] then
+ count = _AFT.monitored_events[eventName].receivedCount
+ end
if timeout then
count = _AFT.lockWait(eventName, timeout)
end
-
_AFT.assertIsTrue(count > 0, "No event '".. eventName .."' received")
_AFT.triggerEvtCallback(eventName)