David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 20 | #include <config.h> |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 21 | #include <concurrency/ThreadManager.h> |
| 22 | #include <concurrency/PosixThreadFactory.h> |
| 23 | #include <concurrency/Monitor.h> |
| 24 | #include <concurrency/Util.h> |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 25 | |
| 26 | #include <assert.h> |
| 27 | #include <set> |
| 28 | #include <iostream> |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 29 | #include <set> |
| 30 | #include <stdint.h> |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 31 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 32 | namespace apache { namespace thrift { namespace concurrency { namespace test { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 33 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 34 | using namespace apache::thrift::concurrency; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 35 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 36 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 37 | * ThreadManagerTests class |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 38 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 39 | * @version $Id:$ |
| 40 | */ |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 41 | class ThreadManagerTests { |
| 42 | |
| 43 | public: |
| 44 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 45 | static const double ERROR; |
| 46 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 47 | class Task: public Runnable { |
| 48 | |
| 49 | public: |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 50 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 51 | Task(Monitor& monitor, size_t& count, int64_t timeout) : |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 52 | _monitor(monitor), |
| 53 | _count(count), |
| 54 | _timeout(timeout), |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 55 | _done(false) {} |
| 56 | |
| 57 | void run() { |
| 58 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 59 | _startTime = Util::currentTime(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 60 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 61 | { |
| 62 | Synchronized s(_sleep); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 63 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 64 | try { |
| 65 | _sleep.wait(_timeout); |
| 66 | } catch(TimedOutException& e) { |
| 67 | ; |
| 68 | }catch(...) { |
| 69 | assert(0); |
| 70 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 71 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 72 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 73 | _endTime = Util::currentTime(); |
| 74 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 75 | _done = true; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 76 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 77 | { |
| 78 | Synchronized s(_monitor); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 79 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 80 | // std::cout << "Thread " << _count << " completed " << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 81 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 82 | _count--; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 83 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 84 | if (_count == 0) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 85 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 86 | _monitor.notify(); |
| 87 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 90 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 91 | Monitor& _monitor; |
| 92 | size_t& _count; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 93 | int64_t _timeout; |
| 94 | int64_t _startTime; |
| 95 | int64_t _endTime; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 96 | bool _done; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 97 | Monitor _sleep; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 100 | /** |
| 101 | * Dispatch count tasks, each of which blocks for timeout milliseconds then |
| 102 | * completes. Verify that all tasks completed and that thread manager cleans |
| 103 | * up properly on delete. |
| 104 | */ |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 105 | bool loadTest(size_t count=100, int64_t timeout=100LL, size_t workerCount=4) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 106 | |
| 107 | Monitor monitor; |
| 108 | |
| 109 | size_t activeCount = count; |
| 110 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 111 | shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 112 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 113 | shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 114 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 115 | threadFactory->setPriority(PosixThreadFactory::HIGHEST); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 116 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 117 | threadManager->threadFactory(threadFactory); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 118 | |
| 119 | threadManager->start(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 120 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 121 | std::set<shared_ptr<ThreadManagerTests::Task> > tasks; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 122 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 123 | for (size_t ix = 0; ix < count; ix++) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 124 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 125 | tasks.insert(shared_ptr<ThreadManagerTests::Task>(new ThreadManagerTests::Task(monitor, activeCount, timeout))); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 128 | int64_t time00 = Util::currentTime(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 129 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 130 | for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 131 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 132 | threadManager->add(*ix); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 135 | { |
| 136 | Synchronized s(monitor); |
| 137 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 138 | while(activeCount > 0) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 139 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 140 | monitor.wait(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 144 | int64_t time01 = Util::currentTime(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 145 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 146 | int64_t firstTime = 9223372036854775807LL; |
| 147 | int64_t lastTime = 0; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 148 | |
| 149 | double averageTime = 0; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 150 | int64_t minTime = 9223372036854775807LL; |
| 151 | int64_t maxTime = 0; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 152 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 153 | for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 154 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 155 | shared_ptr<ThreadManagerTests::Task> task = *ix; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 156 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 157 | int64_t delta = task->_endTime - task->_startTime; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 158 | |
| 159 | assert(delta > 0); |
| 160 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 161 | if (task->_startTime < firstTime) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 162 | firstTime = task->_startTime; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 165 | if (task->_endTime > lastTime) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 166 | lastTime = task->_endTime; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 169 | if (delta < minTime) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 170 | minTime = delta; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 173 | if (delta > maxTime) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 174 | maxTime = delta; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | averageTime+= delta; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 178 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 179 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 180 | averageTime /= count; |
| 181 | |
| 182 | std::cout << "\t\t\tfirst start: " << firstTime << "ms Last end: " << lastTime << "ms min: " << minTime << "ms max: " << maxTime << "ms average: " << averageTime << "ms" << std::endl; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 183 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 184 | double expectedTime = ((count + (workerCount - 1)) / workerCount) * timeout; |
| 185 | |
| 186 | double error = ((time01 - time00) - expectedTime) / expectedTime; |
| 187 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 188 | if (error < 0) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 189 | error*= -1.0; |
| 190 | } |
| 191 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 192 | bool success = error < ERROR; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 193 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 194 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "! expected time: " << expectedTime << "ms elapsed time: "<< time01 - time00 << "ms error%: " << error * 100.0 << std::endl; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 195 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 196 | return success; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 197 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 198 | |
| 199 | class BlockTask: public Runnable { |
| 200 | |
| 201 | public: |
| 202 | |
| 203 | BlockTask(Monitor& monitor, Monitor& bmonitor, size_t& count) : |
| 204 | _monitor(monitor), |
| 205 | _bmonitor(bmonitor), |
| 206 | _count(count) {} |
| 207 | |
| 208 | void run() { |
| 209 | { |
| 210 | Synchronized s(_bmonitor); |
| 211 | |
| 212 | _bmonitor.wait(); |
| 213 | |
| 214 | } |
| 215 | |
| 216 | { |
| 217 | Synchronized s(_monitor); |
| 218 | |
| 219 | _count--; |
| 220 | |
| 221 | if (_count == 0) { |
| 222 | |
| 223 | _monitor.notify(); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | Monitor& _monitor; |
| 229 | Monitor& _bmonitor; |
| 230 | size_t& _count; |
| 231 | }; |
| 232 | |
| 233 | /** |
| 234 | * Block test. Create pendingTaskCountMax tasks. Verify that we block adding the |
| 235 | * pendingTaskCountMax + 1th task. Verify that we unblock when a task completes */ |
| 236 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 237 | bool blockTest(int64_t timeout=100LL, size_t workerCount=2) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 238 | (void) timeout; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 239 | bool success = false; |
| 240 | |
| 241 | try { |
| 242 | |
| 243 | Monitor bmonitor; |
| 244 | Monitor monitor; |
| 245 | |
| 246 | size_t pendingTaskMaxCount = workerCount; |
| 247 | |
| 248 | size_t activeCounts[] = {workerCount, pendingTaskMaxCount, 1}; |
| 249 | |
| 250 | shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount, pendingTaskMaxCount); |
| 251 | |
| 252 | shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
| 253 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 254 | threadFactory->setPriority(PosixThreadFactory::HIGHEST); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 255 | |
| 256 | threadManager->threadFactory(threadFactory); |
| 257 | |
| 258 | threadManager->start(); |
| 259 | |
| 260 | std::set<shared_ptr<ThreadManagerTests::BlockTask> > tasks; |
| 261 | |
| 262 | for (size_t ix = 0; ix < workerCount; ix++) { |
| 263 | |
| 264 | tasks.insert(shared_ptr<ThreadManagerTests::BlockTask>(new ThreadManagerTests::BlockTask(monitor, bmonitor,activeCounts[0]))); |
| 265 | } |
| 266 | |
| 267 | for (size_t ix = 0; ix < pendingTaskMaxCount; ix++) { |
| 268 | |
| 269 | tasks.insert(shared_ptr<ThreadManagerTests::BlockTask>(new ThreadManagerTests::BlockTask(monitor, bmonitor,activeCounts[1]))); |
| 270 | } |
| 271 | |
| 272 | for (std::set<shared_ptr<ThreadManagerTests::BlockTask> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 273 | threadManager->add(*ix); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | if(!(success = (threadManager->totalTaskCount() == pendingTaskMaxCount + workerCount))) { |
| 277 | throw TException("Unexpected pending task count"); |
| 278 | } |
| 279 | |
| 280 | shared_ptr<ThreadManagerTests::BlockTask> extraTask(new ThreadManagerTests::BlockTask(monitor, bmonitor, activeCounts[2])); |
| 281 | |
| 282 | try { |
| 283 | threadManager->add(extraTask, 1); |
| 284 | throw TException("Unexpected success adding task in excess of pending task count"); |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 285 | } catch(TooManyPendingTasksException& e) { |
| 286 | throw TException("Should have timed out adding task in excess of pending task count"); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 287 | } catch(TimedOutException& e) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 288 | // Expected result |
| 289 | } |
| 290 | |
| 291 | try { |
| 292 | threadManager->add(extraTask, -1); |
| 293 | throw TException("Unexpected success adding task in excess of pending task count"); |
| 294 | } catch(TimedOutException& e) { |
| 295 | throw TException("Unexpected timeout adding task in excess of pending task count"); |
| 296 | } catch(TooManyPendingTasksException& e) { |
| 297 | // Expected result |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | std::cout << "\t\t\t" << "Pending tasks " << threadManager->pendingTaskCount() << std::endl; |
| 301 | |
| 302 | { |
| 303 | Synchronized s(bmonitor); |
| 304 | |
| 305 | bmonitor.notifyAll(); |
| 306 | } |
| 307 | |
| 308 | { |
| 309 | Synchronized s(monitor); |
| 310 | |
| 311 | while(activeCounts[0] != 0) { |
| 312 | monitor.wait(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | std::cout << "\t\t\t" << "Pending tasks " << threadManager->pendingTaskCount() << std::endl; |
| 317 | |
| 318 | try { |
| 319 | threadManager->add(extraTask, 1); |
| 320 | } catch(TimedOutException& e) { |
| 321 | std::cout << "\t\t\t" << "add timed out unexpectedly" << std::endl; |
| 322 | throw TException("Unexpected timeout adding task"); |
| 323 | |
| 324 | } catch(TooManyPendingTasksException& e) { |
| 325 | std::cout << "\t\t\t" << "add encountered too many pending exepctions" << std::endl; |
| 326 | throw TException("Unexpected timeout adding task"); |
| 327 | } |
| 328 | |
| 329 | // Wake up tasks that were pending before and wait for them to complete |
| 330 | |
| 331 | { |
| 332 | Synchronized s(bmonitor); |
| 333 | |
| 334 | bmonitor.notifyAll(); |
| 335 | } |
| 336 | |
| 337 | { |
| 338 | Synchronized s(monitor); |
| 339 | |
| 340 | while(activeCounts[1] != 0) { |
| 341 | monitor.wait(); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Wake up the extra task and wait for it to complete |
| 346 | |
| 347 | { |
| 348 | Synchronized s(bmonitor); |
| 349 | |
| 350 | bmonitor.notifyAll(); |
| 351 | } |
| 352 | |
| 353 | { |
| 354 | Synchronized s(monitor); |
| 355 | |
| 356 | while(activeCounts[2] != 0) { |
| 357 | monitor.wait(); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if(!(success = (threadManager->totalTaskCount() == 0))) { |
| 362 | throw TException("Unexpected pending task count"); |
| 363 | } |
| 364 | |
| 365 | } catch(TException& e) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 366 | std::cout << "ERROR: " << e.what() << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") << std::endl; |
| 370 | return success; |
| 371 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 372 | }; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 373 | |
| 374 | const double ThreadManagerTests::ERROR = .20; |
| 375 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 376 | }}}} // apache::thrift::concurrency |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 377 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 378 | using namespace apache::thrift::concurrency::test; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 379 | |