Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_monitor_loop.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE monitor_loop
2#include <boost/test/included/unit_test.hpp>
3
5
7
8using namespace sysio;
9using namespace sysio::resource_monitor;
10using namespace boost::system;
11
17
18 int get_stat(const char *path, struct stat *buf) const {
20 }
21
22 bfs::space_info get_space(const bfs::path& p, boost::system::error_code& ec) const {
23 return fixture.mock_get_space(p, ec);
24 }
25
27 };
28
29 boost::asio::io_context ctx;
30
36
37 void add_file_system(const bfs::path& path_name) {
38 space_handler.add_file_system( path_name );
39 }
40
41 void set_threshold(uint32_t threshold, uint32_t warning_threshold) {
42 space_handler.set_threshold( threshold, warning_threshold );
43 }
44
45 void set_sleep_time(uint32_t sleep_time) {
46 space_handler.set_sleep_time( sleep_time );
47 }
48
49 void set_shutdown_on_exceeded(bool shutdown_on_exceeded) {
50 space_handler.set_shutdown_on_exceeded(shutdown_on_exceeded);
51 }
52
56
60
61 bool test_loop_common(int num_loops, int interval)
62 {
63 mock_get_space = [ i = 0, num_loops ]( const bfs::path& p, boost::system::error_code& ec) mutable -> bfs::space_info {
64 ec = boost::system::errc::make_error_code(errc::success);
65
66 bfs::space_info rc;
67 rc.capacity = 1000000;
68
69 if ( i < num_loops + 1 ) { // "+ 1" for the get_space in add_file_system
70 rc.available = 300000;
71 } else {
72 rc.available = 100000;
73 }
74
75 i++;
76
77 return rc;
78 };
79
80 mock_get_stat = []( const char *path, struct stat *buf ) -> int {
81 buf->st_dev = 0;
82 return 0;
83 };
84
85 set_threshold(80, 75);
87 set_sleep_time(interval);
88 add_file_system("/test");
89
90 auto start = std::chrono::system_clock::now();
91
92 auto monitor_thread = std::thread( [this] {
94 ctx.run();
95 });
96
97 monitor_thread.join();
98
99 auto end = std::chrono::system_clock::now();
100 std::chrono::duration<double> test_duration = end - start;
101
102 // For tests to be repeatable on any platforms under any loads,
103 // particularly for longer runs,
104 // we just make sure the test duration is longer than a margin
105 // of theroretical duration.
106 bool finished_in_time = (test_duration >= std::chrono::duration<double>((num_loops - 1) * interval));
107
108 return finished_in_time;
109 }
110
111 // fixture data and methods
112 std::function<bfs::space_info(const bfs::path& p, boost::system::error_code& ec)> mock_get_space;
113 std::function<int(const char *path, struct stat *buf)> mock_get_stat;
114
116};
117
118BOOST_AUTO_TEST_SUITE(monitor_loop_tests)
120 {
121 BOOST_TEST( test_loop_common(0, 1) );
122 }
123
125 {
126 BOOST_TEST( test_loop_common(1, 1) );
127 }
128
130 {
131 BOOST_TEST( test_loop_common(2, 1) );
132 }
133
135 {
136 BOOST_TEST( test_loop_common(10, 1) );
137 }
138
140 {
141 BOOST_TEST( test_loop_common(1, 5) );
142 }
143
145 {
146 BOOST_TEST( test_loop_common(2, 5) );
147 }
148
150 {
151 BOOST_TEST( test_loop_common(10, 5) );
152 }
153
154 BOOST_FIXTURE_TEST_CASE(one_hundred_twenty_loops_1_sec_interval, space_handler_fixture)
155 {
156 BOOST_TEST( test_loop_common(120, 1) );
157 }
158
159BOOST_AUTO_TEST_SUITE_END()
const mie::Vuint & p
Definition bn.cpp:27
wraps boost::filesystem::path to provide platform independent path manipulation.
void set_threshold(uint32_t new_threshold, uint32_t new_warning_threshold)
void add_file_system(const bfs::path &path_name)
void set_shutdown_on_exceeded(bool new_shutdown_on_exceeded)
unsigned int uint32_t
Definition stdint.h:126
bfs::space_info get_space(const bfs::path &p, boost::system::error_code &ec) const
mock_space_provider(space_handler_fixture &fixture)
int get_stat(const char *path, struct stat *buf) const
std::function< bfs::space_info(const bfs::path &p, boost::system::error_code &ec)> mock_get_space
void set_threshold(uint32_t threshold, uint32_t warning_threshold)
bool test_loop_common(int num_loops, int interval)
void set_sleep_time(uint32_t sleep_time)
void set_shutdown_on_exceeded(bool shutdown_on_exceeded)
boost::asio::io_context ctx
void add_file_system(const bfs::path &path_name)
std::function< int(const char *path, struct stat *buf)> mock_get_stat
file_space_handler_t space_handler
BOOST_FIXTURE_TEST_CASE(zero_loop, space_handler_fixture)
uint8_t buf[2048]
yh_rc rc