2002-07-11 07:28:37 +04:00
|
|
|
#ifndef _beos_threaded_test_caller_h_
|
|
|
|
#define _beos_threaded_test_caller_h_
|
|
|
|
|
|
|
|
//#include <memory>
|
|
|
|
#include <cppunit/TestCase.h>
|
|
|
|
#include <cppunit/TestResult.h>
|
|
|
|
#include <cppunit/TestCaller.h>
|
2002-07-15 10:53:26 +04:00
|
|
|
#include <TestShell.h>
|
2002-07-11 07:28:37 +04:00
|
|
|
#include <ThreadManager.h>
|
|
|
|
#include <map>
|
2002-07-18 09:32:00 +04:00
|
|
|
#include <vector>
|
|
|
|
#include <stdio.h>
|
2002-07-11 07:28:37 +04:00
|
|
|
|
|
|
|
class TestResult;
|
|
|
|
|
|
|
|
template <class TestClass, class ExpectedException = CppUnit::NoExceptionExpected>
|
2004-02-19 00:38:04 +03:00
|
|
|
class CPPUNIT_API BThreadedTestCaller : public CppUnit::TestCase {
|
2002-07-11 07:28:37 +04:00
|
|
|
public:
|
|
|
|
/*! \brief Pointer to a test function in the given class.
|
|
|
|
Each ThreadMethod added with addThread() is run in its own thread.
|
|
|
|
*/
|
|
|
|
typedef void (TestClass::*ThreadMethod)();
|
|
|
|
|
|
|
|
BThreadedTestCaller(std::string name);
|
|
|
|
BThreadedTestCaller(std::string name, TestClass &object);
|
|
|
|
BThreadedTestCaller(std::string name, TestClass *object);
|
|
|
|
virtual ~BThreadedTestCaller();
|
|
|
|
|
2002-07-21 04:51:08 +04:00
|
|
|
virtual CppUnit::TestResult *run();
|
2002-07-11 07:28:37 +04:00
|
|
|
virtual void run(CppUnit::TestResult *result);
|
|
|
|
|
|
|
|
//! Adds a thread to the test. \c threadName must be unique to this BThreadedTestCaller.
|
|
|
|
void addThread(std::string threadName, ThreadMethod method);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void setUp();
|
|
|
|
virtual void tearDown();
|
|
|
|
virtual std::string toString() const;
|
|
|
|
|
|
|
|
typedef std::map<std::string, BThreadManager<TestClass, ExpectedException> *> ThreadManagerMap;
|
|
|
|
|
|
|
|
bool fOwnObject;
|
|
|
|
TestClass *fObject;
|
2002-07-13 00:30:45 +04:00
|
|
|
ThreadManagerMap fThreads;
|
|
|
|
|
|
|
|
sem_id fThreadSem;
|
|
|
|
|
2002-07-11 07:28:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class TestClass, class ExpectedException>
|
|
|
|
BThreadedTestCaller<TestClass, ExpectedException>::BThreadedTestCaller(std::string name)
|
|
|
|
: TestCase(name)
|
|
|
|
, fOwnObject(true)
|
|
|
|
, fObject(new TestClass())
|
2002-07-13 00:30:45 +04:00
|
|
|
, fThreadSem(-1)
|
2002-07-11 07:28:37 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class TestClass, class ExpectedException>
|
|
|
|
BThreadedTestCaller<TestClass, ExpectedException>::BThreadedTestCaller(std::string name, TestClass &object)
|
|
|
|
: TestCase(name)
|
|
|
|
, fOwnObject(false)
|
|
|
|
, fObject(&object)
|
2002-07-13 00:30:45 +04:00
|
|
|
, fThreadSem(-1)
|
2002-07-11 07:28:37 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class TestClass, class ExpectedException>
|
|
|
|
BThreadedTestCaller<TestClass, ExpectedException>::BThreadedTestCaller(std::string name, TestClass *object)
|
|
|
|
: TestCase(name)
|
|
|
|
, fOwnObject(true)
|
|
|
|
, fObject(object)
|
2002-07-13 00:30:45 +04:00
|
|
|
, fThreadSem(-1)
|
2002-07-11 07:28:37 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class TestClass, class ExpectedException>
|
|
|
|
BThreadedTestCaller<TestClass, ExpectedException>::~BThreadedTestCaller() {
|
|
|
|
if (fOwnObject)
|
|
|
|
delete fObject;
|
|
|
|
for (ThreadManagerMap::iterator it = fThreads.begin(); it != fThreads.end (); ++it) {
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
}
|
2002-07-13 00:30:45 +04:00
|
|
|
|
2002-07-11 07:28:37 +04:00
|
|
|
|
|
|
|
template <class TestClass, class ExpectedException>
|
|
|
|
void
|
|
|
|
BThreadedTestCaller<TestClass, ExpectedException>::addThread(std::string threadName, ThreadMethod method) {
|
|
|
|
if (fThreads.find(threadName) == fThreads.end()) {
|
|
|
|
// Unused name, go ahead and add
|
2002-07-13 00:30:45 +04:00
|
|
|
fThreads[threadName] = new BThreadManager<TestClass, ExpectedException>(threadName, fObject, method, fThreadSem);
|
2002-07-11 07:28:37 +04:00
|
|
|
} else {
|
|
|
|
// Duplicate name, throw an exception
|
|
|
|
throw CppUnit::Exception("BThreadedTestCaller::addThread() - Attempt to add thread under duplicated name ('"
|
|
|
|
+ threadName + "')");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-21 04:51:08 +04:00
|
|
|
template <class TestClass, class ExpectedException>
|
|
|
|
CppUnit::TestResult *
|
|
|
|
BThreadedTestCaller<TestClass, ExpectedException>::run() {
|
|
|
|
CppUnit::TestResult *result = new CppUnit::TestResult;
|
|
|
|
run(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-07-11 07:28:37 +04:00
|
|
|
template <class TestClass, class ExpectedException>
|
|
|
|
void
|
|
|
|
BThreadedTestCaller<TestClass, ExpectedException>::run(CppUnit::TestResult *result) {
|
|
|
|
result->startTest(this);
|
2002-07-13 00:30:45 +04:00
|
|
|
|
|
|
|
if (fThreads.size() <= 0)
|
|
|
|
throw CppUnit::Exception("BThreadedTestCaller::run() -- No threads added to BThreadedTestCaller()");
|
2002-07-11 07:28:37 +04:00
|
|
|
|
|
|
|
try {
|
|
|
|
setUp();
|
|
|
|
|
|
|
|
// This try/catch block should never actually have to catch
|
|
|
|
// anything (unless some bonehead passes in a NULL pointer to
|
|
|
|
// the constructor). Each BThreadManager object catches and
|
|
|
|
// handles exceptions for its respective thread, so as not
|
|
|
|
// to disrupt the others.
|
|
|
|
try {
|
2002-07-13 00:30:45 +04:00
|
|
|
// Create our thread semaphore. This semaphore is used to
|
|
|
|
// determine when all the threads have finished executing,
|
|
|
|
// while still allowing *this* thread to handle printing
|
|
|
|
// out NextSubTest() info (since other threads don't appear
|
|
|
|
// to be able to output text while the main thread is
|
|
|
|
// blocked; their output appears later...).
|
|
|
|
//
|
|
|
|
// Each thread will acquire the semaphore once when launched,
|
|
|
|
// thus the initial thread count is equal the number of threads.
|
|
|
|
fThreadSem = create_sem(fThreads.size(), "ThreadSem");
|
|
|
|
if (fThreadSem < B_OK)
|
|
|
|
throw CppUnit::Exception("BThreadedTestCaller::run() -- Error creating fThreadSem");
|
2002-07-11 07:28:37 +04:00
|
|
|
|
|
|
|
// Launch all the threads.
|
|
|
|
for (ThreadManagerMap::iterator i = fThreads.begin();
|
|
|
|
i != fThreads.end ();
|
|
|
|
++i)
|
|
|
|
{
|
|
|
|
status_t err = i->second->LaunchThread(result);
|
|
|
|
if (err != B_OK)
|
|
|
|
result->addError(this, new CppUnit::Exception("Error launching thread '" + i->second->getName() + "'"));
|
|
|
|
// printf("Launch(%s)\n", i->second->getName().c_str());
|
|
|
|
}
|
|
|
|
|
2002-07-13 00:30:45 +04:00
|
|
|
// Now we loop. Before you faint, there is a reason for this:
|
|
|
|
// Calls to NextSubTest() from other threads don't actually
|
|
|
|
// print anything while the main thread is blocked waiting
|
|
|
|
// for another thread. Thus, we have NextSubTest() add the
|
|
|
|
// information to be printed into a queue. The main thread
|
|
|
|
// (this code right here), blocks on a semaphore that it
|
|
|
|
// can only acquire after all the test threads have terminated.
|
|
|
|
// If it times out, it checks the NextSubTest() queue, prints
|
|
|
|
// any pending updates, and tries to acquire the semaphore
|
|
|
|
// again. When it finally manages to acquire it, all the
|
|
|
|
// test threads have terminated, and it's safe to clean up.
|
|
|
|
|
|
|
|
status_t err;
|
|
|
|
do {
|
|
|
|
// Try to acquire the semaphore
|
|
|
|
err = acquire_sem_etc(fThreadSem, fThreads.size(), B_RELATIVE_TIMEOUT, 500000);
|
|
|
|
|
|
|
|
// Empty the UpdateList
|
|
|
|
std::vector<std::string> &list = fObject->AcquireUpdateList();
|
|
|
|
for (std::vector<std::string>::iterator i = list.begin();
|
|
|
|
i != list.end();
|
|
|
|
i++)
|
|
|
|
{
|
2002-07-17 14:50:55 +04:00
|
|
|
// Only print to standard out if the current global shell
|
|
|
|
// lets us (or if no global shell is designated).
|
2002-07-18 05:03:19 +04:00
|
|
|
if (BTestShell::GlobalBeVerbose()) {
|
2002-07-15 10:53:26 +04:00
|
|
|
printf("%s", (*i).c_str());
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2002-07-13 00:30:45 +04:00
|
|
|
}
|
|
|
|
list.clear();
|
|
|
|
fObject->ReleaseUpdateList();
|
|
|
|
|
|
|
|
} while (err != B_OK);
|
|
|
|
|
|
|
|
// If we get this far, we actually managed to acquire the semaphore,
|
|
|
|
// so we should release it now.
|
|
|
|
release_sem_etc(fThreadSem, fThreads.size(), 0);
|
2002-07-19 10:45:28 +04:00
|
|
|
|
|
|
|
// Print out a newline for asthetics :-)
|
|
|
|
printf("\n");
|
|
|
|
|
2002-07-13 00:30:45 +04:00
|
|
|
/*
|
|
|
|
|
|
|
|
|
2002-07-11 07:28:37 +04:00
|
|
|
// Wait for them all to finish, then clean up
|
|
|
|
for (ThreadManagerMap::iterator i = fThreads.begin();
|
|
|
|
i != fThreads.end ();
|
|
|
|
++i)
|
|
|
|
{
|
|
|
|
// printf("Wait(%s)...", i->second->getName().c_str());
|
|
|
|
fflush(stdout);
|
|
|
|
i->second->WaitForThread();
|
|
|
|
// printf("done\n");
|
|
|
|
delete i->second;
|
|
|
|
}
|
2002-07-13 00:30:45 +04:00
|
|
|
*/
|
|
|
|
|
2002-07-11 07:28:37 +04:00
|
|
|
fThreads.clear();
|
|
|
|
|
|
|
|
} catch ( CppUnit::Exception &e ) {
|
|
|
|
// Add on the a note that this exception was caught by the
|
|
|
|
// thread caller (which is a bad thing), then note the exception
|
|
|
|
CppUnit::Exception *threadException = new CppUnit::Exception(
|
|
|
|
std::string(e.what()) + " (NOTE: caught by BThreadedTestCaller)",
|
|
|
|
e.sourceLine()
|
|
|
|
);
|
|
|
|
result->addFailure( fObject, threadException );
|
|
|
|
}
|
|
|
|
catch ( std::exception &e ) {
|
|
|
|
// Add on the thread name, then note the exception
|
|
|
|
CppUnit::Exception *threadException = new CppUnit::Exception(
|
|
|
|
std::string(e.what()) + " (NOTE: caught by BThreadedTestCaller)"
|
|
|
|
);
|
|
|
|
result->addError( fObject, threadException );
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// Add on the thread name, then note the exception
|
|
|
|
CppUnit::Exception *threadException = new CppUnit::Exception(
|
|
|
|
"caught unknown exception (NOTE: caught by BThreadedTestCaller)"
|
|
|
|
);
|
|
|
|
result->addError( fObject, threadException );
|
|
|
|
}
|
|
|
|
|
|
|
|
snooze(50000);
|
|
|
|
|
|
|
|
try {
|
|
|
|
tearDown();
|
|
|
|
} catch (...) {
|
|
|
|
result->addError(this, new CppUnit::Exception("tearDown() failed"));
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
result->addError(this, new CppUnit::Exception("setUp() failed"));
|
|
|
|
} // setUp() try/catch block
|
|
|
|
|
|
|
|
result->endTest(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class TestClass, class ExpectedException>
|
|
|
|
void
|
|
|
|
BThreadedTestCaller<TestClass, ExpectedException>::setUp() {
|
2002-07-13 00:30:45 +04:00
|
|
|
// Verify we have a valid object that's not currently in use first.
|
|
|
|
if (!fObject)
|
|
|
|
throw CppUnit::Exception("BThreadedTestCaller::runTest() -- NULL fObject pointer");
|
|
|
|
if (!fObject->RegisterForUse())
|
|
|
|
throw CppUnit::Exception("BThreadedTestCaller::runTest() -- Attempt to reuse ThreadedTestCase object already in use");
|
|
|
|
|
2002-07-11 07:28:37 +04:00
|
|
|
fObject->setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class TestClass, class ExpectedException>
|
|
|
|
void
|
|
|
|
BThreadedTestCaller<TestClass, ExpectedException>::tearDown() {
|
|
|
|
fObject->tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class TestClass, class ExpectedException>
|
|
|
|
std::string
|
|
|
|
BThreadedTestCaller<TestClass, ExpectedException>::toString() const {
|
2002-07-19 10:45:28 +04:00
|
|
|
return std::string("BThreadedTestCaller for ") + getName();
|
2002-07-11 07:28:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _beos_threaded_test_caller_h_
|