Skip to content

#[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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

AlixANNERAUD
Copy link
Contributor

Resolves #4192

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| {
Copy link
Member

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.

Copy link
Contributor Author

@AlixANNERAUD AlixANNERAUD May 14, 2025

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) };
Copy link
Member

@Dirbaio Dirbaio May 14, 2025

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)

Copy link
Contributor Author

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.

@Dirbaio
Copy link
Member

Dirbaio commented May 14, 2025

What is the use case for this?

On std, you can test async logic that uses embassy-sync, embassy-time etc just fine using #[tokio::test]. All the embassy crates work just fine on non-embassy executors.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature request: #[test] Attribute Macro for embassy-executor (std-only)
2 participants