Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnarMorrigan committed Nov 28, 2024
1 parent 9108239 commit d756866
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 374 deletions.
1 change: 0 additions & 1 deletion mqrstt/README.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ fn main() {
assert_eq!(result.unwrap(), NetworkStatus::OutgoingDisconnect);
});
}

```

### Tokio example:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 3 additions & 5 deletions examples/tcp/src/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ pub struct Handler {
}

impl AsyncEventHandler for Handler {
fn handle(&mut self, incoming_packet: mqrstt::packets::Packet) -> impl std::future::Future<Output = ()> + Send + Sync {
async move {
if let mqrstt::packets::Packet::Publish(publish) = incoming_packet {
self.byte_count += publish.payload.len() as u64;
}
async fn handle(&mut self, incoming_packet: mqrstt::packets::Packet) {
if let mqrstt::packets::Packet::Publish(publish) = incoming_packet {
self.byte_count += publish.payload.len() as u64;
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions mqrstt/src/packets/connect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,7 @@ mod tests {
];

buf.extend_from_slice(packet);
let c = Connect::read(0, 0, buf.into()).unwrap();

dbg!(c);
Connect::read(0, 0, buf.into()).unwrap();
}

#[test]
Expand Down Expand Up @@ -427,8 +425,6 @@ mod tests {
c.write(&mut write_buf).unwrap();

assert_eq!(packet.to_vec(), write_buf.to_vec());

dbg!(c);
}

#[tokio::test]
Expand Down Expand Up @@ -522,8 +518,6 @@ mod tests {

let c = Connect::read(0, 0, buf.into()).unwrap();

dbg!(c.clone());

let mut write_buf = bytes::BytesMut::new();
c.write(&mut write_buf).unwrap();

Expand Down
168 changes: 0 additions & 168 deletions mqrstt/src/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,171 +442,3 @@ impl std::fmt::Display for PacketType {
std::fmt::Debug::fmt(&self, f)
}
}

#[cfg(test)]
mod tests {

use bytes::BytesMut;

use crate::packets::Packet;

use crate::tests::test_packets::*;

#[rstest::rstest]
#[case::connect_case(connect_case())]
#[case::ping_req_case(ping_req_case().1)]
#[case::ping_resp_case(ping_resp_case().1)]
#[case::connack_case(connack_case().1)]
#[case::create_subscribe_packet(create_subscribe_packet(1))]
#[case::create_subscribe_packet(create_subscribe_packet(65335))]
#[case::create_puback_packet(create_puback_packet(1))]
#[case::create_puback_packet(create_puback_packet(65335))]
#[case::create_disconnect_packet(create_disconnect_packet())]
#[case::create_connack_packet(create_connack_packet(true))]
#[case::create_connack_packet(create_connack_packet(false))]
#[case::publish_packet_1(publish_packet_1())]
#[case::publish_packet_2(publish_packet_2())]
#[case::publish_packet_3(publish_packet_3())]
#[case::publish_packet_4(publish_packet_4())]
#[case::create_empty_publish_packet(create_empty_publish_packet())]
#[case::subscribe(subscribe_case())]
#[case::suback(suback_case())]
#[case::unsubscribe(unsubscribe_case())]
#[case::unsuback(unsuback_case())]
#[case::pubcomp_case(pubcomp_case())]
#[case::pubrec_case(pubrec_case())]
#[case::pubrec_case(pubrel_case2())]
#[case::auth_case(auth_case())]
fn test_write_read_write_read_cases(#[case] packet: Packet) {
use crate::packets::WireLength;

let mut buffer = BytesMut::new();

packet.write(&mut buffer).unwrap();

let wire_len = packet.wire_len();
assert_eq!(wire_len, buffer.len());

// dbg!(wire_len);

// let a: Vec<_> = buffer.iter().map(|f| *f as u16).collect();
// println!("{:?}", a);

let res1 = Packet::read(&mut buffer).unwrap();

assert_eq!(packet, res1);

let mut buffer = BytesMut::new();
res1.write(&mut buffer).unwrap();
let res2 = Packet::read(&mut buffer).unwrap();

assert_eq!(res1, res2);
}

#[rstest::rstest]
#[case::connect_case(connect_case())]
#[case::ping_req_case(ping_req_case().1)]
#[case::ping_resp_case(ping_resp_case().1)]
#[case::connack_case(connack_case().1)]
#[case::create_subscribe_packet(create_subscribe_packet(1))]
#[case::create_subscribe_packet(create_subscribe_packet(65335))]
#[case::create_puback_packet(create_puback_packet(1))]
#[case::create_puback_packet(create_puback_packet(65335))]
#[case::create_disconnect_packet(create_disconnect_packet())]
#[case::create_connack_packet(create_connack_packet(true))]
#[case::create_connack_packet(create_connack_packet(false))]
#[case::publish_packet_1(publish_packet_1())]
#[case::publish_packet_2(publish_packet_2())]
#[case::publish_packet_3(publish_packet_3())]
#[case::publish_packet_4(publish_packet_4())]
#[case::create_empty_publish_packet(create_empty_publish_packet())]
#[case::subscribe(subscribe_case())]
#[case::suback(suback_case())]
#[case::unsubscribe(unsubscribe_case())]
#[case::unsuback(unsuback_case())]
#[case::pubcomp_case(pubcomp_case())]
#[case::pubrec_case(pubrec_case())]
#[case::pubrec_case(pubrel_case2())]
#[case::auth_case(auth_case())]
#[tokio::test]
async fn test_async_write_read_write_read_cases(#[case] packet: Packet) {
use crate::packets::WireLength;

let mut buffer = Vec::with_capacity(1000);
let res = packet.async_write(&mut buffer).await.unwrap();

let wire_len = packet.wire_len();

assert_eq!(res, buffer.len());
assert_eq!(wire_len, buffer.len());

let mut buf = buffer.as_slice();

let res1 = Packet::async_read(&mut buf).await.unwrap();

pretty_assertions::assert_eq!(packet, res1);
}

#[rstest::rstest]
#[case::disconnect(disconnect_case())]
#[case::ping_req(ping_req_case())]
#[case::ping_resp(ping_resp_case())]
#[case::publish(publish_case())]
#[case::pubrel(pubrel_case())]
#[case::pubrel_smallest(pubrel_smallest_case())]
fn test_read_write_cases(#[case] (bytes, expected_packet): (&[u8], Packet)) {
let mut buffer = BytesMut::from_iter(bytes);

let res = Packet::read(&mut buffer);

assert!(res.is_ok());

let packet = res.unwrap();

assert_eq!(packet, expected_packet);

buffer.clear();

packet.write(&mut buffer).unwrap();

assert_eq!(buffer.to_vec(), bytes.to_vec())
}

#[rstest::rstest]
#[case::disconnect(disconnect_case())]
#[case::ping_req(ping_req_case())]
#[case::ping_resp(ping_resp_case())]
#[case::publish(publish_case())]
#[case::pubrel(pubrel_case())]
#[case::pubrel_smallest(pubrel_smallest_case())]
#[tokio::test]
async fn test_async_read_write(#[case] (mut bytes, expected_packet): (&[u8], Packet)) {
let input = bytes.to_vec();

let res = Packet::async_read(&mut bytes).await;

dbg!(&res);
assert!(res.is_ok());

let packet = res.unwrap();

assert_eq!(packet, expected_packet);

let mut out = Vec::with_capacity(1000);

packet.async_write(&mut out).await.unwrap();

assert_eq!(out, input)
}

// #[rstest::rstest]
// #[case(&[59, 1, 0, 59])]
// #[case(&[16, 14, 0, 4, 77, 81, 84, 84, 5, 247, 247, 252, 1, 17, 247, 247, 247])]
// fn test_read_error(#[case] bytes: &[u8]) {
// let mut buffer = BytesMut::from_iter(bytes);

// let res = Packet::read_from_buffer(&mut buffer);

// assert!(res.is_err());
// }
}
11 changes: 0 additions & 11 deletions mqrstt/src/packets/subscribe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,6 @@ mod tests {
0x35, 0xd6, 0x02, 0x0b, 0x01, 0x00, 0x16, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x15,
];

// let sub_data = &[0x35, 0xd6, 0x02, 0x0b, 0x01, 0x00, 0x16, 0x73,
// 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
// 0x2f, 0x74, 0x65, 0x73, 0x74, 0x15];

// let sub_data = &[
// 53, 214, 2, 11, 1, 0, 22, 115, 117, 98, 115,
// 99, 114, 105, 98, 101, 47, 101, 120, 97, 109,
// 112, 108, 101, 47, 116, 101, 115, 116, 21,
// ];

let mut bufmut = BytesMut::new();
bufmut.extend(&sub_data[..]);
Expand All @@ -451,8 +442,6 @@ mod tests {

let s = Subscribe::read(0, 30, buf.clone()).unwrap();

let s = dbg!(s);

let mut result = BytesMut::new();
s.write(&mut result).unwrap();

Expand Down
4 changes: 3 additions & 1 deletion mqrstt/src/state_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,11 @@ mod handler_tests {
Packet, PubComp, PubCompProperties, PubCompReasonCode, PubRec, PubRecProperties, PubRecReasonCode, PubRel, PubRelProperties, PubRelReasonCode, QoS, SubAck, SubAckProperties,
SubAckReasonCode, UnsubAck, UnsubAckProperties, UnsubAckReasonCode,
},
tests::test_packets::{create_connack_packet, create_puback_packet, create_publish_packet, create_subscribe_packet, create_unsubscribe_packet},
ConnectOptions, StateHandler,
};

use crate::tests::test_packets::*;

fn handler(clean_start: bool) -> (StateHandler, Receiver<u16>) {
let (apkids, apkids_r) = AvailablePacketIds::new(100);

Expand Down
4 changes: 1 addition & 3 deletions mqrstt/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
pub mod resources;
mod test_bytes;
pub mod test_packets;
pub mod tls;
pub(crate) mod test_packets;
4 changes: 0 additions & 4 deletions mqrstt/src/tests/resources/mod.rs

This file was deleted.

93 changes: 0 additions & 93 deletions mqrstt/src/tests/resources/test_bytes_packet.rs

This file was deleted.

Loading

0 comments on commit d756866

Please sign in to comment.