-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler_test.cpp
More file actions
266 lines (230 loc) · 9.34 KB
/
scheduler_test.cpp
File metadata and controls
266 lines (230 loc) · 9.34 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <gtest/gtest.h>
#include <thread>
#include <chrono>
#include "HALAL/Services/Time/Scheduler.hpp"
int count = 0;
void fake_workload() { count++; }
class SchedulerTests : public ::testing::Test {
protected:
void SetUp() override {
Scheduler::active_task_count_ = 0;
Scheduler::free_bitmap_ = 0xFFFF'FFFF;
Scheduler::ready_bitmap_ = 0;
Scheduler::sorted_task_ids_ = 0;
Scheduler::global_tick_us_ = 0;
Scheduler::current_interval_us_ = 0;
Scheduler_global_timer = TIM2_BASE;
// Reset global callback task count
count = 0;
// Reset Timer
TIM2_BASE->CNT = 0;
TIM2_BASE->ARR = 0;
TIM2_BASE->SR = 0;
TIM2_BASE->CR1 = 0;
TIM2_BASE->DIER = 0;
Scheduler_start();
}
};
TEST_F(SchedulerTests, FreeBitmap) {
Scheduler::register_task(10, &fake_workload);
EXPECT_EQ(Scheduler::free_bitmap_, 0xFFFF'FFFE);
}
TEST_F(SchedulerTests, TaskRegistration) {
Scheduler::register_task(10, &fake_workload);
EXPECT_EQ(Scheduler::tasks_[0].callback, fake_workload);
}
TEST_F(SchedulerTests, TaskExecutionShort) {
Scheduler::register_task(10, &fake_workload);
TIM2_BASE->PSC = 2; // quicker test
constexpr int NUM_TICKS = 1'000;
for (int i = 0; i < NUM_TICKS; i++) {
for (int j = 0; j <= TIM2_BASE->PSC; j++)
TIM2_BASE->inc_cnt_and_check(1);
Scheduler::update();
}
// 1000 ticks / 10 ticks/task = 100 executions.
EXPECT_EQ(count, 100);
}
TEST_F(SchedulerTests, TaskExecutionLong) {
Scheduler::register_task(10, &fake_workload);
// TIM2_BASE->ARR = 500;
TIM2_BASE->generate_update();
TIM2_BASE->PSC = 2; // quicker test
constexpr int NUM_TICKS = 1'000'000;
for (int i = 0; i < NUM_TICKS; i++) {
for (int j = 0; j <= TIM2_BASE->PSC; j++)
TIM2_BASE->inc_cnt_and_check(1);
Scheduler::update();
}
EXPECT_EQ(count, 100'000);
}
TEST_F(SchedulerTests, SetTimeout) {
Scheduler::set_timeout(10, &fake_workload);
TIM2_BASE->PSC = 2; // quicker test
constexpr int NUM_TICKS = 100;
for (int i = 0; i < NUM_TICKS; i++) {
for (int j = 0; j <= TIM2_BASE->PSC; j++)
TIM2_BASE->inc_cnt_and_check(1);
Scheduler::update();
}
EXPECT_EQ(count, 1);
}
TEST_F(SchedulerTests, GlobalTickOverflow) {
Scheduler::global_tick_us_ = 0xFFFFFFF0ULL; // Near 32-bit max
Scheduler::register_task(20, &fake_workload);
TIM2_BASE->PSC = 2; // quicker test
constexpr int NUM_TICKS = 100;
for (int i = 0; i < NUM_TICKS; i++) {
for (int j = 0; j <= TIM2_BASE->PSC; j++)
TIM2_BASE->inc_cnt_and_check(1);
Scheduler::update();
}
// 100 ticks /20 ticks/task = 5 executions.
EXPECT_EQ(count, 5);
}
#define multiple_tasks \
X(1) \
X(2) \
X(3) \
X(4) \
X(5) \
X(6) \
X(7) \
X(8) \
X(9) \
X(10) \
X(11) \
X(12) \
X(13) \
X(14) \
X(15) \
X(16)
#define X(n) \
int multiple_task##n##count = 0; \
void multiple_task_##n(void) { multiple_task##n##count++; }
multiple_tasks
#undef X
TEST_F(SchedulerTests, GlobalTickOverflowManyTasks) {
Scheduler::global_tick_us_ = 0xFFFFFFF0ULL; // Near 32-bit max
Scheduler::register_task(10, &multiple_task_1);
Scheduler::register_task(20, &multiple_task_2);
Scheduler::register_task(30, &multiple_task_3);
TIM2_BASE->PSC = 2; // quicker test
constexpr int NUM_TICKS = 100;
for (int i = 0; i < NUM_TICKS; i++) {
for (int j = 0; j <= TIM2_BASE->PSC; j++)
TIM2_BASE->inc_cnt_and_check(1);
Scheduler::update();
}
// 100 ticks /20 ticks/task = 5 executions.
EXPECT_EQ(multiple_task1count, NUM_TICKS / 10);
EXPECT_EQ(multiple_task2count, NUM_TICKS / 20);
EXPECT_EQ(multiple_task3count, NUM_TICKS / 30);
}
TEST_F(SchedulerTests, TimeoutClearAddTask) {
uint8_t timeout_id = Scheduler::set_timeout(10, &fake_workload);
TIM2_BASE->PSC = 2; // quicker test
constexpr int NUM_TICKS = 100;
for (int i = 0; i < NUM_TICKS; i++) {
for (int j = 0; j <= TIM2_BASE->PSC; j++)
TIM2_BASE->inc_cnt_and_check(1);
Scheduler::update();
}
// timeout is already done here
uint8_t timeout_id_2 = Scheduler::set_timeout(20, &fake_workload);
// after timeout, cancel task
Scheduler::cancel_timeout(timeout_id);
EXPECT_EQ(Scheduler::active_task_count_, 1);
}
int tickidx = 0;
static volatile int connecting_execs{0};
static volatile int operational_execs{0};
static volatile int fault_execs{0};
void connecting_cyclic() {
auto next_connecting_execs = connecting_execs + 1;
connecting_execs = next_connecting_execs;
}
void operational_cyclic() {
auto next_operational_execs = operational_execs + 1;
operational_execs = next_operational_execs;
}
void fault_cyclic() {
auto next_fault_execs = fault_execs + 1;
fault_execs = next_fault_execs;
}
TEST_F(SchedulerTests, TaskDe_ReRegistration) {
uint8_t connecting_task = Scheduler::register_task(10, &connecting_cyclic);
uint8_t operational_task = 0;
uint8_t fault_task = 0;
TIM2_BASE->PSC = 2; // quicker test
constexpr int NUM_TICKS = 100;
for (; tickidx < NUM_TICKS; tickidx++) {
for (int j = 0; j <= TIM2_BASE->PSC; j++)
TIM2_BASE->inc_cnt_and_check(1);
if (tickidx == 21) {
operational_task = Scheduler::register_task(10, operational_cyclic);
Scheduler::unregister_task(connecting_task);
}
if (tickidx == 45) {
fault_task = Scheduler::register_task(10, fault_cyclic);
Scheduler::unregister_task(operational_task);
}
if (tickidx == 70) {
Scheduler::unregister_task(fault_task);
tickidx = 100; // finish test
}
Scheduler::update();
}
EXPECT_EQ(connecting_execs, 2);
EXPECT_EQ(operational_execs, 2);
EXPECT_EQ(fault_execs, 2);
}
TEST_F(SchedulerTests, MultipleTasks) {
multiple_task1count = 0;
multiple_task2count = 0;
multiple_task3count = 0;
multiple_task4count = 0;
multiple_task5count = 0;
multiple_task6count = 0;
Scheduler::register_task(1, &multiple_task_1);
Scheduler::register_task(2, &multiple_task_2);
Scheduler::register_task(3, &multiple_task_3);
Scheduler::register_task(4, &multiple_task_4);
Scheduler::register_task(5, &multiple_task_5);
Scheduler::register_task(6, &multiple_task_6);
TIM2_BASE->PSC = 2; // quicker test
constexpr int NUM_TICKS = 300;
for (int i = 0; i < NUM_TICKS; i++) {
Scheduler::update();
for (int j = 0; j <= TIM2_BASE->PSC; j++)
TIM2_BASE->inc_cnt_and_check(1);
}
EXPECT_EQ(multiple_task1count, NUM_TICKS / 1 - 1);
EXPECT_EQ(multiple_task2count, NUM_TICKS / 2 - 1);
EXPECT_EQ(multiple_task3count, NUM_TICKS / 3 - 1);
EXPECT_EQ(multiple_task4count, NUM_TICKS / 4 - 1);
EXPECT_EQ(multiple_task5count, NUM_TICKS / 5 - 1);
EXPECT_EQ(multiple_task6count, NUM_TICKS / 6 - 1);
}
TEST_F(SchedulerTests, SameTaskMultipleTimes) {
multiple_task1count = 0;
Scheduler::register_task(1, &multiple_task_1);
Scheduler::register_task(2, &multiple_task_1);
Scheduler::register_task(3, &multiple_task_1);
Scheduler::register_task(4, &multiple_task_1);
Scheduler::register_task(5, &multiple_task_1);
Scheduler::register_task(6, &multiple_task_1);
multiple_task1count = 0;
TIM2_BASE->PSC = 2; // quicker test
constexpr int NUM_TICKS = 300;
for (int i = 0; i < NUM_TICKS; i++) {
Scheduler::update();
for (int j = 0; j <= TIM2_BASE->PSC; j++)
TIM2_BASE->inc_cnt_and_check(1);
}
EXPECT_EQ(
multiple_task1count,
NUM_TICKS / 1 - 1 + NUM_TICKS / 2 - 1 + NUM_TICKS / 3 - 1 + NUM_TICKS / 4 - 1 +
NUM_TICKS / 5 - 1 + NUM_TICKS / 6 - 1
);
}