Skip to content

Commit

Permalink
test message to buffer conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Metzner committed Dec 12, 2023
1 parent 30bfb4f commit 564f598
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
path = "examples/pub_client.rs"
test = false
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions tests/message_test.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 564f598

Please sign in to comment.