From e2538ac6e21b7ac5f36768bb614fd1caf4cd09e7 Mon Sep 17 00:00:00 2001 From: Pascal Bach Date: Wed, 12 Nov 2014 15:39:39 +0100 Subject: [PATCH] Add additional test for Poco::Util::Timer Test scheduling with Timestamp and Clock Signed-off-by: Pascal Bach --- Util/testsuite/src/TimerTest.cpp | 124 ++++++++++++++++++++++++++----- Util/testsuite/src/TimerTest.h | 7 +- 2 files changed, 112 insertions(+), 19 deletions(-) diff --git a/Util/testsuite/src/TimerTest.cpp b/Util/testsuite/src/TimerTest.cpp index 82afcefb5..5c4d5ae5c 100644 --- a/Util/testsuite/src/TimerTest.cpp +++ b/Util/testsuite/src/TimerTest.cpp @@ -21,6 +21,7 @@ using Poco::Util::Timer; using Poco::Util::TimerTask; using Poco::Util::TimerTaskAdapter; using Poco::Timestamp; +using Poco::Clock; TimerTest::TimerTest(const std::string& name): CppUnit::TestCase(name) @@ -33,19 +34,41 @@ TimerTest::~TimerTest() } -void TimerTest::testSchedule() +void TimerTest::testScheduleTimestamp() { Timer timer; - + Timestamp time; time += 1000000; - + TimerTask::Ptr pTask = new TimerTaskAdapter(*this, &TimerTest::onTimer); - + assert (pTask->lastExecution() == 0); - + 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(*this, &TimerTest::onTimer); + + assert (pTask->lastExecution() == 0); + + timer.schedule(pTask, clock); + _event.wait(); assert (pTask->lastExecution() >= time); } @@ -54,15 +77,79 @@ void TimerTest::testSchedule() void TimerTest::testScheduleInterval() { Timer timer; - + Timestamp time; - + TimerTask::Ptr pTask = new TimerTaskAdapter(*this, &TimerTest::onTimer); - + assert (pTask->lastExecution() == 0); - + 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(*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(*this, &TimerTest::onTimer); + + assert (pTask->lastExecution() == 0); + + Clock scheduleClock; + scheduleClock += 500 * 1000; + + timer.schedule(pTask, scheduleClock, 500); + _event.wait(); assert (time.elapsed() >= 590000); assert (pTask->lastExecution().elapsed() < 130000); @@ -83,15 +170,15 @@ void TimerTest::testScheduleInterval() void TimerTest::testScheduleAtFixedRate() { Timer timer; - + Timestamp time; - + TimerTask::Ptr pTask = new TimerTaskAdapter(*this, &TimerTest::onTimer); - + assert (pTask->lastExecution() == 0); - + timer.scheduleAtFixedRate(pTask, 500, 500); - + _event.wait(); assert (time.elapsed() >= 500000); assert (pTask->lastExecution().elapsed() < 130000); @@ -160,8 +247,11 @@ CppUnit::Test* TimerTest::suite() { 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, testScheduleIntervalTimestamp); + CppUnit_addTest(pSuite, TimerTest, testScheduleIntervalClock); CppUnit_addTest(pSuite, TimerTest, testScheduleAtFixedRate); CppUnit_addTest(pSuite, TimerTest, testCancel); diff --git a/Util/testsuite/src/TimerTest.h b/Util/testsuite/src/TimerTest.h index 3ef669955..6c4d03ef9 100644 --- a/Util/testsuite/src/TimerTest.h +++ b/Util/testsuite/src/TimerTest.h @@ -28,14 +28,17 @@ public: TimerTest(const std::string& name); ~TimerTest(); - void testSchedule(); + void testScheduleTimestamp(); + void testScheduleClock(); void testScheduleInterval(); void testScheduleAtFixedRate(); + void testScheduleIntervalTimestamp(); + void testScheduleIntervalClock(); void testCancel(); void setUp(); void tearDown(); - + void onTimer(Poco::Util::TimerTask& task); static CppUnit::Test* suite();