From ed594da929614fb21a4acd6f4970e40ee4d848ad Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sat, 9 Apr 2011 18:43:50 +0200 Subject: [PATCH] high freq test for Timer --- include/Timer.hpp | 8 ++++--- src/Timer.cpp | 7 ++++++ test/test_Timer.hpp | 58 ++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/include/Timer.hpp b/include/Timer.hpp index 68cf062..16f68ee 100644 --- a/include/Timer.hpp +++ b/include/Timer.hpp @@ -18,14 +18,16 @@ public: void createTimer(const time_t interval_sec, - const long interval_nsec = 0, - const time_t initExpr_sec = 0, - const long initExpr_nsec = 0); + const long interval_nsec = 0, + const time_t initExpr_sec = 0, + const long initExpr_nsec = 0); void wait(); void stopTimer(); + void gracefulStop(); + private: int m_signal; diff --git a/src/Timer.cpp b/src/Timer.cpp index 0e4bcde..e2e7ddb 100644 --- a/src/Timer.cpp +++ b/src/Timer.cpp @@ -79,3 +79,10 @@ void Timer::stopTimer() timer_settime( m_timerId, 0, &its, 0 ); m_running = false; } + +void Timer::gracefulStop() +{ + TRACE; + + m_running = false; +} diff --git a/test/test_Timer.hpp b/test/test_Timer.hpp index 5af4324..fdc9b1e 100644 --- a/test/test_Timer.hpp +++ b/test/test_Timer.hpp @@ -7,6 +7,7 @@ #include "Thread.hpp" #include +#include class TestTimer : public CxxTest::TestSuite { @@ -78,9 +79,17 @@ private: { 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_maxPeriodicCount(maxPeriodicCount) + , m_interval_sec(interval_sec) + , m_interval_nsec(interval_nsec) + , m_initExpr_sec(initExpr_sec) + , m_initExpr_nsec(initExpr_nsec) { TRACE; } @@ -90,7 +99,10 @@ private: void* run() { TRACE; - createTimer(2); + createTimer( m_interval_sec, + m_interval_nsec, + m_initExpr_sec, + m_initExpr_nsec); wait(); return 0; } @@ -103,9 +115,10 @@ private: m_counter += 100; } - void periodicTimerExpired() + inline void periodicTimerExpired() { - TRACE; + // logging take too much time at high freq + // TRACE; static int count = 0; m_counter++; count++; @@ -119,8 +132,12 @@ private: private: int m_maxPeriodicCount; + time_t m_interval_sec; + long m_interval_nsec; + time_t m_initExpr_sec; + long m_initExpr_nsec; - }; + }; // class DummyTimerThread public: @@ -134,7 +151,7 @@ private: sigaddset( &set, SIGALRM ); sigprocmask(SIG_BLOCK, &set, NULL); - DummyTimerThread t; + DummyTimerThread t(5); t.start(); sleep(4); @@ -143,4 +160,33 @@ private: 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); + } + };