From 0b9dee368cbfa058decc4d4cf7be43e779d9eb25 Mon Sep 17 00:00:00 2001 From: Philipp Caspers <117186241+philipp-caspers@users.noreply.github.com> Date: Fri, 13 Dec 2024 14:35:59 +0000 Subject: [PATCH] test: Parametrize the method integration tests This drastically reduces boilerplate code when adding new tests. --- tests/test_bridge_methods.rs | 91 ++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/tests/test_bridge_methods.rs b/tests/test_bridge_methods.rs index e988006..4dd1f66 100644 --- a/tests/test_bridge_methods.rs +++ b/tests/test_bridge_methods.rs @@ -1,46 +1,59 @@ -use std::{sync::{mpsc, Arc}, time::Duration}; +use std::{ + sync::{mpsc, Arc}, + time::Duration, +}; use common::{wait_for_function_to_pass, ManagedRosBridge}; pub mod common; pub mod helpers; -#[test] -fn e2e_ros_service_to_opc_ua_call() { - let (assertion_tx, assertion_rx) = mpsc::channel(); - let (stop_opc_ua_server_tx, stop_opc_ua_server_rx) = mpsc::channel(); - helpers::opc_ua_test_server::run_opc_ua_test_server(assertion_tx, stop_opc_ua_server_rx); - - let mut _bridge_process = ManagedRosBridge::new().expect("Failed to start subprocess"); - - let service_caller = Arc::new( - helpers::ros_service_caller::ServiceCaller::new( - "/voraus_bridge_node/impedance_control/enable", - ) - .unwrap(), - ); - - // TODO: Figure out why this takes almost 10 s [RR-836] - service_caller.start(); - assert!(*service_caller.number_of_calls.lock().unwrap() == 0); - service_caller.call(); - - wait_for_function_to_pass( - || *service_caller.number_of_calls.lock().unwrap() == 1, - 5000, - ) - .unwrap(); - - wait_for_function_to_pass( - || { - let received = assertion_rx - .recv_timeout(Duration::from_millis(10)) - .unwrap(); - received.contains("impedance_control/enable") - }, - 5000, - ) - .unwrap(); - - stop_opc_ua_server_tx.send(()).unwrap(); +macro_rules! make_testcase_method { + ($value:expr, $testname:ident) => { + #[test] + fn $testname() { + let (assertion_tx, assertion_rx) = mpsc::channel(); + let (stop_opc_ua_server_tx, stop_opc_ua_server_rx) = mpsc::channel(); + helpers::opc_ua_test_server::run_opc_ua_test_server( + assertion_tx, + stop_opc_ua_server_rx, + ); + + let mut _bridge_process = ManagedRosBridge::new().expect("Failed to start subprocess"); + + let service_caller = Arc::new( + helpers::ros_service_caller::ServiceCaller::new(&format!( + "/voraus_bridge_node/{}", + $value + )) + .unwrap(), + ); + + // TODO: Figure out why this takes almost 10 s [RR-836] + service_caller.start(); + assert!(*service_caller.number_of_calls.lock().unwrap() == 0); + service_caller.call(); + + wait_for_function_to_pass( + || *service_caller.number_of_calls.lock().unwrap() == 1, + 5000, + ) + .unwrap(); + + wait_for_function_to_pass( + || { + let received = assertion_rx + .recv_timeout(Duration::from_millis(10)) + .unwrap(); + received.contains($value) + }, + 5000, + ) + .unwrap(); + + stop_opc_ua_server_tx.send(()).unwrap(); + } + }; } + +make_testcase_method!("impedance_control/enable", test_enable_impedance_control);