blob: 62eb4f43fd32093a63921cdcef7577eff55b4d45 [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
T Jake Lucianib5e62212009-01-31 22:36:20 +000028namespace apache { namespace thrift { namespace concurrency { namespace test {
Marc Slemko8a40a762006-07-19 17:46:50 +000029
T Jake Lucianib5e62212009-01-31 22:36:20 +000030using namespace apache::thrift::concurrency;
Marc Slemko8a40a762006-07-19 17:46:50 +000031
Marc Slemko8a40a762006-07-19 17:46:50 +000032class TimerManagerTests {
33
Konrad Grochowski293a40e2014-09-04 17:28:17 +040034 static const double TEST_TOLERANCE;
35
Marc Slemko6f038a72006-08-03 18:58:09 +000036 public:
Mark Sleef5f2be42006-09-05 21:05:31 +000037 class Task: public Runnable {
38 public:
David Reiss0c90f6f2008-02-06 22:18:40 +000039
40 Task(Monitor& monitor, int64_t timeout) :
Marc Slemko6f038a72006-08-03 18:58:09 +000041 _timeout(timeout),
42 _startTime(Util::currentTime()),
43 _monitor(monitor),
44 _success(false),
Mark Sleef5f2be42006-09-05 21:05:31 +000045 _done(false) {}
Marc Slemko8a40a762006-07-19 17:46:50 +000046
Mark Sleef5f2be42006-09-05 21:05:31 +000047 ~Task() { std::cerr << this << std::endl; }
Marc Slemko6f038a72006-08-03 18:58:09 +000048
Marc Slemko8a40a762006-07-19 17:46:50 +000049 void run() {
50
Marc Slemko9f27a4e2006-07-19 20:02:22 +000051 _endTime = Util::currentTime();
52
53 // Figure out error percentage
54
Mark Slee9b82d272007-05-23 05:16:07 +000055 int64_t delta = _endTime - _startTime;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000056
57
58 delta = delta > _timeout ? delta - _timeout : _timeout - delta;
59
60 float error = delta / _timeout;
61
Konrad Grochowski293a40e2014-09-04 17:28:17 +040062 if(error < TEST_TOLERANCE) {
David Reiss96d23882007-07-26 21:10:32 +000063 _success = true;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000064 }
David Reiss0c90f6f2008-02-06 22:18:40 +000065
Marc Slemko8a40a762006-07-19 17:46:50 +000066 _done = true;
Marc Slemko6f038a72006-08-03 18:58:09 +000067
David Reiss0c90f6f2008-02-06 22:18:40 +000068 std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; //debug
Marc Slemko6f038a72006-08-03 18:58:09 +000069
Marc Slemko8a40a762006-07-19 17:46:50 +000070 {Synchronized s(_monitor);
David Reiss96d23882007-07-26 21:10:32 +000071 _monitor.notifyAll();
Marc Slemko8a40a762006-07-19 17:46:50 +000072 }
David Reiss0c90f6f2008-02-06 22:18:40 +000073 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +000074
Mark Slee9b82d272007-05-23 05:16:07 +000075 int64_t _timeout;
76 int64_t _startTime;
77 int64_t _endTime;
Marc Slemko8a40a762006-07-19 17:46:50 +000078 Monitor& _monitor;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000079 bool _success;
Marc Slemko8a40a762006-07-19 17:46:50 +000080 bool _done;
81 };
82
Mark Sleef5f2be42006-09-05 21:05:31 +000083 /**
84 * This test creates two tasks and waits for the first to expire within 10%
85 * of the expected expiration time. It then verifies that the timer manager
86 * properly clean up itself and the remaining orphaned timeout task when the
87 * manager goes out of scope and its destructor is called.
88 */
Mark Slee9b82d272007-05-23 05:16:07 +000089 bool test00(int64_t timeout=1000LL) {
Marc Slemko8a40a762006-07-19 17:46:50 +000090
Marc Slemko6f038a72006-08-03 18:58:09 +000091 shared_ptr<TimerManagerTests::Task> orphanTask = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout));
Marc Slemko8a40a762006-07-19 17:46:50 +000092
Marc Slemko9f27a4e2006-07-19 20:02:22 +000093 {
Marc Slemko8a40a762006-07-19 17:46:50 +000094
Marc Slemko9f27a4e2006-07-19 20:02:22 +000095 TimerManager timerManager;
David Reiss0c90f6f2008-02-06 22:18:40 +000096
Roger Meier3faaedf2011-10-02 10:51:45 +000097 timerManager.threadFactory(shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory()));
David Reiss0c90f6f2008-02-06 22:18:40 +000098
Marc Slemko9f27a4e2006-07-19 20:02:22 +000099 timerManager.start();
David Reiss0c90f6f2008-02-06 22:18:40 +0000100
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000101 assert(timerManager.state() == TimerManager::STARTED);
Marc Slemko8a40a762006-07-19 17:46:50 +0000102
David Reiss52687eb2009-06-04 00:32:57 +0000103 // Don't create task yet, because its constructor sets the expected completion time, and we
104 // need to delay between inserting the two tasks into the run queue.
105 shared_ptr<TimerManagerTests::Task> task;
Marc Slemko8a40a762006-07-19 17:46:50 +0000106
Mark Sleef5f2be42006-09-05 21:05:31 +0000107 {
108 Synchronized s(_monitor);
Marc Slemko8a40a762006-07-19 17:46:50 +0000109
David Reiss96d23882007-07-26 21:10:32 +0000110 timerManager.add(orphanTask, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +0000111
David Reiss52687eb2009-06-04 00:32:57 +0000112 try {
113 // Wait for 1 second in order to give timerManager a chance to start sleeping in response
114 // to adding orphanTask. We need to do this so we can verify that adding the second task
115 // kicks the dispatcher out of the current wait and starts the new 1 second wait.
116 _monitor.wait (1000);
117 assert (0 == "ERROR: This wait should time out. TimerManager dispatcher may have a problem.");
118 } catch (TimedOutException &ex) {
119 }
120
121 task.reset (new TimerManagerTests::Task(_monitor, timeout));
122
David Reiss96d23882007-07-26 21:10:32 +0000123 timerManager.add(task, timeout);
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000124
David Reiss96d23882007-07-26 21:10:32 +0000125 _monitor.wait();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000126 }
127
128 assert(task->_done);
129
130
131 std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +0000132 }
133
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000134 // timerManager.stop(); This is where it happens via destructor
Marc Slemko8a40a762006-07-19 17:46:50 +0000135
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000136 assert(!orphanTask->_done);
Marc Slemko8a40a762006-07-19 17:46:50 +0000137
Marc Slemko8a40a762006-07-19 17:46:50 +0000138 return true;
139 }
140
141 friend class TestTask;
142
143 Monitor _monitor;
144};
Marc Slemko8a40a762006-07-19 17:46:50 +0000145
Konrad Grochowski293a40e2014-09-04 17:28:17 +0400146const double TimerManagerTests::TEST_TOLERANCE = .20;
David Reiss0c90f6f2008-02-06 22:18:40 +0000147
T Jake Lucianib5e62212009-01-31 22:36:20 +0000148}}}} // apache::thrift::concurrency