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 | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 20 | #include <thrift/concurrency/TimerManager.h> |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 21 | #include <thrift/concurrency/ThreadFactory.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 22 | #include <thrift/concurrency/Monitor.h> |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 23 | |
| 24 | #include <assert.h> |
| 25 | #include <iostream> |
| 26 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 27 | namespace apache { |
| 28 | namespace thrift { |
| 29 | namespace concurrency { |
| 30 | namespace test { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 31 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 32 | using namespace apache::thrift::concurrency; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 33 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 34 | class TimerManagerTests { |
| 35 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 36 | public: |
| 37 | class Task : public Runnable { |
| 38 | public: |
| 39 | Task(Monitor& monitor, int64_t timeout) |
| 40 | : _timeout(timeout), |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 41 | _startTime(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count()), |
James E. King, III | 3620090 | 2016-10-05 14:47:18 -0400 | [diff] [blame] | 42 | _endTime(0), |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 43 | _monitor(monitor), |
| 44 | _success(false), |
| 45 | _done(false) {} |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 46 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 47 | ~Task() { std::cerr << this << std::endl; } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 48 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 49 | void run() { |
| 50 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 51 | _endTime = 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] | 52 | _success = (_endTime - _startTime) >= _timeout; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 53 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 54 | { |
| 55 | Synchronized s(_monitor); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 56 | _done = true; |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 57 | _monitor.notifyAll(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 58 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 59 | } |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 60 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 61 | int64_t _timeout; |
| 62 | int64_t _startTime; |
| 63 | int64_t _endTime; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 64 | Monitor& _monitor; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 65 | bool _success; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 66 | bool _done; |
| 67 | }; |
| 68 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 69 | /** |
| 70 | * This test creates two tasks and waits for the first to expire within 10% |
| 71 | * of the expected expiration time. It then verifies that the timer manager |
| 72 | * properly clean up itself and the remaining orphaned timeout task when the |
| 73 | * manager goes out of scope and its destructor is called. |
| 74 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 75 | bool test00(int64_t timeout = 1000LL) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 76 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 77 | shared_ptr<TimerManagerTests::Task> orphanTask |
| 78 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout)); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 79 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 80 | { |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 81 | TimerManager timerManager; |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 82 | timerManager.threadFactory(shared_ptr<ThreadFactory>(new ThreadFactory())); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 83 | timerManager.start(); |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 84 | if (timerManager.state() != TimerManager::STARTED) { |
| 85 | std::cerr << "timerManager is not in the STARTED state, but should be" << std::endl; |
| 86 | return false; |
| 87 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 88 | |
David Reiss | 52687eb | 2009-06-04 00:32:57 +0000 | [diff] [blame] | 89 | // Don't create task yet, because its constructor sets the expected completion time, and we |
| 90 | // need to delay between inserting the two tasks into the run queue. |
| 91 | shared_ptr<TimerManagerTests::Task> task; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 92 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 93 | { |
| 94 | Synchronized s(_monitor); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 95 | timerManager.add(orphanTask, 10 * timeout); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 96 | |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 97 | THRIFT_SLEEP_USEC(timeout * 1000); |
David Reiss | 52687eb | 2009-06-04 00:32:57 +0000 | [diff] [blame] | 98 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 99 | task.reset(new TimerManagerTests::Task(_monitor, timeout)); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 100 | timerManager.add(task, timeout); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 101 | _monitor.wait(); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 102 | } |
| 103 | |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 104 | if (!task->_done) { |
| 105 | std::cerr << "task is not done, but it should have executed" << std::endl; |
| 106 | return false; |
| 107 | } |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 108 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 109 | std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 110 | } |
| 111 | |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 112 | if (orphanTask->_done) { |
| 113 | std::cerr << "orphan task is done, but it should not have executed" << std::endl; |
| 114 | return false; |
| 115 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 116 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
Francois Ferrand | cc2d558 | 2017-08-25 09:01:26 +0200 | [diff] [blame] | 120 | /** |
| 121 | * This test creates two tasks, removes the first one then waits for the second one. It then |
| 122 | * verifies that the timer manager properly clean up itself and the remaining orphaned timeout |
| 123 | * task when the manager goes out of scope and its destructor is called. |
| 124 | */ |
| 125 | bool test01(int64_t timeout = 1000LL) { |
| 126 | TimerManager timerManager; |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 127 | timerManager.threadFactory(shared_ptr<ThreadFactory>(new ThreadFactory())); |
Francois Ferrand | cc2d558 | 2017-08-25 09:01:26 +0200 | [diff] [blame] | 128 | timerManager.start(); |
| 129 | assert(timerManager.state() == TimerManager::STARTED); |
| 130 | |
| 131 | Synchronized s(_monitor); |
| 132 | |
| 133 | // Setup the two tasks |
| 134 | shared_ptr<TimerManagerTests::Task> taskToRemove |
| 135 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout / 2)); |
| 136 | timerManager.add(taskToRemove, taskToRemove->_timeout); |
| 137 | |
| 138 | shared_ptr<TimerManagerTests::Task> task |
| 139 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout)); |
| 140 | timerManager.add(task, task->_timeout); |
| 141 | |
| 142 | // Remove one task and wait until the other has completed |
| 143 | timerManager.remove(taskToRemove); |
| 144 | _monitor.wait(timeout * 2); |
| 145 | |
| 146 | assert(!taskToRemove->_done); |
| 147 | assert(task->_done); |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * This test creates two tasks with the same callback and another one, then removes the two |
| 154 | * duplicated then waits for the last one. It then verifies that the timer manager properly |
| 155 | * clean up itself and the remaining orphaned timeout task when the manager goes out of scope |
| 156 | * and its destructor is called. |
| 157 | */ |
| 158 | bool test02(int64_t timeout = 1000LL) { |
| 159 | TimerManager timerManager; |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 160 | timerManager.threadFactory(shared_ptr<ThreadFactory>(new ThreadFactory())); |
Francois Ferrand | cc2d558 | 2017-08-25 09:01:26 +0200 | [diff] [blame] | 161 | timerManager.start(); |
| 162 | assert(timerManager.state() == TimerManager::STARTED); |
| 163 | |
| 164 | Synchronized s(_monitor); |
| 165 | |
| 166 | // Setup the one tasks and add it twice |
| 167 | shared_ptr<TimerManagerTests::Task> taskToRemove |
| 168 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout / 3)); |
| 169 | timerManager.add(taskToRemove, taskToRemove->_timeout); |
| 170 | timerManager.add(taskToRemove, taskToRemove->_timeout * 2); |
| 171 | |
| 172 | shared_ptr<TimerManagerTests::Task> task |
| 173 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout)); |
| 174 | timerManager.add(task, task->_timeout); |
| 175 | |
| 176 | // Remove the first task (e.g. two timers) and wait until the other has completed |
| 177 | timerManager.remove(taskToRemove); |
| 178 | _monitor.wait(timeout * 2); |
| 179 | |
| 180 | assert(!taskToRemove->_done); |
| 181 | assert(task->_done); |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
Francois Ferrand | 6960370 | 2017-09-11 12:09:40 +0200 | [diff] [blame] | 186 | /** |
| 187 | * This test creates two tasks, removes the first one then waits for the second one. It then |
| 188 | * verifies that the timer manager properly clean up itself and the remaining orphaned timeout |
| 189 | * task when the manager goes out of scope and its destructor is called. |
| 190 | */ |
| 191 | bool test03(int64_t timeout = 1000LL) { |
| 192 | TimerManager timerManager; |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 193 | timerManager.threadFactory(shared_ptr<ThreadFactory>(new ThreadFactory())); |
Francois Ferrand | 6960370 | 2017-09-11 12:09:40 +0200 | [diff] [blame] | 194 | timerManager.start(); |
| 195 | assert(timerManager.state() == TimerManager::STARTED); |
| 196 | |
| 197 | Synchronized s(_monitor); |
| 198 | |
| 199 | // Setup the two tasks |
| 200 | shared_ptr<TimerManagerTests::Task> taskToRemove |
| 201 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout / 2)); |
| 202 | TimerManager::Timer timer = timerManager.add(taskToRemove, taskToRemove->_timeout); |
| 203 | |
| 204 | shared_ptr<TimerManagerTests::Task> task |
| 205 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout)); |
| 206 | timerManager.add(task, task->_timeout); |
| 207 | |
| 208 | // Remove one task and wait until the other has completed |
| 209 | timerManager.remove(timer); |
| 210 | _monitor.wait(timeout * 2); |
| 211 | |
| 212 | assert(!taskToRemove->_done); |
| 213 | assert(task->_done); |
| 214 | |
| 215 | // Verify behavior when removing the removed task |
| 216 | try { |
| 217 | timerManager.remove(timer); |
| 218 | assert(0 == "ERROR: This remove should send a NoSuchTaskException exception."); |
| 219 | } catch (NoSuchTaskException&) { |
| 220 | } |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * This test creates one tasks, and tries to remove it after it has expired. |
| 227 | */ |
| 228 | bool test04(int64_t timeout = 1000LL) { |
| 229 | TimerManager timerManager; |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 230 | timerManager.threadFactory(shared_ptr<ThreadFactory>(new ThreadFactory())); |
Francois Ferrand | 6960370 | 2017-09-11 12:09:40 +0200 | [diff] [blame] | 231 | timerManager.start(); |
| 232 | assert(timerManager.state() == TimerManager::STARTED); |
| 233 | |
| 234 | Synchronized s(_monitor); |
| 235 | |
| 236 | // Setup the task |
| 237 | shared_ptr<TimerManagerTests::Task> task |
| 238 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout / 10)); |
| 239 | TimerManager::Timer timer = timerManager.add(task, task->_timeout); |
| 240 | |
| 241 | // Wait until the task has completed |
| 242 | _monitor.wait(timeout); |
| 243 | |
| 244 | // Verify behavior when removing the expired task |
| 245 | try { |
| 246 | timerManager.remove(timer); |
| 247 | assert(0 == "ERROR: This remove should send a NoSuchTaskException exception."); |
| 248 | } catch (NoSuchTaskException&) { |
| 249 | } |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 254 | friend class TestTask; |
| 255 | |
| 256 | Monitor _monitor; |
| 257 | }; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 258 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | } // apache::thrift::concurrency |