Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed May 8, 2024
1 parent b86c2eb commit 4a8db6b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
14 changes: 7 additions & 7 deletions satrs-minisim/src/acs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,18 @@ pub mod tests {
use satrs_minisim::{
acs::{
lis3mdl::{self, MgmLis3MdlReply},
MgmRequest, MgtDipole, MgtHkSet, MgtReply, MgtRequest,
MgmRequestLis3Mdl, MgtDipole, MgtHkSet, MgtReply, MgtRequest,
},
eps::PcduSwitch,
SerializableSimMsgPayload, SimMessageProvider, SimRequest, SimTarget,
SerializableSimMsgPayload, SimComponent, SimMessageProvider, SimRequest,
};

use crate::{eps::tests::switch_device_on, test_helpers::SimTestbench};

#[test]
fn test_basic_mgm_request() {
let mut sim_testbench = SimTestbench::new();
let request = SimRequest::new_with_epoch_time(MgmRequest::RequestSensorData);
let request = SimRequest::new_with_epoch_time(MgmRequestLis3Mdl::RequestSensorData);
sim_testbench
.send_request(request)
.expect("sending MGM request failed");
Expand All @@ -208,7 +208,7 @@ pub mod tests {
let sim_reply = sim_testbench.try_receive_next_reply();
assert!(sim_reply.is_some());
let sim_reply = sim_reply.unwrap();
assert_eq!(sim_reply.component(), SimTarget::MgmLis3Mdl);
assert_eq!(sim_reply.component(), SimComponent::MgmLis3Mdl);
let reply = MgmLis3MdlReply::from_sim_message(&sim_reply)
.expect("failed to deserialize MGM sensor values");
assert_eq!(reply.common.switch_state, SwitchStateBinary::Off);
Expand All @@ -222,7 +222,7 @@ pub mod tests {
let mut sim_testbench = SimTestbench::new();
switch_device_on(&mut sim_testbench, PcduSwitch::Mgm);

let mut request = SimRequest::new_with_epoch_time(MgmRequest::RequestSensorData);
let mut request = SimRequest::new_with_epoch_time(MgmRequestLis3Mdl::RequestSensorData);
sim_testbench
.send_request(request)
.expect("sending MGM request failed");
Expand All @@ -231,12 +231,12 @@ pub mod tests {
let mut sim_reply_res = sim_testbench.try_receive_next_reply();
assert!(sim_reply_res.is_some());
let mut sim_reply = sim_reply_res.unwrap();
assert_eq!(sim_reply.component(), SimTarget::MgmLis3Mdl);
assert_eq!(sim_reply.component(), SimComponent::MgmLis3Mdl);
let first_reply = MgmLis3MdlReply::from_sim_message(&sim_reply)
.expect("failed to deserialize MGM sensor values");
sim_testbench.step_by(Duration::from_millis(50));

request = SimRequest::new_with_epoch_time(MgmRequest::RequestSensorData);
request = SimRequest::new_with_epoch_time(MgmRequestLis3Mdl::RequestSensorData);
sim_testbench
.send_request(request)
.expect("sending MGM request failed");
Expand Down
55 changes: 52 additions & 3 deletions satrs-minisim/src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl SimUdpServer {
mod tests {
use std::{
io::ErrorKind,
net::{SocketAddr, UdpSocket},
sync::{
atomic::{AtomicBool, Ordering},
mpsc, Arc,
Expand All @@ -159,7 +160,6 @@ mod tests {

use satrs_minisim::{
eps::{PcduReply, PcduRequest},
udp::{ReceptionError, SimUdpClient},
SimCtrlReply, SimCtrlRequest, SimReply, SimRequest,
};

Expand All @@ -171,8 +171,57 @@ mod tests {
// Wait time to ensure even possibly laggy systems like CI servers can run the tests.
const SERVER_WAIT_TIME_MS: u64 = 50;

#[derive(thiserror::Error, Debug)]
pub enum ReceptionError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Serde JSON error: {0}")]
SerdeJson(#[from] serde_json::Error),
}

pub struct SimUdpTestClient {
socket: UdpSocket,
pub reply_buf: [u8; 4096],
}

impl SimUdpTestClient {
pub fn new(
server_addr: &SocketAddr,
non_blocking: bool,
read_timeot_ms: Option<u64>,
) -> std::io::Result<Self> {
let socket = UdpSocket::bind("127.0.0.1:0")?;
socket.set_nonblocking(non_blocking)?;
socket
.connect(server_addr)
.expect("could not connect to server addr");
if let Some(read_timeout) = read_timeot_ms {
// Set a read timeout so the test does not hang on failures.
socket.set_read_timeout(Some(Duration::from_millis(read_timeout)))?;
}
Ok(Self {
socket,
reply_buf: [0; 4096],
})
}

pub fn send_request(&self, sim_request: &SimRequest) -> std::io::Result<usize> {
self.socket.send(
&serde_json::to_vec(sim_request).expect("conversion of request to vector failed"),
)
}

pub fn recv_raw(&mut self) -> std::io::Result<usize> {
self.socket.recv(&mut self.reply_buf)
}

pub fn recv_sim_reply(&mut self) -> Result<SimReply, ReceptionError> {
let read_len = self.recv_raw()?;
Ok(serde_json::from_slice(&self.reply_buf[0..read_len])?)
}
}
struct UdpTestbench {
client: SimUdpClient,
client: SimUdpTestClient,
stop_signal: Arc<AtomicBool>,
request_receiver: mpsc::Receiver<SimRequest>,
reply_sender: mpsc::Sender<SimReply>,
Expand All @@ -197,7 +246,7 @@ mod tests {
let server_addr = server.server_addr()?;
Ok((
Self {
client: SimUdpClient::new(
client: SimUdpTestClient::new(
&server_addr,
client_non_blocking,
client_read_timeout_ms,
Expand Down

0 comments on commit 4a8db6b

Please sign in to comment.