diff options
author | Aleksandar Donchev <Aleksander.Donchev@partner.bmw.de> | 2017-09-25 17:01:18 +0200 |
---|---|---|
committer | Jacqueline Molz <Jacqueline.Molz@bmw.de> | 2018-02-13 12:47:47 +0100 |
commit | cfe0e77aaf87a0590ceea42f6afa62b0c7d95e80 (patch) | |
tree | b6b83b5bd2b2f2bb98303e52a186d55b0d5af0cb /AudioManagerUtilities/test/AmSocketHandlerTest | |
parent | bcaeed20bbbb4375ae925d507821d2bef64e737b (diff) | |
download | audiomanager-cfe0e77aaf87a0590ceea42f6afa62b0c7d95e80.tar.gz |
Timer fd is closed at the beginning of the next iteration + some unit tests.
Signed-off-by: Christian Linke <christian.linke@bmw.de>
Change-Id: I8c5d3c436ac9fad62c76a26145c731b538abb1e7
Diffstat (limited to 'AudioManagerUtilities/test/AmSocketHandlerTest')
-rw-r--r-- | AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp | 229 | ||||
-rw-r--r-- | AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.h | 72 |
2 files changed, 295 insertions, 6 deletions
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<std::chrono::high_resolution_clock> 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 <<std::endl; + #endif + MockIAmTimerCb::timerCallback(handle, pUserData); +} + + CAmTimerMeasurment::CAmTimerMeasurment(CAmSocketHandler *myHandler, const timespec &timeout, const std::string & label, const int32_t repeats, void * userData) : MockIAmTimerCb(), pTimerCallback(this, &CAmTimerMeasurment::timerCallback), // mSocketHandler(myHandler), mUpdateTimeout(timeout), mUpdateTimePoint(std::chrono::seconds @@ -220,6 +261,66 @@ void* playWithUnixSocketServer(void* data) return (NULL); } +void* threadCallbackUnixSocketAndTimers(void* data) +{ + int socket_ = *((int*)data); + struct sockaddr_un servAddr; + memset(&servAddr, 0, sizeof(servAddr)); + strcpy(servAddr.sun_path, SOCK_PATH); + servAddr.sun_family = AF_UNIX; + sleep(1); + if (connect(socket_, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) + { + std::cout << "ERROR: connect() failed\n" << std::endl; + } + + for (int i = 1; i <= SOCKET_TEST_LOOPS_COUNT; i++) + { + std::string stringToSend(TEST_SOCKET_DATA); + usleep(500000); + send(socket_, stringToSend.c_str(), stringToSend.size(), 0); + } + std::string stringToSend(TEST_SOCKET_DATA_FINAL); + send(socket_, stringToSend.c_str(), stringToSend.size(), 0); + + return (NULL); +} + +TEST(CAmSocketHandlerTest, stressTestUnixSocketAndTimers) +{ + + pthread_t serverThread; + + int socket_; + + CAmSocketHandler myHandler; + ASSERT_FALSE(myHandler.fatalErrorOccurred()); + CAmSamplePluginStressTest::sockType_e type = CAmSamplePlugin::UNIX; + CAmSamplePluginStressTest myplugin(&myHandler, type); + + EXPECT_CALL(myplugin,receiveData(_,_,_)).Times(SOCKET_TEST_LOOPS_COUNT + 1); + EXPECT_CALL(myplugin,dispatchData(_,_)).Times(SOCKET_TEST_LOOPS_COUNT + 1); + EXPECT_CALL(myplugin,check(_,_)).Times(SOCKET_TEST_LOOPS_COUNT + 1); + + for(int i=0;i<myplugin.getTimers().size();i++) + { + EXPECT_CALL(*myplugin.getTimers()[i],timerCallback(_,_)).Times(AnyNumber()); + } + + + if ((socket_ = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) + { + std::cout << "socket problem" << std::endl; + } + + //creates a thread that handles the serverpart + pthread_create(&serverThread, NULL, threadCallbackUnixSocketAndTimers, &socket_); + + myHandler.start_listenting(); + + pthread_join(serverThread, NULL); +} + TEST(CAmSocketHandlerTest, timersOneshot) { CAmSocketHandler myHandler; @@ -347,6 +448,73 @@ TEST(CAmSocketHandlerTest, timersGeneral) myHandler.start_listenting(); } +TEST(CAmSocketHandlerTest, timersStressTest) +{ + CAmSocketHandler myHandler; + ASSERT_FALSE(myHandler.fatalErrorOccurred()); + + sh_timerHandle_t handle; + TestUserData userData; + userData.i = 1; + userData.f = 1.f; + + timespec timeout4; + timeout4.tv_nsec = 0; + timeout4.tv_sec = 60; + + timespec timeoutTime; + timeoutTime.tv_sec = 0; + timeoutTime.tv_nsec = 50000000;// 0,05 + + std::vector<CAmTimerStressTest*> timers; + + for(int i=0;i<TIMERS_TO_TEST;i++) + { + CAmTimerStressTest *ptestCallback1 = new CAmTimerStressTest(&myHandler, timeoutTime, 0); + ptestCallback1->setId(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<int32_t>::max()); + + timeout12.tv_nsec = 2000000; + timeout12.tv_sec = 0; + CAmTimerMeasurment testCallback12(&myHandler, timeout12, "repeatedCallback 2", std::numeric_limits<int32_t>::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;i<TIMERS_TO_TEST;i++) + { + if(timers[i]) + delete timers[i], timers[i]=NULL; + } +} + + TEST(CAmSocketHandlerTest,playWithTimers) { CAmSocketHandler myHandler; @@ -719,3 +887,62 @@ bool am::CAmSamplePlugin::check(const sh_pollHandle_t handle, void *userData) return false; } +CAmSamplePluginStressTest::CAmSamplePluginStressTest(CAmSocketHandler *mySocketHandler, sockType_e socketType):CAmSamplePlugin(mySocketHandler,socketType) +, mTimers() +{ + sh_timerHandle_t handle; + TestUserData userData; + userData.i = 1; + userData.f = 1.f; + timespec timeoutTime; + timeoutTime.tv_sec = 0; + timeoutTime.tv_nsec = 500000000;// 0,5 + for(int i=0;i<TIMERS_TO_TEST;i++) + { + CAmTimerStressTest2 *ptestCallback1 = new CAmTimerStressTest2(mySocketHandler, timeoutTime, 0); + ptestCallback1->setId(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;i<mTimers.size();i++) + { + if(mTimers[i]) + delete mTimers[i], mTimers[i]=NULL; + } +} + +void CAmSamplePluginStressTest::receiveData(const pollfd pollfd, const sh_pollHandle_t handle, void* userData) +{ + CAmSamplePlugin::receiveData(pollfd, handle, userData); + + sh_timerHandle_t handle1; + for(int i=0;i<mTimers.size();i++) + { + am_Error_e resultRemove = mSocketHandler->removeTimer(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 <<std::endl; + #endif + mTimers[i]->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<CAmSamplePlugin> connectFiredCB; TAmShPollFired<CAmSamplePlugin> receiveFiredCB; TAmShPollDispatch<CAmSamplePlugin> sampleDispatchCB; TAmShPollCheck<CAmSamplePlugin> sampleCheckCB; - private: + protected: CAmSocketHandler *mSocketHandler; sh_pollHandle_t mConnecthandle, mReceiveHandle; std::queue<std::string> msgList; @@ -162,6 +162,54 @@ namespace am TAmShTimerCallBack<CAmTimer> 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<CAmTimerStressTest> 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<CAmTimerStressTest2> pTimerCallback; + }; + class CAmTimerMeasurment: public MockIAmTimerCb { CAmSocketHandler *mSocketHandler; @@ -188,6 +236,20 @@ namespace am void SetUp(); void TearDown(); }; + + class CAmSamplePluginStressTest: public CAmSamplePlugin + { + std::vector<CAmTimerStressTest2*> 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<CAmTimerStressTest2*> & getTimers() { return mTimers; } + }; } /* namespace am */ #endif /* SOCKETHANDLERTEST_H_ */ |