mirror of
https://github.com/pocoproject/poco.git
synced 2025-11-24 22:29:47 +01:00
added Util::Timer
This commit is contained in:
158
Util/testsuite/src/TimerTest.cpp
Normal file
158
Util/testsuite/src/TimerTest.cpp
Normal file
@@ -0,0 +1,158 @@
|
||||
//
|
||||
// TimerTest.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Util/testsuite/src/TimerTest.cpp#1 $
|
||||
//
|
||||
// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "TimerTest.h"
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/Util/Timer.h"
|
||||
#include "Poco/Util/TimerTaskAdapter.h"
|
||||
|
||||
|
||||
using Poco::Util::Timer;
|
||||
using Poco::Util::TimerTask;
|
||||
using Poco::Util::TimerTaskAdapter;
|
||||
using Poco::Timestamp;
|
||||
|
||||
|
||||
TimerTest::TimerTest(const std::string& name): CppUnit::TestCase(name)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TimerTest::~TimerTest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void TimerTest::testSchedule()
|
||||
{
|
||||
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::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() >= 600000);
|
||||
assert (pTask->lastExecution().elapsed() < 130000);
|
||||
|
||||
_event.wait();
|
||||
assert (time.elapsed() >= 1200000);
|
||||
assert (pTask->lastExecution().elapsed() < 130000);
|
||||
|
||||
_event.wait();
|
||||
assert (time.elapsed() >= 1800000);
|
||||
assert (pTask->lastExecution().elapsed() < 130000);
|
||||
|
||||
pTask->cancel();
|
||||
assert (pTask->isCancelled());
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
_event.wait();
|
||||
assert (time.elapsed() >= 1000000);
|
||||
assert (pTask->lastExecution().elapsed() < 130000);
|
||||
|
||||
_event.wait();
|
||||
assert (time.elapsed() >= 1500000);
|
||||
assert (pTask->lastExecution().elapsed() < 130000);
|
||||
|
||||
pTask->cancel();
|
||||
assert (pTask->isCancelled());
|
||||
}
|
||||
|
||||
|
||||
void TimerTest::setUp()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void TimerTest::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void TimerTest::onTimer(TimerTask& task)
|
||||
{
|
||||
Poco::Thread::sleep(100);
|
||||
_event.set();
|
||||
}
|
||||
|
||||
|
||||
CppUnit::Test* TimerTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TimerTest");
|
||||
|
||||
CppUnit_addTest(pSuite, TimerTest, testSchedule);
|
||||
CppUnit_addTest(pSuite, TimerTest, testScheduleInterval);
|
||||
CppUnit_addTest(pSuite, TimerTest, testScheduleAtFixedRate);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
67
Util/testsuite/src/TimerTest.h
Normal file
67
Util/testsuite/src/TimerTest.h
Normal file
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// TimerTest.h
|
||||
//
|
||||
// $Id: //poco/Main/Util/testsuite/src/TimerTest.h#1 $
|
||||
//
|
||||
// Definition of the TimerTest class.
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#ifndef TimerTest_INCLUDED
|
||||
#define TimerTest_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Util/Util.h"
|
||||
#include "CppUnit/TestCase.h"
|
||||
#include "Poco/Util/TimerTask.h"
|
||||
#include "Poco/Event.h"
|
||||
|
||||
|
||||
class TimerTest: public CppUnit::TestCase
|
||||
{
|
||||
public:
|
||||
TimerTest(const std::string& name);
|
||||
~TimerTest();
|
||||
|
||||
void testSchedule();
|
||||
void testScheduleInterval();
|
||||
void testScheduleAtFixedRate();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
void onTimer(Poco::Util::TimerTask& task);
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
Poco::Event _event;
|
||||
};
|
||||
|
||||
|
||||
#endif // TimerTest_INCLUDED
|
||||
44
Util/testsuite/src/TimerTestSuite.cpp
Normal file
44
Util/testsuite/src/TimerTestSuite.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// TimerTestSuite.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Util/testsuite/src/TimerTestSuite.cpp#1 $
|
||||
//
|
||||
// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "TimerTestSuite.h"
|
||||
#include "TimerTest.h"
|
||||
|
||||
|
||||
CppUnit::Test* TimerTestSuite::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TimerTestSuite");
|
||||
|
||||
pSuite->addTest(TimerTest::suite());
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
49
Util/testsuite/src/TimerTestSuite.h
Normal file
49
Util/testsuite/src/TimerTestSuite.h
Normal file
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// TimerTestSuite.h
|
||||
//
|
||||
// $Id: //poco/Main/Util/testsuite/src/TimerTestSuite.h#1 $
|
||||
//
|
||||
// Definition of the TimerTestSuite class.
|
||||
//
|
||||
// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#ifndef TimerTestSuite_INCLUDED
|
||||
#define TimerTestSuite_INCLUDED
|
||||
|
||||
|
||||
#include "CppUnit/TestSuite.h"
|
||||
|
||||
|
||||
class TimerTestSuite
|
||||
{
|
||||
public:
|
||||
static CppUnit::Test* suite();
|
||||
};
|
||||
|
||||
|
||||
#endif // TimerTestSuite_INCLUDED
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// UtilTestSuite.cpp
|
||||
//
|
||||
// $Id: //poco/1.3/Util/testsuite/src/UtilTestSuite.cpp#4 $
|
||||
// $Id: //poco/Main/Util/testsuite/src/UtilTestSuite.cpp#8 $
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
@@ -33,6 +33,7 @@
|
||||
#include "UtilTestSuite.h"
|
||||
#include "ConfigurationTestSuite.h"
|
||||
#include "OptionsTestSuite.h"
|
||||
#include "TimerTestSuite.h"
|
||||
#if defined(_MSC_VER)
|
||||
#include "WindowsTestSuite.h"
|
||||
#endif
|
||||
@@ -44,6 +45,7 @@ CppUnit::Test* UtilTestSuite::suite()
|
||||
|
||||
pSuite->addTest(ConfigurationTestSuite::suite());
|
||||
pSuite->addTest(OptionsTestSuite::suite());
|
||||
pSuite->addTest(TimerTestSuite::suite());
|
||||
#if defined(_MSC_VER)
|
||||
pSuite->addTest(WindowsTestSuite::suite());
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user