From 36ded93b621feab1d45611396da81202fc8dfcd6 Mon Sep 17 00:00:00 2001 From: Philipp Caspers <117186241+philipp-caspers@users.noreply.github.com> Date: Fri, 13 Dec 2024 14:46:58 +0000 Subject: [PATCH] test: Split integration tests into method and variables --- tests/test_bridge.rs | 97 +--------------------------------- tests/test_bridge_methods.rs | 39 ++++++++++++++ tests/test_bridge_variables.rs | 71 +++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 96 deletions(-) create mode 100644 tests/test_bridge_methods.rs create mode 100644 tests/test_bridge_variables.rs diff --git a/tests/test_bridge.rs b/tests/test_bridge.rs index 0408da1..d227423 100644 --- a/tests/test_bridge.rs +++ b/tests/test_bridge.rs @@ -1,110 +1,15 @@ use std::{ env, ffi::OsString, - iter::zip, sync::{atomic::Ordering, mpsc, Arc}, time::Duration, }; -use common::{wait_for_function_to_pass, ManagedRosBridge}; +use common::ManagedRosBridge; pub mod common; pub mod helpers; -#[test] -fn test_joint_states() { - 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(None).expect("Failed to start subprocess"); - - let subscription = Arc::new( - helpers::ros_subscriber::Subscriber::new( - "joint_states_subscriber", - "/voraus_bridge_node/joint_states", - ) - .unwrap(), - ); - let clone = Arc::clone(&subscription.node); - // TODO: Figure out why this takes almost 10 s [RR-836] - rclrs::spin_once(clone, Some(Duration::from_secs(25))).expect("Could not spin"); - - wait_for_function_to_pass( - || { - rclrs::spin_once(subscription.node.clone(), Some(Duration::new(5, 0))) - .expect("Could not spin"); - let joint_state = subscription.data.lock().unwrap().clone().unwrap(); - let mut joint_state_flat = joint_state - .position - .into_iter() - .chain(joint_state.velocity.into_iter().chain(joint_state.effort)); - joint_state_flat.all(|x| x != 0.0) - }, - 5000, - ) - .unwrap(); - - let number_of_messages_received = subscription.num_messages.load(Ordering::SeqCst); - let first_value = subscription.data.lock().unwrap().clone().unwrap(); - assert!(number_of_messages_received >= 2); - let joint_state_first_value_flat = first_value - .position - .into_iter() - .chain(first_value.velocity.into_iter().chain(first_value.effort)); - - wait_for_function_to_pass( - || { - rclrs::spin_once(subscription.node.clone(), Some(Duration::new(5, 0))) - .expect("Could not spin"); - let joint_state = subscription.data.lock().unwrap().clone().unwrap(); - let joint_state_flat = joint_state - .position - .into_iter() - .chain(joint_state.velocity.into_iter().chain(joint_state.effort)); - zip(joint_state_flat, joint_state_first_value_flat.clone()) - .all(|(current, first)| current > first) - }, - 5000, - ) - .unwrap(); - - stop_opc_ua_server_tx.send(()).unwrap(); -} - -#[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(None).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(); - bridge_process.terminate(); - assert!(bridge_process - .get_std_err() - .unwrap() - .contains("ROS service called")); - stop_opc_ua_server_tx.send(()).unwrap(); -} - #[test] fn e2e_frame_id_prefix() { let (assertion_tx, _assertion_rx) = mpsc::channel(); diff --git a/tests/test_bridge_methods.rs b/tests/test_bridge_methods.rs new file mode 100644 index 0000000..21cd162 --- /dev/null +++ b/tests/test_bridge_methods.rs @@ -0,0 +1,39 @@ +use std::sync::{mpsc, Arc}; + +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(None).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(); + bridge_process.terminate(); + assert!(bridge_process + .get_std_err() + .unwrap() + .contains("ROS service called")); + stop_opc_ua_server_tx.send(()).unwrap(); +} diff --git a/tests/test_bridge_variables.rs b/tests/test_bridge_variables.rs new file mode 100644 index 0000000..6799198 --- /dev/null +++ b/tests/test_bridge_variables.rs @@ -0,0 +1,71 @@ +use std::{ + iter::zip, + sync::{atomic::Ordering, mpsc, Arc}, + time::Duration, +}; + +use common::{wait_for_function_to_pass, ManagedRosBridge}; + +pub mod common; +pub mod helpers; + +#[test] +fn test_joint_states() { + 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(None).expect("Failed to start subprocess"); + + let subscription = Arc::new( + helpers::ros_subscriber::Subscriber::new( + "joint_states_subscriber", + "/voraus_bridge_node/joint_states", + ) + .unwrap(), + ); + let clone = Arc::clone(&subscription.node); + // TODO: Figure out why this takes almost 10 s [RR-836] + rclrs::spin_once(clone, Some(Duration::from_secs(25))).expect("Could not spin"); + + wait_for_function_to_pass( + || { + rclrs::spin_once(subscription.node.clone(), Some(Duration::new(5, 0))) + .expect("Could not spin"); + let joint_state = subscription.data.lock().unwrap().clone().unwrap(); + let mut joint_state_flat = joint_state + .position + .into_iter() + .chain(joint_state.velocity.into_iter().chain(joint_state.effort)); + joint_state_flat.all(|x| x != 0.0) + }, + 5000, + ) + .unwrap(); + + let number_of_messages_received = subscription.num_messages.load(Ordering::SeqCst); + let first_value = subscription.data.lock().unwrap().clone().unwrap(); + assert!(number_of_messages_received >= 2); + let joint_state_first_value_flat = first_value + .position + .into_iter() + .chain(first_value.velocity.into_iter().chain(first_value.effort)); + + wait_for_function_to_pass( + || { + rclrs::spin_once(subscription.node.clone(), Some(Duration::new(5, 0))) + .expect("Could not spin"); + let joint_state = subscription.data.lock().unwrap().clone().unwrap(); + let joint_state_flat = joint_state + .position + .into_iter() + .chain(joint_state.velocity.into_iter().chain(joint_state.effort)); + zip(joint_state_flat, joint_state_first_value_flat.clone()) + .all(|(current, first)| current > first) + }, + 5000, + ) + .unwrap(); + + stop_opc_ua_server_tx.send(()).unwrap(); +}