Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // 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 Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 7 | #include <concurrency/TimerManager.h> |
8 | #include <concurrency/PosixThreadFactory.h> | ||||
9 | #include <concurrency/Monitor.h> | ||||
10 | #include <concurrency/Util.h> | ||||
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 11 | |
12 | #include <assert.h> | ||||
13 | #include <iostream> | ||||
14 | |||||
15 | namespace facebook { namespace thrift { namespace concurrency { namespace test { | ||||
16 | |||||
17 | using namespace facebook::thrift::concurrency; | ||||
18 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 19 | /** |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 20 | * ThreadManagerTests class |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 21 | * |
22 | * @author marc | ||||
23 | * @version $Id:$ | ||||
24 | */ | ||||
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 25 | class TimerManagerTests { |
26 | |||||
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 27 | public: |
28 | |||||
29 | static const double ERROR; | ||||
30 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 31 | class Task: public Runnable { |
32 | public: | ||||
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 33 | |
34 | Task(Monitor& monitor, int64_t timeout) : | ||||
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 35 | _timeout(timeout), |
36 | _startTime(Util::currentTime()), | ||||
37 | _monitor(monitor), | ||||
38 | _success(false), | ||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 39 | _done(false) {} |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 40 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 41 | ~Task() { std::cerr << this << std::endl; } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 42 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 43 | void run() { |
44 | |||||
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 45 | _endTime = Util::currentTime(); |
46 | |||||
47 | // Figure out error percentage | ||||
48 | |||||
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 49 | int64_t delta = _endTime - _startTime; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 50 | |
51 | |||||
52 | delta = delta > _timeout ? delta - _timeout : _timeout - delta; | ||||
53 | |||||
54 | float error = delta / _timeout; | ||||
55 | |||||
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 56 | if(error < ERROR) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 57 | _success = true; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 58 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 59 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 60 | _done = true; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 61 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 62 | std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; //debug |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 63 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 64 | {Synchronized s(_monitor); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 65 | _monitor.notifyAll(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 66 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 67 | } |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 68 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 69 | int64_t _timeout; |
70 | int64_t _startTime; | ||||
71 | int64_t _endTime; | ||||
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 72 | Monitor& _monitor; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 73 | bool _success; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 74 | bool _done; |
75 | }; | ||||
76 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 77 | /** |
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 | */ | ||||
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 83 | bool test00(int64_t timeout=1000LL) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 84 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 85 | 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] | 86 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 87 | { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 88 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 89 | TimerManager timerManager; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 90 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 91 | timerManager.threadFactory(shared_ptr<PosixThreadFactory>(new PosixThreadFactory())); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 92 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 93 | timerManager.start(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 94 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 95 | assert(timerManager.state() == TimerManager::STARTED); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 96 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 97 | 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] | 98 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 99 | { |
100 | Synchronized s(_monitor); | ||||
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 101 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 102 | timerManager.add(orphanTask, 10 * timeout); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 103 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 104 | timerManager.add(task, timeout); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 105 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 106 | _monitor.wait(); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 107 | } |
108 | |||||
109 | assert(task->_done); | ||||
110 | |||||
111 | |||||
112 | std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl; | ||||
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 113 | } |
114 | |||||
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 115 | // timerManager.stop(); This is where it happens via destructor |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 116 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 117 | assert(!orphanTask->_done); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 118 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 119 | return true; |
120 | } | ||||
121 | |||||
122 | friend class TestTask; | ||||
123 | |||||
124 | Monitor _monitor; | ||||
125 | }; | ||||
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 126 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 127 | const double TimerManagerTests::ERROR = .20; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 128 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 129 | }}}} // facebook::thrift::concurrency |
130 |