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:
Родитель
33998a2156
Коммит
e2538ac6e2
|
@ -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<TimerTest>(*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<TimerTest>(*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<TimerTest>(*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<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();
|
||||
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<TimerTest>(*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);
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
Загрузка…
Ссылка в новой задаче