blob: c6fa4cfa1600720a3d4554fd52714f161ce2030c [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>
21#include <thrift/concurrency/PlatformThreadFactory.h>
22#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 Grochowski293a40e2014-09-04 17:28:17 +040037 static const double TEST_TOLERANCE;
38
Konrad Grochowski16a23a62014-11-13 15:33:38 +010039public:
40 class Task : public Runnable {
41 public:
42 Task(Monitor& monitor, int64_t timeout)
43 : _timeout(timeout),
44 _startTime(Util::currentTime()),
James E. King, III36200902016-10-05 14:47:18 -040045 _endTime(0),
Konrad Grochowski16a23a62014-11-13 15:33:38 +010046 _monitor(monitor),
47 _success(false),
48 _done(false) {}
Marc Slemko8a40a762006-07-19 17:46:50 +000049
Mark Sleef5f2be42006-09-05 21:05:31 +000050 ~Task() { std::cerr << this << std::endl; }
Marc Slemko6f038a72006-08-03 18:58:09 +000051
Marc Slemko8a40a762006-07-19 17:46:50 +000052 void run() {
53
Marc Slemko9f27a4e2006-07-19 20:02:22 +000054 _endTime = Util::currentTime();
55
56 // Figure out error percentage
57
Mark Slee9b82d272007-05-23 05:16:07 +000058 int64_t delta = _endTime - _startTime;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000059
Konrad Grochowski16a23a62014-11-13 15:33:38 +010060 delta = delta > _timeout ? delta - _timeout : _timeout - delta;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000061
ben-craigfae08e72015-07-15 11:34:47 -050062 double error = double(delta) / _timeout;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000063
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064 if (error < TEST_TOLERANCE) {
David Reiss96d23882007-07-26 21:10:32 +000065 _success = true;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000066 }
David Reiss0c90f6f2008-02-06 22:18:40 +000067
Marc Slemko8a40a762006-07-19 17:46:50 +000068 _done = true;
Marc Slemko6f038a72006-08-03 18:58:09 +000069
Konrad Grochowski16a23a62014-11-13 15:33:38 +010070 std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; // debug
Marc Slemko6f038a72006-08-03 18:58:09 +000071
Konrad Grochowski16a23a62014-11-13 15:33:38 +010072 {
73 Synchronized s(_monitor);
David Reiss96d23882007-07-26 21:10:32 +000074 _monitor.notifyAll();
Marc Slemko8a40a762006-07-19 17:46:50 +000075 }
David Reiss0c90f6f2008-02-06 22:18:40 +000076 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +000077
Mark Slee9b82d272007-05-23 05:16:07 +000078 int64_t _timeout;
79 int64_t _startTime;
80 int64_t _endTime;
Marc Slemko8a40a762006-07-19 17:46:50 +000081 Monitor& _monitor;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000082 bool _success;
Marc Slemko8a40a762006-07-19 17:46:50 +000083 bool _done;
84 };
85
Mark Sleef5f2be42006-09-05 21:05:31 +000086 /**
87 * This test creates two tasks and waits for the first to expire within 10%
88 * of the expected expiration time. It then verifies that the timer manager
89 * properly clean up itself and the remaining orphaned timeout task when the
90 * manager goes out of scope and its destructor is called.
91 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +010092 bool test00(int64_t timeout = 1000LL) {
Marc Slemko8a40a762006-07-19 17:46:50 +000093
Konrad Grochowski16a23a62014-11-13 15:33:38 +010094 shared_ptr<TimerManagerTests::Task> orphanTask
95 = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout));
Marc Slemko8a40a762006-07-19 17:46:50 +000096
Marc Slemko9f27a4e2006-07-19 20:02:22 +000097 {
Marc Slemko8a40a762006-07-19 17:46:50 +000098
Marc Slemko9f27a4e2006-07-19 20:02:22 +000099 TimerManager timerManager;
David Reiss0c90f6f2008-02-06 22:18:40 +0000100
Roger Meier3faaedf2011-10-02 10:51:45 +0000101 timerManager.threadFactory(shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory()));
David Reiss0c90f6f2008-02-06 22:18:40 +0000102
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000103 timerManager.start();
David Reiss0c90f6f2008-02-06 22:18:40 +0000104
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000105 assert(timerManager.state() == TimerManager::STARTED);
Marc Slemko8a40a762006-07-19 17:46:50 +0000106
David Reiss52687eb2009-06-04 00:32:57 +0000107 // Don't create task yet, because its constructor sets the expected completion time, and we
108 // need to delay between inserting the two tasks into the run queue.
109 shared_ptr<TimerManagerTests::Task> task;
Marc Slemko8a40a762006-07-19 17:46:50 +0000110
Mark Sleef5f2be42006-09-05 21:05:31 +0000111 {
112 Synchronized s(_monitor);
Marc Slemko8a40a762006-07-19 17:46:50 +0000113
David Reiss96d23882007-07-26 21:10:32 +0000114 timerManager.add(orphanTask, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +0000115
David Reiss52687eb2009-06-04 00:32:57 +0000116 try {
117 // Wait for 1 second in order to give timerManager a chance to start sleeping in response
118 // to adding orphanTask. We need to do this so we can verify that adding the second task
119 // kicks the dispatcher out of the current wait and starts the new 1 second wait.
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100120 _monitor.wait(1000);
121 assert(
122 0 == "ERROR: This wait should time out. TimerManager dispatcher may have a problem.");
ben-craigfae08e72015-07-15 11:34:47 -0500123 } catch (TimedOutException&) {
David Reiss52687eb2009-06-04 00:32:57 +0000124 }
125
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100126 task.reset(new TimerManagerTests::Task(_monitor, timeout));
David Reiss52687eb2009-06-04 00:32:57 +0000127
David Reiss96d23882007-07-26 21:10:32 +0000128 timerManager.add(task, timeout);
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000129
David Reiss96d23882007-07-26 21:10:32 +0000130 _monitor.wait();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000131 }
132
133 assert(task->_done);
134
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000135 std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +0000136 }
137
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000138 // timerManager.stop(); This is where it happens via destructor
Marc Slemko8a40a762006-07-19 17:46:50 +0000139
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000140 assert(!orphanTask->_done);
Marc Slemko8a40a762006-07-19 17:46:50 +0000141
Marc Slemko8a40a762006-07-19 17:46:50 +0000142 return true;
143 }
144
145 friend class TestTask;
146
147 Monitor _monitor;
148};
Marc Slemko8a40a762006-07-19 17:46:50 +0000149
Konrad Grochowski293a40e2014-09-04 17:28:17 +0400150const double TimerManagerTests::TEST_TOLERANCE = .20;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100151}
152}
153}
154} // apache::thrift::concurrency