mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-26 01:36:18 +02:00
test(ThreadPool): unit test for thread pool shutdown when no worker is running. (#2450)
This commit is contained in:
parent
727db08000
commit
24b6af2bd3
@ -14,6 +14,7 @@
|
|||||||
#include "Poco/ThreadPool.h"
|
#include "Poco/ThreadPool.h"
|
||||||
#include "Poco/RunnableAdapter.h"
|
#include "Poco/RunnableAdapter.h"
|
||||||
#include "Poco/Exception.h"
|
#include "Poco/Exception.h"
|
||||||
|
#include "Poco/Timestamp.h"
|
||||||
#include "Poco/Thread.h"
|
#include "Poco/Thread.h"
|
||||||
|
|
||||||
|
|
||||||
@ -126,6 +127,50 @@ void ThreadPoolTest::testThreadPool()
|
|||||||
assertTrue (pool.available() == 4);
|
assertTrue (pool.available() == 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Worker : public Poco::Runnable
|
||||||
|
{
|
||||||
|
static bool _shutDown;
|
||||||
|
public:
|
||||||
|
Worker()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void run()
|
||||||
|
{
|
||||||
|
while (!_shutDown)
|
||||||
|
{
|
||||||
|
Poco::Thread::sleep(200);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void Initialize() {
|
||||||
|
_shutDown = false;
|
||||||
|
}
|
||||||
|
static void Shutdown() {
|
||||||
|
_shutDown = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool Worker::_shutDown = false;
|
||||||
|
|
||||||
|
void ThreadPoolTest::testThreadPoolImmediateShutdown()
|
||||||
|
{
|
||||||
|
Worker::Initialize();
|
||||||
|
|
||||||
|
Worker worker1; // create worker threads
|
||||||
|
Worker worker2;
|
||||||
|
Worker::Shutdown(); // workers will come up, and then immediatly be told to go away
|
||||||
|
|
||||||
|
Poco::Timestamp stime;
|
||||||
|
|
||||||
|
Poco::ThreadPool::defaultPool().start(worker1);
|
||||||
|
Poco::ThreadPool::defaultPool().start(worker2);
|
||||||
|
|
||||||
|
Poco::ThreadPool::defaultPool().joinAll();
|
||||||
|
Poco::Timestamp etime;
|
||||||
|
|
||||||
|
Poco::Timestamp::TimeDiff d = etime - stime;
|
||||||
|
assertTrue (d <= 100000);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void ThreadPoolTest::setUp()
|
void ThreadPoolTest::setUp()
|
||||||
{
|
{
|
||||||
@ -156,6 +201,7 @@ CppUnit::Test* ThreadPoolTest::suite()
|
|||||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ThreadPoolTest");
|
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ThreadPoolTest");
|
||||||
|
|
||||||
CppUnit_addTest(pSuite, ThreadPoolTest, testThreadPool);
|
CppUnit_addTest(pSuite, ThreadPoolTest, testThreadPool);
|
||||||
|
CppUnit_addTest(pSuite, ThreadPoolTest, testThreadPoolImmediateShutdown);
|
||||||
|
|
||||||
return pSuite;
|
return pSuite;
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ public:
|
|||||||
~ThreadPoolTest();
|
~ThreadPoolTest();
|
||||||
|
|
||||||
void testThreadPool();
|
void testThreadPool();
|
||||||
|
void testThreadPoolImmediateShutdown();
|
||||||
|
|
||||||
void setUp();
|
void setUp();
|
||||||
void tearDown();
|
void tearDown();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user