-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_timer.h
120 lines (98 loc) · 3.99 KB
/
test_timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef SIOUX_SOURCE_SERVER_TEST_TIMER_H
#define SIOUX_SOURCE_SERVER_TEST_TIMER_H
#include <boost/asio/io_service.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/noncopyable.hpp>
#include <boost/function.hpp>
namespace asio_mocks {
/**
* @brief test replacement for a boost::asio::deadline_timer
*/
class timer : boost::noncopyable
{
public:
explicit timer( boost::asio::io_service& );
~timer();
typedef boost::posix_time::ptime time_type;
typedef boost::posix_time::time_duration duration_type;
template< typename WaitHandler >
void async_wait( WaitHandler handler );
/**
* @brief Cancel any asynchronous operations that are waiting on the timer.
*
* This function forces the completion of any pending asynchronous wait operations against the timer.
* The handler for each cancelled operation will be invoked with the boost::asio::error::operation_aborted
* error code. Cancelling the timer does not change the expiry time.
*/
std::size_t cancel();
std::size_t cancel( boost::system::error_code & ec );
/**
* @brief Get the timer's expire time as an absolute time.
*/
time_type expires_at() const;
/**
* @brief Set the timer's expire time as an absolute time.
*
* This function sets the expiry time. Any pending asynchronous wait operations will be cancelled.
* The handler for each cancelled operation will be invoked with the boost::asio::error::operation_aborted
* error code.
*/
std::size_t expires_at( const time_type & expiry_time );
/**
* @brief overload version the function above.
* @param expiry_time sets a new expiration time for the timer
* @param ec will never by altered in the test library.
*/
std::size_t expires_at( const time_type & expiry_time, boost::system::error_code & ec );
/**
* @brief Get the timer's expire time relative to now.
*/
duration_type expires_from_now() const;
/**
* @brief Set the timer's expire time relative to now.
*/
std::size_t expires_from_now( const duration_type & expiry_time );
std::size_t expires_from_now( const duration_type & expiry_time, boost::system::error_code & ec );
private:
void async_wait_impl( const boost::function< void ( const boost::system::error_code& ) >& handler );
boost::asio::io_service& queue_;
};
/**
* @brief returns the currently simulated time
*
* If the simulated time was not initialized (by calling current_time( ptime ) ), the function will return
* boost::posix_time::time_from_string( "1970-01-01 00:00:00")
*/
boost::posix_time::ptime current_time();
/**
* @brief sets the currently simulated time
* @pre new_time >= current_time()
* @post current_time() will yield new_time
*/
void current_time( const boost::posix_time::ptime& new_time );
/**
* @brief resets the time to 1970-01-01 00:00:00
*/
void reset_time();
/**
* @brief updates the current time
*
* Equivalent to current_time( current_time() + delay )
* @pre delay >= 0
*/
void advance_time( const boost::posix_time::time_duration& delay );
/**
* @brief advances the simulated time up to the next point, where a timer is scheduled to expire.
*
* @return returns the number of scheduled timer that reached there expiration times by advancing the
* time to the nearest expirtion time.
*/
unsigned advance_time();
// implementation
template< typename WaitHandler >
void timer::async_wait( WaitHandler handler )
{
async_wait_impl( handler );
}
} // namespace asio_mocks
#endif