blob: dda16edb68cf99f8e3972930f7937fc530c68ae4 [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()),
45 _monitor(monitor),
46 _success(false),
47 _done(false) {}
Marc Slemko8a40a762006-07-19 17:46:50 +000048
Mark Sleef5f2be42006-09-05 21:05:31 +000049 ~Task() { std::cerr << this << std::endl; }
Marc Slemko6f038a72006-08-03 18:58:09 +000050
Marc Slemko8a40a762006-07-19 17:46:50 +000051 void run() {
52
Marc Slemko9f27a4e2006-07-19 20:02:22 +000053 _endTime = Util::currentTime();
54
55 // Figure out error percentage
56
Mark Slee9b82d272007-05-23 05:16:07 +000057 int64_t delta = _endTime - _startTime;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000058
Konrad Grochowski16a23a62014-11-13 15:33:38 +010059 delta = delta > _timeout ? delta - _timeout : _timeout - delta;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000060
61 float error = delta / _timeout;
62
Konrad Grochowski16a23a62014-11-13 15:33:38 +010063 if (error < TEST_TOLERANCE) {
David Reiss96d23882007-07-26 21:10:32 +000064 _success = true;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000065 }
David Reiss0c90f6f2008-02-06 22:18:40 +000066
Marc Slemko8a40a762006-07-19 17:46:50 +000067 _done = true;
Marc Slemko6f038a72006-08-03 18:58:09 +000068
Konrad Grochowski16a23a62014-11-13 15:33:38 +010069 std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; // debug
Marc Slemko6f038a72006-08-03 18:58:09 +000070
Konrad Grochowski16a23a62014-11-13 15:33:38 +010071 {
72 Synchronized s(_monitor);
David Reiss96d23882007-07-26 21:10:32 +000073 _monitor.notifyAll();
Marc Slemko8a40a762006-07-19 17:46:50 +000074 }
David Reiss0c90f6f2008-02-06 22:18:40 +000075 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +000076
Mark Slee9b82d272007-05-23 05:16:07 +000077 int64_t _timeout;
78 int64_t _startTime;
79 int64_t _endTime;
Marc Slemko8a40a762006-07-19 17:46:50 +000080 Monitor& _monitor;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000081 bool _success;
Marc Slemko8a40a762006-07-19 17:46:50 +000082 bool _done;
83 };
84
Mark Sleef5f2be42006-09-05 21:05:31 +000085 /**
86 * This test creates two tasks and waits for the first to expire within 10%
87 * of the expected expiration time. It then verifies that the timer manager
88 * properly clean up itself and the remaining orphaned timeout task when the
89 * manager goes out of scope and its destructor is called.
90 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +010091 bool test00(int64_t timeout = 1000LL) {
Marc Slemko8a40a762006-07-19 17:46:50 +000092
Konrad Grochowski16a23a62014-11-13 15:33:38 +010093 shared_ptr<TimerManagerTests::Task> orphanTask
94 = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout));
Marc Slemko8a40a762006-07-19 17:46:50 +000095
Marc Slemko9f27a4e2006-07-19 20:02:22 +000096 {
Marc Slemko8a40a762006-07-19 17:46:50 +000097
Marc Slemko9f27a4e2006-07-19 20:02:22 +000098 TimerManager timerManager;
David Reiss0c90f6f2008-02-06 22:18:40 +000099
Roger Meier3faaedf2011-10-02 10:51:45 +0000100 timerManager.threadFactory(shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory()));
David Reiss0c90f6f2008-02-06 22:18:40 +0000101
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000102 timerManager.start();
David Reiss0c90f6f2008-02-06 22:18:40 +0000103
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000104 assert(timerManager.state() == TimerManager::STARTED);
Marc Slemko8a40a762006-07-19 17:46:50 +0000105
David Reiss52687eb2009-06-04 00:32:57 +0000106 // Don't create task yet, because its constructor sets the expected completion time, and we
107 // need to delay between inserting the two tasks into the run queue.
108 shared_ptr<TimerManagerTests::Task> task;
Marc Slemko8a40a762006-07-19 17:46:50 +0000109
Mark Sleef5f2be42006-09-05 21:05:31 +0000110 {
111 Synchronized s(_monitor);
Marc Slemko8a40a762006-07-19 17:46:50 +0000112
David Reiss96d23882007-07-26 21:10:32 +0000113 timerManager.add(orphanTask, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +0000114
David Reiss52687eb2009-06-04 00:32:57 +0000115 try {
116 // Wait for 1 second in order to give timerManager a chance to start sleeping in response
117 // to adding orphanTask. We need to do this so we can verify that adding the second task
118 // kicks the dispatcher out of the current wait and starts the new 1 second wait.
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100119 _monitor.wait(1000);
120 assert(
121 0 == "ERROR: This wait should time out. TimerManager dispatcher may have a problem.");
122 } catch (TimedOutException& ex) {
David Reiss52687eb2009-06-04 00:32:57 +0000123 }
124
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100125 task.reset(new TimerManagerTests::Task(_monitor, timeout));
David Reiss52687eb2009-06-04 00:32:57 +0000126
David Reiss96d23882007-07-26 21:10:32 +0000127 timerManager.add(task, timeout);
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000128
David Reiss96d23882007-07-26 21:10:32 +0000129 _monitor.wait();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000130 }
131
132 assert(task->_done);
133
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000134 std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +0000135 }
136
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000137 // timerManager.stop(); This is where it happens via destructor
Marc Slemko8a40a762006-07-19 17:46:50 +0000138
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000139 assert(!orphanTask->_done);
Marc Slemko8a40a762006-07-19 17:46:50 +0000140
Marc Slemko8a40a762006-07-19 17:46:50 +0000141 return true;
142 }
143
144 friend class TestTask;
145
146 Monitor _monitor;
147};
Marc Slemko8a40a762006-07-19 17:46:50 +0000148
Konrad Grochowski293a40e2014-09-04 17:28:17 +0400149const double TimerManagerTests::TEST_TOLERANCE = .20;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100150}
151}
152}
153} // apache::thrift::concurrency