blob: c15b14b8099ad86d18220e85f7592875d0f83c73 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Roger Meier49ff8b12012-04-13 09:12:31 +000020#include <thrift/concurrency/TimerManager.h>
cyyca8af9b2019-01-11 22:13:12 +080021#include <thrift/concurrency/ThreadFactory.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000022#include <thrift/concurrency/Monitor.h>
23#include <thrift/concurrency/Util.h>
Marc Slemko8a40a762006-07-19 17:46:50 +000024
25#include <assert.h>
26#include <iostream>
27
Konrad Grochowski16a23a62014-11-13 15:33:38 +010028namespace apache {
29namespace thrift {
30namespace concurrency {
31namespace test {
Marc Slemko8a40a762006-07-19 17:46:50 +000032
T Jake Lucianib5e62212009-01-31 22:36:20 +000033using namespace apache::thrift::concurrency;
Marc Slemko8a40a762006-07-19 17:46:50 +000034
Marc Slemko8a40a762006-07-19 17:46:50 +000035class TimerManagerTests {
36
Konrad Grochowski16a23a62014-11-13 15:33:38 +010037public:
38 class Task : public Runnable {
39 public:
40 Task(Monitor& monitor, int64_t timeout)
41 : _timeout(timeout),
42 _startTime(Util::currentTime()),
James E. King, III36200902016-10-05 14:47:18 -040043 _endTime(0),
Konrad Grochowski16a23a62014-11-13 15:33:38 +010044 _monitor(monitor),
45 _success(false),
46 _done(false) {}
Marc Slemko8a40a762006-07-19 17:46:50 +000047
Mark Sleef5f2be42006-09-05 21:05:31 +000048 ~Task() { std::cerr << this << std::endl; }
Marc Slemko6f038a72006-08-03 18:58:09 +000049
Marc Slemko8a40a762006-07-19 17:46:50 +000050 void run() {
51
Marc Slemko9f27a4e2006-07-19 20:02:22 +000052 _endTime = Util::currentTime();
James E. King, IIIdf899132016-11-12 15:16:30 -050053 _success = (_endTime - _startTime) >= _timeout;
Marc Slemko6f038a72006-08-03 18:58:09 +000054
Konrad Grochowski16a23a62014-11-13 15:33:38 +010055 {
56 Synchronized s(_monitor);
James E. King, IIIdf899132016-11-12 15:16:30 -050057 _done = true;
David Reiss96d23882007-07-26 21:10:32 +000058 _monitor.notifyAll();
Marc Slemko8a40a762006-07-19 17:46:50 +000059 }
David Reiss0c90f6f2008-02-06 22:18:40 +000060 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +000061
Mark Slee9b82d272007-05-23 05:16:07 +000062 int64_t _timeout;
63 int64_t _startTime;
64 int64_t _endTime;
Marc Slemko8a40a762006-07-19 17:46:50 +000065 Monitor& _monitor;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000066 bool _success;
Marc Slemko8a40a762006-07-19 17:46:50 +000067 bool _done;
68 };
69
Mark Sleef5f2be42006-09-05 21:05:31 +000070 /**
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 Grochowski16a23a62014-11-13 15:33:38 +010076 bool test00(int64_t timeout = 1000LL) {
Marc Slemko8a40a762006-07-19 17:46:50 +000077
Konrad Grochowski16a23a62014-11-13 15:33:38 +010078 shared_ptr<TimerManagerTests::Task> orphanTask
79 = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout));
Marc Slemko8a40a762006-07-19 17:46:50 +000080
Marc Slemko9f27a4e2006-07-19 20:02:22 +000081 {
Marc Slemko9f27a4e2006-07-19 20:02:22 +000082 TimerManager timerManager;
cyyca8af9b2019-01-11 22:13:12 +080083 timerManager.threadFactory(shared_ptr<ThreadFactory>(new ThreadFactory()));
Marc Slemko9f27a4e2006-07-19 20:02:22 +000084 timerManager.start();
James E. King III9bea32f2018-03-16 16:07:42 -040085 if (timerManager.state() != TimerManager::STARTED) {
86 std::cerr << "timerManager is not in the STARTED state, but should be" << std::endl;
87 return false;
88 }
Marc Slemko8a40a762006-07-19 17:46:50 +000089
David Reiss52687eb2009-06-04 00:32:57 +000090 // Don't create task yet, because its constructor sets the expected completion time, and we
91 // need to delay between inserting the two tasks into the run queue.
92 shared_ptr<TimerManagerTests::Task> task;
Marc Slemko8a40a762006-07-19 17:46:50 +000093
Mark Sleef5f2be42006-09-05 21:05:31 +000094 {
95 Synchronized s(_monitor);
David Reiss96d23882007-07-26 21:10:32 +000096 timerManager.add(orphanTask, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +000097
James E. King III9bea32f2018-03-16 16:07:42 -040098 THRIFT_SLEEP_USEC(timeout * 1000);
David Reiss52687eb2009-06-04 00:32:57 +000099
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100100 task.reset(new TimerManagerTests::Task(_monitor, timeout));
David Reiss96d23882007-07-26 21:10:32 +0000101 timerManager.add(task, timeout);
David Reiss96d23882007-07-26 21:10:32 +0000102 _monitor.wait();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000103 }
104
James E. King III9bea32f2018-03-16 16:07:42 -0400105 if (!task->_done) {
106 std::cerr << "task is not done, but it should have executed" << std::endl;
107 return false;
108 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000109
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000110 std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +0000111 }
112
James E. King III9bea32f2018-03-16 16:07:42 -0400113 if (orphanTask->_done) {
114 std::cerr << "orphan task is done, but it should not have executed" << std::endl;
115 return false;
116 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000117
Marc Slemko8a40a762006-07-19 17:46:50 +0000118 return true;
119 }
120
Francois Ferrandcc2d5582017-08-25 09:01:26 +0200121 /**
122 * This test creates two tasks, removes the first one then waits for the second one. It then
123 * verifies that the timer manager properly clean up itself and the remaining orphaned timeout
124 * task when the manager goes out of scope and its destructor is called.
125 */
126 bool test01(int64_t timeout = 1000LL) {
127 TimerManager timerManager;
cyyca8af9b2019-01-11 22:13:12 +0800128 timerManager.threadFactory(shared_ptr<ThreadFactory>(new ThreadFactory()));
Francois Ferrandcc2d5582017-08-25 09:01:26 +0200129 timerManager.start();
130 assert(timerManager.state() == TimerManager::STARTED);
131
132 Synchronized s(_monitor);
133
134 // Setup the two tasks
135 shared_ptr<TimerManagerTests::Task> taskToRemove
136 = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout / 2));
137 timerManager.add(taskToRemove, taskToRemove->_timeout);
138
139 shared_ptr<TimerManagerTests::Task> task
140 = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout));
141 timerManager.add(task, task->_timeout);
142
143 // Remove one task and wait until the other has completed
144 timerManager.remove(taskToRemove);
145 _monitor.wait(timeout * 2);
146
147 assert(!taskToRemove->_done);
148 assert(task->_done);
149
150 return true;
151 }
152
153 /**
154 * This test creates two tasks with the same callback and another one, then removes the two
155 * duplicated then waits for the last one. It then verifies that the timer manager properly
156 * clean up itself and the remaining orphaned timeout task when the manager goes out of scope
157 * and its destructor is called.
158 */
159 bool test02(int64_t timeout = 1000LL) {
160 TimerManager timerManager;
cyyca8af9b2019-01-11 22:13:12 +0800161 timerManager.threadFactory(shared_ptr<ThreadFactory>(new ThreadFactory()));
Francois Ferrandcc2d5582017-08-25 09:01:26 +0200162 timerManager.start();
163 assert(timerManager.state() == TimerManager::STARTED);
164
165 Synchronized s(_monitor);
166
167 // Setup the one tasks and add it twice
168 shared_ptr<TimerManagerTests::Task> taskToRemove
169 = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout / 3));
170 timerManager.add(taskToRemove, taskToRemove->_timeout);
171 timerManager.add(taskToRemove, taskToRemove->_timeout * 2);
172
173 shared_ptr<TimerManagerTests::Task> task
174 = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout));
175 timerManager.add(task, task->_timeout);
176
177 // Remove the first task (e.g. two timers) and wait until the other has completed
178 timerManager.remove(taskToRemove);
179 _monitor.wait(timeout * 2);
180
181 assert(!taskToRemove->_done);
182 assert(task->_done);
183
184 return true;
185 }
186
Francois Ferrand69603702017-09-11 12:09:40 +0200187 /**
188 * This test creates two tasks, removes the first one then waits for the second one. It then
189 * verifies that the timer manager properly clean up itself and the remaining orphaned timeout
190 * task when the manager goes out of scope and its destructor is called.
191 */
192 bool test03(int64_t timeout = 1000LL) {
193 TimerManager timerManager;
cyyca8af9b2019-01-11 22:13:12 +0800194 timerManager.threadFactory(shared_ptr<ThreadFactory>(new ThreadFactory()));
Francois Ferrand69603702017-09-11 12:09:40 +0200195 timerManager.start();
196 assert(timerManager.state() == TimerManager::STARTED);
197
198 Synchronized s(_monitor);
199
200 // Setup the two tasks
201 shared_ptr<TimerManagerTests::Task> taskToRemove
202 = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout / 2));
203 TimerManager::Timer timer = timerManager.add(taskToRemove, taskToRemove->_timeout);
204
205 shared_ptr<TimerManagerTests::Task> task
206 = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout));
207 timerManager.add(task, task->_timeout);
208
209 // Remove one task and wait until the other has completed
210 timerManager.remove(timer);
211 _monitor.wait(timeout * 2);
212
213 assert(!taskToRemove->_done);
214 assert(task->_done);
215
216 // Verify behavior when removing the removed task
217 try {
218 timerManager.remove(timer);
219 assert(0 == "ERROR: This remove should send a NoSuchTaskException exception.");
220 } catch (NoSuchTaskException&) {
221 }
222
223 return true;
224 }
225
226 /**
227 * This test creates one tasks, and tries to remove it after it has expired.
228 */
229 bool test04(int64_t timeout = 1000LL) {
230 TimerManager timerManager;
cyyca8af9b2019-01-11 22:13:12 +0800231 timerManager.threadFactory(shared_ptr<ThreadFactory>(new ThreadFactory()));
Francois Ferrand69603702017-09-11 12:09:40 +0200232 timerManager.start();
233 assert(timerManager.state() == TimerManager::STARTED);
234
235 Synchronized s(_monitor);
236
237 // Setup the task
238 shared_ptr<TimerManagerTests::Task> task
239 = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout / 10));
240 TimerManager::Timer timer = timerManager.add(task, task->_timeout);
241
242 // Wait until the task has completed
243 _monitor.wait(timeout);
244
245 // Verify behavior when removing the expired task
246 try {
247 timerManager.remove(timer);
248 assert(0 == "ERROR: This remove should send a NoSuchTaskException exception.");
249 } catch (NoSuchTaskException&) {
250 }
251
252 return true;
253 }
254
Marc Slemko8a40a762006-07-19 17:46:50 +0000255 friend class TestTask;
256
257 Monitor _monitor;
258};
Marc Slemko8a40a762006-07-19 17:46:50 +0000259
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100260}
261}
262}
263} // apache::thrift::concurrency