Browse Source

integration-test: Add `tokio_integration_test` macro

This new macro runs a test in a Tokio runtime and it can be used for
asynchronous tests (defined as `async fn`).
Michal Rostecki 2 years ago
parent
commit
d31a1805da

+ 1 - 0
test/integration-test-macros/Cargo.toml

@@ -6,6 +6,7 @@ publish = false
 
 [dependencies]
 quote = "1"
+proc-macro2 = "1.0"
 syn = {version = "1.0", features = ["full"]}
 
 [lib]

+ 28 - 1
test/integration-test-macros/src/lib.rs

@@ -1,6 +1,7 @@
 use proc_macro::TokenStream;
+use proc_macro2::Span;
 use quote::quote;
-use syn::{parse_macro_input, ItemFn};
+use syn::{parse_macro_input, Ident, ItemFn};
 
 #[proc_macro_attribute]
 pub fn integration_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
@@ -17,3 +18,29 @@ pub fn integration_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
     };
     TokenStream::from(expanded)
 }
+
+#[proc_macro_attribute]
+pub fn tokio_integration_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
+    let item = parse_macro_input!(item as ItemFn);
+    let name = &item.sig.ident;
+    let name_str = &item.sig.ident.to_string();
+    let sync_name_str = format!("sync_{name_str}");
+    let sync_name = Ident::new(&sync_name_str, Span::call_site());
+    let expanded = quote! {
+        #item
+
+        fn #sync_name() {
+            let rt = tokio::runtime::Builder::new_current_thread()
+                .enable_all()
+                .build()
+                .unwrap();
+            rt.block_on(#name());
+        }
+
+        inventory::submit!(crate::IntegrationTest {
+            name: concat!(module_path!(), "::", #sync_name_str),
+            test_fn: #sync_name,
+        });
+    };
+    TokenStream::from(expanded)
+}

+ 2 - 0
test/integration-test/Cargo.toml

@@ -10,6 +10,7 @@ aya = { path = "../../aya" }
 aya-obj = { path = "../../aya-obj" }
 clap = { version = "4", features = ["derive"] }
 env_logger = "0.10"
+futures-core = "0.3"
 inventory = "0.3"
 integration-test-macros = { path = "../integration-test-macros" }
 lazy_static = "1"
@@ -20,3 +21,4 @@ rbpf = "0.1.0"
 regex = "1"
 tempfile = "3.3.0"
 libtest-mimic = "0.6.0"
+tokio = { version = "1.24", features = ["rt", "rt-multi-thread"] }

+ 2 - 1
test/integration-test/src/tests/mod.rs

@@ -11,7 +11,8 @@ pub mod rbpf;
 pub mod relocations;
 pub mod smoke;
 
-pub use integration_test_macros::integration_test;
+pub use integration_test_macros::{integration_test, tokio_integration_test};
+
 #[derive(Debug)]
 pub struct IntegrationTest {
     pub name: &'static str,