From 5c3503e35c1b883e97ed6874f7683d0a7b5938b7 Mon Sep 17 00:00:00 2001 From: Frederic Marec Date: Tue, 9 Apr 2019 17:52:22 +0200 Subject: Fix event management in test framework 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 --- conf.d/controller/lua.d/aft.lua | 126 ++++++++++++++++---------------- docs/4_Tests_Examples.md | 3 + src/aft.c | 3 +- test/CMakeLists.txt | 6 +- test/afb-test/fixtures/mapi_low-can.lua | 34 +++++++-- test/afb-test/tests/mapi_tests.lua | 6 +- 6 files changed, 102 insertions(+), 76 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) diff --git a/docs/4_Tests_Examples.md b/docs/4_Tests_Examples.md index caccea4..a68fb9f 100644 --- a/docs/4_Tests_Examples.md +++ b/docs/4_Tests_Examples.md @@ -43,6 +43,8 @@ The example will run some basics tests on API verb calls and events received. _AFT.testVerbStatusSuccess('testEventSub', 'hello', 'eventsub', {tag = 'evt'}) _AFT.testVerbStatusSuccess('testEventPush', 'hello', 'eventpush', {tag = 'evt', data = { key = 'weird others data', another_key = 123.456}}) + _AFT.testVerbStatusSkipped('testEventSub', 'hello', 'eventsub', {tag = 'evt'}) + _AFT.testVerbStatusSuccess('testGenerateWarning', 'hello', 'verbose', {level = 4, message = 'My Warning message!'}) _AFT.testEvtGrpReceived("TestEventGroupReceived",{"hello/anEvent","hello/anotherEvent"},300000) @@ -96,6 +98,7 @@ function _callback(responseJ) _AFT.assertStrContains(responseJ.response, "Some S function _callbackError(responseJ) _AFT.assertStrContains(responseJ.request.info, "Ping Binder Daemon fails") end _AFT.describe("testAssertVerbStatusSuccess",function() _AFT.assertVerbStatusSuccess('hello', 'ping', {}) end) +_AFT.describe("testAssertVerbStatusSkipped",function() _AFT.assertVerbStatusSkipped('hello', 'ping', {}) end) _AFT.describe("testAssertVerbResponseEquals",function() _AFT.assertVerbResponseEquals('hello', 'ping', {},"Some String") end) _AFT.describe("testAssertVerbCb",function() _AFT.assertVerbCb('hello', 'ping', {},_callback) end) _AFT.describe("testAssertVerbStatusError",function() _AFT.assertVerbStatusError('hello', 'pingfail', {}) end) diff --git a/src/aft.c b/src/aft.c index 7d8af8a..14fb003 100644 --- a/src/aft.c +++ b/src/aft.c @@ -39,7 +39,8 @@ static void onTraceEvent(void *closure, const char *event, json_object *data, af */ pthread_mutex_lock(&memo_lock); if(memo_sync) { - afb_req_reply(memo_sync, json_object_get(data), NULL, event);afb_req_unref(memo_sync); + afb_req_reply(memo_sync, json_object_get(data), NULL, event); + afb_req_unref(memo_sync); memo_sync = NULL; } pthread_mutex_unlock(&memo_lock); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d75b1c9..1dbb54d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,7 +1,7 @@ ########################################################################### -# Copyright 2015 - 2018 IoT.bzh +# Copyright 2015 - 2019 IoT.bzh # -# author: Romain Forlot +# author: Forlot Romain # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ # ----------------------------------------------------- PROJECT_SUBDIRS_ADD(${PROJECT_SRC_DIR_PATTERN}) -ADD_TEST(NAME AFB-TEST_TESTS +ADD_TEST(NAME afTest_tests WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND afm-test "${CMAKE_BINARY_DIR}/package" "${CMAKE_BINARY_DIR}/package-test" SERVICE ) diff --git a/test/afb-test/fixtures/mapi_low-can.lua b/test/afb-test/fixtures/mapi_low-can.lua index 9909492..b88fc2f 100644 --- a/test/afb-test/fixtures/mapi_low-can.lua +++ b/test/afb-test/fixtures/mapi_low-can.lua @@ -26,16 +26,34 @@ function _unsubscribe(source, args) AFB:success(source) end +function _evtpush(source, context, val) + local event = val[0] + return AFB:evtpush(source, _evtHandles[event], val[1]) +end + function _get(source, args) - local evtHandle1 = AFB:evtmake(source, 'messages_engine_speed') - local evtHandle2 = AFB:evtmake(source, 'messages_vehicle_speed') - if type(evtHandle1) == "userdata" and type(evtHandle2) == "userdata" then - AFB:subscribe(source, evtHandle1) - AFB:evtpush(source,evtHandle1,{value = 1234}) - AFB:subscribe(source, evtHandle2) - AFB:evtpush(source,evtHandle2,{value = 5678}) - end + _evtHandles = {} + _messageHandles = {} + + _evtHandles['messages_engine_speed'] = nil + _evtHandles['messages_vehicle_speed'] = nil + + _messageHandles['messages_engine_speed'] = 1234 + _messageHandles['messages_vehicle_speed'] = 5678 + + for k,v in pairs(_messageHandles) do + if type(_evtHandles[k]) ~= "userdata" then + _evt = AFB:evtmake(source, k) + _evtHandles[k] = _evt + end + if type(_evtHandles[k]) == "userdata" then + AFB:subscribe(source, _evtHandles[k]) + AFB:timerset(source, {uid="evtpush_"..k, delay=1, count=1}, "_evtpush", {k, v}) + end + end + AFB:success(source) + end function _list(source, args) diff --git a/test/afb-test/tests/mapi_tests.lua b/test/afb-test/tests/mapi_tests.lua index e8cf142..2007c85 100644 --- a/test/afb-test/tests/mapi_tests.lua +++ b/test/afb-test/tests/mapi_tests.lua @@ -34,12 +34,12 @@ end) _AFT.describe("testLockWait",function() - local evt1 = "low-can/messages_engine_speed" + local evt1 = "low-can/messages_engine" + local evt2 = "low-can/messages_engine_not_receive" local timeout = 2000000 _AFT.addEventToMonitor(evt1) local start = os.time() * 1000000 - _AFT.assertEvtNotReceived(evt1, timeout) + _AFT.assertEvtNotReceived(evt2, timeout) local stop = os.time() * 1000000 - _AFT.assertIsTrue( (stop - start) >= timeout, "Timeout not reached, LockWait feature is not working." ) end, nil, nil) -- cgit 1.2.3-korg