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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions embassy-executor-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,30 @@ pub fn main_wasm(args: TokenStream, item: TokenStream) -> TokenStream {
pub fn main_unspecified(args: TokenStream, item: TokenStream) -> TokenStream {
main::run(args.into(), item.into(), &main::ARCH_UNSPECIFIED).into()
}

/// Declares an async test that can be run by `embassy-executor`.
///
/// This macro allows you to create asynchronous tests that use Embassy's executor capabilities.
/// The test runner will spawn the test function using Embassy's executor and wait for it to complete.
///
/// The following restrictions apply:
///
/// * The function must be declared `async`.
/// * The function must not use generics.
/// * The function must return a result type compatible with the standard test framework.
///
/// ## Examples
///
/// Creating a simple async test:
///
/// ``` rust
/// #[embassy_executor::test]
/// async fn my_test() {
/// // Async test code
/// assert_eq!(1 + 1, 2);
/// }
/// ```
#[proc_macro_attribute]
pub fn test(args: TokenStream, input: TokenStream) -> TokenStream {
macros::test::test(args, input)
}
1 change: 1 addition & 0 deletions embassy-executor-macros/src/macros/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod main;
pub mod task;
pub mod test;
30 changes: 30 additions & 0 deletions embassy-executor-macros/src/macros/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{parse_macro_input, ItemFn};

pub fn test(_args: TokenStream, input: TokenStream) -> TokenStream {
let input_fn = parse_macro_input!(input as ItemFn);
let fn_name = &input_fn.sig.ident;

let task_name = format_ident!("__{}_task", fn_name);

let gen = quote! {
#[::embassy_executor::task()]
#[allow(clippy::future_not_send)]
async fn #task_name() {
#input_fn
}


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

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?

spawner.spawn(#task_name()).unwrap();
});
}
};

gen.into()
}
2 changes: 1 addition & 1 deletion embassy-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// This mod MUST go first, so that the others see its macros.
pub(crate) mod fmt;

pub use embassy_executor_macros::task;
pub use embassy_executor_macros::{task, test};

macro_rules! check_at_most_one {
(@amo [$($feats:literal)*] [] [$($res:tt)*]) => {
Expand Down