-
Notifications
You must be signed in to change notification settings - Fork 1k
#[test] Attribute Macro for embassy-executor #4201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
fn #fn_name() { | ||
let mut executor = ::embassy_executor::Executor::new(); | ||
let executor = unsafe { ::core::mem::transmute::<_, &'static mut ::embassy_executor::Executor>(&mut executor) }; | ||
executor.run(|spawner| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will run forever, the test won't complete even if the task returns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, you're so fast! I didn't even have time to make the formatter happy.
You're right, that's actually one of my concerns.
Currently, run
doesn't return even when there are no more pending tasks in the queue.
I think this could be fixed by changing how the executor behaves — maybe by implementing a custom fn run_and_return
, which would, in addition to polling and waiting, also check if the queue is empty.
From what I can see, Executor
and its inner components (raw::Executor
, SyncExecutor
, RunQueue
) doesn't seem to have mechanism to check whether the queue is empty. But it it looks feasible.
That said, I'll need to implement all of that for all arch by myself.
What do you think?
#[test] | ||
fn #fn_name() { | ||
let mut executor = ::embassy_executor::Executor::new(); | ||
let executor = unsafe { ::core::mem::transmute::<_, &'static mut ::embassy_executor::Executor>(&mut executor) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is unsound, the executor really needs &'static mut self
. Even if all tasks stop running, they keep pointers to the executors and anything can keep a pointer to a task through a waker, so you can't ensure the executor won't be use-after-free'd.
(it's currently sound due to my other comment (run never returrns) but it won't be when that's fixed)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little unsound indeed.
What would be the best approach then? Should I make it static inside the function body using a StaticCell
?
That would require the end user to either import static_cell
themselves, or for static_cell
to be re-exported by Embassy.
What is the use case for this? On std, you can test async logic that uses embassy-sync, embassy-time etc just fine using On no-std have you seen https://crates.io/crates/embedded-test ? I'm not sure that we should have this in embassy-executor. It doesn't work on no-std, but embassy-executor is designed for no-std so people will inevitably try to use it on no-std and fail and then try to extend it until it does, at which point we'd have reinvented embedded-test. Doing it properly is hard, I'd rather people use embedded-test. |
Resolves #4192