blob: faab7336c5d6b5e16b6eefa45e79506232a83848 [file] [log] [blame]
Marc Slemko6f038a72006-08-03 18:58:09 +00001#include <concurrency/TimerManager.h>
2#include <concurrency/PosixThreadFactory.h>
3#include <concurrency/Monitor.h>
4#include <concurrency/Util.h>
Marc Slemko8a40a762006-07-19 17:46:50 +00005
6#include <assert.h>
7#include <iostream>
8
9namespace facebook { namespace thrift { namespace concurrency { namespace test {
10
11using namespace facebook::thrift::concurrency;
12
Mark Sleef5f2be42006-09-05 21:05:31 +000013/**
14 * ThreadManagerTests class
15 *
16 * @author marc
17 * @version $Id:$
18 */
Marc Slemko8a40a762006-07-19 17:46:50 +000019class TimerManagerTests {
20
Marc Slemko6f038a72006-08-03 18:58:09 +000021 public:
22
23 static const double ERROR;
24
Mark Sleef5f2be42006-09-05 21:05:31 +000025 class Task: public Runnable {
26 public:
Marc Slemko8a40a762006-07-19 17:46:50 +000027
Marc Slemko6f038a72006-08-03 18:58:09 +000028 Task(Monitor& monitor, long long timeout) :
29 _timeout(timeout),
30 _startTime(Util::currentTime()),
31 _monitor(monitor),
32 _success(false),
Mark Sleef5f2be42006-09-05 21:05:31 +000033 _done(false) {}
Marc Slemko8a40a762006-07-19 17:46:50 +000034
Mark Sleef5f2be42006-09-05 21:05:31 +000035 ~Task() { std::cerr << this << std::endl; }
Marc Slemko6f038a72006-08-03 18:58:09 +000036
Marc Slemko8a40a762006-07-19 17:46:50 +000037 void run() {
38
Marc Slemko9f27a4e2006-07-19 20:02:22 +000039 _endTime = Util::currentTime();
40
41 // Figure out error percentage
42
43 long long delta = _endTime - _startTime;
44
45
46 delta = delta > _timeout ? delta - _timeout : _timeout - delta;
47
48 float error = delta / _timeout;
49
Marc Slemko6f038a72006-08-03 18:58:09 +000050 if(error < ERROR) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +000051 _success = true;
52 }
53
Marc Slemko8a40a762006-07-19 17:46:50 +000054 _done = true;
Marc Slemko6f038a72006-08-03 18:58:09 +000055
56 std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; //debug
57
Marc Slemko8a40a762006-07-19 17:46:50 +000058 {Synchronized s(_monitor);
59 _monitor.notifyAll();
60 }
Mark Sleef5f2be42006-09-05 21:05:31 +000061 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +000062
63 long long _timeout;
64 long long _startTime;
65 long long _endTime;
Marc Slemko8a40a762006-07-19 17:46:50 +000066 Monitor& _monitor;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000067 bool _success;
Marc Slemko8a40a762006-07-19 17:46:50 +000068 bool _done;
69 };
70
Mark Sleef5f2be42006-09-05 21:05:31 +000071 /**
72 * This test creates two tasks and waits for the first to expire within 10%
73 * of the expected expiration time. It then verifies that the timer manager
74 * properly clean up itself and the remaining orphaned timeout task when the
75 * manager goes out of scope and its destructor is called.
76 */
Marc Slemko9f27a4e2006-07-19 20:02:22 +000077 bool test00(long long timeout=1000LL) {
Marc Slemko8a40a762006-07-19 17:46:50 +000078
Marc Slemko6f038a72006-08-03 18:58:09 +000079 shared_ptr<TimerManagerTests::Task> orphanTask = 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;
84
Marc Slemko6f038a72006-08-03 18:58:09 +000085 timerManager.threadFactory(shared_ptr<PosixThreadFactory>(new PosixThreadFactory()));
Marc Slemko9f27a4e2006-07-19 20:02:22 +000086
87 timerManager.start();
88
89 assert(timerManager.state() == TimerManager::STARTED);
Marc Slemko8a40a762006-07-19 17:46:50 +000090
Marc Slemko6f038a72006-08-03 18:58:09 +000091 shared_ptr<TimerManagerTests::Task> task = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout));
Marc Slemko8a40a762006-07-19 17:46:50 +000092
Mark Sleef5f2be42006-09-05 21:05:31 +000093 {
94 Synchronized s(_monitor);
Marc Slemko8a40a762006-07-19 17:46:50 +000095
Marc Slemko9f27a4e2006-07-19 20:02:22 +000096 timerManager.add(orphanTask, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +000097
Marc Slemko9f27a4e2006-07-19 20:02:22 +000098 timerManager.add(task, timeout);
99
100 _monitor.wait();
101 }
102
103 assert(task->_done);
104
105
106 std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +0000107 }
108
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000109 // timerManager.stop(); This is where it happens via destructor
Marc Slemko8a40a762006-07-19 17:46:50 +0000110
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000111 assert(!orphanTask->_done);
Marc Slemko8a40a762006-07-19 17:46:50 +0000112
Marc Slemko8a40a762006-07-19 17:46:50 +0000113 return true;
114 }
115
116 friend class TestTask;
117
118 Monitor _monitor;
119};
Marc Slemko8a40a762006-07-19 17:46:50 +0000120
Marc Slemko6f038a72006-08-03 18:58:09 +0000121const double TimerManagerTests::ERROR = .20;
122
Marc Slemko8a40a762006-07-19 17:46:50 +0000123}}}} // facebook::thrift::concurrency
124