basic unittests for PUS event man

This commit is contained in:
Robin Müller 2022-11-01 18:03:01 +01:00
parent 0ac9f888b2
commit cc1f3ed091
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
4 changed files with 150 additions and 7 deletions

View File

@ -285,6 +285,18 @@ impl<SEVERITY: HasSeverity> From<EventU32TypedSev<SEVERITY>> for EventU32 {
}
}
impl<Severity: HasSeverity> AsRef<EventU32> for EventU32TypedSev<Severity> {
fn as_ref(&self) -> &EventU32 {
&self.event
}
}
impl<Severity: HasSeverity> AsMut<EventU32> for EventU32TypedSev<Severity> {
fn as_mut(&mut self) -> &mut EventU32 {
&mut self.event
}
}
impl_event_provider!(EventU32, EventU32TypedSev, u32, u16, u16);
impl EventU32 {
@ -423,6 +435,18 @@ pub struct EventU16TypedSev<SEVERITY> {
phantom: PhantomData<SEVERITY>,
}
impl<Severity: HasSeverity> AsRef<EventU16> for EventU16TypedSev<Severity> {
fn as_ref(&self) -> &EventU16 {
&self.event
}
}
impl<Severity: HasSeverity> AsMut<EventU16> for EventU16TypedSev<Severity> {
fn as_mut(&mut self) -> &mut EventU16 {
&mut self.event
}
}
impl EventU16 {
/// Generate a small event. The raw representation of a small event has 16 bits.
/// If the passed group ID is invalid (too large), [None] wil be returned

View File

@ -174,6 +174,20 @@ impl<BackendError, Event: GenericEvent> PusEventTmManager<BackendError, Event> {
}
impl<BackendError> PusEventTmManager<BackendError, EventU32> {
pub fn enable_tm_for_event_with_sev<Severity: HasSeverity>(
&mut self,
event: &EventU32TypedSev<Severity>,
) -> Result<bool, BackendError> {
self.backend.enable_event_reporting(event.as_ref())
}
pub fn disable_tm_for_event_with_sev<Severity: HasSeverity>(
&mut self,
event: &EventU32TypedSev<Severity>,
) -> Result<bool, BackendError> {
self.backend.disable_event_reporting(event.as_ref())
}
pub fn generate_pus_event_tm<E, Severity: HasSeverity>(
&mut self,
sender: &mut (impl EcssTmSender<Error = E> + ?Sized),
@ -190,3 +204,98 @@ impl<BackendError> PusEventTmManager<BackendError, EventU32> {
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::events::SeverityInfo;
use spacepackets::tm::PusTm;
use std::sync::mpsc::{channel, SendError, TryRecvError};
use std::vec::Vec;
const INFO_EVENT: EventU32TypedSev<SeverityInfo> =
EventU32TypedSev::<SeverityInfo>::const_new(1, 0);
const LOW_SEV_EVENT: EventU32 = EventU32::const_new(Severity::LOW, 1, 5);
const EMPTY_STAMP: [u8; 7] = [0; 7];
struct EventTmSender {
sender: std::sync::mpsc::Sender<Vec<u8>>,
}
impl EcssTmSender for EventTmSender {
type Error = SendError<Vec<u8>>;
fn send_tm(&mut self, tm: PusTm) -> Result<(), EcssTmError<Self::Error>> {
let mut vec = Vec::new();
tm.append_to_vec(&mut vec)?;
self.sender.send(vec).map_err(EcssTmError::SendError)?;
Ok(())
}
}
fn create_basic_man() -> PusEventTmManager<(), EventU32> {
let reporter = EventReporter::new(0x02, 128).expect("Creating event repoter failed");
let backend = DefaultPusMgmtBackendProvider::<EventU32>::default();
PusEventTmManager::new(reporter, Box::new(backend))
}
#[test]
fn test_basic() {
let mut event_man = create_basic_man();
let (event_tx, event_rx) = channel();
let mut sender = EventTmSender { sender: event_tx };
let event_sent = event_man
.generate_pus_event_tm(&mut sender, &EMPTY_STAMP, INFO_EVENT, None)
.expect("Sending info event failed");
assert!(event_sent);
// Will not check packet here, correctness of packet was tested somewhere else
event_rx.try_recv().expect("Receiving event TM failed");
}
#[test]
fn test_disable_event() {
let mut event_man = create_basic_man();
let (event_tx, event_rx) = channel();
let mut sender = EventTmSender { sender: event_tx };
let res = event_man.disable_tm_for_event(&LOW_SEV_EVENT);
assert!(res.is_ok());
assert!(res.unwrap());
let mut event_sent = event_man
.generate_pus_event_tm_generic(
Severity::LOW,
&mut sender,
&EMPTY_STAMP,
LOW_SEV_EVENT,
None,
)
.expect("Sending low severity event failed");
assert!(!event_sent);
let res = event_rx.try_recv();
assert!(res.is_err());
assert!(matches!(res.unwrap_err(), TryRecvError::Empty));
// Check that only the low severity event was disabled
event_sent = event_man
.generate_pus_event_tm(&mut sender, &EMPTY_STAMP, INFO_EVENT, None)
.expect("Sending info event failed");
assert!(event_sent);
event_rx.try_recv().expect("No info event received");
}
#[test]
fn test_reenable_event() {
let mut event_man = create_basic_man();
let (event_tx, event_rx) = channel();
let mut sender = EventTmSender { sender: event_tx };
let mut res = event_man.disable_tm_for_event_with_sev(&INFO_EVENT);
assert!(res.is_ok());
assert!(res.unwrap());
res = event_man.enable_tm_for_event_with_sev(&INFO_EVENT);
assert!(res.is_ok());
assert!(res.unwrap());
let event_sent = event_man
.generate_pus_event_tm(&mut sender, &EMPTY_STAMP, INFO_EVENT, None)
.expect("Sending info event failed");
assert!(event_sent);
event_rx.try_recv().expect("No info event received");
}
}

View File

@ -1131,7 +1131,8 @@ mod tests {
#[derive(Default)]
struct FallibleSender {}
impl EcssTmSender<DummyError> for FallibleSender {
impl EcssTmSender for FallibleSender {
type Error = DummyError;
fn send_tm(&mut self, _: PusTm) -> Result<(), EcssTmError<DummyError>> {
Err(EcssTmError::SendError(DummyError {}))
}

View File

@ -2,7 +2,7 @@ use fsrc_core::events::{EventU32, EventU32TypedSev, Severity, SeverityInfo};
use fsrc_core::pus::event_man::{DefaultPusMgmtBackendProvider, EventReporter, PusEventTmManager};
use fsrc_core::pus::{EcssTmError, EcssTmSender};
use spacepackets::tm::PusTm;
use std::sync::mpsc::{channel, SendError};
use std::sync::mpsc::{channel, SendError, TryRecvError};
const INFO_EVENT: EventU32TypedSev<SeverityInfo> =
EventU32TypedSev::<SeverityInfo>::const_new(1, 0);
@ -24,18 +24,27 @@ impl EcssTmSender for EventTmSender {
}
#[test]
fn main() {
fn test_basic() {
let reporter = EventReporter::new(0x02, 128).expect("Creating event repoter failed");
let backend = DefaultPusMgmtBackendProvider::<EventU32>::default();
let mut event_man = PusEventTmManager::new(reporter, Box::new(backend));
let (event_tx, event_rx) = channel();
let mut sender = EventTmSender { sender: event_tx };
let event_sent = event_man
let mut event_sent = event_man
.generate_pus_event_tm(&mut sender, &EMPTY_STAMP, INFO_EVENT, None)
.expect("Sending info event failed");
assert!(event_sent);
let packet = event_rx.recv().expect("Receiving event TM failed");
println!("{:?}", packet);
//let event_man;
// Will not check packet here, correctness of packet was tested somewhere else
event_rx.recv().expect("Receiving event TM failed");
let res = event_man.disable_tm_for_event_with_sev(&INFO_EVENT);
assert!(res.is_ok());
assert!(res.unwrap());
event_sent = event_man
.generate_pus_event_tm(&mut sender, &EMPTY_STAMP, INFO_EVENT, None)
.expect("Sending info event failed");
assert!(!event_sent);
let res = event_rx.try_recv();
assert!(res.is_err());
assert!(matches!(res.unwrap_err(), TryRecvError::Empty));
}