Skip to content

Commit

Permalink
enh(Poco::Util::Timer): Add idle() method to check if timer has any t…
Browse files Browse the repository at this point in the history
…asks scheduled #4488
  • Loading branch information
obiltschnig committed Mar 11, 2024
1 parent 9381e6b commit baac5cc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Util/include/Poco/Util/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ class Util_API Timer: protected Poco::Runnable
/// If task execution takes longer than the given interval,
/// further executions are delayed.

bool idle() const;
/// Returns true if the task queue is empty, otherwise false.

template <typename Fn>
static TimerTask::Ptr func(const Fn& fn)
/// Helper function template to use a functor or lambda
Expand Down Expand Up @@ -188,6 +191,15 @@ class Util_API Timer: protected Poco::Runnable
};


//
// inlines
//
inline bool Timer::idle() const
{
return _queue.empty();
}


} } // namespace Poco::Util


Expand Down
23 changes: 23 additions & 0 deletions Util/testsuite/src/TimerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
Expand Down Expand Up @@ -346,6 +368,7 @@ CppUnit::Test* TimerTest::suite()
CppUnit_addTest(pSuite, TimerTest, testCancelAllWaitStop);
CppUnit_addTest(pSuite, TimerTest, testMultiCancelAllWaitStop);
CppUnit_addTest(pSuite, TimerTest, testFunc);
CppUnit_addTest(pSuite, TimerTest, testIdle);

return pSuite;
}
1 change: 1 addition & 0 deletions Util/testsuite/src/TimerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TimerTest: public CppUnit::TestCase
void testCancelAllWaitStop();
void testMultiCancelAllWaitStop();
void testFunc();
void testIdle();

void setUp();
void tearDown();
Expand Down

0 comments on commit baac5cc

Please sign in to comment.