From 16a5f6b801683693bcececf6fd6b62c75feab047 Mon Sep 17 00:00:00 2001 From: N8BWert Date: Mon, 6 Nov 2023 13:41:29 -0500 Subject: [PATCH] write tests for packed udp publisher and subscriber --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/publisher_subscriber/packed_udp.rs | 122 +++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f2d6ac..a707c33 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,7 +50,7 @@ checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" [[package]] name = "ncomm" -version = "0.2.1" +version = "0.3.0" dependencies = [ "ctrlc", "packed_struct", diff --git a/Cargo.toml b/Cargo.toml index 0f73531..5226162 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ncomm" -version = "0.2.1" +version = "0.3.0" edition = "2021" license = "MIT" description = "Rust Node-Based Communication Prototype Framework" diff --git a/src/publisher_subscriber/packed_udp.rs b/src/publisher_subscriber/packed_udp.rs index 27d232c..e57d066 100644 --- a/src/publisher_subscriber/packed_udp.rs +++ b/src/publisher_subscriber/packed_udp.rs @@ -155,4 +155,126 @@ impl Receive for Buff } } } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::{thread, time}; + use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; + + use packed_struct::prelude::*; + + #[derive(PackedStruct, Clone, Copy, Debug, PartialEq)] + #[packed_struct(bit_numbering="msb0")] + pub struct TestData { + #[packed_field(bits="0..=2")] + tiny_int: Integer>, + #[packed_field(bits="3..=4", ty = "enum")] + mode: SelfTestMode, + #[packed_field(bits="7")] + enabled: bool + } + + #[derive(PrimitiveEnum_u8, Clone, Copy, Debug, PartialEq)] + pub enum SelfTestMode { + NormalMode = 0, + PositiveSignSelfTest = 1, + NegativeSignSelfTest = 2, + DebugMode = 3, + } + + #[test] + // Test that a packed udp publisher and subscriber can be created + fn test_create_packed_udp_publisher_and_subscriber() { + let subscriber: PackedUdpSubscriber = PackedUdpSubscriber::new("127.0.0.1:10001", Some("127.0.0.1:10000")); + let publisher: PackedUdpPublisher = PackedUdpPublisher::new("127.0.0.1:10000", vec!["127.0.0.1:10001"]); + + assert_eq!(subscriber.rx.local_addr().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 10001))); + assert_eq!(subscriber.data, None); + + assert_eq!(publisher.tx.local_addr().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 10000))); + assert_eq!(String::from(publisher.addresses[0]), String::from("127.0.0.1:10001")); + } + + #[test] + // Test that a packed udp publisher and subscriber can send data + fn test_send_data_packed_udp_publisher_and_subscriber() { + let mut subscriber: PackedUdpSubscriber = PackedUdpSubscriber::new("127.0.0.1:10003", Some("127.0.0.1:10002")); + let mut publisher: PackedUdpPublisher = PackedUdpPublisher::new("127.0.0.1:10002", vec!["127.0.0.1:10003"]); + + publisher.send(TestData { tiny_int: 5.into(), mode: SelfTestMode::DebugMode, enabled: true }); + + thread::sleep(time::Duration::from_millis(10)); + + subscriber.update_data(); + + assert_eq!(subscriber.data.unwrap(), TestData { tiny_int: 5.into(), mode: SelfTestMode::DebugMode, enabled: true }); + } + + #[test] + // Test that a packed udp publisher and subscriber can send multiple pieces of data + fn test_send_many_packed_data_udp_publisher_and_subscriber() { + let mut subscriber: PackedUdpSubscriber = PackedUdpSubscriber::new("127.0.0.1:10005", Some("127.0.0.1:10004")); + let mut publisher: PackedUdpPublisher = PackedUdpPublisher::new("127.0.0.1:10004", vec!["127.0.0.1:10005"]); + + for i in 0..=5u8 { + publisher.send(TestData { tiny_int: i.into(), mode: SelfTestMode::DebugMode, enabled: true }); + } + + thread::sleep(time::Duration::from_millis(10)); + + subscriber.update_data(); + + assert_eq!(subscriber.data.unwrap(), TestData { tiny_int: 5.into(), mode: SelfTestMode::DebugMode, enabled: true }); + } + + #[test] + // Test that a buffered udp subscriber can be created + fn test_create_buffered_udp_subscriber() { + let subscriber: BufferedPackedUdpSubscriber = BufferedPackedUdpSubscriber::new("127.0.0.1:10007", Some("127.0.0.1:10006")); + let publisher: PackedUdpPublisher = PackedUdpPublisher::new("127.0.0.1:10006", vec!["127.0.0.1:10007"]); + + assert_eq!(subscriber.rx.local_addr().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 10007))); + assert_eq!(subscriber.data.len(), 0); + + assert_eq!(publisher.tx.local_addr().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 10006))); + assert_eq!(String::from(publisher.addresses[0]), String::from("127.0.0.1:10007")); + } + + #[test] + // Test that a udp publisher can send data to the buffered udp subscriber + fn test_send_packed_data_buffered_udp_subscriber() { + let mut subscriber: BufferedPackedUdpSubscriber = BufferedPackedUdpSubscriber::new("127.0.0.1:10009", Some("127.0.0.1:10008")); + let mut publisher: PackedUdpPublisher = PackedUdpPublisher::new("127.0.0.1:10008", vec!["127.0.0.1:10009"]); + + publisher.send(TestData { tiny_int: 5.into(), mode: SelfTestMode::DebugMode, enabled: true }); + + thread::sleep(time::Duration::from_millis(10)); + + subscriber.update_data(); + + assert_eq!(subscriber.data.len(), 1); + assert_eq!(subscriber.data[0], TestData { tiny_int: 5.into(), mode: SelfTestMode::DebugMode, enabled: true }); + } + + #[test] + // Test that a udp publisher can send many data to the buffered udp subscriber + fn test_send_many_packed_data_buffered_udp_subscriber() { + let mut subscriber: BufferedPackedUdpSubscriber = BufferedPackedUdpSubscriber::new("127.0.0.1:10011", Some("127.0.0.1:10010")); + let mut publisher: PackedUdpPublisher = PackedUdpPublisher::new("127.0.0.1:10010", vec!["127.0.0.1:10011"]); + + for i in 0..=5u8 { + publisher.send(TestData { tiny_int: i.into(), mode: SelfTestMode::DebugMode, enabled: true }); + } + + thread::sleep(time::Duration::from_millis(10)); + + subscriber.update_data(); + + assert_eq!(subscriber.data.len(), 6); + for i in 0..=5u8 { + assert_eq!(subscriber.data[i as usize], TestData { tiny_int: i.into(), mode: SelfTestMode::DebugMode, enabled: true }); + } + } } \ No newline at end of file