blob: 0041d9cb24c81503034a5ceecce406927e22d9e3 [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
15namespace facebook { namespace thrift { namespace concurrency { namespace test {
16
17using namespace facebook::thrift::concurrency;
18
Mark Sleef5f2be42006-09-05 21:05:31 +000019/**
20 * ThreadManagerTests class
21 *
22 * @author marc
23 * @version $Id:$
24 */
Marc Slemko8a40a762006-07-19 17:46:50 +000025class TimerManagerTests {
26
Marc Slemko6f038a72006-08-03 18:58:09 +000027 public:
28
29 static const double ERROR;
30
Mark Sleef5f2be42006-09-05 21:05:31 +000031 class Task: public Runnable {
32 public:
Marc Slemko8a40a762006-07-19 17:46:50 +000033
Marc Slemko6f038a72006-08-03 18:58:09 +000034 Task(Monitor& monitor, long long timeout) :
35 _timeout(timeout),
36 _startTime(Util::currentTime()),
37 _monitor(monitor),
38 _success(false),
Mark Sleef5f2be42006-09-05 21:05:31 +000039 _done(false) {}
Marc Slemko8a40a762006-07-19 17:46:50 +000040
Mark Sleef5f2be42006-09-05 21:05:31 +000041 ~Task() { std::cerr << this << std::endl; }
Marc Slemko6f038a72006-08-03 18:58:09 +000042
Marc Slemko8a40a762006-07-19 17:46:50 +000043 void run() {
44
Marc Slemko9f27a4e2006-07-19 20:02:22 +000045 _endTime = Util::currentTime();
46
47 // Figure out error percentage
48
49 long long delta = _endTime - _startTime;
50
51
52 delta = delta > _timeout ? delta - _timeout : _timeout - delta;
53
54 float error = delta / _timeout;
55
Marc Slemko6f038a72006-08-03 18:58:09 +000056 if(error < ERROR) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +000057 _success = true;
58 }
59
Marc Slemko8a40a762006-07-19 17:46:50 +000060 _done = true;
Marc Slemko6f038a72006-08-03 18:58:09 +000061
62 std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; //debug
63
Marc Slemko8a40a762006-07-19 17:46:50 +000064 {Synchronized s(_monitor);
65 _monitor.notifyAll();
66 }
Mark Sleef5f2be42006-09-05 21:05:31 +000067 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +000068
69 long long _timeout;
70 long long _startTime;
71 long long _endTime;
Marc Slemko8a40a762006-07-19 17:46:50 +000072 Monitor& _monitor;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000073 bool _success;
Marc Slemko8a40a762006-07-19 17:46:50 +000074 bool _done;
75 };
76
Mark Sleef5f2be42006-09-05 21:05:31 +000077 /**
78 * This test creates two tasks and waits for the first to expire within 10%
79 * of the expected expiration time. It then verifies that the timer manager
80 * properly clean up itself and the remaining orphaned timeout task when the
81 * manager goes out of scope and its destructor is called.
82 */
Marc Slemko9f27a4e2006-07-19 20:02:22 +000083 bool test00(long long timeout=1000LL) {
Marc Slemko8a40a762006-07-19 17:46:50 +000084
Marc Slemko6f038a72006-08-03 18:58:09 +000085 shared_ptr<TimerManagerTests::Task> orphanTask = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout));
Marc Slemko8a40a762006-07-19 17:46:50 +000086
Marc Slemko9f27a4e2006-07-19 20:02:22 +000087 {
Marc Slemko8a40a762006-07-19 17:46:50 +000088
Marc Slemko9f27a4e2006-07-19 20:02:22 +000089 TimerManager timerManager;
90
Marc Slemko6f038a72006-08-03 18:58:09 +000091 timerManager.threadFactory(shared_ptr<PosixThreadFactory>(new PosixThreadFactory()));
Marc Slemko9f27a4e2006-07-19 20:02:22 +000092
93 timerManager.start();
94
95 assert(timerManager.state() == TimerManager::STARTED);
Marc Slemko8a40a762006-07-19 17:46:50 +000096
Marc Slemko6f038a72006-08-03 18:58:09 +000097 shared_ptr<TimerManagerTests::Task> task = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout));
Marc Slemko8a40a762006-07-19 17:46:50 +000098
Mark Sleef5f2be42006-09-05 21:05:31 +000099 {
100 Synchronized s(_monitor);
Marc Slemko8a40a762006-07-19 17:46:50 +0000101
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000102 timerManager.add(orphanTask, 10 * timeout);
Marc Slemko8a40a762006-07-19 17:46:50 +0000103
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000104 timerManager.add(task, timeout);
105
106 _monitor.wait();
107 }
108
109 assert(task->_done);
110
111
112 std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +0000113 }
114
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000115 // timerManager.stop(); This is where it happens via destructor
Marc Slemko8a40a762006-07-19 17:46:50 +0000116
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000117 assert(!orphanTask->_done);
Marc Slemko8a40a762006-07-19 17:46:50 +0000118
Marc Slemko8a40a762006-07-19 17:46:50 +0000119 return true;
120 }
121
122 friend class TestTask;
123
124 Monitor _monitor;
125};
Marc Slemko8a40a762006-07-19 17:46:50 +0000126
Marc Slemko6f038a72006-08-03 18:58:09 +0000127const double TimerManagerTests::ERROR = .20;
128
Marc Slemko8a40a762006-07-19 17:46:50 +0000129}}}} // facebook::thrift::concurrency
130