-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_ControllerThread.cpp
222 lines (156 loc) · 5.21 KB
/
test_ControllerThread.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
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
/*
* This file is part of LSST cRIO CPP tests. Tests ControllerThread.
*
* Developed for the Vera C. Rubin Observatory Telescope & Site Software Systems.
* This product includes software developed by the Vera C.Rubin Observatory Project
* (https://www.lsst.org). See the COPYRIGHT file at the top-level directory of
* this distribution for details of code ownership.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <atomic>
#include <catch2/catch_test_macros.hpp>
#include <cRIO/ControllerThread.h>
#include <cRIO/InterruptHandler.h>
#include <cRIO/Task.h>
#include <TestFPGA.h>
using namespace LSST::cRIO;
using namespace std::chrono_literals;
std::atomic<int> tv = 0;
std::atomic<int> iv = 0;
class TestTask : public Task {
public:
task_return_t run() override {
tv++;
return 10000;
}
};
TEST_CASE("Run ControllerThread and stop it", "[ControllerThread]") {
tv = 0;
REQUIRE(tv == 0);
// run controller thread
ControllerThread::instance().start();
std::this_thread::sleep_for(100ms);
// enqueue command into controller thread
ControllerThread::instance().enqueue(std::make_shared<TestTask>());
std::this_thread::sleep_for(1ms);
// stop controller thread
ControllerThread::instance().stop();
REQUIRE(tv == 1);
}
TEST_CASE("Queue to controller before run", "[ControllerThread]") {
tv = 0;
REQUIRE(tv == 0);
for (int i = 0; i < 10; i++) {
ControllerThread::instance().enqueue(std::make_shared<TestTask>());
}
// run controller thread
ControllerThread::instance().start();
std::this_thread::sleep_for(10ms);
// stop controller thread
ControllerThread::instance().stop();
REQUIRE(tv == 10);
}
TEST_CASE("Queue at different times", "[ControllerThread]") {
tv = 0;
REQUIRE(tv == 0);
auto when = std::chrono::steady_clock::now() + 500ms;
for (int i = 0; i < 3; i++) {
ControllerThread::instance().enqueue_at(std::make_shared<TestTask>(), when);
when -= 1ms;
}
when = std::chrono::steady_clock::now() + 200ms;
for (int i = 0; i < 4; i++) {
ControllerThread::instance().enqueue_at(std::make_shared<TestTask>(), when);
when -= 1ms;
}
when = std::chrono::steady_clock::now();
for (int i = 0; i < 2; i++) {
ControllerThread::instance().enqueue_at(std::make_shared<TestTask>(), when);
when += 1ms;
}
ControllerThread::instance().start();
std::this_thread::sleep_for(100ms);
CHECK(tv == 2);
std::this_thread::sleep_for(200ms);
CHECK(tv == 6);
std::this_thread::sleep_for(300ms);
CHECK(tv == 9);
ControllerThread::instance().stop();
REQUIRE(tv == 9);
}
std::atomic<int> rv = 0;
class TestRescheduledTask : public Task {
public:
TestRescheduledTask(task_return_t wait) : _wait(wait) {}
task_return_t run() override {
rv += 1;
return _wait;
}
private:
task_return_t _wait;
};
TEST_CASE("Task rescheduling", "[ControllerThread]") {
rv = 0;
REQUIRE(rv == 0);
for (int i = 0; i < 3; i++) {
ControllerThread::instance().enqueue(std::make_shared<TestRescheduledTask>(20));
}
for (int i = 0; i < 2; i++) {
ControllerThread::instance().enqueue(std::make_shared<TestRescheduledTask>(30));
}
ControllerThread::instance().start();
std::this_thread::sleep_for(22ms);
CHECK(rv == 8);
std::this_thread::sleep_for(10ms);
CHECK(rv == 10);
std::this_thread::sleep_for(70ms);
CHECK(rv == 26);
ControllerThread::instance().stop();
REQUIRE(rv == 26);
}
class TestHandler : public InterruptHandler {
public:
void handleInterrupt(uint8_t interrupt) override { iv++; }
};
class TestHandlerTask : public TestHandler, public Task {
public:
task_return_t run() override {
tv++;
return 10000;
}
};
class TestTaskHandler : public TestTask, public InterruptHandler {
public:
void handleInterrupt(uint8_t interrupt) override { iv++; }
};
TEST_CASE("Handling interrupts", "[ControllerThread]") {
iv = 0;
REQUIRE(iv == 0);
for (int i = 0; i < 3; i++) {
ControllerThread::instance().enqueue(std::make_shared<TestRescheduledTask>(20));
}
for (int i = 0; i < 3; i++) {
ControllerThread::instance().setInterruptHandler(std::make_shared<TestHandler>(), i + 1);
}
TestFPGA fpga;
fpga.setSimulatedIRQs(0x7);
TestILC ilc(1);
REQUIRE_NOTHROW(ControllerThread::instance().startInterruptWatcherTask(&fpga));
ControllerThread::instance().start();
std::this_thread::sleep_for(22ms);
CHECK(iv == 6);
ControllerThread::instance().stop();
REQUIRE(iv == 6);
}