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