From 00f90156bdd20cbf304671aab8c389549dd9658c Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 12 Feb 2023 02:33:48 +0100 Subject: [PATCH] test --- kubi-udp/Cargo.toml | 3 +++ kubi-udp/src/client.rs | 11 ++++++----- kubi-udp/tests/test.rs | 7 +++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/kubi-udp/Cargo.toml b/kubi-udp/Cargo.toml index 86da226..f08a674 100644 --- a/kubi-udp/Cargo.toml +++ b/kubi-udp/Cargo.toml @@ -11,3 +11,6 @@ anyhow = "1.0" hashbrown = "0.13" nohash-hasher = "0.2.0" log = "0.4" + +[dev-dependencies] +kubi-logging = { path = "../kubi-logging" } diff --git a/kubi-udp/src/client.rs b/kubi-udp/src/client.rs index 0174048..049c01b 100644 --- a/kubi-udp/src/client.rs +++ b/kubi-udp/src/client.rs @@ -19,8 +19,7 @@ pub enum DisconnectReason { NotConnected, ClientDisconnected, KickedByServer(Option), - ClientTimeout, - ServerTimeout, + Timeout, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -105,6 +104,7 @@ impl Client where S: Encode + Decode, R: Encode + Decode { pub fn connect(&mut self) -> Result<()> { + log::info!("client connect called"); if self.status != ClientStatus::Disconnected { bail!("Not Disconnected"); } @@ -139,7 +139,7 @@ impl Client where S: Encode + Decode, R: Encode + Decode { if self.timeout.elapsed() > self.config.timeout { log::warn!("Client timed out"); //We don't care if this packet actually gets sent because the server is likely dead - let _ = self.disconnect_inner(DisconnectReason::ClientDisconnected, false).map_err(|_| { + let _ = self.disconnect_inner(DisconnectReason::Timeout, false).map_err(|_| { log::warn!("Failed to send disconnect packet"); }); return Ok(()) @@ -162,15 +162,16 @@ impl Client where S: Encode + Decode, R: Encode + Decode { self.reset_timeout(); match packet { ServerPacket::Connected(client_id) => { + log::info!("client connected with id {client_id}"); self.client_id = Some(client_id); self.status = ClientStatus::Connected; self.event_queue.push_back(ClientEvent::Connected(client_id)); return Ok(()) }, ServerPacket::Disconnected(reason) => { + log::info!("client kicked: {reason}"); let reason = DisconnectReason::KickedByServer(Some(reason)); - //this should never fail but we're handling the error anyway - self.disconnect_inner(reason, true)?; + self.disconnect_inner(reason, true)?; //this should never fail but we're handling the error anyway return Ok(()) }, ServerPacket::Data(message) => { diff --git a/kubi-udp/tests/test.rs b/kubi-udp/tests/test.rs index 82d1bbd..d40da5e 100644 --- a/kubi-udp/tests/test.rs +++ b/kubi-udp/tests/test.rs @@ -4,7 +4,7 @@ use kubi_udp::{ }; use std::{thread, time::Duration}; -const TEST_ADDR: &str = "127.0.0.1:12345"; +const TEST_ADDR: &str = "127.0.0.1:22342"; type CtsMessage = u32; type StcMessage = u64; @@ -14,6 +14,9 @@ const STC_MSG: StcMessage = 0xdead_beef_cafe_face; #[test] fn test_connection() { + //Init logging + kubi_logging::init(); + //Create server and client let mut server: Server = Server::bind( TEST_ADDR.parse().expect("Invalid TEST_ADDR"), @@ -82,6 +85,6 @@ fn test_connection() { } }); - client_handle.join().unwrap(); server_handle.join().unwrap(); + client_handle.join().unwrap(); }