Skip to content

Commit 9ddd194

Browse files
committed
test(test_suite): add another test for Task::set_priority
1 parent 117b76f commit 9ddd194

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//! Checks that the task priority is reset whenever a task is activated.
2+
use constance::{
3+
kernel::{cfg::CfgBuilder, Hunk, Task},
4+
prelude::*,
5+
};
6+
7+
use super::Driver;
8+
use crate::utils::SeqTracker;
9+
10+
pub struct App<System> {
11+
task2: Task<System>,
12+
seq: Hunk<System, SeqTracker>,
13+
}
14+
15+
impl<System: Kernel> App<System> {
16+
pub const fn new<D: Driver<Self>>(b: &mut CfgBuilder<System>) -> Self {
17+
Task::build()
18+
.start(task1_body::<System, D>)
19+
.priority(1)
20+
.active(true)
21+
.finish(b);
22+
let task2 = Task::build()
23+
.start(task2_body::<System, D>)
24+
.priority(2)
25+
.active(true)
26+
.finish(b);
27+
28+
let seq = Hunk::<_, SeqTracker>::build().finish(b);
29+
30+
App { task2, seq }
31+
}
32+
}
33+
34+
fn task1_body<System: Kernel, D: Driver<App<System>>>(_: usize) {
35+
// `task1` executes first because it has a higher priority.
36+
D::app().seq.expect_and_replace(0, 1);
37+
38+
// Raise `task2`'s priority to higher than `task1`. `task2` will start
39+
// executing.
40+
D::app().task2.set_priority(0).unwrap();
41+
42+
// Back from `task2`...
43+
D::app().seq.expect_and_replace(2, 3);
44+
45+
// Activate `task2` again. Its priority is back to the initial value
46+
// (lower than `task1`). This time we don't raise its priority, so the
47+
// system won't perform a context switch until `task1` exits.
48+
D::app().task2.activate().unwrap();
49+
50+
D::app().seq.expect_and_replace(3, 4);
51+
52+
// Exit from `task1`, relinquishing the control to `task2`.
53+
}
54+
55+
fn task2_body<System: Kernel, D: Driver<App<System>>>(_: usize) {
56+
match D::app().seq.get() {
57+
1 => {
58+
D::app().seq.expect_and_replace(1, 2);
59+
}
60+
_ => {
61+
D::app().seq.expect_and_replace(4, 5);
62+
D::success();
63+
}
64+
}
65+
}

src/constance_test_suite/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ pub mod kernel_tests {
164164
(mod task_park_priority_boost {}, "task_park_priority_boost"),
165165
(mod task_park_reset {}, "task_park_reset"),
166166
(mod task_priority_boost_reset {}, "task_priority_boost_reset"),
167+
(mod task_priority_reset {}, "task_priority_reset"),
167168
(mod task_queue_fifo {}, "task_queue_fifo"),
168169
(mod task_set_priority {}, "task_set_priority"),
169170
(mod task_take_interrupt_at_return {}, "task_take_interrupt_at_return"),

0 commit comments

Comments
 (0)