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
This commit is contained in:
nitram96 2024-10-30 16:59:08 +01:00 committed by GitHub
parent 7df5ec45b8
commit 6dadf9adc5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 0 deletions

View File

@ -180,9 +180,11 @@ protected:
}
catch (...)
{
_running = false;
_done.set();
throw;
}
_running = false;
_done.set();
}

View File

@ -55,6 +55,44 @@ namespace
Activity<ActiveObject> _activity;
Poco::UInt64 _count;
};
class BriefActiveObject
{
public:
BriefActiveObject():
_activity(this, &BriefActiveObject::run),
_count(0)
{
}
~BriefActiveObject()
{
}
Activity<BriefActiveObject>& activity()
{
return _activity;
}
Poco::UInt64 count() const
{
return _count;
}
protected:
void run()
{
while (!_activity.isStopped())
{
++_count;
if(_count > 2)
break;
}
}
private:
Activity<BriefActiveObject> _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;
}

View File

@ -25,6 +25,7 @@ public:
~ActivityTest();
void testActivity();
void testActivityFinishes();
void setUp();
void tearDown();