From 564f5982d28671df6c1101d77873531333f743e6 Mon Sep 17 00:00:00 2001 From: Lukas Metzner Date: Tue, 12 Dec 2023 22:37:47 +0100 Subject: [PATCH] test message to buffer conversion --- Cargo.toml | 6 +++++- src/config.rs | 2 +- tests/message_test.rs | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/message_test.rs diff --git a/Cargo.toml b/Cargo.toml index ed91475..2289db5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,15 +14,19 @@ tracing-subscriber = "0.3.18" [lib] name = "totoro" path = "src/lib.rs" +test = false [[bin]] name = "totoro" path = "src/main.rs" +test = false [[bin]] name = "sub-client" path = "examples/sub_client.rs" +test = false [[bin]] name = "pub-client" -path = "examples/pub_client.rs" \ No newline at end of file +path = "examples/pub_client.rs" +test = false \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index c536f67..09130e4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ const DEFAULT_MAX_CHANNEL_SIZE: usize = 64000; -const DEFAULT_MAX_BUFFER_SIZE: usize = 1024; const DEFAULT_LISTEN_ADDRESS: &str = "0.0.0.0:8000"; +pub const DEFAULT_MAX_BUFFER_SIZE: usize = 1024; #[derive(Clone)] pub struct TotoroConfig { diff --git a/tests/message_test.rs b/tests/message_test.rs new file mode 100644 index 0000000..656eade --- /dev/null +++ b/tests/message_test.rs @@ -0,0 +1,23 @@ +use totoro::{message::{ClientType, Message}, config::DEFAULT_MAX_BUFFER_SIZE}; + +#[test] +pub fn test_message_to_buffer() { + let sub_registration = Message::Registration(ClientType::Subscriber).to_buffer(&Default::default()); + assert_eq!(sub_registration[0], 0); // package type + assert_eq!(sub_registration[1], 0); // client type + + let pub_registration = Message::Registration(ClientType::Publisher).to_buffer(&Default::default()); + assert_eq!(pub_registration[0], 0); // package type + assert_eq!(pub_registration[1], 1); // client type + + let ack_registration = Message::RegistrationAck.to_buffer(&Default::default()); + assert_eq!(ack_registration[0], 1); // package type + + let message_str = "hello world".to_string(); + // substract one, because package will have first byte reserved to indicate package type + let mut test_buffer: [u8; DEFAULT_MAX_BUFFER_SIZE - 1] = [0; DEFAULT_MAX_BUFFER_SIZE - 1]; + test_buffer[..message_str.len()].copy_from_slice(message_str.as_bytes()); + let data = Message::Data(message_str.clone()).to_buffer(&Default::default()); + assert_eq!(data[0], 2); // package type + assert_eq!(data[1..], test_buffer); +}