mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-28 19:51:58 +01:00
Merge pull request #601 from martin-osborne/issue_532-3
Issue #532 - Changes to address `FastMutex` being non-recursive on Win32 platforms
This commit is contained in:
143
Foundation/testsuite/src/MutexTest.cpp
Normal file
143
Foundation/testsuite/src/MutexTest.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
//
|
||||
// MutexTest.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/Foundation/testsuite/src/MutexTest.cpp#1 $
|
||||
//
|
||||
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "MutexTest.h"
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/Mutex.h"
|
||||
#include "Poco/Runnable.h"
|
||||
#include "Poco/Thread.h"
|
||||
#include "Poco/Timestamp.h"
|
||||
|
||||
|
||||
using Poco::Mutex;
|
||||
using Poco::Runnable;
|
||||
using Poco::SystemException;
|
||||
using Poco::Thread;
|
||||
using Poco::Timestamp;
|
||||
|
||||
namespace
|
||||
{
|
||||
class SelfDeadlockRunnable: public Runnable
|
||||
{
|
||||
public:
|
||||
SelfDeadlockRunnable():
|
||||
_ran(false)
|
||||
{
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
Mutex mtx(Mutex::MUTEX_NONRECURSIVE);
|
||||
mtx.lock();
|
||||
mtx.lock();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
_ran = true;
|
||||
}
|
||||
|
||||
bool ran() const
|
||||
{
|
||||
return _ran;
|
||||
}
|
||||
|
||||
private:
|
||||
bool _ran;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
MutexTest::MutexTest(const std::string& name): CppUnit::TestCase(name)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
MutexTest::~MutexTest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void MutexTest::testMutexRecursion()
|
||||
{
|
||||
Mutex mtx(Mutex::MUTEX_NONRECURSIVE);
|
||||
mtx.lock();
|
||||
|
||||
bool success = mtx.tryLock();
|
||||
assert (!success);
|
||||
|
||||
Timestamp mark;
|
||||
success = mtx.tryLock(2000); // Wait 2 seconds
|
||||
assert (!success);
|
||||
|
||||
// Test that we waited approx. 2 sec
|
||||
Timestamp::TimeDiff elapsed = mark.elapsed();
|
||||
assert (elapsed > 1000000);
|
||||
assert (elapsed < 3000000);
|
||||
|
||||
success = mtx.tryLock(0);
|
||||
assert (!success);
|
||||
|
||||
SelfDeadlockRunnable r;
|
||||
Thread t;
|
||||
|
||||
t.start(r);
|
||||
success = t.tryJoin(2000);
|
||||
assert (!success);
|
||||
assert (!r.ran());
|
||||
}
|
||||
|
||||
|
||||
void MutexTest::testRecursiveMutexRecursion()
|
||||
{
|
||||
Mutex mtx;
|
||||
mtx.lock();
|
||||
|
||||
bool success = mtx.tryLock();
|
||||
assert (success);
|
||||
|
||||
Timestamp mark;
|
||||
success = mtx.tryLock(2000); // Wait 2 seconds
|
||||
assert (success);
|
||||
|
||||
// Test that we completed almost immediately
|
||||
Timestamp::TimeDiff elapsed = mark.elapsed();
|
||||
assert (elapsed < 100000);
|
||||
|
||||
success = mtx.tryLock(0);
|
||||
assert (success);
|
||||
}
|
||||
|
||||
void MutexTest::setUp()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void MutexTest::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
CppUnit::Test* MutexTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MutexTest");
|
||||
|
||||
CppUnit_addTest(pSuite, MutexTest, testMutexRecursion);
|
||||
CppUnit_addTest(pSuite, MutexTest, testRecursiveMutexRecursion);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
41
Foundation/testsuite/src/MutexTest.h
Normal file
41
Foundation/testsuite/src/MutexTest.h
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// MutexTest.h
|
||||
//
|
||||
// $Id: //poco/1.4/Foundation/testsuite/src/MutexTest.h#1 $
|
||||
//
|
||||
// Definition of the MutexTest class.
|
||||
//
|
||||
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#ifndef MutexTest_INCLUDED
|
||||
#define MutexTest_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "CppUnit/TestCase.h"
|
||||
|
||||
|
||||
class MutexTest: public CppUnit::TestCase
|
||||
{
|
||||
public:
|
||||
MutexTest(const std::string& name);
|
||||
~MutexTest();
|
||||
|
||||
void testMutexRecursion();
|
||||
void testRecursiveMutexRecursion();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
#endif // MutexTest_INCLUDED
|
||||
@@ -19,12 +19,13 @@
|
||||
#include "Poco/Thread.h"
|
||||
|
||||
|
||||
using Poco::Event;
|
||||
using Poco::ThreadPool;
|
||||
using Poco::RunnableAdapter;
|
||||
using Poco::Thread;
|
||||
|
||||
|
||||
ThreadPoolTest::ThreadPoolTest(const std::string& name): CppUnit::TestCase(name), _event(false)
|
||||
ThreadPoolTest::ThreadPoolTest(const std::string& name): CppUnit::TestCase(name), _event(Event::EVENT_MANUALRESET)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "ThreadingTestSuite.h"
|
||||
#include "ThreadTest.h"
|
||||
#include "MutexTest.h"
|
||||
#include "SemaphoreTest.h"
|
||||
#include "RWLockTest.h"
|
||||
#include "ThreadPoolTest.h"
|
||||
@@ -28,6 +29,7 @@ CppUnit::Test* ThreadingTestSuite::suite()
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ThreadingTestSuite");
|
||||
|
||||
pSuite->addTest(ThreadTest::suite());
|
||||
pSuite->addTest(MutexTest::suite());
|
||||
pSuite->addTest(SemaphoreTest::suite());
|
||||
pSuite->addTest(RWLockTest::suite());
|
||||
pSuite->addTest(ThreadPoolTest::suite());
|
||||
|
||||
Reference in New Issue
Block a user