blob: b89074c076d3079c202ea34e748b1fb99ddb3a27 [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
Marc Slemko6f038a72006-08-03 18:58:09 +000020#include <concurrency/TimerManager.h>
21#include <concurrency/PosixThreadFactory.h>
22#include <concurrency/Monitor.h>
23#include <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
Mark Sleef5f2be42006-09-05 21:05:31 +000032/**
David Reiss0c90f6f2008-02-06 22:18:40 +000033 * ThreadManagerTests class
Mark Sleef5f2be42006-09-05 21:05:31 +000034 *
Mark Sleef5f2be42006-09-05 21:05:31 +000035 * @version $Id:$
36 */
Marc Slemko8a40a762006-07-19 17:46:50 +000037class TimerManagerTests {
38
Marc Slemko6f038a72006-08-03 18:58:09 +000039 public:
40
41 static const double ERROR;
42
Mark Sleef5f2be42006-09-05 21:05:31 +000043 class Task: public Runnable {
44 public:
David Reiss0c90f6f2008-02-06 22:18:40 +000045
46 Task(Monitor& monitor, int64_t timeout) :
Marc Slemko6f038a72006-08-03 18:58:09 +000047 _timeout(timeout),
48 _startTime(Util::currentTime()),
49 _monitor(monitor),
50 _success(false),
Mark Sleef5f2be42006-09-05 21:05:31 +000051 _done(false) {}
Marc Slemko8a40a762006-07-19 17:46:50 +000052
Mark Sleef5f2be42006-09-05 21:05:31 +000053 ~Task() { std::cerr << this << std::endl; }
Marc Slemko6f038a72006-08-03 18:58:09 +000054
Marc Slemko8a40a762006-07-19 17:46:50 +000055 void run() {
56
Marc Slemko9f27a4e2006-07-19 20:02:22 +000057 _endTime = Util::currentTime();
58
59 // Figure out error percentage
60
Mark Slee9b82d272007-05-23 05:16:07 +000061 int64_t delta = _endTime - _startTime;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000062
63
64 delta = delta > _timeout ? delta - _timeout : _timeout - delta;
65
66 float error = delta / _timeout;
67
Marc Slemko6f038a72006-08-03 18:58:09 +000068 if(error < ERROR) {
David Reiss96d23882007-07-26 21:10:32 +000069 _success = true;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000070 }
David Reiss0c90f6f2008-02-06 22:18:40 +000071
Marc Slemko8a40a762006-07-19 17:46:50 +000072 _done = true;
Marc Slemko6f038a72006-08-03 18:58:09 +000073
David Reiss0c90f6f2008-02-06 22:18:40 +000074 std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; //debug
Marc Slemko6f038a72006-08-03 18:58:09 +000075
Marc Slemko8a40a762006-07-19 17:46:50 +000076 {Synchronized s(_monitor);
David Reiss96d23882007-07-26 21:10:32 +000077 _monitor.notifyAll();
Marc Slemko8a40a762006-07-19 17:46:50 +000078 }
David Reiss0c90f6f2008-02-06 22:18:40 +000079 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +000080
Mark Slee9b82d272007-05-23 05:16:07 +000081 int64_t _timeout;
82 int64_t _startTime;
83 int64_t _endTime;
Marc Slemko8a40a762006-07-19 17:46:50 +000084 Monitor& _monitor;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000085 bool _success;
Marc Slemko8a40a762006-07-19 17:46:50 +000086 bool _done;
87 };
88
Mark Sleef5f2be42006-09-05 21:05:31 +000089 /**
90 * This test creates two tasks and waits for the first to expire within 10%
91 * of the expected expiration time. It then verifies that the timer manager
92 * properly clean up itself and the remaining orphaned timeout task when the
93 * manager goes out of scope and its destructor is called.
94 */
Mark Slee9b82d272007-05-23 05:16:07 +000095 bool test00(int64_t timeout=1000LL) {
Marc Slemko8a40a762006-07-19 17:46:50 +000096
Marc Slemko6f038a72006-08-03 18:58:09 +000097 shared_ptr<TimerManagerTests::Task> orphanTask = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout));
Marc Slemko8a40a762006-07-19 17:46:50 +000098
Marc Slemko9f27a4e2006-07-19 20:02:22 +000099 {
Marc Slemko8a40a762006-07-19 17:46:50 +0000100
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000101 TimerManager timerManager;
David Reiss0c90f6f2008-02-06 22:18:40 +0000102
Marc Slemko6f038a72006-08-03 18:58:09 +0000103 timerManager.threadFactory(shared_ptr<PosixThreadFactory>(new PosixThreadFactory()));
David Reiss0c90f6f2008-02-06 22:18:40 +0000104
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000105 timerManager.start();
David Reiss0c90f6f2008-02-06 22:18:40 +0000106
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000107 assert(timerManager.state() == TimerManager::STARTED);
Marc Slemko8a40a762006-07-19 17:46:50 +0000108
David Reiss52687eb2009-06-04 00:32:57 +0000109 // Don't create task yet, because its constructor sets the expected completion time, and we
110 // need to delay between inserting the two tasks into the run queue.
111 shared_ptr<TimerManagerTests::Task> task;
Marc Slemko8a40a762006-07-19 17:46:50 +0000112
Mark Sleef5f2be42006-09-05 21:05:31 +0000113 {
114 Synchronized s(_monitor);
Marc Slemko8a40a762006-07-19 17:46:50 +0000115
David Reiss96d23882007-07-26 21:10:32 +0000116 timerManager.add(orphanTask, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +0000117
David Reiss52687eb2009-06-04 00:32:57 +0000118 try {
119 // Wait for 1 second in order to give timerManager a chance to start sleeping in response
120 // to adding orphanTask. We need to do this so we can verify that adding the second task
121 // kicks the dispatcher out of the current wait and starts the new 1 second wait.
122 _monitor.wait (1000);
123 assert (0 == "ERROR: This wait should time out. TimerManager dispatcher may have a problem.");
124 } catch (TimedOutException &ex) {
125 }
126
127 task.reset (new TimerManagerTests::Task(_monitor, timeout));
128
David Reiss96d23882007-07-26 21:10:32 +0000129 timerManager.add(task, timeout);
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000130
David Reiss96d23882007-07-26 21:10:32 +0000131 _monitor.wait();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000132 }
133
134 assert(task->_done);
135
136
137 std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +0000138 }
139
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000140 // timerManager.stop(); This is where it happens via destructor
Marc Slemko8a40a762006-07-19 17:46:50 +0000141
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000142 assert(!orphanTask->_done);
Marc Slemko8a40a762006-07-19 17:46:50 +0000143
Marc Slemko8a40a762006-07-19 17:46:50 +0000144 return true;
145 }
146
147 friend class TestTask;
148
149 Monitor _monitor;
150};
Marc Slemko8a40a762006-07-19 17:46:50 +0000151
Marc Slemko6f038a72006-08-03 18:58:09 +0000152const double TimerManagerTests::ERROR = .20;
David Reiss0c90f6f2008-02-06 22:18:40 +0000153
T Jake Lucianib5e62212009-01-31 22:36:20 +0000154}}}} // apache::thrift::concurrency
Marc Slemko8a40a762006-07-19 17:46:50 +0000155