mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-29 20:59:45 +01:00
Add additional test for Poco::Util::Timer
Test scheduling with Timestamp and Clock Signed-off-by: Pascal Bach <pascal.bach@siemens.com>
This commit is contained in:
@@ -21,6 +21,7 @@ using Poco::Util::Timer;
|
|||||||
using Poco::Util::TimerTask;
|
using Poco::Util::TimerTask;
|
||||||
using Poco::Util::TimerTaskAdapter;
|
using Poco::Util::TimerTaskAdapter;
|
||||||
using Poco::Timestamp;
|
using Poco::Timestamp;
|
||||||
|
using Poco::Clock;
|
||||||
|
|
||||||
|
|
||||||
TimerTest::TimerTest(const std::string& name): CppUnit::TestCase(name)
|
TimerTest::TimerTest(const std::string& name): CppUnit::TestCase(name)
|
||||||
@@ -33,19 +34,41 @@ TimerTest::~TimerTest()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void TimerTest::testSchedule()
|
void TimerTest::testScheduleTimestamp()
|
||||||
{
|
{
|
||||||
Timer timer;
|
Timer timer;
|
||||||
|
|
||||||
Timestamp time;
|
Timestamp time;
|
||||||
time += 1000000;
|
time += 1000000;
|
||||||
|
|
||||||
TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
|
TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
|
||||||
|
|
||||||
assert (pTask->lastExecution() == 0);
|
assert (pTask->lastExecution() == 0);
|
||||||
|
|
||||||
timer.schedule(pTask, time);
|
timer.schedule(pTask, time);
|
||||||
|
|
||||||
|
_event.wait();
|
||||||
|
assert (pTask->lastExecution() >= time);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TimerTest::testScheduleClock()
|
||||||
|
{
|
||||||
|
Timer timer;
|
||||||
|
|
||||||
|
// As reference
|
||||||
|
Timestamp time;
|
||||||
|
time += 1000000;
|
||||||
|
|
||||||
|
Clock clock;
|
||||||
|
clock += 1000000;
|
||||||
|
|
||||||
|
TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
|
||||||
|
|
||||||
|
assert (pTask->lastExecution() == 0);
|
||||||
|
|
||||||
|
timer.schedule(pTask, clock);
|
||||||
|
|
||||||
_event.wait();
|
_event.wait();
|
||||||
assert (pTask->lastExecution() >= time);
|
assert (pTask->lastExecution() >= time);
|
||||||
}
|
}
|
||||||
@@ -54,15 +77,79 @@ void TimerTest::testSchedule()
|
|||||||
void TimerTest::testScheduleInterval()
|
void TimerTest::testScheduleInterval()
|
||||||
{
|
{
|
||||||
Timer timer;
|
Timer timer;
|
||||||
|
|
||||||
Timestamp time;
|
Timestamp time;
|
||||||
|
|
||||||
TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
|
TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
|
||||||
|
|
||||||
assert (pTask->lastExecution() == 0);
|
assert (pTask->lastExecution() == 0);
|
||||||
|
|
||||||
timer.schedule(pTask, 500, 500);
|
timer.schedule(pTask, 500, 500);
|
||||||
|
|
||||||
|
_event.wait();
|
||||||
|
assert (time.elapsed() >= 590000);
|
||||||
|
assert (pTask->lastExecution().elapsed() < 130000);
|
||||||
|
|
||||||
|
_event.wait();
|
||||||
|
assert (time.elapsed() >= 1190000);
|
||||||
|
assert (pTask->lastExecution().elapsed() < 130000);
|
||||||
|
|
||||||
|
_event.wait();
|
||||||
|
assert (time.elapsed() >= 1790000);
|
||||||
|
assert (pTask->lastExecution().elapsed() < 130000);
|
||||||
|
|
||||||
|
pTask->cancel();
|
||||||
|
assert (pTask->isCancelled());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TimerTest::testScheduleIntervalTimestamp()
|
||||||
|
{
|
||||||
|
Timer timer;
|
||||||
|
|
||||||
|
Timestamp time;
|
||||||
|
|
||||||
|
TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
|
||||||
|
|
||||||
|
assert (pTask->lastExecution() == 0);
|
||||||
|
|
||||||
|
Timestamp scheduleTime;
|
||||||
|
scheduleTime += 500 * 1000;
|
||||||
|
|
||||||
|
timer.schedule(pTask, scheduleTime, 500);
|
||||||
|
|
||||||
|
_event.wait();
|
||||||
|
assert (time.elapsed() >= 590000);
|
||||||
|
assert (pTask->lastExecution().elapsed() < 130000);
|
||||||
|
|
||||||
|
_event.wait();
|
||||||
|
assert (time.elapsed() >= 1190000);
|
||||||
|
assert (pTask->lastExecution().elapsed() < 130000);
|
||||||
|
|
||||||
|
_event.wait();
|
||||||
|
assert (time.elapsed() >= 1790000);
|
||||||
|
assert (pTask->lastExecution().elapsed() < 130000);
|
||||||
|
|
||||||
|
pTask->cancel();
|
||||||
|
assert (pTask->isCancelled());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TimerTest::testScheduleIntervalClock()
|
||||||
|
{
|
||||||
|
Timer timer;
|
||||||
|
|
||||||
|
Timestamp time;
|
||||||
|
|
||||||
|
TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
|
||||||
|
|
||||||
|
assert (pTask->lastExecution() == 0);
|
||||||
|
|
||||||
|
Clock scheduleClock;
|
||||||
|
scheduleClock += 500 * 1000;
|
||||||
|
|
||||||
|
timer.schedule(pTask, scheduleClock, 500);
|
||||||
|
|
||||||
_event.wait();
|
_event.wait();
|
||||||
assert (time.elapsed() >= 590000);
|
assert (time.elapsed() >= 590000);
|
||||||
assert (pTask->lastExecution().elapsed() < 130000);
|
assert (pTask->lastExecution().elapsed() < 130000);
|
||||||
@@ -83,15 +170,15 @@ void TimerTest::testScheduleInterval()
|
|||||||
void TimerTest::testScheduleAtFixedRate()
|
void TimerTest::testScheduleAtFixedRate()
|
||||||
{
|
{
|
||||||
Timer timer;
|
Timer timer;
|
||||||
|
|
||||||
Timestamp time;
|
Timestamp time;
|
||||||
|
|
||||||
TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
|
TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
|
||||||
|
|
||||||
assert (pTask->lastExecution() == 0);
|
assert (pTask->lastExecution() == 0);
|
||||||
|
|
||||||
timer.scheduleAtFixedRate(pTask, 500, 500);
|
timer.scheduleAtFixedRate(pTask, 500, 500);
|
||||||
|
|
||||||
_event.wait();
|
_event.wait();
|
||||||
assert (time.elapsed() >= 500000);
|
assert (time.elapsed() >= 500000);
|
||||||
assert (pTask->lastExecution().elapsed() < 130000);
|
assert (pTask->lastExecution().elapsed() < 130000);
|
||||||
@@ -160,8 +247,11 @@ CppUnit::Test* TimerTest::suite()
|
|||||||
{
|
{
|
||||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TimerTest");
|
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TimerTest");
|
||||||
|
|
||||||
CppUnit_addTest(pSuite, TimerTest, testSchedule);
|
CppUnit_addTest(pSuite, TimerTest, testScheduleTimestamp);
|
||||||
|
CppUnit_addTest(pSuite, TimerTest, testScheduleClock);
|
||||||
CppUnit_addTest(pSuite, TimerTest, testScheduleInterval);
|
CppUnit_addTest(pSuite, TimerTest, testScheduleInterval);
|
||||||
|
CppUnit_addTest(pSuite, TimerTest, testScheduleIntervalTimestamp);
|
||||||
|
CppUnit_addTest(pSuite, TimerTest, testScheduleIntervalClock);
|
||||||
CppUnit_addTest(pSuite, TimerTest, testScheduleAtFixedRate);
|
CppUnit_addTest(pSuite, TimerTest, testScheduleAtFixedRate);
|
||||||
CppUnit_addTest(pSuite, TimerTest, testCancel);
|
CppUnit_addTest(pSuite, TimerTest, testCancel);
|
||||||
|
|
||||||
|
|||||||
@@ -28,14 +28,17 @@ public:
|
|||||||
TimerTest(const std::string& name);
|
TimerTest(const std::string& name);
|
||||||
~TimerTest();
|
~TimerTest();
|
||||||
|
|
||||||
void testSchedule();
|
void testScheduleTimestamp();
|
||||||
|
void testScheduleClock();
|
||||||
void testScheduleInterval();
|
void testScheduleInterval();
|
||||||
void testScheduleAtFixedRate();
|
void testScheduleAtFixedRate();
|
||||||
|
void testScheduleIntervalTimestamp();
|
||||||
|
void testScheduleIntervalClock();
|
||||||
void testCancel();
|
void testCancel();
|
||||||
|
|
||||||
void setUp();
|
void setUp();
|
||||||
void tearDown();
|
void tearDown();
|
||||||
|
|
||||||
void onTimer(Poco::Util::TimerTask& task);
|
void onTimer(Poco::Util::TimerTask& task);
|
||||||
|
|
||||||
static CppUnit::Test* suite();
|
static CppUnit::Test* suite();
|
||||||
|
|||||||
Reference in New Issue
Block a user