From cfe0e77aaf87a0590ceea42f6afa62b0c7d95e80 Mon Sep 17 00:00:00 2001 From: Aleksandar Donchev Date: Mon, 25 Sep 2017 17:01:18 +0200 Subject: Timer fd is closed at the beginning of the next iteration + some unit tests. Signed-off-by: Christian Linke Change-Id: I8c5d3c436ac9fad62c76a26145c731b538abb1e7 --- AudioManagerUtilities/include/CAmSocketHandler.h | 11 +- AudioManagerUtilities/src/CAmSocketHandler.cpp | 103 ++++++--- .../test/AmSerializerTest/CAmSerializerTest.cpp | 7 +- .../AmSocketHandlerTest/CAmSocketHandlerTest.cpp | 229 ++++++++++++++++++++- .../AmSocketHandlerTest/CAmSocketHandlerTest.h | 72 ++++++- 5 files changed, 378 insertions(+), 44 deletions(-) diff --git a/AudioManagerUtilities/include/CAmSocketHandler.h b/AudioManagerUtilities/include/CAmSocketHandler.h index 717f792..7baa496 100644 --- a/AudioManagerUtilities/include/CAmSocketHandler.h +++ b/AudioManagerUtilities/include/CAmSocketHandler.h @@ -244,10 +244,11 @@ class CAmSocketHandler sh_timer_s() : handle(0) #ifdef WITH_TIMERFD - , fd(0) + , fd(-1) #endif , countdown(), callback(), userData(0) {} + }; struct sh_signal_s @@ -291,7 +292,11 @@ class CAmSocketHandler VectorListPoll_t mListPoll; //! mListTimer; //! mListActiveTimer; //! mListRemovedTimers; +#endif sh_identifier_s mSetSignalhandlerKeys; //!A set of all used signal handler keys VectorSignalHandlers_t mSignalHandlers; bool mRecreatePollfds; //! & listSi * @return E_OK if the descriptor was added, E_NON_EXISTENT if the fd is not valid */ -am_Error_e CAmSocketHandler::addFDPoll(const int fd, const short event, std::function prepare, - std::function fired, std::function check, - std::function dispatch, void* userData, sh_pollHandle_t& handle) +am_Error_e CAmSocketHandler::addFDPoll(const int fd, + const short event, + std::function prepare, + std::function fired, + std::function check, + std::function dispatch, + void* userData, + sh_pollHandle_t& handle) { if (!fdIsValid(fd)) return (E_NON_EXISTENT); @@ -533,7 +548,6 @@ am_Error_e CAmSocketHandler::addTimer(const timespec & timeouts, std::functionbool - { - callback(handle, userData); - return false; - }, - NULL, userData, handle); + err = addFDPoll(timerItem.fd, + POLLIN, + NULL, + actionPoll, + [callback](const sh_pollHandle_t handle, void* userData)->bool{ + callback(handle, userData); + return false; + }, + NULL, + userData, + handle); if (E_OK == err) { timerItem.handle = handle; @@ -608,20 +627,22 @@ am_Error_e CAmSocketHandler::removeTimer(const sh_timerHandle_t handle) if (it == mListTimer.end()) return (E_NON_EXISTENT); - close(it->fd); + mListRemovedTimers.push_back(*it); mListTimer.erase(it); return removeFDPoll(handle); #else stopTimer(handle); std::list::iterator it(mListTimer.begin()); - for (; it != mListTimer.end(); ++it) + while (it != mListTimer.end()) { if (it->handle == handle) { - it = mListTimer.erase(it); + it = mListTimer.erase(it); mSetTimerKeys.pollHandles.erase(handle); return (E_OK); } + else + ++it; } return (E_UNKNOWN); #endif @@ -657,7 +678,7 @@ am_Error_e CAmSocketHandler::updateTimer(const sh_timerHandle_t handle, const ti } else { - if (timerfd_settime(it->fd, 0, &it->countdown, NULL)) + if (timerfd_settime(it->fd, 0, &it->countdown, NULL)<0) { logError("Failed to set timer duration"); return E_NOT_POSSIBLE; @@ -737,7 +758,7 @@ am_Error_e CAmSocketHandler::restartTimer(const sh_timerHandle_t handle) } else { - if (timerfd_settime(it->fd, 0, &it->countdown, NULL)) + if (timerfd_settime(it->fd, 0, &it->countdown, NULL)<0) { logError("Failed to set timer duration"); return E_NOT_POSSIBLE; @@ -811,7 +832,7 @@ am_Error_e CAmSocketHandler::stopTimer(const sh_timerHandle_t handle) countdown.it_value.tv_nsec = 0; countdown.it_value.tv_sec = 0; - if (timerfd_settime(it->fd, 0, &countdown, NULL)) + if (timerfd_settime(it->fd, 0, &countdown, NULL)<0) { logError("Failed to set timer duration"); return E_NOT_POSSIBLE; @@ -820,13 +841,16 @@ am_Error_e CAmSocketHandler::stopTimer(const sh_timerHandle_t handle) #else //go through the list and remove the timer with the handle std::list::iterator it(mListActiveTimer.begin()); - for (; it != mListActiveTimer.end(); ++it) + + while (it != mListActiveTimer.end()) { if (it->handle == handle) { it = mListActiveTimer.erase(it); return (E_OK); } + else + it++; } return (E_NON_EXISTENT); #endif @@ -1031,19 +1055,32 @@ inline timespec* CAmSocketHandler::insertTime(timespec& buffertime) am_Error_e CAmSocketHandler::createTimeFD(const itimerspec & timeouts, int & fd) { fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); - if (fd <= 0) + if (fd < 0) { logError("Failed to create timer"); return E_NOT_POSSIBLE; } - if (timerfd_settime(fd, 0, &timeouts, NULL)) + if (timerfd_settime(fd, 0, &timeouts, NULL) < 0) { logError("Failed to set timer duration"); return E_NOT_POSSIBLE; } return E_OK; } + +void CAmSocketHandler::closeRemovedTimers() +{ + std::list::iterator it(mListRemovedTimers.begin()); + while (it != mListRemovedTimers.end()) + { + if( it->fd > -1 ) + close( it->fd ); + ++it; + } + mListRemovedTimers.clear(); +} + #endif void CAmSocketHandler::callTimer(sh_timer_s& a) diff --git a/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.cpp b/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.cpp index 49c6738..b18b284 100644 --- a/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.cpp +++ b/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.cpp @@ -79,6 +79,8 @@ struct SerializerData V2::CAmSerializer *pSerializer; }; +#define ASYNCLOOP 100 + void* ptSerializerSync(void* data) { SerializerData *pData = (SerializerData*) data; @@ -96,6 +98,7 @@ void* ptSerializerSync(void* data) return (NULL); } + void* ptSerializerASync(void* data) { SerializerData *pData = (SerializerData*) data; @@ -106,7 +109,7 @@ void* ptSerializerASync(void* data) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" - for (uint32_t i = 0; i < 5; i++) + for (uint32_t i = 0; i < ASYNCLOOP; i++) { testStr = pData->testStr; pData->pSerializer->asyncCall(pData->pSerCb, &MockIAmSerializerCb::dispatchData, i, testStr); @@ -191,7 +194,7 @@ TEST(CAmSerializerTest, asyncTest) EXPECT_CALL(serCb,check()).Times(2); EXPECT_CALL(serCb,checkInt()).Times(1).WillRepeatedly(Return(100)); - for (int i = 0; i < 5; i++) + for (int i = 0; i < ASYNCLOOP; i++) EXPECT_CALL(serCb,dispatchData(i,testStr)).WillOnce(DoAll(ActionDispatchData(), Return(true))); myHandler.start_listenting(); diff --git a/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp b/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp index ecd38fe..129f896 100644 --- a/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp +++ b/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp @@ -38,7 +38,8 @@ #define SOCK_PATH "/tmp/mysock" -#define SOCKET_TEST_LOOPS_COUNT 1000 +#define SOCKET_TEST_LOOPS_COUNT 10 +#define TIMERS_TO_TEST 500 using namespace testing; using namespace am; @@ -48,6 +49,11 @@ static const char * TEST_SOCKET_DATA_FINAL = "finish!"; static const std::chrono::time_point TP_ZERO; +struct TestUserData +{ + int i; + float f; +}; MockIAmSignalHandler *pMockSignalHandler = NULL; static void signalHandler(int sig, siginfo_t *siginfo, void *context) @@ -149,6 +155,41 @@ void am::CAmTimer::timerCallback(sh_timerHandle_t handle, void* userData) } } +CAmTimerStressTest::CAmTimerStressTest(CAmSocketHandler *myHandler, const timespec &timeout, const int32_t repeats) : + MockIAmTimerCb(), mpSocketHandler(myHandler), mUpdateTimeout(timeout), pTimerCallback(this, &CAmTimerStressTest::timerCallback), mRepeats(repeats), mId(0), mHandle(0) +{ +} + +am::CAmTimerStressTest::~CAmTimerStressTest() +{ +} + +void am::CAmTimerStressTest::timerCallback(sh_timerHandle_t handle, void* pUserData) +{ + mpSocketHandler->removeTimer(handle); + MockIAmTimerCb::timerCallback(handle, pUserData); + sh_timerHandle_t handle1; + mpSocketHandler->addTimer(mUpdateTimeout, &pTimerCallback, handle1, &(*((TestUserData*)pUserData)), true); +} + +CAmTimerStressTest2::CAmTimerStressTest2(CAmSocketHandler *myHandler, const timespec &timeout, const int32_t repeats) : + MockIAmTimerCb(), mpSocketHandler(myHandler), mUpdateTimeout(timeout), pTimerCallback(this, &CAmTimerStressTest2::timerCallback), mRepeats(repeats), mId(0) +{ +} + +am::CAmTimerStressTest2::~CAmTimerStressTest2() +{ +} + +void am::CAmTimerStressTest2::timerCallback(sh_timerHandle_t handle, void* pUserData) +{ + #ifdef ENABLED_SOCKETHANDLER_TEST_OUTPUT + std::cout<<"timerCallback handle=" << handle < timers; + + for(int i=0;isetId(i); + timers.push_back( ptestCallback1 ); + myHandler.addTimer(timeoutTime, &(ptestCallback1->pTimerCallback), handle, &userData, true); + EXPECT_CALL(*ptestCallback1,timerCallback(_,&userData)).Times(AnyNumber()); + } + + timespec timeoutTime11, timeout12, timeout13; + timeoutTime11.tv_sec = 1; + timeoutTime11.tv_nsec = 34000000; + CAmTimerMeasurment testCallback11(&myHandler, timeoutTime11, "repeatedCallback 1", std::numeric_limits::max()); + + timeout12.tv_nsec = 2000000; + timeout12.tv_sec = 0; + CAmTimerMeasurment testCallback12(&myHandler, timeout12, "repeatedCallback 2", std::numeric_limits::max()); + + timeout13.tv_nsec = 333000000; + timeout13.tv_sec = 3; + CAmTimerMeasurment testCallback13(&myHandler, timeout13, "oneshotCallback 3"); + + myHandler.addTimer(timeoutTime, &testCallback11.pTimerCallback, handle, NULL, true); + EXPECT_CALL(testCallback11,timerCallback(_,NULL)).Times(AnyNumber()); + + myHandler.addTimer(timeout12, &testCallback12.pTimerCallback, handle, NULL, true); + EXPECT_CALL(testCallback12,timerCallback(_,NULL)).Times(AnyNumber()); + + myHandler.addTimer(timeout13, &testCallback13.pTimerCallback, handle, NULL); + EXPECT_CALL(testCallback13,timerCallback(_,NULL)).Times(AnyNumber()); + + + CAmTimerSockethandlerController testCallback4(&myHandler, timeout4); + + myHandler.addTimer(timeout4, &testCallback4.pTimerCallback, handle, NULL); + + EXPECT_CALL(testCallback4,timerCallback(_,NULL)).Times(1); + myHandler.start_listenting(); + + for(int i=0;isetId(i); + if(E_OK==mySocketHandler->addTimer(timeoutTime, &(ptestCallback1->pTimerCallback), handle, &userData, true)) + { + mTimers.push_back( ptestCallback1 ); + ptestCallback1->setHandle(handle); + } + + EXPECT_CALL(*ptestCallback1,timerCallback(_,&userData)).Times(AnyNumber()); + } +} + +CAmSamplePluginStressTest::~CAmSamplePluginStressTest() +{ + for(int i=0;iremoveTimer(mTimers[i]->getHandle()); + am_Error_e resultAdd = mSocketHandler->addTimer(mTimers[i]->getUpdateTimeout(), &(mTimers[i]->pTimerCallback), handle1, NULL, true); + #ifdef ENABLED_SOCKETHANDLER_TEST_OUTPUT + std::cout << "receiveData return removeTimer=" << resultRemove << " return addTimer=" << resultAdd <setHandle(handle1); + } +} + +bool CAmSamplePluginStressTest::dispatchData(const sh_pollHandle_t handle, void* userData) +{ + return CAmSamplePlugin::dispatchData( handle, userData); +} + +bool CAmSamplePluginStressTest::check(const sh_pollHandle_t handle, void* userData) +{ + return CAmSamplePlugin::check( handle, userData); +} + diff --git a/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.h b/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.h index ba2bf51..269e5da 100644 --- a/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.h +++ b/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.h @@ -101,20 +101,20 @@ namespace am UNIX, INET }; CAmSamplePlugin(CAmSocketHandler *mySocketHandler, sockType_e socketType); - ~CAmSamplePlugin() + virtual ~CAmSamplePlugin() { } ; void connectSocket(const pollfd pollfd, const sh_pollHandle_t handle, void* userData); - void receiveData(const pollfd pollfd, const sh_pollHandle_t handle, void* userData); - bool dispatchData(const sh_pollHandle_t handle, void* userData); - bool check(const sh_pollHandle_t handle, void* userData); + virtual void receiveData(const pollfd pollfd, const sh_pollHandle_t handle, void* userData); + virtual bool dispatchData(const sh_pollHandle_t handle, void* userData); + virtual bool check(const sh_pollHandle_t handle, void* userData); TAmShPollFired connectFiredCB; TAmShPollFired receiveFiredCB; TAmShPollDispatch sampleDispatchCB; TAmShPollCheck sampleCheckCB; - private: + protected: CAmSocketHandler *mSocketHandler; sh_pollHandle_t mConnecthandle, mReceiveHandle; std::queue msgList; @@ -162,6 +162,54 @@ namespace am TAmShTimerCallBack pTimerCallback; }; + class CAmTimerStressTest: public MockIAmTimerCb + { + CAmSocketHandler *mpSocketHandler; + timespec mUpdateTimeout; + int32_t mRepeats; + int32_t mId; + int32_t mHandle; + public: + explicit CAmTimerStressTest(CAmSocketHandler *SocketHandler, const timespec &timeout, const int32_t repeats = 0u); + virtual ~CAmTimerStressTest(); + + int32_t getId() { return mId; } + void setId(const int32_t id) { mId=id; } + + int32_t getHandle() { return mHandle; } + void setHandle(const int32_t id) { mHandle=id; } + + timespec getUpdateTimeout( ) { return mUpdateTimeout; } + + void timerCallback(sh_timerHandle_t handle, void * userData); + + TAmShTimerCallBack pTimerCallback; + }; + + class CAmTimerStressTest2: public MockIAmTimerCb + { + CAmSocketHandler *mpSocketHandler; + timespec mUpdateTimeout; + int32_t mRepeats; + int32_t mId; + int32_t mHandle; + public: + explicit CAmTimerStressTest2(CAmSocketHandler *SocketHandler, const timespec &timeout, const int32_t repeats = 0u); + virtual ~CAmTimerStressTest2(); + + int32_t getId() { return mId; } + void setId(const int32_t id) { mId=id; } + + int32_t getHandle() { return mHandle; } + void setHandle(const int32_t id) { mHandle=id; } + + timespec getUpdateTimeout( ) { return mUpdateTimeout; } + + void timerCallback(sh_timerHandle_t handle, void * userData); + + TAmShTimerCallBack pTimerCallback; + }; + class CAmTimerMeasurment: public MockIAmTimerCb { CAmSocketHandler *mSocketHandler; @@ -188,6 +236,20 @@ namespace am void SetUp(); void TearDown(); }; + + class CAmSamplePluginStressTest: public CAmSamplePlugin + { + std::vector mTimers; + public: + CAmSamplePluginStressTest(CAmSocketHandler *mySocketHandler, sockType_e socketType); + ~CAmSamplePluginStressTest(); + + virtual void receiveData(const pollfd pollfd, const sh_pollHandle_t handle, void* userData); + virtual bool dispatchData(const sh_pollHandle_t handle, void* userData); + virtual bool check(const sh_pollHandle_t handle, void* userData); + + std::vector & getTimers() { return mTimers; } + }; } /* namespace am */ #endif /* SOCKETHANDLERTEST_H_ */ -- cgit v1.2.1