Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
mock_time.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <fc/time.hpp>
4#include <boost/asio/deadline_timer.hpp>
5
6namespace fc {
7
10 typedef boost::asio::deadline_timer::traits_type source_traits;
11public:
12 typedef source_traits::time_type time_type;
13 typedef source_traits::duration_type duration_type;
14
15 // Requires set_now() to be called first on main thread before any calls to fc::time_point::now()
16 static time_type now() noexcept;
17
18 // First call should be on one thread before any calls to fc::time_point::now()
19 static void set_now( time_type t );
20 static void set_now( const fc::time_point& now );
21
22 // Thread safe only if first call to set_now is before any threads are spawned or memory barrier introduced
23 static bool is_set() { return mock_enabled_; }
24
25 static time_type add( time_type t, duration_type d ) { return source_traits::add( t, d ); }
26 static duration_type subtract( time_type t1, time_type t2 ) { return source_traits::subtract( t1, t2 ); }
27 static bool less_than( time_type t1, time_type t2 ) { return source_traits::less_than( t1, t2 ); }
28
29 // This function is called by asio to determine how often to check
30 // if the timer is ready to fire. By manipulating this function, we
31 // can make sure asio detects changes to now_ in a timely fashion.
32 static boost::posix_time::time_duration to_posix_duration( duration_type d ) {
33 return d < boost::posix_time::milliseconds( 1 ) ? d : boost::posix_time::milliseconds( 1 );
34 }
35
36 // return now as fc::time_point, used by fc::time_point::now() if mock_time_traits is_set()
37 static fc::time_point fc_now();
38
39private:
40 static bool mock_enabled_;
41 static const boost::posix_time::ptime epoch_;
42 static std::atomic<int64_t> now_;
43};
44
45typedef boost::asio::basic_deadline_timer<boost::posix_time::ptime, mock_time_traits> mock_deadline_timer;
46
47} // namespace fc
mock out fc::time_point::now() and provide a mock deadline timer
Definition mock_time.hpp:9
static fc::time_point fc_now()
Definition mock_time.cpp:29
static time_type now() noexcept
Definition mock_time.cpp:10
static time_type add(time_type t, duration_type d)
Definition mock_time.hpp:25
static void set_now(time_type t)
Definition mock_time.cpp:14
source_traits::time_type time_type
Definition mock_time.hpp:12
static boost::posix_time::time_duration to_posix_duration(duration_type d)
Definition mock_time.hpp:32
source_traits::duration_type duration_type
Definition mock_time.hpp:13
static duration_type subtract(time_type t1, time_type t2)
Definition mock_time.hpp:26
static bool is_set()
Definition mock_time.hpp:23
static bool less_than(time_type t1, time_type t2)
Definition mock_time.hpp:27
namespace sysio::chain
Definition authority.cpp:3
boost::asio::basic_deadline_timer< boost::posix_time::ptime, mock_time_traits > mock_deadline_timer
Definition mock_time.hpp:45
CK_ULONG d