From 5d2993196f82e09e09aeb4eb1e2103556641de03 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 30 Jan 2025 10:55:18 +0100 Subject: [PATCH] add first tests for assy helper --- satrs/src/dev_mgmt.rs | 82 ++++++++++++++++++++++++++++++++++++++-- satrs/src/mode.rs | 40 +++++++++++++++++++- satrs/src/subsystem.rs | 37 ++---------------- satrs/tests/mode_tree.rs | 16 +++----- 4 files changed, 125 insertions(+), 50 deletions(-) diff --git a/satrs/src/dev_mgmt.rs b/satrs/src/dev_mgmt.rs index 263d95e..11c1c3a 100644 --- a/satrs/src/dev_mgmt.rs +++ b/satrs/src/dev_mgmt.rs @@ -26,10 +26,10 @@ pub struct AssemblyCommandingHelper { /// structure. pub children_mode_store: ModeStoreVec, /// Target mode used for mode commanding. - pub target_mode: Option, + target_mode: Option, /// Request ID of active mode commanding request. - pub active_request_id: Option, - pub state: ModeTreeHelperState, + active_request_id: Option, + state: ModeTreeHelperState, } pub type DevManagerCommandingHelper = AssemblyCommandingHelper; @@ -59,6 +59,18 @@ impl AssemblyCommandingHelper { Ok(()) } + pub fn target_mode(&self) -> Option { + self.target_mode + } + + pub fn active_request_id(&self) -> Option { + self.active_request_id + } + + pub fn state(&self) -> ModeTreeHelperState { + self.state + } + pub fn send_announce_mode_cmd_to_children( &self, request_id: RequestId, @@ -151,6 +163,68 @@ impl AssemblyCommandingHelper { #[cfg(test)] mod tests { + use crate::mode::{tests::ModeReqSenderMock, UNKNOWN_MODE}; + + use super::*; + + pub enum ExampleId { + Id1 = 1, + Id2 = 2, + } + #[test] - fn test_basic() {} + fn test_basic() { + let assy_helper = AssemblyCommandingHelper::default(); + assert_eq!(assy_helper.state(), ModeTreeHelperState::Idle); + assert!(assy_helper.active_request_id().is_none()); + assert!(assy_helper.target_mode().is_none()); + } + + #[test] + fn test_mode_announce() { + let mut assy_helper = AssemblyCommandingHelper::default(); + let mode_req_sender = ModeReqSenderMock::default(); + assy_helper.add_mode_child(ExampleId::Id1 as u64, UNKNOWN_MODE); + assy_helper.add_mode_child(ExampleId::Id2 as u64, UNKNOWN_MODE); + assy_helper + .send_announce_mode_cmd_to_children(1, &mode_req_sender, false) + .unwrap(); + assert_eq!(mode_req_sender.requests.borrow().len(), 2); + let mut req = mode_req_sender.requests.borrow_mut().pop_front().unwrap(); + assert_eq!(req.target_id, ExampleId::Id1 as u64); + assert_eq!(req.request_id, 1); + assert_eq!(req.request, ModeRequest::AnnounceMode); + req = mode_req_sender.requests.borrow_mut().pop_front().unwrap(); + assert_eq!(req.target_id, ExampleId::Id2 as u64); + assert_eq!(req.request_id, 1); + assert_eq!(req.request, ModeRequest::AnnounceMode); + } + + #[test] + fn test_mode_announce_recursive() { + let mut assy_helper = AssemblyCommandingHelper::default(); + let mode_req_sender = ModeReqSenderMock::default(); + assy_helper.add_mode_child(ExampleId::Id1 as u64, UNKNOWN_MODE); + assy_helper.add_mode_child(ExampleId::Id2 as u64, UNKNOWN_MODE); + assy_helper + .send_announce_mode_cmd_to_children(1, &mode_req_sender, true) + .unwrap(); + assert_eq!(mode_req_sender.requests.borrow().len(), 2); + let mut req = mode_req_sender.requests.borrow_mut().pop_front().unwrap(); + assert_eq!(req.target_id, ExampleId::Id1 as u64); + assert_eq!(req.request_id, 1); + assert_eq!(req.request, ModeRequest::AnnounceModeRecursive); + req = mode_req_sender.requests.borrow_mut().pop_front().unwrap(); + assert_eq!(req.target_id, ExampleId::Id2 as u64); + assert_eq!(req.request_id, 1); + assert_eq!(req.request, ModeRequest::AnnounceModeRecursive); + } + + #[test] + fn test_mode_commanding() { + let mut assy_helper = AssemblyCommandingHelper::default(); + let mode_req_sender = ModeReqSenderMock::default(); + assy_helper.send_mode_cmd_to_all_children_with_reply_awaition(1, , forced, mode_req_sender) + + } } diff --git a/satrs/src/mode.rs b/satrs/src/mode.rs index 6265bca..cd29c8a 100644 --- a/satrs/src/mode.rs +++ b/satrs/src/mode.rs @@ -718,4 +718,42 @@ pub mod std_mod { } #[cfg(test)] -mod tests {} +pub(crate) mod tests { + use core::cell::RefCell; + use std::collections::VecDeque; + + use crate::{request::RequestId, ComponentId}; + + use super::*; + + pub struct ModeReqWrapper { + pub request_id: RequestId, + pub target_id: ComponentId, + pub request: ModeRequest, + } + + #[derive(Default)] + pub struct ModeReqSenderMock { + pub requests: RefCell>, + } + + impl ModeRequestSender for ModeReqSenderMock { + fn local_channel_id(&self) -> crate::ComponentId { + 0 + } + + fn send_mode_request( + &self, + request_id: RequestId, + target_id: ComponentId, + request: ModeRequest, + ) -> Result<(), GenericTargetedMessagingError> { + self.requests.borrow_mut().push_back(ModeReqWrapper { + request_id, + target_id, + request, + }); + Ok(()) + } + } +} diff --git a/satrs/src/subsystem.rs b/satrs/src/subsystem.rs index 733bd1d..2a458d0 100644 --- a/satrs/src/subsystem.rs +++ b/satrs/src/subsystem.rs @@ -654,11 +654,11 @@ impl SubsystemCommandingHelper { #[cfg(test)] mod tests { use super::*; - use core::cell::RefCell; - use std::collections::VecDeque; use crate::{ - mode::{Mode, ModeAndSubmode, ModeReply, ModeRequest, ModeRequestSender, UNKNOWN_MODE}, + mode::{ + tests::{ModeReqSenderMock, ModeReqWrapper}, Mode, ModeAndSubmode, ModeReply, ModeRequest, UNKNOWN_MODE, + }, mode_tree::{ ModeStoreProvider, ModeStoreVec, SequenceModeTables, SequenceTableEntry, SequenceTableMapTable, SequenceTablesMapValue, TargetModeTables, @@ -683,37 +683,6 @@ mod tests { Mode2 = 3, } - pub struct ModeReqWrapper { - pub request_id: RequestId, - pub target_id: ComponentId, - pub request: ModeRequest, - } - - #[derive(Default)] - pub struct ModeReqSenderMock { - pub requests: RefCell>, - } - - impl ModeRequestSender for ModeReqSenderMock { - fn local_channel_id(&self) -> crate::ComponentId { - 0 - } - - fn send_mode_request( - &self, - request_id: RequestId, - target_id: ComponentId, - request: ModeRequest, - ) -> Result<(), GenericTargetedMessagingError> { - self.requests.borrow_mut().push_back(ModeReqWrapper { - request_id, - target_id, - request, - }); - Ok(()) - } - } - pub struct SequenceExecutorTestbench { pub sender: ModeReqSenderMock, pub mode_store: ModeStoreVec, diff --git a/satrs/tests/mode_tree.rs b/satrs/tests/mode_tree.rs index f1384d2..7bfb979 100644 --- a/satrs/tests/mode_tree.rs +++ b/satrs/tests/mode_tree.rs @@ -488,12 +488,6 @@ impl ModeRequestHandler for AcsSubsystem { } } -impl AcsSubsystem { - fn internal_request_id(&self) -> Option { - self.subsystem_helper.internal_request_id() - } -} - struct MgmAssembly { pub mode_node: ModeRequestorAndHandlerMpscBounded, pub mode_requestor_info: Option, @@ -544,9 +538,9 @@ impl MgmAssembly { // Otherwise, we command everything OFF, because we can not keep the mode. } AssemblyHelperResult::ModeCommandingDone => { - if self.commanding_helper.target_mode.is_some() { + if self.commanding_helper.target_mode().is_some() { // Complete the mode command. - self.mode_and_submode = self.commanding_helper.target_mode.take().unwrap(); + self.mode_and_submode = self.commanding_helper.target_mode().unwrap(); self.handle_mode_reached(self.mode_requestor_info)?; } } @@ -593,7 +587,7 @@ impl ModeRequestHandler for MgmAssembly { forced: bool, ) -> Result<(), Self::Error> { // Always accept forced commands and commands to mode OFF. - if self.commanding_helper.target_mode.is_some() + if self.commanding_helper.target_mode().is_some() && !forced && mode_and_submode.mode() != DefaultMode::OFF as u32 { @@ -739,10 +733,10 @@ impl DeviceManager { } } AssemblyHelperResult::ModeCommandingDone => { - if self.commanding_helper.target_mode.is_some() { + if self.commanding_helper.target_mode().is_some() { // Complete the mode command. self.handle_mode_reached(self.mode_requestor_info)?; - self.mode_and_submode = self.commanding_helper.target_mode.take().unwrap(); + self.mode_and_submode = self.commanding_helper.target_mode().unwrap(); } } }