-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperiodic_timer_test.cpp
More file actions
134 lines (101 loc) · 3.8 KB
/
periodic_timer_test.cpp
File metadata and controls
134 lines (101 loc) · 3.8 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
/** @file
*
* @brief Test scenarios for @c periodic_timer class template.
*
* @author Cliff Green
*
* @copyright (c) 2017-2025 by Cliff Green
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
#define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER
#include "catch2/catch_test_macros.hpp"
#include <chrono>
#include <thread>
#include <optional>
#include <system_error>
#include "asio/executor_work_guard.hpp"
#include "timer/periodic_timer.hpp"
constexpr int Expected = 9;
int count = 0;
template <typename D>
bool lambda_util (std::error_code err, D elap) {
++count;
INFO ("count = " << count << ", err code = " << err.value() << ", " << err.message());
return count < Expected;
}
using wk_guard = asio::executor_work_guard<asio::io_context::executor_type>;
void wait_util (std::chrono::milliseconds ms, wk_guard& wg, std::thread& thr) {
std::this_thread::sleep_for(ms);
wg.reset();
thr.join();
}
template <typename Clock>
void test_util () {
GIVEN ( "A clock and a duration") {
asio::io_context ioc;
chops::periodic_timer<Clock> timer {ioc};
wk_guard wg { asio::make_work_guard(ioc) };
std::thread thr([&ioc] () { ioc.run(); } );
count = 0;
WHEN ( "The duration is 100 ms" ) {
auto test_dur { 100 };
timer.start_duration_timer(std::chrono::milliseconds(test_dur),
[] (std::error_code err, typename Clock::duration elap) {
return lambda_util(err, elap);
}
);
wait_util (std::chrono::milliseconds((Expected+1)*test_dur), wg, thr);
THEN ( "the timer callback count should match expected") {
REQUIRE (count == Expected);
}
}
WHEN ( "The duration is 200 ms and the start time is 2 seconds in the future" ) {
auto test_dur { 200 };
timer.start_duration_timer(std::chrono::milliseconds(test_dur), Clock::now() + std::chrono::seconds(2),
[] (std::error_code err, typename Clock::duration elap) {
return lambda_util(err, elap);
}
);
wait_util(std::chrono::milliseconds((Expected+1)*test_dur + 2000), wg, thr);
THEN ( "the timer callback count should match expected") {
REQUIRE (count == Expected);
}
}
WHEN ( "The duration is 100 ms and the timer pops on timepoints" ) {
auto test_dur { 100 };
timer.start_timepoint_timer(std::chrono::milliseconds(test_dur),
[] (std::error_code err, typename Clock::duration elap) {
return lambda_util(err, elap);
}
);
wait_util (std::chrono::milliseconds((Expected+1)*test_dur), wg, thr);
THEN ( "the timer callback count should match expected") {
REQUIRE (count == Expected);
}
}
WHEN ( "The duration is 200 ms and the timer pops on timepoints starting 2 seconds in the future" ) {
auto test_dur { 200 };
timer.start_timepoint_timer(std::chrono::milliseconds(test_dur), Clock::now() + std::chrono::seconds(2),
[] (std::error_code err, typename Clock::duration elap) {
return lambda_util(err, elap);
}
);
wait_util(std::chrono::milliseconds((Expected+1)*test_dur + 2000), wg, thr);
THEN ( "the timer callback count should match expected") {
REQUIRE (count == Expected);
}
}
} // end given
}
SCENARIO ( "A periodic timer can be instantiated on the steady clock", "[periodic_timer] [steady_clock]" ) {
test_util<std::chrono::steady_clock>();
}
SCENARIO ( "A periodic timer can be instantiated on the system clock", "[periodic_timer] [system_clock]" ) {
test_util<std::chrono::system_clock>();
}
SCENARIO ( "A periodic timer can be instantiated on the high resolution clock", "[periodic_timer] [high_resolution_clock]" ) {
test_util<std::chrono::high_resolution_clock>();
}