-
-
Notifications
You must be signed in to change notification settings - Fork 485
/
call_every_handler_test.cpp
123 lines (93 loc) · 2.68 KB
/
call_every_handler_test.cpp
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
#include "call_every_handler.h"
#include <gtest/gtest.h>
#include <atomic>
#include "log.h"
using namespace dronecore;
TEST(CallEveryHandler, Single)
{
CallEveryHandler ceh;
int num_called = 0;
void *cookie = nullptr;
ceh.add([&num_called]() { ++num_called; }, 0.1f, &cookie);
for (int i = 0; i < 11; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
ceh.run_once();
}
EXPECT_EQ(num_called, 1);
UNUSED(cookie);
}
TEST(CallEveryHandler, Multiple)
{
CallEveryHandler ceh;
int num_called = 0;
void *cookie = nullptr;
ceh.add([&num_called]() { ++num_called; }, 0.1f, &cookie);
for (int i = 0; i < 10; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
ceh.run_once();
}
EXPECT_EQ(num_called, 10);
num_called = 0;
ceh.change(0.2f, cookie);
for (int i = 0; i < 20; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
ceh.run_once();
}
EXPECT_EQ(num_called, 10);
num_called = 0;
ceh.remove(cookie);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
ceh.run_once();
EXPECT_EQ(num_called, 0);
}
TEST(CallEveryHandler, InParallel)
{
CallEveryHandler ceh;
int num_called1 = 0;
int num_called2 = 0;
void *cookie1 = nullptr;
void *cookie2 = nullptr;
ceh.add([&num_called1]() { ++num_called1; }, 0.1f, &cookie1);
ceh.add([&num_called2]() { ++num_called2; }, 0.2f, &cookie2);
for (int i = 0; i < 10; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
ceh.run_once();
}
EXPECT_EQ(num_called1, 10);
EXPECT_EQ(num_called2, 5);
num_called1 = 0;
num_called2 = 0;
ceh.change(0.4f, cookie1);
ceh.change(0.1f, cookie2);
for (int i = 0; i < 10; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
ceh.run_once();
}
EXPECT_EQ(num_called1, 2);
EXPECT_EQ(num_called2, 10);
}
TEST(CallEveryHandler, Reset)
{
CallEveryHandler ceh;
int num_called = 0;
void *cookie = nullptr;
ceh.add([&num_called]() { ++num_called; }, 0.1f, &cookie);
for (int i = 0; i < 8; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
ceh.run_once();
if (i == 8) {
}
}
EXPECT_EQ(num_called, 0);
ceh.reset(cookie);
for (int i = 0; i < 8; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
ceh.run_once();
}
EXPECT_EQ(num_called, 0);
for (int i = 0; i < 3; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
ceh.run_once();
}
EXPECT_EQ(num_called, 1);
}