From dbd97c0df6703bbd28eaf1fd0a21f0b3a1d1ac5c Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 1 Mar 2024 10:23:29 +0100 Subject: [PATCH] add more tests --- satrs/src/mode.rs | 67 ++++++++++++++++++++++++++++++++++++++++++-- satrs/src/request.rs | 45 ++++++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/satrs/src/mode.rs b/satrs/src/mode.rs index 72a316d..9be379b 100644 --- a/satrs/src/mode.rs +++ b/satrs/src/mode.rs @@ -546,11 +546,11 @@ mod tests { use std::sync::mpsc; use crate::{ - mode::{ModeAndSubmode, ModeReply}, + mode::{ModeAndSubmode, ModeReply, ModeReplySender, ModeRequestSender}, request::GenericMessage, }; - use super::{ModeRequest, ModeRequestorMpsc}; + use super::{ModeRequest, ModeRequestorAndHandlerMpsc, ModeRequestorMpsc}; const TEST_CHANNEL_ID_0: u32 = 5; const TEST_CHANNEL_ID_1: u32 = 6; @@ -593,4 +593,67 @@ mod tests { #[test] fn test_simple_mode_request_handler() {} + + #[test] + fn test_mode_requestor_and_request_handler_request_sending() { + let (reply_sender_to_connector, reply_receiver_of_connector) = mpsc::channel(); + let (request_sender_to_connector, request_receiver_of_connector) = mpsc::channel(); + + let (request_sender_to_channel_1, request_receiver_channel_1) = mpsc::channel(); + //let (reply_sender_to_channel_2, reply_receiver_channel_2) = mpsc::channel(); + let mut mode_connector = ModeRequestorAndHandlerMpsc::new( + TEST_CHANNEL_ID_0, + request_receiver_of_connector, + reply_receiver_of_connector, + ); + assert_eq!( + ModeRequestSender::local_channel_id(&mode_connector), + TEST_CHANNEL_ID_0 + ); + assert_eq!( + ModeReplySender::local_channel_id(&mode_connector), + TEST_CHANNEL_ID_0 + ); + assert_eq!(mode_connector.local_channel_id_generic(), TEST_CHANNEL_ID_0); + + mode_connector.add_request_target(TEST_CHANNEL_ID_1, request_sender_to_channel_1); + //mode_connector.add_reply_target(TEST_CHANNEL_ID_2, reply_sender_to_channel_2); + + // Send a request and verify it arrives at the receiver. + let request_id = 2; + let sent_request = ModeRequest::ReadMode; + mode_connector + .send_mode_request(request_id, TEST_CHANNEL_ID_1, sent_request) + .expect("send failed"); + + let request = request_receiver_channel_1.recv().expect("recv failed"); + assert_eq!(request.request_id, 2); + assert_eq!(request.sender_id, TEST_CHANNEL_ID_0); + assert_eq!(request.message, ModeRequest::ReadMode); + } + + #[test] + fn test_mode_requestor_and_request_handler_reply_sending() { + let (reply_sender_to_connector, reply_receiver_of_connector) = mpsc::channel(); + let (request_sender_to_connector, request_receiver_of_connector) = mpsc::channel(); + + let (reply_sender_to_channel_2, reply_receiver_channel_2) = mpsc::channel(); + let mut mode_connector = ModeRequestorAndHandlerMpsc::new( + TEST_CHANNEL_ID_0, + request_receiver_of_connector, + reply_receiver_of_connector, + ); + mode_connector.add_reply_target(TEST_CHANNEL_ID_2, reply_sender_to_channel_2); + + // Send a request and verify it arrives at the receiver. + let request_id = 2; + let sent_reply = ModeReply::ModeInfo(ModeAndSubmode::new(3, 5)); + mode_connector + .send_mode_reply(request_id, TEST_CHANNEL_ID_2, sent_reply) + .expect("send failed"); + let reply = reply_receiver_channel_2.recv().expect("recv failed"); + assert_eq!(reply.request_id, 2); + assert_eq!(reply.sender_id, TEST_CHANNEL_ID_0); + assert_eq!(reply.message, sent_reply); + } } diff --git a/satrs/src/request.rs b/satrs/src/request.rs index c9b6818..047c3e8 100644 --- a/satrs/src/request.rs +++ b/satrs/src/request.rs @@ -382,10 +382,13 @@ mod tests { ByteConversionError, SpHeader, }; - use super::{GenericMessage, MessageReceiverWithId, TargetAndApidId}; + use crate::queue::{GenericSendError, GenericTargetedMessagingError}; + + use super::{GenericMessage, MessageReceiverWithId, MessageSenderMapWithId, TargetAndApidId}; const TEST_CHANNEL_ID_0: u32 = 1; const TEST_CHANNEL_ID_1: u32 = 2; + const TEST_CHANNEL_ID_2: u32 = 3; #[test] fn test_basic_target_id_with_apid() { @@ -443,4 +446,44 @@ mod tests { assert_eq!(reply.sender_id, TEST_CHANNEL_ID_1); assert_eq!(reply.message, ()); } + + #[test] + fn test_sender_map() { + let (sender0, receiver0) = mpsc::channel(); + let (sender1, receiver1) = mpsc::channel(); + let mut sender_map_with_id = MessageSenderMapWithId::new(TEST_CHANNEL_ID_0); + sender_map_with_id.add_message_target(TEST_CHANNEL_ID_1, sender0); + sender_map_with_id.add_message_target(TEST_CHANNEL_ID_2, sender1); + sender_map_with_id + .send_message(1, TEST_CHANNEL_ID_1, 5) + .expect("sending message failed"); + let mut reply = receiver0.recv().expect("receiving message failed"); + assert_eq!(reply.request_id, 1); + assert_eq!(reply.sender_id, TEST_CHANNEL_ID_0); + assert_eq!(reply.message, 5); + sender_map_with_id + .send_message(2, TEST_CHANNEL_ID_2, 10) + .expect("sending message failed"); + reply = receiver1.recv().expect("receiving message failed"); + assert_eq!(reply.request_id, 2); + assert_eq!(reply.sender_id, TEST_CHANNEL_ID_0); + assert_eq!(reply.message, 10); + } + + #[test] + fn test_sender_map_target_does_not_exist() { + let (sender0, receiver0) = mpsc::channel(); + let mut sender_map_with_id = MessageSenderMapWithId::new(TEST_CHANNEL_ID_0); + sender_map_with_id.add_message_target(TEST_CHANNEL_ID_1, sender0); + let result = sender_map_with_id.send_message(1, TEST_CHANNEL_ID_2, 5); + assert!(result.is_err()); + let error = result.unwrap_err(); + if let GenericTargetedMessagingError::Send(GenericSendError::TargetDoesNotExist(target)) = + error + { + assert_eq!(target, TEST_CHANNEL_ID_2); + } else { + panic!("Unexpected error type"); + } + } }