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 | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 32 | namespace apache { |
| 33 | namespace thrift { |
| 34 | namespace concurrency { |
| 35 | namespace test { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 36 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 37 | using namespace apache::thrift::concurrency; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 38 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 39 | class ThreadManagerTests { |
| 40 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 41 | static const double TEST_TOLERANCE; |
| 42 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 43 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 44 | class Task : public Runnable { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 45 | |
| 46 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 47 | Task(Monitor& monitor, size_t& count, int64_t timeout) |
| 48 | : _monitor(monitor), _count(count), _timeout(timeout), _done(false) {} |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 49 | |
| 50 | void run() { |
| 51 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 52 | _startTime = Util::currentTime(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 53 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 54 | { |
| 55 | Synchronized s(_sleep); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 56 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 57 | try { |
| 58 | _sleep.wait(_timeout); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 59 | } catch (TimedOutException& e) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 60 | ; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 61 | } catch (...) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 62 | assert(0); |
| 63 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 64 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 65 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 66 | _endTime = Util::currentTime(); |
| 67 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 68 | _done = true; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 69 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 70 | { |
| 71 | Synchronized s(_monitor); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 72 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 73 | // std::cout << "Thread " << _count << " completed " << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 74 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 75 | _count--; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 76 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 77 | if (_count == 0) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 78 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 79 | _monitor.notify(); |
| 80 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 83 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 84 | Monitor& _monitor; |
| 85 | size_t& _count; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 86 | int64_t _timeout; |
| 87 | int64_t _startTime; |
| 88 | int64_t _endTime; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 89 | bool _done; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 90 | Monitor _sleep; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 93 | /** |
| 94 | * Dispatch count tasks, each of which blocks for timeout milliseconds then |
| 95 | * completes. Verify that all tasks completed and that thread manager cleans |
| 96 | * up properly on delete. |
| 97 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 98 | 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] | 99 | |
| 100 | Monitor monitor; |
| 101 | |
| 102 | size_t activeCount = count; |
| 103 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 104 | shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 105 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 106 | shared_ptr<PlatformThreadFactory> threadFactory |
| 107 | = 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 | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 120 | tasks.insert(shared_ptr<ThreadManagerTests::Task>( |
| 121 | new ThreadManagerTests::Task(monitor, activeCount, timeout))); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 124 | int64_t time00 = Util::currentTime(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 125 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 126 | for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); |
| 127 | ix != tasks.end(); |
| 128 | ix++) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 129 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 130 | threadManager->add(*ix); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 133 | { |
| 134 | Synchronized s(monitor); |
| 135 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 136 | while (activeCount > 0) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 137 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 138 | monitor.wait(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 142 | int64_t time01 = Util::currentTime(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 143 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 144 | int64_t firstTime = 9223372036854775807LL; |
| 145 | int64_t lastTime = 0; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 146 | |
| 147 | double averageTime = 0; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 148 | int64_t minTime = 9223372036854775807LL; |
| 149 | int64_t maxTime = 0; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 150 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 151 | for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); |
| 152 | ix != tasks.end(); |
| 153 | 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 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 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 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 182 | std::cout << "\t\t\tfirst start: " << firstTime << "ms Last end: " << lastTime |
| 183 | << "ms min: " << minTime << "ms max: " << maxTime << "ms average: " << averageTime |
| 184 | << "ms" << std::endl; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 185 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 186 | double expectedTime = ((count + (workerCount - 1)) / workerCount) * timeout; |
| 187 | |
| 188 | double error = ((time01 - time00) - expectedTime) / expectedTime; |
| 189 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 190 | if (error < 0) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 191 | error *= -1.0; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 194 | bool success = error < TEST_TOLERANCE; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 195 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 196 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") |
| 197 | << "! expected time: " << expectedTime << "ms elapsed time: " << time01 - time00 |
| 198 | << "ms error%: " << error * 100.0 << std::endl; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 199 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 200 | return success; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 201 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 202 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 203 | class BlockTask : public Runnable { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 204 | |
| 205 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 206 | BlockTask(Monitor& monitor, Monitor& bmonitor, size_t& count) |
| 207 | : _monitor(monitor), _bmonitor(bmonitor), _count(count) {} |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 208 | |
| 209 | void run() { |
| 210 | { |
| 211 | Synchronized s(_bmonitor); |
| 212 | |
| 213 | _bmonitor.wait(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 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 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 237 | bool blockTest(int64_t timeout = 100LL, size_t workerCount = 2) { |
| 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 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 250 | shared_ptr<ThreadManager> threadManager |
| 251 | = ThreadManager::newSimpleThreadManager(workerCount, pendingTaskMaxCount); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 252 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 253 | shared_ptr<PlatformThreadFactory> threadFactory |
| 254 | = shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory()); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 255 | |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 256 | #ifndef USE_BOOST_THREAD |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 257 | threadFactory->setPriority(PosixThreadFactory::HIGHEST); |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 258 | #endif |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 259 | threadManager->threadFactory(threadFactory); |
| 260 | |
| 261 | threadManager->start(); |
| 262 | |
| 263 | std::set<shared_ptr<ThreadManagerTests::BlockTask> > tasks; |
| 264 | |
| 265 | for (size_t ix = 0; ix < workerCount; ix++) { |
| 266 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 267 | tasks.insert(shared_ptr<ThreadManagerTests::BlockTask>( |
| 268 | new ThreadManagerTests::BlockTask(monitor, bmonitor, activeCounts[0]))); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | for (size_t ix = 0; ix < pendingTaskMaxCount; ix++) { |
| 272 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 273 | tasks.insert(shared_ptr<ThreadManagerTests::BlockTask>( |
| 274 | new ThreadManagerTests::BlockTask(monitor, bmonitor, activeCounts[1]))); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 277 | for (std::set<shared_ptr<ThreadManagerTests::BlockTask> >::iterator ix = tasks.begin(); |
| 278 | ix != tasks.end(); |
| 279 | ix++) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 280 | threadManager->add(*ix); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 283 | if (!(success = (threadManager->totalTaskCount() == pendingTaskMaxCount + workerCount))) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 284 | throw TException("Unexpected pending task count"); |
| 285 | } |
| 286 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 287 | shared_ptr<ThreadManagerTests::BlockTask> extraTask( |
| 288 | new ThreadManagerTests::BlockTask(monitor, bmonitor, activeCounts[2])); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 289 | |
| 290 | try { |
| 291 | threadManager->add(extraTask, 1); |
| 292 | throw TException("Unexpected success adding task in excess of pending task count"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 293 | } catch (TooManyPendingTasksException& e) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 294 | throw TException("Should have timed out adding task in excess of pending task count"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 295 | } catch (TimedOutException& e) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 296 | // Expected result |
| 297 | } |
| 298 | |
| 299 | try { |
| 300 | threadManager->add(extraTask, -1); |
| 301 | throw TException("Unexpected success adding task in excess of pending task count"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 302 | } catch (TimedOutException& e) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 303 | throw TException("Unexpected timeout adding task in excess of pending task count"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 304 | } catch (TooManyPendingTasksException& e) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 305 | // Expected result |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 308 | std::cout << "\t\t\t" |
| 309 | << "Pending tasks " << threadManager->pendingTaskCount() << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 310 | |
| 311 | { |
| 312 | Synchronized s(bmonitor); |
| 313 | |
| 314 | bmonitor.notifyAll(); |
| 315 | } |
| 316 | |
| 317 | { |
| 318 | Synchronized s(monitor); |
| 319 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 320 | while (activeCounts[0] != 0) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 321 | monitor.wait(); |
| 322 | } |
| 323 | } |
| 324 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 325 | std::cout << "\t\t\t" |
| 326 | << "Pending tasks " << threadManager->pendingTaskCount() << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 327 | |
| 328 | try { |
| 329 | threadManager->add(extraTask, 1); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 330 | } catch (TimedOutException& e) { |
| 331 | std::cout << "\t\t\t" |
| 332 | << "add timed out unexpectedly" << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 333 | throw TException("Unexpected timeout adding task"); |
| 334 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 335 | } catch (TooManyPendingTasksException& e) { |
| 336 | std::cout << "\t\t\t" |
| 337 | << "add encountered too many pending exepctions" << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 338 | throw TException("Unexpected timeout adding task"); |
| 339 | } |
| 340 | |
| 341 | // Wake up tasks that were pending before and wait for them to complete |
| 342 | |
| 343 | { |
| 344 | Synchronized s(bmonitor); |
| 345 | |
| 346 | bmonitor.notifyAll(); |
| 347 | } |
| 348 | |
| 349 | { |
| 350 | Synchronized s(monitor); |
| 351 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 352 | while (activeCounts[1] != 0) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 353 | monitor.wait(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Wake up the extra task and wait for it to complete |
| 358 | |
| 359 | { |
| 360 | Synchronized s(bmonitor); |
| 361 | |
| 362 | bmonitor.notifyAll(); |
| 363 | } |
| 364 | |
| 365 | { |
| 366 | Synchronized s(monitor); |
| 367 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 368 | while (activeCounts[2] != 0) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 369 | monitor.wait(); |
| 370 | } |
| 371 | } |
| 372 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 373 | if (!(success = (threadManager->totalTaskCount() == 0))) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 374 | throw TException("Unexpected pending task count"); |
| 375 | } |
| 376 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 377 | } catch (TException& e) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 378 | std::cout << "ERROR: " << e.what() << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") << std::endl; |
| 382 | return success; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 383 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 384 | }; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 385 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 386 | const double ThreadManagerTests::TEST_TOLERANCE = .20; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | } // apache::thrift::concurrency |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 391 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 392 | using namespace apache::thrift::concurrency::test; |