mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-28 03:20:11 +01:00
Moved work for isue 532 into it's own branch.
This commit is contained in:
122
Foundation/testsuite/src/MutexTest.cpp
Normal file
122
Foundation/testsuite/src/MutexTest.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// 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::FastMutex;
|
||||
using Poco::Runnable;
|
||||
using Poco::SystemException;
|
||||
using Poco::Thread;
|
||||
using Poco::Timestamp;
|
||||
|
||||
namespace
|
||||
{
|
||||
class SelfDeadlockRunnable: public Runnable
|
||||
{
|
||||
public:
|
||||
SelfDeadlockRunnable():
|
||||
_ran(false)
|
||||
{
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
FastMutex mtx;
|
||||
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::testFastMutexRecursion()
|
||||
{
|
||||
FastMutex mtx;
|
||||
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::setUp()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void MutexTest::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
CppUnit::Test* MutexTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MutexTest");
|
||||
|
||||
CppUnit_addTest(pSuite, MutexTest, testFastMutexRecursion);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
40
Foundation/testsuite/src/MutexTest.h
Normal file
40
Foundation/testsuite/src/MutexTest.h
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// 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 testFastMutexRecursion();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
#endif // MutexTest_INCLUDED
|
||||
@@ -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