Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 1 | #include <TimerManager.h> |
| 2 | #include <PosixThreadFactory.h> |
| 3 | #include <Monitor.h> |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 4 | #include <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 | |
| 20 | class Task: public Runnable { |
| 21 | |
| 22 | public: |
| 23 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 24 | Task(Monitor& monitor, long long timeout) : |
| 25 | _timeout(timeout), |
| 26 | _startTime(Util::currentTime()), |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 27 | _monitor(monitor), |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 28 | _success(false), |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 29 | _done(false) {} |
| 30 | |
| 31 | void run() { |
| 32 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 33 | _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 Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 48 | std::cout << "\t\t\tHello World" << std::endl; |
| 49 | |
| 50 | _done = true; |
| 51 | |
| 52 | {Synchronized s(_monitor); |
| 53 | _monitor.notifyAll(); |
| 54 | } |
| 55 | } |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | long long _timeout; |
| 59 | long long _startTime; |
| 60 | long long _endTime; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 61 | Monitor& _monitor; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 62 | bool _success; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 63 | bool _done; |
| 64 | }; |
| 65 | |
| 66 | public: |
| 67 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 68 | /** 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 Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 71 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 72 | bool test00(long long timeout=1000LL) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 73 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 74 | TimerManagerTests::Task* orphanTask = new TimerManagerTests::Task(_monitor, 10 * timeout); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 75 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 76 | { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 77 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 78 | TimerManager timerManager; |
| 79 | |
| 80 | timerManager.threadFactory(new PosixThreadFactory()); |
| 81 | |
| 82 | timerManager.start(); |
| 83 | |
| 84 | assert(timerManager.state() == TimerManager::STARTED); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 85 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 86 | TimerManagerTests::Task* task = new TimerManagerTests::Task(_monitor, timeout); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 87 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 88 | {Synchronized s(_monitor); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 89 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 90 | timerManager.add(orphanTask, 10 * timeout); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 91 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 92 | 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 Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 105 | // timerManager.stop(); This is where it happens via destructor |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 106 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 107 | assert(!orphanTask->_done); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 108 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 109 | delete orphanTask; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | friend class TestTask; |
| 115 | |
| 116 | Monitor _monitor; |
| 117 | }; |
| 118 | |
| 119 | |
| 120 | }}}} // facebook::thrift::concurrency |
| 121 | |