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> |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 22 | #include <thrift/concurrency/ThreadFactory.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 23 | #include <thrift/concurrency/Monitor.h> |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 24 | |
| 25 | #include <assert.h> |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 26 | #include <deque> |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 27 | #include <set> |
| 28 | #include <iostream> |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 29 | #include <stdint.h> |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 30 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 31 | namespace apache { |
| 32 | namespace thrift { |
| 33 | namespace concurrency { |
| 34 | namespace test { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 35 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 36 | using namespace apache::thrift::concurrency; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 37 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 38 | static std::deque<std::shared_ptr<Runnable> > m_expired; |
| 39 | static void expiredNotifier(std::shared_ptr<Runnable> runnable) |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 40 | { |
| 41 | m_expired.push_back(runnable); |
| 42 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 43 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 44 | static void sleep_(int64_t millisec) { |
| 45 | Monitor _sleep; |
| 46 | Synchronized s(_sleep); |
| 47 | |
| 48 | try { |
| 49 | _sleep.wait(millisec); |
| 50 | } catch (TimedOutException&) { |
| 51 | ; |
| 52 | } catch (...) { |
| 53 | assert(0); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | class ThreadManagerTests { |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 58 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 59 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 60 | class Task : public Runnable { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 61 | |
| 62 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 63 | Task(Monitor& monitor, size_t& count, int64_t timeout) |
James E. King, III | 3620090 | 2016-10-05 14:47:18 -0400 | [diff] [blame] | 64 | : _monitor(monitor), _count(count), _timeout(timeout), _startTime(0), _endTime(0), _done(false) {} |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 65 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 66 | void run() override { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 67 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 68 | _startTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 69 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 70 | sleep_(_timeout); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 71 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 72 | _endTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 73 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 74 | _done = true; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 75 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 76 | { |
| 77 | Synchronized s(_monitor); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 78 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 79 | // std::cout << "Thread " << _count << " completed " << '\n'; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 80 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 81 | _count--; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 82 | if (_count % 10000 == 0) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 83 | _monitor.notify(); |
| 84 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 87 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 88 | Monitor& _monitor; |
| 89 | size_t& _count; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 90 | int64_t _timeout; |
| 91 | int64_t _startTime; |
| 92 | int64_t _endTime; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 93 | bool _done; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 94 | Monitor _sleep; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 97 | /** |
| 98 | * Dispatch count tasks, each of which blocks for timeout milliseconds then |
| 99 | * completes. Verify that all tasks completed and that thread manager cleans |
| 100 | * up properly on delete. |
| 101 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 102 | 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] | 103 | |
| 104 | Monitor monitor; |
| 105 | |
| 106 | size_t activeCount = count; |
| 107 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 108 | shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 109 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 110 | shared_ptr<ThreadFactory> threadFactory |
| 111 | = shared_ptr<ThreadFactory>(new ThreadFactory(false)); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 112 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 113 | threadManager->threadFactory(threadFactory); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 114 | |
| 115 | threadManager->start(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 116 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 117 | std::set<shared_ptr<ThreadManagerTests::Task> > tasks; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 118 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 119 | for (size_t ix = 0; ix < count; ix++) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 120 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 121 | tasks.insert(shared_ptr<ThreadManagerTests::Task>( |
| 122 | new ThreadManagerTests::Task(monitor, activeCount, timeout))); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 123 | } |
| 124 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 125 | int64_t time00 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 126 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 127 | for (auto ix = tasks.begin(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 128 | ix != tasks.end(); |
| 129 | ix++) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 130 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 131 | threadManager->add(*ix); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 132 | } |
| 133 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 134 | std::cout << "\t\t\t\tloaded " << count << " tasks to execute" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 135 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 136 | { |
| 137 | Synchronized s(monitor); |
| 138 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 139 | while (activeCount > 0) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 140 | std::cout << "\t\t\t\tactiveCount = " << activeCount << '\n'; |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 141 | monitor.wait(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 145 | int64_t time01 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 146 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 147 | int64_t firstTime = 9223372036854775807LL; |
| 148 | int64_t lastTime = 0; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 149 | |
| 150 | double averageTime = 0; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 151 | int64_t minTime = 9223372036854775807LL; |
| 152 | int64_t maxTime = 0; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 153 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 154 | for (auto ix = tasks.begin(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 155 | ix != tasks.end(); |
| 156 | ix++) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 157 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 158 | shared_ptr<ThreadManagerTests::Task> task = *ix; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 159 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 160 | int64_t delta = task->_endTime - task->_startTime; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 161 | |
| 162 | assert(delta > 0); |
| 163 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 164 | if (task->_startTime < firstTime) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 165 | firstTime = task->_startTime; |
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 (task->_endTime > lastTime) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 169 | lastTime = task->_endTime; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 172 | if (delta < minTime) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 173 | minTime = delta; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 176 | if (delta > maxTime) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 177 | maxTime = delta; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 180 | averageTime += delta; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 181 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 182 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 183 | averageTime /= count; |
| 184 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 185 | std::cout << "\t\t\tfirst start: " << firstTime << " Last end: " << lastTime |
| 186 | << " min: " << minTime << "ms max: " << maxTime << "ms average: " << averageTime |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 187 | << "ms" << '\n'; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 188 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 189 | bool success = (time01 - time00) >= ((int64_t)count * timeout) / (int64_t)workerCount; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 190 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 191 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 192 | << "! expected time: " << ((int64_t)count * timeout) / (int64_t)workerCount << "ms elapsed time: " << time01 - time00 |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 193 | << "ms" << '\n'; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 194 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 195 | return success; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 196 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 197 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 198 | class BlockTask : public Runnable { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 199 | |
| 200 | public: |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 201 | BlockTask(Monitor& entryMonitor, Monitor& blockMonitor, bool& blocked, Monitor& doneMonitor, size_t& count) |
| 202 | : _entryMonitor(entryMonitor), _entered(false), _blockMonitor(blockMonitor), _blocked(blocked), _doneMonitor(doneMonitor), _count(count) {} |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 203 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 204 | void run() override { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 205 | { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 206 | Synchronized s(_entryMonitor); |
| 207 | _entered = true; |
| 208 | _entryMonitor.notify(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 212 | Synchronized s(_blockMonitor); |
| 213 | while (_blocked) { |
| 214 | _blockMonitor.wait(); |
| 215 | } |
| 216 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 217 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 218 | { |
| 219 | Synchronized s(_doneMonitor); |
| 220 | if (--_count == 0) { |
| 221 | _doneMonitor.notify(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 226 | Monitor& _entryMonitor; |
| 227 | bool _entered; |
| 228 | Monitor& _blockMonitor; |
| 229 | bool& _blocked; |
| 230 | Monitor& _doneMonitor; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 231 | size_t& _count; |
| 232 | }; |
| 233 | |
| 234 | /** |
| 235 | * Block test. Create pendingTaskCountMax tasks. Verify that we block adding the |
| 236 | * pendingTaskCountMax + 1th task. Verify that we unblock when a task completes */ |
| 237 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 238 | bool blockTest(int64_t timeout = 100LL, size_t workerCount = 2) { |
| 239 | (void)timeout; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 240 | bool success = false; |
| 241 | |
| 242 | try { |
| 243 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 244 | Monitor entryMonitor; // not used by this test |
| 245 | Monitor blockMonitor; |
| 246 | bool blocked[] = {true, true, true}; |
| 247 | Monitor doneMonitor; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 248 | |
| 249 | size_t pendingTaskMaxCount = workerCount; |
| 250 | |
| 251 | size_t activeCounts[] = {workerCount, pendingTaskMaxCount, 1}; |
| 252 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 253 | shared_ptr<ThreadManager> threadManager |
| 254 | = ThreadManager::newSimpleThreadManager(workerCount, pendingTaskMaxCount); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 255 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 256 | shared_ptr<ThreadFactory> threadFactory |
| 257 | = shared_ptr<ThreadFactory>(new ThreadFactory()); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 258 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 259 | threadManager->threadFactory(threadFactory); |
| 260 | |
| 261 | threadManager->start(); |
| 262 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 263 | std::vector<shared_ptr<ThreadManagerTests::BlockTask> > tasks; |
| 264 | tasks.reserve(workerCount + pendingTaskMaxCount); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 265 | |
| 266 | for (size_t ix = 0; ix < workerCount; ix++) { |
| 267 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 268 | tasks.push_back(shared_ptr<ThreadManagerTests::BlockTask>( |
| 269 | new ThreadManagerTests::BlockTask(entryMonitor, blockMonitor, blocked[0], doneMonitor, activeCounts[0]))); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | for (size_t ix = 0; ix < pendingTaskMaxCount; ix++) { |
| 273 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 274 | tasks.push_back(shared_ptr<ThreadManagerTests::BlockTask>( |
| 275 | new ThreadManagerTests::BlockTask(entryMonitor, blockMonitor, blocked[1], doneMonitor, activeCounts[1]))); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 278 | for (auto ix = tasks.begin(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 279 | ix != tasks.end(); |
| 280 | ix++) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 281 | threadManager->add(*ix); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 284 | if (!(success = (threadManager->totalTaskCount() == pendingTaskMaxCount + workerCount))) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 285 | throw TException("Unexpected pending task count"); |
| 286 | } |
| 287 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 288 | shared_ptr<ThreadManagerTests::BlockTask> extraTask( |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 289 | new ThreadManagerTests::BlockTask(entryMonitor, blockMonitor, blocked[2], doneMonitor, activeCounts[2])); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 290 | |
| 291 | try { |
| 292 | threadManager->add(extraTask, 1); |
| 293 | throw TException("Unexpected success adding task in excess of pending task count"); |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 294 | } catch (TooManyPendingTasksException&) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 295 | throw TException("Should have timed out adding task in excess of pending task count"); |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 296 | } catch (TimedOutException&) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 297 | // Expected result |
| 298 | } |
| 299 | |
| 300 | try { |
| 301 | threadManager->add(extraTask, -1); |
| 302 | throw TException("Unexpected success adding task in excess of pending task count"); |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 303 | } catch (TimedOutException&) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 304 | throw TException("Unexpected timeout adding task in excess of pending task count"); |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 305 | } catch (TooManyPendingTasksException&) { |
David Reiss | 9fcacc8 | 2009-06-04 00:32:54 +0000 | [diff] [blame] | 306 | // Expected result |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 309 | std::cout << "\t\t\t" |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 310 | << "Pending tasks " << threadManager->pendingTaskCount() << '\n'; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 311 | |
| 312 | { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 313 | Synchronized s(blockMonitor); |
| 314 | blocked[0] = false; |
| 315 | blockMonitor.notifyAll(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 319 | Synchronized s(doneMonitor); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 320 | while (activeCounts[0] != 0) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 321 | doneMonitor.wait(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 325 | std::cout << "\t\t\t" |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 326 | << "Pending tasks " << threadManager->pendingTaskCount() << '\n'; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 327 | |
| 328 | try { |
| 329 | threadManager->add(extraTask, 1); |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 330 | } catch (TimedOutException&) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 331 | std::cout << "\t\t\t" |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 332 | << "add timed out unexpectedly" << '\n'; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 333 | throw TException("Unexpected timeout adding task"); |
| 334 | |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 335 | } catch (TooManyPendingTasksException&) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 336 | std::cout << "\t\t\t" |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 337 | << "add encountered too many pending exepctions" << '\n'; |
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 | { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 344 | Synchronized s(blockMonitor); |
| 345 | blocked[1] = false; |
| 346 | blockMonitor.notifyAll(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 350 | Synchronized s(doneMonitor); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 351 | while (activeCounts[1] != 0) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 352 | doneMonitor.wait(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | // Wake up the extra task and wait for it to complete |
| 357 | |
| 358 | { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 359 | Synchronized s(blockMonitor); |
| 360 | blocked[2] = false; |
| 361 | blockMonitor.notifyAll(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 365 | Synchronized s(doneMonitor); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 366 | while (activeCounts[2] != 0) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 367 | doneMonitor.wait(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 371 | threadManager->stop(); |
| 372 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 373 | if (!(success = (threadManager->totalTaskCount() == 0))) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 374 | throw TException("Unexpected total task count"); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 377 | } catch (TException& e) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 378 | std::cout << "ERROR: " << e.what() << '\n'; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 379 | } |
| 380 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 381 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") << '\n'; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 382 | return success; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 383 | } |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 384 | |
| 385 | |
| 386 | bool apiTest() { |
| 387 | |
| 388 | // prove currentTime has milliseconds granularity since many other things depend on it |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 389 | int64_t a = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 390 | sleep_(100); |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 391 | int64_t b = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 392 | if (b - a < 50 || b - a > 150) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 393 | std::cerr << "\t\t\texpected 100ms gap, found " << (b-a) << "ms gap instead." << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 394 | return false; |
| 395 | } |
| 396 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 397 | return apiTestWithThreadFactory(shared_ptr<ThreadFactory>(new ThreadFactory())); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 398 | |
| 399 | } |
| 400 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 401 | bool apiTestWithThreadFactory(shared_ptr<ThreadFactory> threadFactory) |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 402 | { |
| 403 | shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(1); |
| 404 | threadManager->threadFactory(threadFactory); |
| 405 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 406 | std::cout << "\t\t\t\tstarting.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 407 | |
| 408 | threadManager->start(); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 409 | threadManager->setExpireCallback(expiredNotifier); // std::bind(&ThreadManagerTests::expiredNotifier, this)); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 410 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 411 | #define EXPECT(FUNC, COUNT) { size_t c = FUNC; if (c != COUNT) { std::cerr << "expected " #FUNC" to be " #COUNT ", but was " << c << '\n'; return false; } } |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 412 | |
| 413 | EXPECT(threadManager->workerCount(), 1); |
| 414 | EXPECT(threadManager->idleWorkerCount(), 1); |
| 415 | EXPECT(threadManager->pendingTaskCount(), 0); |
| 416 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 417 | std::cout << "\t\t\t\tadd 2nd worker.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 418 | |
| 419 | threadManager->addWorker(); |
| 420 | |
| 421 | EXPECT(threadManager->workerCount(), 2); |
| 422 | EXPECT(threadManager->idleWorkerCount(), 2); |
| 423 | EXPECT(threadManager->pendingTaskCount(), 0); |
| 424 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 425 | std::cout << "\t\t\t\tremove 2nd worker.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 426 | |
| 427 | threadManager->removeWorker(); |
| 428 | |
| 429 | EXPECT(threadManager->workerCount(), 1); |
| 430 | EXPECT(threadManager->idleWorkerCount(), 1); |
| 431 | EXPECT(threadManager->pendingTaskCount(), 0); |
| 432 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 433 | std::cout << "\t\t\t\tremove 1st worker.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 434 | |
| 435 | threadManager->removeWorker(); |
| 436 | |
| 437 | EXPECT(threadManager->workerCount(), 0); |
| 438 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 439 | EXPECT(threadManager->pendingTaskCount(), 0); |
| 440 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 441 | std::cout << "\t\t\t\tadd blocking task.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 442 | |
| 443 | // We're going to throw a blocking task into the mix |
| 444 | Monitor entryMonitor; // signaled when task is running |
| 445 | Monitor blockMonitor; // to be signaled to unblock the task |
| 446 | bool blocked(true); // set to false before notifying |
| 447 | Monitor doneMonitor; // signaled when count reaches zero |
| 448 | size_t activeCount = 1; |
| 449 | shared_ptr<ThreadManagerTests::BlockTask> blockingTask( |
| 450 | new ThreadManagerTests::BlockTask(entryMonitor, blockMonitor, blocked, doneMonitor, activeCount)); |
| 451 | threadManager->add(blockingTask); |
| 452 | |
| 453 | EXPECT(threadManager->workerCount(), 0); |
| 454 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 455 | EXPECT(threadManager->pendingTaskCount(), 1); |
| 456 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 457 | std::cout << "\t\t\t\tadd other task.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 458 | |
| 459 | shared_ptr<ThreadManagerTests::Task> otherTask( |
| 460 | new ThreadManagerTests::Task(doneMonitor, activeCount, 0)); |
| 461 | |
| 462 | threadManager->add(otherTask); |
| 463 | |
| 464 | EXPECT(threadManager->workerCount(), 0); |
| 465 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 466 | EXPECT(threadManager->pendingTaskCount(), 2); |
| 467 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 468 | std::cout << "\t\t\t\tremove blocking task specifically.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 469 | |
| 470 | threadManager->remove(blockingTask); |
| 471 | |
| 472 | EXPECT(threadManager->workerCount(), 0); |
| 473 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 474 | EXPECT(threadManager->pendingTaskCount(), 1); |
| 475 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 476 | std::cout << "\t\t\t\tremove next pending task.." << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 477 | |
| 478 | shared_ptr<Runnable> nextTask = threadManager->removeNextPending(); |
| 479 | if (nextTask != otherTask) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 480 | std::cerr << "\t\t\t\t\texpected removeNextPending to return otherTask" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 481 | return false; |
| 482 | } |
| 483 | |
| 484 | EXPECT(threadManager->workerCount(), 0); |
| 485 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 486 | EXPECT(threadManager->pendingTaskCount(), 0); |
| 487 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 488 | std::cout << "\t\t\t\tremove next pending task (none left).." << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 489 | |
| 490 | nextTask = threadManager->removeNextPending(); |
| 491 | if (nextTask) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 492 | std::cerr << "\t\t\t\t\texpected removeNextPending to return an empty Runnable" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 493 | return false; |
| 494 | } |
| 495 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 496 | std::cout << "\t\t\t\tadd 2 expired tasks and 1 not.." << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 497 | |
| 498 | shared_ptr<ThreadManagerTests::Task> expiredTask( |
| 499 | new ThreadManagerTests::Task(doneMonitor, activeCount, 0)); |
| 500 | |
| 501 | threadManager->add(expiredTask, 0, 1); |
| 502 | threadManager->add(blockingTask); // add one that hasn't expired to make sure it gets skipped |
| 503 | threadManager->add(expiredTask, 0, 1); // add a second expired to ensure removeExpiredTasks removes both |
| 504 | |
| 505 | sleep_(50); // make sure enough time elapses for it to expire - the shortest expiration time is 1 millisecond |
| 506 | |
| 507 | EXPECT(threadManager->workerCount(), 0); |
| 508 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 509 | EXPECT(threadManager->pendingTaskCount(), 3); |
| 510 | EXPECT(threadManager->expiredTaskCount(), 0); |
| 511 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 512 | std::cout << "\t\t\t\tremove expired tasks.." << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 513 | |
| 514 | if (!m_expired.empty()) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 515 | std::cerr << "\t\t\t\t\texpected m_expired to be empty" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | |
| 519 | threadManager->removeExpiredTasks(); |
| 520 | |
| 521 | if (m_expired.size() != 2) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 522 | std::cerr << "\t\t\t\t\texpected m_expired to be set" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 523 | return false; |
| 524 | } |
| 525 | |
| 526 | if (m_expired.front() != expiredTask) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 527 | std::cerr << "\t\t\t\t\texpected m_expired[0] to be the expired task" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | m_expired.pop_front(); |
| 531 | |
| 532 | if (m_expired.front() != expiredTask) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 533 | std::cerr << "\t\t\t\t\texpected m_expired[1] to be the expired task" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 534 | return false; |
| 535 | } |
| 536 | |
| 537 | m_expired.clear(); |
| 538 | |
| 539 | threadManager->remove(blockingTask); |
| 540 | |
| 541 | EXPECT(threadManager->workerCount(), 0); |
| 542 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 543 | EXPECT(threadManager->pendingTaskCount(), 0); |
| 544 | EXPECT(threadManager->expiredTaskCount(), 2); |
| 545 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 546 | std::cout << "\t\t\t\tadd expired task (again).." << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 547 | |
| 548 | threadManager->add(expiredTask, 0, 1); // expires in 1ms |
| 549 | sleep_(50); // make sure enough time elapses for it to expire - the shortest expiration time is 1ms |
| 550 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 551 | std::cout << "\t\t\t\tadd worker to consume expired task.." << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 552 | |
| 553 | threadManager->addWorker(); |
| 554 | sleep_(100); // make sure it has time to spin up and expire the task |
| 555 | |
| 556 | if (m_expired.empty()) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 557 | std::cerr << "\t\t\t\t\texpected m_expired to be set" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 558 | return false; |
| 559 | } |
| 560 | |
| 561 | if (m_expired.front() != expiredTask) { |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 562 | std::cerr << "\t\t\t\t\texpected m_expired to be the expired task" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 563 | return false; |
| 564 | } |
| 565 | |
| 566 | m_expired.clear(); |
| 567 | |
| 568 | EXPECT(threadManager->workerCount(), 1); |
| 569 | EXPECT(threadManager->idleWorkerCount(), 1); |
| 570 | EXPECT(threadManager->pendingTaskCount(), 0); |
| 571 | EXPECT(threadManager->expiredTaskCount(), 3); |
| 572 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 573 | std::cout << "\t\t\t\ttry to remove too many workers" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 574 | try { |
| 575 | threadManager->removeWorker(2); |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 576 | std::cerr << "\t\t\t\t\texpected InvalidArgumentException" << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 577 | return false; |
| 578 | } catch (const InvalidArgumentException&) { |
| 579 | /* expected */ |
| 580 | } |
| 581 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 582 | std::cout << "\t\t\t\tremove worker.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 583 | |
| 584 | threadManager->removeWorker(); |
| 585 | |
| 586 | EXPECT(threadManager->workerCount(), 0); |
| 587 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 588 | EXPECT(threadManager->pendingTaskCount(), 0); |
| 589 | EXPECT(threadManager->expiredTaskCount(), 3); |
| 590 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 591 | std::cout << "\t\t\t\tadd blocking task.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 592 | |
| 593 | threadManager->add(blockingTask); |
| 594 | |
| 595 | EXPECT(threadManager->workerCount(), 0); |
| 596 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 597 | EXPECT(threadManager->pendingTaskCount(), 1); |
| 598 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 599 | std::cout << "\t\t\t\tadd worker.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 600 | |
| 601 | threadManager->addWorker(); |
| 602 | { |
| 603 | Synchronized s(entryMonitor); |
| 604 | while (!blockingTask->_entered) { |
| 605 | entryMonitor.wait(); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | EXPECT(threadManager->workerCount(), 1); |
| 610 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 611 | EXPECT(threadManager->pendingTaskCount(), 0); |
| 612 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 613 | std::cout << "\t\t\t\tunblock task and remove worker.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 614 | |
| 615 | { |
| 616 | Synchronized s(blockMonitor); |
| 617 | blocked = false; |
| 618 | blockMonitor.notifyAll(); |
| 619 | } |
| 620 | threadManager->removeWorker(); |
| 621 | |
| 622 | EXPECT(threadManager->workerCount(), 0); |
| 623 | EXPECT(threadManager->idleWorkerCount(), 0); |
| 624 | EXPECT(threadManager->pendingTaskCount(), 0); |
| 625 | |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 626 | std::cout << "\t\t\t\tcleanup.. " << '\n'; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 627 | |
| 628 | blockingTask.reset(); |
| 629 | threadManager.reset(); |
| 630 | return true; |
| 631 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 632 | }; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 633 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | } |
| 637 | } // apache::thrift::concurrency |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 638 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 639 | using namespace apache::thrift::concurrency::test; |