Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 1 | #include <concurrency/TimerManager.h> |
| 2 | #include <concurrency/PosixThreadFactory.h> |
| 3 | #include <concurrency/Monitor.h> |
| 4 | #include <concurrency/Util.h> |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 5 | |
| 6 | #include <assert.h> |
| 7 | #include <iostream> |
| 8 | |
| 9 | namespace facebook { namespace thrift { namespace concurrency { namespace test { |
| 10 | |
| 11 | using namespace facebook::thrift::concurrency; |
| 12 | |
| 13 | /** ThreadManagerTests class |
| 14 | |
| 15 | @author marc |
| 16 | @version $Id:$ */ |
| 17 | |
| 18 | class TimerManagerTests { |
| 19 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 20 | public: |
| 21 | |
| 22 | static const double ERROR; |
| 23 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 24 | class Task: public Runnable { |
| 25 | |
| 26 | public: |
| 27 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 28 | Task(Monitor& monitor, long long timeout) : |
| 29 | _timeout(timeout), |
| 30 | _startTime(Util::currentTime()), |
| 31 | _monitor(monitor), |
| 32 | _success(false), |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 33 | _done(false) {} |
| 34 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 35 | ~Task() {std::cerr << this << std::endl;} |
| 36 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 37 | void run() { |
| 38 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 39 | _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 Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 50 | if(error < ERROR) { |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 51 | _success = true; |
| 52 | } |
| 53 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 54 | _done = true; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 55 | |
| 56 | std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; //debug |
| 57 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 58 | {Synchronized s(_monitor); |
| 59 | _monitor.notifyAll(); |
| 60 | } |
| 61 | } |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 62 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 63 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 64 | |
| 65 | long long _timeout; |
| 66 | long long _startTime; |
| 67 | long long _endTime; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 68 | Monitor& _monitor; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 69 | bool _success; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 70 | bool _done; |
| 71 | }; |
| 72 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 73 | /** This test creates two tasks and waits for the first to expire within 10% of the expected expiration time. It then verifies that |
| 74 | the timer manager properly clean up itself and the remaining orphaned timeout task when the manager goes out of scope and its |
| 75 | destructor is called. */ |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 76 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 77 | bool test00(long long timeout=1000LL) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 78 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 79 | shared_ptr<TimerManagerTests::Task> orphanTask = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout)); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 80 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 81 | { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 82 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 83 | TimerManager timerManager; |
| 84 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 85 | timerManager.threadFactory(shared_ptr<PosixThreadFactory>(new PosixThreadFactory())); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 86 | |
| 87 | timerManager.start(); |
| 88 | |
| 89 | assert(timerManager.state() == TimerManager::STARTED); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 90 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 91 | shared_ptr<TimerManagerTests::Task> task = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout)); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 92 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 93 | {Synchronized s(_monitor); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 94 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 95 | timerManager.add(orphanTask, 10 * timeout); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 96 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 97 | timerManager.add(task, timeout); |
| 98 | |
| 99 | _monitor.wait(); |
| 100 | } |
| 101 | |
| 102 | assert(task->_done); |
| 103 | |
| 104 | |
| 105 | std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 108 | // timerManager.stop(); This is where it happens via destructor |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 109 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 110 | assert(!orphanTask->_done); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 111 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | |
| 115 | friend class TestTask; |
| 116 | |
| 117 | Monitor _monitor; |
| 118 | }; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 119 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 120 | const double TimerManagerTests::ERROR = .20; |
| 121 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 122 | }}}} // facebook::thrift::concurrency |
| 123 | |