From ef0ae0c644b37b2bcc130b7435941c77fe2bb437 Mon Sep 17 00:00:00 2001 From: Clément Bénier Date: Mon, 9 Jul 2018 18:55:49 +0200 Subject: lockwait: add lua lockwait function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit this function handles to wait for one specific afb event Change-Id: Ief730b6b5f2109379ca0191d98b013d1c9e4225e Signed-off-by: Clément Bénier --- conf.d/controller/lua.d/aft.lua | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/conf.d/controller/lua.d/aft.lua b/conf.d/controller/lua.d/aft.lua index 329cefd..6fd1882 100644 --- a/conf.d/controller/lua.d/aft.lua +++ b/conf.d/controller/lua.d/aft.lua @@ -141,12 +141,26 @@ end Assert and test functions about the event part. ]] -function _AFT.assertEvtNotReceived(eventName) - local count = 0 +function _AFT.lockwait(eventName, timeout) + local count = 0 if _AFT.monitored_events[eventName].receivedCount then - count = _AFT.monitored_events[eventName].receivedCount + if timeout then + count = _AFT.monitored_events[eventName].receivedCount + end end + while timeout > 0 do + timeout = AFB:lockwait(_AFT.context, timeout) + if _AFT.monitored_events[eventName].receivedCount == count + 1 then + return 1 + end + end + return 0 +end + +function _AFT.assertEvtNotReceived(eventName, timeout) + local count = _AFT.lockwait(eventName, timeout) + _AFT.assertIsTrue(count == 0, "Event '".. eventName .."' received but it shouldn't") if _AFT.monitored_events[eventName].cb then @@ -155,11 +169,8 @@ function _AFT.assertEvtNotReceived(eventName) end end -function _AFT.assertEvtReceived(eventName) - local count = 0 - if _AFT.monitored_events[eventName].receivedCount then - count = _AFT.monitored_events[eventName].receivedCount - end +function _AFT.assertEvtReceived(eventName, timeout) + local count = _AFT.lockwait(eventName, timeout) _AFT.assertIsTrue(count > 0, "No event '".. eventName .."' received") @@ -172,8 +183,7 @@ end function _AFT.testEvtNotReceived(testName, eventName, timeout, setUp, tearDown) table.insert(_AFT.tests_list, {testName, function() if _AFT.beforeEach then _AFT.beforeEach() end - if timeout then sleep(timeout) end - _AFT.assertEvtNotReceived(eventName) + _AFT.assertEvtNotReceived(eventName, timeout) if _AFT.afterEach then _AFT.afterEach() end end}) end @@ -181,8 +191,7 @@ end function _AFT.testEvtReceived(testName, eventName, timeout, setUp, tearDown) table.insert(_AFT.tests_list, {testName, function() if _AFT.beforeEach then _AFT.beforeEach() end - if timeout then sleep(timeout) end - _AFT.assertEvtReceived(eventName) + _AFT.assertEvtReceived(eventName, timeout) if _AFT.afterEach then _AFT.afterEach() end end}) end -- cgit 1.2.3-korg