kubi/kubi-udp/tests/test.rs

94 lines
2.8 KiB
Rust
Raw Normal View History

use kubi_udp::{
server::{Server, ServerConfig, ServerEvent},
client::{Client, ClientConfig, ClientEvent},
};
use std::{thread, time::Duration};
2023-02-11 19:33:48 -06:00
const TEST_ADDR: &str = "127.0.0.1:22342";
type CtsMessage = u32;
type StcMessage = u64;
const CTS_MSG: CtsMessage = 0xbeef_face;
const STC_MSG: StcMessage = 0xdead_beef_cafe_face;
#[test]
fn test_connection() {
2023-02-11 19:33:48 -06:00
//Init logging
kubi_logging::init();
2023-02-11 20:15:31 -06:00
//Create server and client
let mut server: Server<StcMessage, CtsMessage> = Server::bind(
TEST_ADDR.parse().expect("Invalid TEST_ADDR"),
ServerConfig::default()
).expect("Failed to create server");
let mut client: Client<CtsMessage, StcMessage> = Client::new(
TEST_ADDR.parse().unwrap(),
ClientConfig::default()
).expect("Failed to create client");
//Start server update thread
let server_handle = thread::spawn(move || {
let mut message_received = false;
loop {
2023-02-11 20:15:31 -06:00
server.update().unwrap();
let events: Vec<_> = server.process_events().collect();
for event in events {
match event {
ServerEvent::Connected(id) => {
assert_eq!(id.get(), 1, "Unexpected client id");
2023-02-11 20:15:31 -06:00
server.send_message(id, STC_MSG).unwrap();
},
ServerEvent::Disconnected(id) => {
2023-02-11 20:38:18 -06:00
assert!(message_received, "Client {id} disconnected from the server before sending the message");
return;
},
ServerEvent::MessageReceived { from, message } => {
2023-02-11 20:38:18 -06:00
log::info!("server received message");
assert_eq!(message, CTS_MSG, "Received message not equal");
message_received = true;
},
_ => ()
}
}
}
});
//Wait a bit
thread::sleep(Duration::from_secs(1));
//Connect client
client.connect().expect("Client connect failed");
//Start updating the client
let client_handle = thread::spawn(move || {
let mut message_received = false;
loop {
2023-02-11 20:15:31 -06:00
client.update().unwrap();
let events: Vec<_> = client.process_events().collect();
for event in events {
match event {
ClientEvent::Connected(id) => {
assert_eq!(id.get(), 1, "Unexpected client id");
2023-02-11 20:15:31 -06:00
client.send_message(CTS_MSG).unwrap();
},
ClientEvent::Disconnected(reason) => {
2023-02-11 20:38:18 -06:00
assert!(message_received, "Client lost connection to the server before sending the message with reason: {reason:?}");
return;
},
ClientEvent::MessageReceived(data) => {
2023-02-11 20:38:18 -06:00
log::info!("client received message");
assert_eq!(data, STC_MSG, "Received message not equal");
message_received = true;
2023-03-07 10:05:55 -06:00
client.disconnect().unwrap();
},
_ => ()
}
}
}
});
server_handle.join().unwrap();
2023-02-11 19:33:48 -06:00
client_handle.join().unwrap();
}