From 675a877def1c99f1b5f238e0017d1a1fe68b6728 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sat, 9 Apr 2011 16:35:29 +0200 Subject: [PATCH] test_Timer new test for threading --- src/Timer.cpp | 7 ----- test/test_Timer.hpp | 77 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/Timer.cpp b/src/Timer.cpp index 15d1b1e..0e4bcde 100644 --- a/src/Timer.cpp +++ b/src/Timer.cpp @@ -3,13 +3,8 @@ #include "Common.hpp" #include // sigset_t -// #include // assert #include // timer_t -// #include // EINVAL, EAGAIN, EINTR -// #include #include // strerror -// #include -// #include Timer::Timer(const int signal) : m_signal(signal) @@ -38,7 +33,6 @@ void Timer::createTimer(const time_t interval_sec, sigev.sigev_signo = m_signal; sigev.sigev_value.sival_ptr = &m_timerId; if ( timer_create( CLOCK_MONOTONIC, &sigev, &m_timerId ) == -1 ) { -// std::cout << "Error from timer_create: " << strerror(errno) << std::endl; LOG ( Logger::FINEST, "Error from timer_create: " /*strerror(errno)*/ ); return; } @@ -53,7 +47,6 @@ void Timer::createTimer(const time_t interval_sec, if ( initExpr_sec != 0 or initExpr_nsec != 0 ) m_periodic = true; if ( timer_settime( m_timerId, 0, &its, 0 ) == -1 ) { -// std::cout << "Error from timer_settime: " << strerror(errno) << std::endl; LOG ( Logger::FINEST, "Error from timer_settime: " /*strerror(errno)*/ ); return; } diff --git a/test/test_Timer.hpp b/test/test_Timer.hpp index ce19782..5af4324 100644 --- a/test/test_Timer.hpp +++ b/test/test_Timer.hpp @@ -2,14 +2,11 @@ #include "Fixture.hpp" -// #define private public // reach TimerThread's private multimap #include "Timer.hpp" +#include "Thread.hpp" -// #include - - - +#include class TestTimer : public CxxTest::TestSuite { @@ -75,5 +72,75 @@ public: TS_ASSERT_EQUALS( t.m_counter, 105 ); } +private: + + class DummyTimerThread : public Timer, public Thread + { + public: + + DummyTimerThread(int maxPeriodicCount = 5) + : m_counter(0) + , m_maxPeriodicCount(maxPeriodicCount) + { + TRACE; + } + + private: + + void* run() + { + TRACE; + createTimer(2); + wait(); + return 0; + } + + public: + + void timerExpired() + { + TRACE; + m_counter += 100; + } + + void periodicTimerExpired() + { + TRACE; + static int count = 0; + m_counter++; + count++; + if ( count >= m_maxPeriodicCount ) { + stopTimer(); + } + } + + int m_counter; + + private: + + int m_maxPeriodicCount; + + }; + + public: + + void testBasicTimerThread( void ) + { + TEST_HEADER; + + // the main thread shall ignore the SIGALRM + sigset_t set; + sigemptyset( &set ); + sigaddset( &set, SIGALRM ); + sigprocmask(SIG_BLOCK, &set, NULL); + + DummyTimerThread t; + + t.start(); + sleep(4); + t.join(); + + TS_ASSERT_EQUALS( t.m_counter, 100 ); + } };