blob: a6a26610c3b770edc063f6769f93955edfcb147d [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Marc Slemko6f038a72006-08-03 18:58:09 +00007#include <concurrency/TimerManager.h>
8#include <concurrency/PosixThreadFactory.h>
9#include <concurrency/Monitor.h>
10#include <concurrency/Util.h>
Marc Slemko8a40a762006-07-19 17:46:50 +000011
12#include <assert.h>
13#include <iostream>
14
T Jake Lucianib5e62212009-01-31 22:36:20 +000015namespace apache { namespace thrift { namespace concurrency { namespace test {
Marc Slemko8a40a762006-07-19 17:46:50 +000016
T Jake Lucianib5e62212009-01-31 22:36:20 +000017using namespace apache::thrift::concurrency;
Marc Slemko8a40a762006-07-19 17:46:50 +000018
Mark Sleef5f2be42006-09-05 21:05:31 +000019/**
David Reiss0c90f6f2008-02-06 22:18:40 +000020 * ThreadManagerTests class
Mark Sleef5f2be42006-09-05 21:05:31 +000021 *
Mark Sleef5f2be42006-09-05 21:05:31 +000022 * @version $Id:$
23 */
Marc Slemko8a40a762006-07-19 17:46:50 +000024class TimerManagerTests {
25
Marc Slemko6f038a72006-08-03 18:58:09 +000026 public:
27
28 static const double ERROR;
29
Mark Sleef5f2be42006-09-05 21:05:31 +000030 class Task: public Runnable {
31 public:
David Reiss0c90f6f2008-02-06 22:18:40 +000032
33 Task(Monitor& monitor, int64_t timeout) :
Marc Slemko6f038a72006-08-03 18:58:09 +000034 _timeout(timeout),
35 _startTime(Util::currentTime()),
36 _monitor(monitor),
37 _success(false),
Mark Sleef5f2be42006-09-05 21:05:31 +000038 _done(false) {}
Marc Slemko8a40a762006-07-19 17:46:50 +000039
Mark Sleef5f2be42006-09-05 21:05:31 +000040 ~Task() { std::cerr << this << std::endl; }
Marc Slemko6f038a72006-08-03 18:58:09 +000041
Marc Slemko8a40a762006-07-19 17:46:50 +000042 void run() {
43
Marc Slemko9f27a4e2006-07-19 20:02:22 +000044 _endTime = Util::currentTime();
45
46 // Figure out error percentage
47
Mark Slee9b82d272007-05-23 05:16:07 +000048 int64_t delta = _endTime - _startTime;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000049
50
51 delta = delta > _timeout ? delta - _timeout : _timeout - delta;
52
53 float error = delta / _timeout;
54
Marc Slemko6f038a72006-08-03 18:58:09 +000055 if(error < ERROR) {
David Reiss96d23882007-07-26 21:10:32 +000056 _success = true;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000057 }
David Reiss0c90f6f2008-02-06 22:18:40 +000058
Marc Slemko8a40a762006-07-19 17:46:50 +000059 _done = true;
Marc Slemko6f038a72006-08-03 18:58:09 +000060
David Reiss0c90f6f2008-02-06 22:18:40 +000061 std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; //debug
Marc Slemko6f038a72006-08-03 18:58:09 +000062
Marc Slemko8a40a762006-07-19 17:46:50 +000063 {Synchronized s(_monitor);
David Reiss96d23882007-07-26 21:10:32 +000064 _monitor.notifyAll();
Marc Slemko8a40a762006-07-19 17:46:50 +000065 }
David Reiss0c90f6f2008-02-06 22:18:40 +000066 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +000067
Mark Slee9b82d272007-05-23 05:16:07 +000068 int64_t _timeout;
69 int64_t _startTime;
70 int64_t _endTime;
Marc Slemko8a40a762006-07-19 17:46:50 +000071 Monitor& _monitor;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000072 bool _success;
Marc Slemko8a40a762006-07-19 17:46:50 +000073 bool _done;
74 };
75
Mark Sleef5f2be42006-09-05 21:05:31 +000076 /**
77 * This test creates two tasks and waits for the first to expire within 10%
78 * of the expected expiration time. It then verifies that the timer manager
79 * properly clean up itself and the remaining orphaned timeout task when the
80 * manager goes out of scope and its destructor is called.
81 */
Mark Slee9b82d272007-05-23 05:16:07 +000082 bool test00(int64_t timeout=1000LL) {
Marc Slemko8a40a762006-07-19 17:46:50 +000083
Marc Slemko6f038a72006-08-03 18:58:09 +000084 shared_ptr<TimerManagerTests::Task> orphanTask = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout));
Marc Slemko8a40a762006-07-19 17:46:50 +000085
Marc Slemko9f27a4e2006-07-19 20:02:22 +000086 {
Marc Slemko8a40a762006-07-19 17:46:50 +000087
Marc Slemko9f27a4e2006-07-19 20:02:22 +000088 TimerManager timerManager;
David Reiss0c90f6f2008-02-06 22:18:40 +000089
Marc Slemko6f038a72006-08-03 18:58:09 +000090 timerManager.threadFactory(shared_ptr<PosixThreadFactory>(new PosixThreadFactory()));
David Reiss0c90f6f2008-02-06 22:18:40 +000091
Marc Slemko9f27a4e2006-07-19 20:02:22 +000092 timerManager.start();
David Reiss0c90f6f2008-02-06 22:18:40 +000093
Marc Slemko9f27a4e2006-07-19 20:02:22 +000094 assert(timerManager.state() == TimerManager::STARTED);
Marc Slemko8a40a762006-07-19 17:46:50 +000095
Marc Slemko6f038a72006-08-03 18:58:09 +000096 shared_ptr<TimerManagerTests::Task> task = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout));
Marc Slemko8a40a762006-07-19 17:46:50 +000097
Mark Sleef5f2be42006-09-05 21:05:31 +000098 {
99 Synchronized s(_monitor);
Marc Slemko8a40a762006-07-19 17:46:50 +0000100
David Reiss96d23882007-07-26 21:10:32 +0000101 timerManager.add(orphanTask, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +0000102
David Reiss96d23882007-07-26 21:10:32 +0000103 timerManager.add(task, timeout);
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000104
David Reiss96d23882007-07-26 21:10:32 +0000105 _monitor.wait();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000106 }
107
108 assert(task->_done);
109
110
111 std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +0000112 }
113
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000114 // timerManager.stop(); This is where it happens via destructor
Marc Slemko8a40a762006-07-19 17:46:50 +0000115
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000116 assert(!orphanTask->_done);
Marc Slemko8a40a762006-07-19 17:46:50 +0000117
Marc Slemko8a40a762006-07-19 17:46:50 +0000118 return true;
119 }
120
121 friend class TestTask;
122
123 Monitor _monitor;
124};
Marc Slemko8a40a762006-07-19 17:46:50 +0000125
Marc Slemko6f038a72006-08-03 18:58:09 +0000126const double TimerManagerTests::ERROR = .20;
David Reiss0c90f6f2008-02-06 22:18:40 +0000127
T Jake Lucianib5e62212009-01-31 22:36:20 +0000128}}}} // apache::thrift::concurrency
Marc Slemko8a40a762006-07-19 17:46:50 +0000129