From 6dadf9adc57ab00a1d5212dcda7f4c5fa9f7ec8b Mon Sep 17 00:00:00 2001 From: nitram96 Date: Wed, 30 Oct 2024 16:59:08 +0100 Subject: [PATCH] Activity now sets _running flag to false when it finishes (#4748) Activity _running flag is now set to false when the activity finishes or throws, previously it was only set if wait had been called. Added test for this scenario --- Foundation/include/Poco/Activity.h | 2 + Foundation/testsuite/src/ActivityTest.cpp | 50 +++++++++++++++++++++++ Foundation/testsuite/src/ActivityTest.h | 1 + 3 files changed, 53 insertions(+) diff --git a/Foundation/include/Poco/Activity.h b/Foundation/include/Poco/Activity.h index 9f5081625..9da1d8a8d 100644 --- a/Foundation/include/Poco/Activity.h +++ b/Foundation/include/Poco/Activity.h @@ -180,9 +180,11 @@ protected: } catch (...) { + _running = false; _done.set(); throw; } + _running = false; _done.set(); } diff --git a/Foundation/testsuite/src/ActivityTest.cpp b/Foundation/testsuite/src/ActivityTest.cpp index 11607f3e1..bd87893f8 100644 --- a/Foundation/testsuite/src/ActivityTest.cpp +++ b/Foundation/testsuite/src/ActivityTest.cpp @@ -55,6 +55,44 @@ namespace Activity _activity; Poco::UInt64 _count; }; + + class BriefActiveObject + { + public: + BriefActiveObject(): + _activity(this, &BriefActiveObject::run), + _count(0) + { + } + + ~BriefActiveObject() + { + } + + Activity& activity() + { + return _activity; + } + + Poco::UInt64 count() const + { + return _count; + } + protected: + void run() + { + while (!_activity.isStopped()) + { + ++_count; + if(_count > 2) + break; + + } + } + private: + Activity _activity; + Poco::UInt64 _count; + }; } @@ -81,6 +119,17 @@ void ActivityTest::testActivity() assertTrue (activeObj.count() > 0); } +void ActivityTest::testActivityFinishes() +{ + BriefActiveObject briefActiveObj; + assertTrue (briefActiveObj.activity().isStopped()); + briefActiveObj.activity().start(); + assertTrue (!briefActiveObj.activity().isStopped()); + Thread::sleep(100); + assertTrue (!briefActiveObj.activity().isRunning()); + assertTrue (briefActiveObj.count() == 3); +} + void ActivityTest::setUp() { @@ -97,6 +146,7 @@ CppUnit::Test* ActivityTest::suite() CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ActivityTest"); CppUnit_addTest(pSuite, ActivityTest, testActivity); + CppUnit_addTest(pSuite, ActivityTest, testActivityFinishes); return pSuite; } diff --git a/Foundation/testsuite/src/ActivityTest.h b/Foundation/testsuite/src/ActivityTest.h index da8bbdb57..7f0779a44 100644 --- a/Foundation/testsuite/src/ActivityTest.h +++ b/Foundation/testsuite/src/ActivityTest.h @@ -25,6 +25,7 @@ public: ~ActivityTest(); void testActivity(); + void testActivityFinishes(); void setUp(); void tearDown();