Skip to content

Commit

Permalink
write tests for packed udp publisher and subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
N8BWert committed Nov 6, 2023
1 parent f585703 commit 16a5f6b
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
122 changes: 122 additions & 0 deletions src/publisher_subscriber/packed_udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,126 @@ impl<Data: PackedStruct + Send + Clone, const DATA_SIZE: usize> 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<u8, packed_bits::Bits::<3>>,
#[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<TestData, 1> = PackedUdpSubscriber::new("127.0.0.1:10001", Some("127.0.0.1:10000"));
let publisher: PackedUdpPublisher<TestData> = 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<TestData, 1> = PackedUdpSubscriber::new("127.0.0.1:10003", Some("127.0.0.1:10002"));
let mut publisher: PackedUdpPublisher<TestData> = 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<TestData, 1> = PackedUdpSubscriber::new("127.0.0.1:10005", Some("127.0.0.1:10004"));
let mut publisher: PackedUdpPublisher<TestData> = 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<TestData, 1> = BufferedPackedUdpSubscriber::new("127.0.0.1:10007", Some("127.0.0.1:10006"));
let publisher: PackedUdpPublisher<TestData> = 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<TestData, 1> = BufferedPackedUdpSubscriber::new("127.0.0.1:10009", Some("127.0.0.1:10008"));
let mut publisher: PackedUdpPublisher<TestData> = 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<TestData, 1> = BufferedPackedUdpSubscriber::new("127.0.0.1:10011", Some("127.0.0.1:10010"));
let mut publisher: PackedUdpPublisher<TestData> = 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 });
}
}
}

0 comments on commit 16a5f6b

Please sign in to comment.