From: Marc Slemko Date: Thu, 20 Jul 2006 00:31:02 +0000 (+0000) Subject: Forgot this one X-Git-Tag: 0.2.0~1745 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=740343dad2583899a2e3751fcf568c1cc9254d48;p=common%2Fthrift.git Forgot this one git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664727 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/concurrency/test/ThreadManagerTests.h b/lib/cpp/src/concurrency/test/ThreadManagerTests.h new file mode 100644 index 00000000..aad63320 --- /dev/null +++ b/lib/cpp/src/concurrency/test/ThreadManagerTests.h @@ -0,0 +1,129 @@ +#include +#include +#include +#include + +#include +#include +#include + +namespace facebook { namespace thrift { namespace concurrency { namespace test { + +using namespace facebook::thrift::concurrency; + +/** ThreadManagerTests class + + @author marc + @version $Id:$ */ + +class ThreadManagerTests { + +public: + + class Task: public Runnable { + + public: + + Task(Monitor& monitor, size_t& count, long long timeout) : + _monitor(monitor), + _count(count), + _timeout(timeout), + _addTime(Util::currentTime()), + _success(false), + _done(false) {} + + void run() { + + _startTime = Util::currentTime(); + + Monitor sleep; + + {Synchronized s(sleep); + + sleep.wait(_timeout); + } + + _endTime = Util::currentTime(); + + _done = true; + + _success = true; + + {Synchronized s(_monitor); + + // std::cout << "Thread " << _count << " completed " << std::endl; + + _count--; + + if(_count == 0) { + + _monitor.notify(); + } + } + } + + Monitor& _monitor; + size_t& _count; + long long _timeout; + long long _addTime; + long long _startTime; + long long _endTime; + bool _success; + bool _done; + }; + + /** Dispatch count tasks, each of which blocks for timeout milliseconds then completes. + Verify that all tasks completed and that thread manager cleans up properly on delete. */ + + bool test00(size_t count=100, long long timeout=100LL, size_t workerCount=4) { + + Monitor monitor; + + size_t activeCount = count; + + ThreadManager* threadManager = ThreadManager::newSimpleThreadManager(workerCount); + + threadManager->threadFactory(new PosixThreadFactory()); + + std::set tasks; + + for(size_t ix = 0; ix < count; ix++) { + + tasks.insert(new ThreadManagerTests::Task(monitor, activeCount, timeout)); + } + + for(std::set::iterator ix = tasks.begin(); ix != tasks.end(); ix++) { + + threadManager->add(*ix); + } + + {Synchronized s(monitor); + + while(activeCount > 0) { + + monitor.wait(); + } + } + + bool success; + + for(std::set::iterator ix = tasks.begin(); ix != tasks.end(); ix++) { + + success = success || (*ix)->_success; + + delete *ix; + + } + + delete threadManager; + + std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "!" << std::endl; + + return true; + } +}; + +}}}} // facebook::thrift::concurrency + +using namespace facebook::thrift::concurrency::test; +