Repository navigation
Expand file tree
/
Copy pathtimer.cpp
More file actions
67 lines (61 loc) · 1.64 KB
/
Copy pathtimer.cpp
File metadata and controls
67 lines (61 loc) · 1.64 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
#include <catch2/catch.hpp>
#include <fty/timer.h>
#include <chrono>
TEST_CASE("Timer")
{
using namespace std::literals::chrono_literals;
SECTION("Simple timer")
{
bool run = false;
auto t = fty::Timer::singleShot(500, [&](){
run = true;
});
CHECK(t.isActive());
t.finish.wait();
CHECK(run);
}
SECTION("Stop simple timer")
{
bool run = false;
fty::Timer t = fty::Timer::singleShot(500ms, [&](){
run = true;
});
CHECK(t.isActive());
CHECK(!t.isRepeatable());
std::this_thread::sleep_for(100ms);
t.stop();
t.finish.wait();
CHECK(!run);
}
SECTION("Member function")
{
struct Listen
{
void onTimeout()
{
m_runned = true;
}
bool m_runned = false;
};
Listen lst;
auto t = fty::Timer::singleShot(100ms, &Listen::onTimeout, &lst);
CHECK(t.isActive());
t.finish.wait();
CHECK(lst.m_runned);
}
SECTION("Repeatable timer")
{
int count = 0;
auto start = std::chrono::steady_clock::now();
fty::Timer t = fty::Timer::repeatable(100ms, [&](){
++count;
std::cerr << "tick " << count << std::endl;
std::cerr << "elapsed " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() << std::endl;
return count != 5;
});
CHECK(t.isActive());
CHECK(t.isRepeatable());
t.finish.wait();
CHECK(count == 5);
}
}