blob: aad63320767caadbb0ede59a8689ca6920a963a9 [file] [log] [blame]
Marc Slemko740343d2006-07-20 00:31:02 +00001#include <ThreadManager.h>
2#include <PosixThreadFactory.h>
3#include <Monitor.h>
4#include <Util.h>
5
6#include <assert.h>
7#include <set>
8#include <iostream>
9
10namespace facebook { namespace thrift { namespace concurrency { namespace test {
11
12using namespace facebook::thrift::concurrency;
13
14/** ThreadManagerTests class
15
16 @author marc
17 @version $Id:$ */
18
19class ThreadManagerTests {
20
21public:
22
23 class Task: public Runnable {
24
25 public:
26
27 Task(Monitor& monitor, size_t& count, long long timeout) :
28 _monitor(monitor),
29 _count(count),
30 _timeout(timeout),
31 _addTime(Util::currentTime()),
32 _success(false),
33 _done(false) {}
34
35 void run() {
36
37 _startTime = Util::currentTime();
38
39 Monitor sleep;
40
41 {Synchronized s(sleep);
42
43 sleep.wait(_timeout);
44 }
45
46 _endTime = Util::currentTime();
47
48 _done = true;
49
50 _success = true;
51
52 {Synchronized s(_monitor);
53
54 // std::cout << "Thread " << _count << " completed " << std::endl;
55
56 _count--;
57
58 if(_count == 0) {
59
60 _monitor.notify();
61 }
62 }
63 }
64
65 Monitor& _monitor;
66 size_t& _count;
67 long long _timeout;
68 long long _addTime;
69 long long _startTime;
70 long long _endTime;
71 bool _success;
72 bool _done;
73 };
74
75 /** Dispatch count tasks, each of which blocks for timeout milliseconds then completes.
76 Verify that all tasks completed and that thread manager cleans up properly on delete. */
77
78 bool test00(size_t count=100, long long timeout=100LL, size_t workerCount=4) {
79
80 Monitor monitor;
81
82 size_t activeCount = count;
83
84 ThreadManager* threadManager = ThreadManager::newSimpleThreadManager(workerCount);
85
86 threadManager->threadFactory(new PosixThreadFactory());
87
88 std::set<ThreadManagerTests::Task*> tasks;
89
90 for(size_t ix = 0; ix < count; ix++) {
91
92 tasks.insert(new ThreadManagerTests::Task(monitor, activeCount, timeout));
93 }
94
95 for(std::set<ThreadManagerTests::Task*>::iterator ix = tasks.begin(); ix != tasks.end(); ix++) {
96
97 threadManager->add(*ix);
98 }
99
100 {Synchronized s(monitor);
101
102 while(activeCount > 0) {
103
104 monitor.wait();
105 }
106 }
107
108 bool success;
109
110 for(std::set<ThreadManagerTests::Task*>::iterator ix = tasks.begin(); ix != tasks.end(); ix++) {
111
112 success = success || (*ix)->_success;
113
114 delete *ix;
115
116 }
117
118 delete threadManager;
119
120 std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "!" << std::endl;
121
122 return true;
123 }
124};
125
126}}}} // facebook::thrift::concurrency
127
128using namespace facebook::thrift::concurrency::test;
129