test_Timer new test for threading

master
Denes Matetelki 14 years ago
parent 03627c0ed5
commit 675a877def

@ -3,13 +3,8 @@
#include "Common.hpp" #include "Common.hpp"
#include <signal.h> // sigset_t #include <signal.h> // sigset_t
// #include <assert.h> // assert
#include <time.h> // timer_t #include <time.h> // timer_t
// #include <errno.h> // EINVAL, EAGAIN, EINTR
// #include <set>
#include <string.h> // strerror #include <string.h> // strerror
// #include <iostream>
// #include <stdio.h>
Timer::Timer(const int signal) Timer::Timer(const int signal)
: m_signal(signal) : m_signal(signal)
@ -38,7 +33,6 @@ void Timer::createTimer(const time_t interval_sec,
sigev.sigev_signo = m_signal; sigev.sigev_signo = m_signal;
sigev.sigev_value.sival_ptr = &m_timerId; sigev.sigev_value.sival_ptr = &m_timerId;
if ( timer_create( CLOCK_MONOTONIC, &sigev, &m_timerId ) == -1 ) { 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)*/ ); LOG ( Logger::FINEST, "Error from timer_create: " /*strerror(errno)*/ );
return; 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 ( initExpr_sec != 0 or initExpr_nsec != 0 ) m_periodic = true;
if ( timer_settime( m_timerId, 0, &its, 0 ) == -1 ) { 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)*/ ); LOG ( Logger::FINEST, "Error from timer_settime: " /*strerror(errno)*/ );
return; return;
} }

@ -2,14 +2,11 @@
#include "Fixture.hpp" #include "Fixture.hpp"
// #define private public // reach TimerThread's private multimap
#include "Timer.hpp" #include "Timer.hpp"
#include "Thread.hpp"
// #include <time.h> #include <signal.h>
class TestTimer : public CxxTest::TestSuite class TestTimer : public CxxTest::TestSuite
{ {
@ -75,5 +72,75 @@ public:
TS_ASSERT_EQUALS( t.m_counter, 105 ); 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 );
}
}; };

Loading…
Cancel
Save