blob: 3c7fc0bc5dc988ae4521562d1aa6e7216ad2e063 [file] [log] [blame]
Marc Slemko8a40a762006-07-19 17:46:50 +00001#include <TimerManager.h>
2#include <PosixThreadFactory.h>
3#include <Monitor.h>
Marc Slemko9f27a4e2006-07-19 20:02:22 +00004#include <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
13/** ThreadManagerTests class
14
15 @author marc
16 @version $Id:$ */
17
18class TimerManagerTests {
19
20 class Task: public Runnable {
21
22 public:
23
Marc Slemko9f27a4e2006-07-19 20:02:22 +000024 Task(Monitor& monitor, long long timeout) :
25 _timeout(timeout),
26 _startTime(Util::currentTime()),
Marc Slemko8a40a762006-07-19 17:46:50 +000027 _monitor(monitor),
Marc Slemko9f27a4e2006-07-19 20:02:22 +000028 _success(false),
Marc Slemko8a40a762006-07-19 17:46:50 +000029 _done(false) {}
30
31 void run() {
32
Marc Slemko9f27a4e2006-07-19 20:02:22 +000033 _endTime = Util::currentTime();
34
35 // Figure out error percentage
36
37 long long delta = _endTime - _startTime;
38
39
40 delta = delta > _timeout ? delta - _timeout : _timeout - delta;
41
42 float error = delta / _timeout;
43
44 if(error < .10) {
45 _success = true;
46 }
47
Marc Slemko8a40a762006-07-19 17:46:50 +000048 std::cout << "\t\t\tHello World" << std::endl;
49
50 _done = true;
51
52 {Synchronized s(_monitor);
53 _monitor.notifyAll();
54 }
55 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +000056
57
58 long long _timeout;
59 long long _startTime;
60 long long _endTime;
Marc Slemko8a40a762006-07-19 17:46:50 +000061 Monitor& _monitor;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000062 bool _success;
Marc Slemko8a40a762006-07-19 17:46:50 +000063 bool _done;
64 };
65
66public:
67
Marc Slemko9f27a4e2006-07-19 20:02:22 +000068 /** This test creates two tasks and waits for the first to expire within 10% of the expected expiration time. It then verifies that
69 the timer manager properly clean up itself and the remaining orphaned timeout task when the manager goes out of scope and its
70 destructor is called. */
Marc Slemko8a40a762006-07-19 17:46:50 +000071
Marc Slemko9f27a4e2006-07-19 20:02:22 +000072 bool test00(long long timeout=1000LL) {
Marc Slemko8a40a762006-07-19 17:46:50 +000073
Marc Slemko9f27a4e2006-07-19 20:02:22 +000074 TimerManagerTests::Task* orphanTask = new TimerManagerTests::Task(_monitor, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +000075
Marc Slemko9f27a4e2006-07-19 20:02:22 +000076 {
Marc Slemko8a40a762006-07-19 17:46:50 +000077
Marc Slemko9f27a4e2006-07-19 20:02:22 +000078 TimerManager timerManager;
79
80 timerManager.threadFactory(new PosixThreadFactory());
81
82 timerManager.start();
83
84 assert(timerManager.state() == TimerManager::STARTED);
Marc Slemko8a40a762006-07-19 17:46:50 +000085
Marc Slemko9f27a4e2006-07-19 20:02:22 +000086 TimerManagerTests::Task* task = new TimerManagerTests::Task(_monitor, timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +000087
Marc Slemko9f27a4e2006-07-19 20:02:22 +000088 {Synchronized s(_monitor);
Marc Slemko8a40a762006-07-19 17:46:50 +000089
Marc Slemko9f27a4e2006-07-19 20:02:22 +000090 timerManager.add(orphanTask, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +000091
Marc Slemko9f27a4e2006-07-19 20:02:22 +000092 timerManager.add(task, timeout);
93
94 _monitor.wait();
95 }
96
97 assert(task->_done);
98
99
100 std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
101
102 delete task;
Marc Slemko8a40a762006-07-19 17:46:50 +0000103 }
104
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000105 // timerManager.stop(); This is where it happens via destructor
Marc Slemko8a40a762006-07-19 17:46:50 +0000106
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000107 assert(!orphanTask->_done);
Marc Slemko8a40a762006-07-19 17:46:50 +0000108
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000109 delete orphanTask;
Marc Slemko8a40a762006-07-19 17:46:50 +0000110
111 return true;
112 }
113
114 friend class TestTask;
115
116 Monitor _monitor;
117};
118
119
120}}}} // facebook::thrift::concurrency
121