enh(Poco::Util::Timer): Add idle() method to check if timer has any tasks scheduled #4488

This commit is contained in:
Günter Obiltschnig 2024-03-11 10:06:23 +08:00
parent bf164a85a4
commit 072ee8ff9e
3 changed files with 36 additions and 0 deletions

View File

@ -159,6 +159,9 @@ public:
/// If task execution takes longer than the given interval, /// If task execution takes longer than the given interval,
/// further executions are delayed. /// further executions are delayed.
bool idle() const;
/// Returns true if the task queue is empty, otherwise false.
template <typename Fn> template <typename Fn>
static TimerTask::Ptr func(const Fn& fn) static TimerTask::Ptr func(const Fn& fn)
/// Helper function template to use a functor or lambda /// Helper function template to use a functor or lambda
@ -188,6 +191,15 @@ private:
}; };
//
// inlines
//
inline bool Timer::idle() const
{
return _queue.empty();
}
} } // namespace Poco::Util } } // namespace Poco::Util

View File

@ -314,6 +314,28 @@ void TimerTest::testFunc()
} }
void TimerTest::testIdle()
{
Timer timer;
assertTrue (timer.idle());
Timestamp time;
time += 1000000;
TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
timer.schedule(pTask, time);
assertFalse (timer.idle());
_event.wait();
assertTrue (pTask->lastExecution() >= time);
assertTrue (timer.idle());
}
void TimerTest::setUp() void TimerTest::setUp()
{ {
} }
@ -346,6 +368,7 @@ CppUnit::Test* TimerTest::suite()
CppUnit_addTest(pSuite, TimerTest, testCancelAllWaitStop); CppUnit_addTest(pSuite, TimerTest, testCancelAllWaitStop);
CppUnit_addTest(pSuite, TimerTest, testMultiCancelAllWaitStop); CppUnit_addTest(pSuite, TimerTest, testMultiCancelAllWaitStop);
CppUnit_addTest(pSuite, TimerTest, testFunc); CppUnit_addTest(pSuite, TimerTest, testFunc);
CppUnit_addTest(pSuite, TimerTest, testIdle);
return pSuite; return pSuite;
} }

View File

@ -37,6 +37,7 @@ public:
void testCancelAllWaitStop(); void testCancelAllWaitStop();
void testMultiCancelAllWaitStop(); void testMultiCancelAllWaitStop();
void testFunc(); void testFunc();
void testIdle();
void setUp(); void setUp();
void tearDown(); void tearDown();