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> |
| 21 | #include <thrift/concurrency/PlatformThreadFactory.h> |
| 22 | #include <thrift/concurrency/Monitor.h> |
| 23 | #include <thrift/concurrency/Util.h> |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 24 | |
| 25 | #include <assert.h> |
| 26 | #include <iostream> |
| 27 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 28 | namespace apache { |
| 29 | namespace thrift { |
| 30 | namespace concurrency { |
| 31 | namespace test { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 32 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 33 | using namespace apache::thrift::concurrency; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 34 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 35 | class TimerManagerTests { |
| 36 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 37 | public: |
| 38 | class Task : public Runnable { |
| 39 | public: |
| 40 | Task(Monitor& monitor, int64_t timeout) |
| 41 | : _timeout(timeout), |
| 42 | _startTime(Util::currentTime()), |
James E. King, III | 3620090 | 2016-10-05 14:47:18 -0400 | [diff] [blame] | 43 | _endTime(0), |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 44 | _monitor(monitor), |
| 45 | _success(false), |
| 46 | _done(false) {} |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 47 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 48 | ~Task() { std::cerr << this << std::endl; } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 49 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 50 | void run() { |
| 51 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 52 | _endTime = Util::currentTime(); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 53 | _success = (_endTime - _startTime) >= _timeout; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 54 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 55 | { |
| 56 | Synchronized s(_monitor); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 57 | _done = true; |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 58 | _monitor.notifyAll(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 59 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 60 | } |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 61 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 62 | int64_t _timeout; |
| 63 | int64_t _startTime; |
| 64 | int64_t _endTime; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 65 | Monitor& _monitor; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 66 | bool _success; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 67 | bool _done; |
| 68 | }; |
| 69 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 70 | /** |
| 71 | * This test creates two tasks and waits for the first to expire within 10% |
| 72 | * of the expected expiration time. It then verifies that the timer manager |
| 73 | * properly clean up itself and the remaining orphaned timeout task when the |
| 74 | * manager goes out of scope and its destructor is called. |
| 75 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 76 | bool test00(int64_t timeout = 1000LL) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 77 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 78 | shared_ptr<TimerManagerTests::Task> orphanTask |
| 79 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout)); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 80 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 81 | { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 82 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 83 | TimerManager timerManager; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 84 | |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 85 | timerManager.threadFactory(shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory())); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 86 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 87 | timerManager.start(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 88 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 89 | assert(timerManager.state() == TimerManager::STARTED); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 90 | |
David Reiss | 52687eb | 2009-06-04 00:32:57 +0000 | [diff] [blame] | 91 | // Don't create task yet, because its constructor sets the expected completion time, and we |
| 92 | // need to delay between inserting the two tasks into the run queue. |
| 93 | shared_ptr<TimerManagerTests::Task> task; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 94 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 95 | { |
| 96 | Synchronized s(_monitor); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 97 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 98 | timerManager.add(orphanTask, 10 * timeout); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 99 | |
David Reiss | 52687eb | 2009-06-04 00:32:57 +0000 | [diff] [blame] | 100 | try { |
| 101 | // Wait for 1 second in order to give timerManager a chance to start sleeping in response |
| 102 | // to adding orphanTask. We need to do this so we can verify that adding the second task |
| 103 | // kicks the dispatcher out of the current wait and starts the new 1 second wait. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 104 | _monitor.wait(1000); |
| 105 | assert( |
| 106 | 0 == "ERROR: This wait should time out. TimerManager dispatcher may have a problem."); |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 107 | } catch (TimedOutException&) { |
David Reiss | 52687eb | 2009-06-04 00:32:57 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 110 | task.reset(new TimerManagerTests::Task(_monitor, timeout)); |
David Reiss | 52687eb | 2009-06-04 00:32:57 +0000 | [diff] [blame] | 111 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 112 | timerManager.add(task, timeout); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 113 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 114 | _monitor.wait(); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | assert(task->_done); |
| 118 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 119 | std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 122 | // timerManager.stop(); This is where it happens via destructor |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 123 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 124 | assert(!orphanTask->_done); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 125 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
Francois Ferrand | cc2d558 | 2017-08-25 09:01:26 +0200 | [diff] [blame] | 129 | /** |
| 130 | * This test creates two tasks, removes the first one then waits for the second one. It then |
| 131 | * verifies that the timer manager properly clean up itself and the remaining orphaned timeout |
| 132 | * task when the manager goes out of scope and its destructor is called. |
| 133 | */ |
| 134 | bool test01(int64_t timeout = 1000LL) { |
| 135 | TimerManager timerManager; |
| 136 | timerManager.threadFactory(shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory())); |
| 137 | timerManager.start(); |
| 138 | assert(timerManager.state() == TimerManager::STARTED); |
| 139 | |
| 140 | Synchronized s(_monitor); |
| 141 | |
| 142 | // Setup the two tasks |
| 143 | shared_ptr<TimerManagerTests::Task> taskToRemove |
| 144 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout / 2)); |
| 145 | timerManager.add(taskToRemove, taskToRemove->_timeout); |
| 146 | |
| 147 | shared_ptr<TimerManagerTests::Task> task |
| 148 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout)); |
| 149 | timerManager.add(task, task->_timeout); |
| 150 | |
| 151 | // Remove one task and wait until the other has completed |
| 152 | timerManager.remove(taskToRemove); |
| 153 | _monitor.wait(timeout * 2); |
| 154 | |
| 155 | assert(!taskToRemove->_done); |
| 156 | assert(task->_done); |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * This test creates two tasks with the same callback and another one, then removes the two |
| 163 | * duplicated then waits for the last one. It then verifies that the timer manager properly |
| 164 | * clean up itself and the remaining orphaned timeout task when the manager goes out of scope |
| 165 | * and its destructor is called. |
| 166 | */ |
| 167 | bool test02(int64_t timeout = 1000LL) { |
| 168 | TimerManager timerManager; |
| 169 | timerManager.threadFactory(shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory())); |
| 170 | timerManager.start(); |
| 171 | assert(timerManager.state() == TimerManager::STARTED); |
| 172 | |
| 173 | Synchronized s(_monitor); |
| 174 | |
| 175 | // Setup the one tasks and add it twice |
| 176 | shared_ptr<TimerManagerTests::Task> taskToRemove |
| 177 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout / 3)); |
| 178 | timerManager.add(taskToRemove, taskToRemove->_timeout); |
| 179 | timerManager.add(taskToRemove, taskToRemove->_timeout * 2); |
| 180 | |
| 181 | shared_ptr<TimerManagerTests::Task> task |
| 182 | = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout)); |
| 183 | timerManager.add(task, task->_timeout); |
| 184 | |
| 185 | // Remove the first task (e.g. two timers) and wait until the other has completed |
| 186 | timerManager.remove(taskToRemove); |
| 187 | _monitor.wait(timeout * 2); |
| 188 | |
| 189 | assert(!taskToRemove->_done); |
| 190 | assert(task->_done); |
| 191 | |
| 192 | return true; |
| 193 | } |
| 194 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 195 | friend class TestTask; |
| 196 | |
| 197 | Monitor _monitor; |
| 198 | }; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 199 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | } // apache::thrift::concurrency |