add first tests for assy helper

This commit is contained in:
Robin Müller 2025-01-30 10:55:18 +01:00
parent 211e6063cb
commit 5d2993196f
4 changed files with 125 additions and 50 deletions

View File

@ -26,10 +26,10 @@ pub struct AssemblyCommandingHelper {
/// structure.
pub children_mode_store: ModeStoreVec,
/// Target mode used for mode commanding.
pub target_mode: Option<ModeAndSubmode>,
target_mode: Option<ModeAndSubmode>,
/// Request ID of active mode commanding request.
pub active_request_id: Option<RequestId>,
pub state: ModeTreeHelperState,
active_request_id: Option<RequestId>,
state: ModeTreeHelperState,
}
pub type DevManagerCommandingHelper = AssemblyCommandingHelper;
@ -59,6 +59,18 @@ impl AssemblyCommandingHelper {
Ok(())
}
pub fn target_mode(&self) -> Option<ModeAndSubmode> {
self.target_mode
}
pub fn active_request_id(&self) -> Option<RequestId> {
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)
}
}

View File

@ -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<VecDeque<ModeReqWrapper>>,
}
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(())
}
}
}

View File

@ -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<VecDeque<ModeReqWrapper>>,
}
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,

View File

@ -488,12 +488,6 @@ impl ModeRequestHandler for AcsSubsystem {
}
}
impl AcsSubsystem {
fn internal_request_id(&self) -> Option<RequestId> {
self.subsystem_helper.internal_request_id()
}
}
struct MgmAssembly {
pub mode_node: ModeRequestorAndHandlerMpscBounded,
pub mode_requestor_info: Option<MessageMetadata>,
@ -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();
}
}
}