Skip to content

Commit

Permalink
Activity now sets _running flag to false when it finishes (#4748)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nitram96 authored Oct 30, 2024
1 parent 7df5ec4 commit 6dadf9a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Foundation/include/Poco/Activity.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ class Activity: public Runnable
}
catch (...)
{
_running = false;
_done.set();
throw;
}
_running = false;
_done.set();
}

Expand Down
50 changes: 50 additions & 0 deletions Foundation/testsuite/src/ActivityTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}


Expand All @@ -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()
{
Expand All @@ -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;
}
1 change: 1 addition & 0 deletions Foundation/testsuite/src/ActivityTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ActivityTest: public CppUnit::TestCase
~ActivityTest();

void testActivity();
void testActivityFinishes();

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

0 comments on commit 6dadf9a

Please sign in to comment.