2023-02-11 19:15:00 -06:00
|
|
|
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";
|
2023-02-11 19:15:00 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-02-11 19:15:00 -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();
|
2023-02-11 19:15:00 -06:00
|
|
|
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();
|
2023-02-11 19:15:00 -06:00
|
|
|
},
|
|
|
|
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;
|
2023-02-11 19:15:00 -06:00
|
|
|
},
|
|
|
|
ServerEvent::MessageReceived { from, message } => {
|
2023-02-11 20:38:18 -06:00
|
|
|
log::info!("server received message");
|
2023-02-11 19:15:00 -06:00
|
|
|
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();
|
2023-02-11 19:15:00 -06:00
|
|
|
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();
|
2023-02-11 19:15:00 -06:00
|
|
|
},
|
|
|
|
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;
|
2023-02-11 19:15:00 -06:00
|
|
|
},
|
|
|
|
ClientEvent::MessageReceived(data) => {
|
2023-02-11 20:38:18 -06:00
|
|
|
log::info!("client received message");
|
2023-02-11 19:15:00 -06:00
|
|
|
assert_eq!(data, STC_MSG, "Received message not equal");
|
|
|
|
message_received = true;
|
2023-02-11 20:38:18 -06:00
|
|
|
client.disconnect();
|
2023-02-11 19:15:00 -06:00
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server_handle.join().unwrap();
|
2023-02-11 19:33:48 -06:00
|
|
|
client_handle.join().unwrap();
|
2023-02-11 19:15:00 -06:00
|
|
|
}
|