15auto test_fifo_basic_functionality() ->
bool {
16 klog::Info(
"Running test_fifo_basic_functionality...");
21 EXPECT_EQ(scheduler.
name[0],
'F',
"Scheduler name should start with F");
36 "Queue size should be 0 for empty queue");
38 "PickNext should return nullptr for empty queue");
43 "Queue size should be 1 after enqueue");
48 "Queue size should be 3 after 3 enqueues");
51 auto* picked1 = scheduler.
PickNext();
52 EXPECT_EQ(picked1, &task1,
"First picked task should be task1");
54 auto* picked2 = scheduler.
PickNext();
55 EXPECT_EQ(picked2, &task2,
"Second picked task should be task2");
57 auto* picked3 = scheduler.
PickNext();
58 EXPECT_EQ(picked3, &task3,
"Third picked task should be task3");
61 "PickNext should return nullptr after all tasks picked");
63 klog::Info(
"test_fifo_basic_functionality passed");
67auto test_fifo_ordering() ->
bool {
71 constexpr size_t kTaskCount = 10;
75 for (
size_t i = 0; i < kTaskCount; ++i) {
82 "Queue size should match task count");
85 for (
size_t i = 0; i < kTaskCount; ++i) {
87 EXPECT_NE(picked,
nullptr,
"Picked task should not be nullptr");
88 EXPECT_EQ(picked, tasks[i],
"Task should be picked in FIFO order");
94 for (
size_t i = 0; i < kTaskCount; ++i) {
102auto test_fifo_dequeue() ->
bool {
126 "Queue size should be 3 after dequeue");
131 "Queue size should be 2 after dequeue");
134 auto* picked1 = scheduler.
PickNext();
135 EXPECT_EQ(picked1, &task3,
"First remaining task should be task3");
137 auto* picked2 = scheduler.
PickNext();
138 EXPECT_EQ(picked2, &task4,
"Second remaining task should be task4");
146auto test_fifo_statistics() ->
bool {
147 klog::Info(
"Running test_fifo_statistics...");
156 EXPECT_EQ(stats.total_enqueues, 0,
"Initial enqueues should be 0");
157 EXPECT_EQ(stats.total_dequeues, 0,
"Initial dequeues should be 0");
158 EXPECT_EQ(stats.total_picks, 0,
"Initial picks should be 0");
159 EXPECT_EQ(stats.total_preemptions, 0,
"Initial preemptions should be 0");
165 EXPECT_EQ(stats.total_enqueues, 2,
"Enqueues should be 2");
171 EXPECT_EQ(stats.total_picks, 2,
"Picks should be 2");
177 EXPECT_EQ(stats.total_dequeues, 1,
"Dequeues should be 1");
183 EXPECT_EQ(stats.total_preemptions, 2,
"Preemptions should be 2");
188 EXPECT_EQ(stats.total_enqueues, 0,
"Enqueues should be 0 after reset");
189 EXPECT_EQ(stats.total_dequeues, 0,
"Dequeues should be 0 after reset");
190 EXPECT_EQ(stats.total_picks, 0,
"Picks should be 0 after reset");
191 EXPECT_EQ(stats.total_preemptions, 0,
"Preemptions should be 0 after reset");
197auto test_fifo_mixed_operations() ->
bool {
198 klog::Info(
"Running test_fifo_mixed_operations...");
218 auto* picked1 = scheduler.
PickNext();
219 EXPECT_EQ(picked1, &task1,
"First pick should be task1");
228 auto* picked2 = scheduler.
PickNext();
229 EXPECT_EQ(picked2, &task2,
"Second pick should be task2");
231 auto* picked3 = scheduler.
PickNext();
232 EXPECT_EQ(picked3, &task4,
"Third pick should be task4");
234 auto* picked4 = scheduler.
PickNext();
235 EXPECT_EQ(picked4, &task5,
"Fourth pick should be task5");
239 klog::Info(
"test_fifo_mixed_operations passed");
243auto test_fifo_repeated_enqueue() ->
bool {
244 klog::Info(
"Running test_fifo_repeated_enqueue...");
253 auto* picked1 = scheduler.
PickNext();
254 EXPECT_EQ(picked1, &task1,
"First pick should be task1");
257 auto* picked2 = scheduler.
PickNext();
258 EXPECT_EQ(picked2, &task1,
"Second pick should be task1");
261 auto* picked3 = scheduler.
PickNext();
262 EXPECT_EQ(picked3, &task1,
"Third pick should be task1");
266 klog::Info(
"test_fifo_repeated_enqueue passed");
270auto test_fifo_hooks() ->
bool {
277 task1.sched_info.priority = 5;
286 bool need_resched = scheduler.
OnTick(&task1);
287 EXPECT_EQ(need_resched,
false,
"OnTick should return false for FIFO");
292 "OnTimeSliceExpired should return true for FIFO");
296 auto* picked = scheduler.
PickNext();
297 EXPECT_EQ(picked, &task1,
"Scheduler should still work after hook calls");
303auto test_fifo_robustness() ->
bool {
304 klog::Info(
"Running test_fifo_robustness...");
313 "PickNext on empty queue should return nullptr");
329 klog::Info(
"\n=== FIFO Scheduler System Tests ===\n");
331 if (!test_fifo_basic_functionality()) {
335 if (!test_fifo_ordering()) {
339 if (!test_fifo_dequeue()) {
343 if (!test_fifo_statistics()) {
347 if (!test_fifo_mixed_operations()) {
351 if (!test_fifo_repeated_enqueue()) {
355 if (!test_fifo_hooks()) {
359 if (!test_fifo_robustness()) {
363 klog::Info(
"=== All FIFO Scheduler Tests Passed ===\n");
auto Enqueue(TaskControlBlock *task) -> void override
将任务加入就绪队列尾部
auto OnPreempted(TaskControlBlock *task) -> void override
任务被抢占时调用
auto PickNext() -> TaskControlBlock *override
选择下一个要运行的任务(队列头部)
auto IsEmpty() const -> bool override
判断队列是否为空
auto GetQueueSize() const -> size_t override
获取就绪队列大小
auto Dequeue(TaskControlBlock *task) -> void override
从就绪队列中移除指定任务
virtual auto OnTick(TaskControlBlock *current) -> bool
Tick 更新:每个时钟中断时调用,用于更新调度器状态
virtual auto BoostPriority(TaskControlBlock *task, int new_priority) -> void
优先级提升:当任务持有资源时被更高优先级任务等待,提升其优先级
virtual auto RestorePriority(TaskControlBlock *task) -> void
优先级恢复:当任务释放资源后,恢复其原始优先级
virtual auto GetStats() const -> const Stats &
获取调度器统计信息
virtual auto OnScheduled(TaskControlBlock *task) -> void
任务开始运行时调用 (从 Ready 变为 Running)
virtual auto ResetStats() -> void
重置统计信息
virtual auto OnTimeSliceExpired(TaskControlBlock *task) -> bool
时间片耗尽处理:当任务时间片用完时调用
auto Receive(const etl::imessage &msg) -> void
向 FSM 发送消息
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
auto fifo_scheduler_test() -> bool
#define EXPECT_TRUE(cond, msg)
#define EXPECT_NE(val1, val2, msg)
#define EXPECT_EQ(val1, val2, msg)