blob: 32d39355f881da08b010521d6ebc415829114523 [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 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 Slemko8a40a762006-07-19 17:46:50 +000082
Marc Slemko9f27a4e2006-07-19 20:02:22 +000083 TimerManager timerManager;
David Reiss0c90f6f2008-02-06 22:18:40 +000084
Roger Meier3faaedf2011-10-02 10:51:45 +000085 timerManager.threadFactory(shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory()));
David Reiss0c90f6f2008-02-06 22:18:40 +000086
Marc Slemko9f27a4e2006-07-19 20:02:22 +000087 timerManager.start();
David Reiss0c90f6f2008-02-06 22:18:40 +000088
Marc Slemko9f27a4e2006-07-19 20:02:22 +000089 assert(timerManager.state() == TimerManager::STARTED);
Marc Slemko8a40a762006-07-19 17:46:50 +000090
David Reiss52687eb2009-06-04 00:32:57 +000091 // 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 Slemko8a40a762006-07-19 17:46:50 +000094
Mark Sleef5f2be42006-09-05 21:05:31 +000095 {
96 Synchronized s(_monitor);
Marc Slemko8a40a762006-07-19 17:46:50 +000097
David Reiss96d23882007-07-26 21:10:32 +000098 timerManager.add(orphanTask, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +000099
David Reiss52687eb2009-06-04 00:32:57 +0000100 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 Grochowski16a23a62014-11-13 15:33:38 +0100104 _monitor.wait(1000);
105 assert(
106 0 == "ERROR: This wait should time out. TimerManager dispatcher may have a problem.");
ben-craigfae08e72015-07-15 11:34:47 -0500107 } catch (TimedOutException&) {
David Reiss52687eb2009-06-04 00:32:57 +0000108 }
109
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100110 task.reset(new TimerManagerTests::Task(_monitor, timeout));
David Reiss52687eb2009-06-04 00:32:57 +0000111
David Reiss96d23882007-07-26 21:10:32 +0000112 timerManager.add(task, timeout);
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000113
David Reiss96d23882007-07-26 21:10:32 +0000114 _monitor.wait();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000115 }
116
117 assert(task->_done);
118
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000119 std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +0000120 }
121
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000122 // timerManager.stop(); This is where it happens via destructor
Marc Slemko8a40a762006-07-19 17:46:50 +0000123
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000124 assert(!orphanTask->_done);
Marc Slemko8a40a762006-07-19 17:46:50 +0000125
Marc Slemko8a40a762006-07-19 17:46:50 +0000126 return true;
127 }
128
129 friend class TestTask;
130
131 Monitor _monitor;
132};
Marc Slemko8a40a762006-07-19 17:46:50 +0000133
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100134}
135}
136}
137} // apache::thrift::concurrency