high freq test for Timer

master
Denes Matetelki 14 years ago
parent 675a877def
commit ed594da929

@ -26,6 +26,8 @@ public:
void stopTimer(); void stopTimer();
void gracefulStop();
private: private:
int m_signal; int m_signal;

@ -79,3 +79,10 @@ void Timer::stopTimer()
timer_settime( m_timerId, 0, &its, 0 ); timer_settime( m_timerId, 0, &its, 0 );
m_running = false; m_running = false;
} }
void Timer::gracefulStop()
{
TRACE;
m_running = false;
}

@ -7,6 +7,7 @@
#include "Thread.hpp" #include "Thread.hpp"
#include <signal.h> #include <signal.h>
#include <climits>
class TestTimer : public CxxTest::TestSuite class TestTimer : public CxxTest::TestSuite
{ {
@ -78,9 +79,17 @@ private:
{ {
public: public:
DummyTimerThread(int maxPeriodicCount = 5) DummyTimerThread(const int maxPeriodicCount = INT_MAX - 1,
const time_t interval_sec = 2,
const long interval_nsec = 0,
const time_t initExpr_sec = 0,
const long initExpr_nsec = 0)
: m_counter(0) : m_counter(0)
, m_maxPeriodicCount(maxPeriodicCount) , m_maxPeriodicCount(maxPeriodicCount)
, m_interval_sec(interval_sec)
, m_interval_nsec(interval_nsec)
, m_initExpr_sec(initExpr_sec)
, m_initExpr_nsec(initExpr_nsec)
{ {
TRACE; TRACE;
} }
@ -90,7 +99,10 @@ private:
void* run() void* run()
{ {
TRACE; TRACE;
createTimer(2); createTimer( m_interval_sec,
m_interval_nsec,
m_initExpr_sec,
m_initExpr_nsec);
wait(); wait();
return 0; return 0;
} }
@ -103,9 +115,10 @@ private:
m_counter += 100; m_counter += 100;
} }
void periodicTimerExpired() inline void periodicTimerExpired()
{ {
TRACE; // logging take too much time at high freq
// TRACE;
static int count = 0; static int count = 0;
m_counter++; m_counter++;
count++; count++;
@ -119,8 +132,12 @@ private:
private: private:
int m_maxPeriodicCount; int m_maxPeriodicCount;
time_t m_interval_sec;
long m_interval_nsec;
time_t m_initExpr_sec;
long m_initExpr_nsec;
}; }; // class DummyTimerThread
public: public:
@ -134,7 +151,7 @@ private:
sigaddset( &set, SIGALRM ); sigaddset( &set, SIGALRM );
sigprocmask(SIG_BLOCK, &set, NULL); sigprocmask(SIG_BLOCK, &set, NULL);
DummyTimerThread t; DummyTimerThread t(5);
t.start(); t.start();
sleep(4); sleep(4);
@ -143,4 +160,33 @@ private:
TS_ASSERT_EQUALS( t.m_counter, 100 ); TS_ASSERT_EQUALS( t.m_counter, 100 );
} }
void testTimerThreadHighFreq( void )
{
TEST_HEADER;
// the main thread shall ignore the SIGALRM
sigset_t set;
sigemptyset( &set );
sigaddset( &set, SIGALRM );
sigprocmask(SIG_BLOCK, &set, NULL);
int nano = 1000000000; // 10^9
int freq = 80000;
DummyTimerThread t(INT_MAX - 1,
1, 0,
0, nano / freq );
t.start();
int circle = 10;
sleep( 1 + circle );
t.gracefulStop();
t.join();
// expected 800000 + 100 got 795510
// accurcy: ~ < 99.5%
TS_ASSERT_DELTA ( t.m_counter, 100 + freq * circle, (100 + freq * circle) * 0.995);
TS_ASSERT_EQUALS ( t.m_counter, 100 + freq * circle);
}
}; };

Loading…
Cancel
Save