//! src/udp_client_test.rs use ugly_smart_lib::net_pub::{INetCallBack, INetwork}; use ugly_smart_lib::smart_pub::StdArc; use ugly_smart_lib::CUdpClient; use ugly_smart_lib::SUdpClientParams; use crate::helper::init_log; #[tokio::test] async fn udp_client_test() { struct MyClient { pub sender: ugly_smart_lib::smart_pub::ArcMutex< Option)>>, >, } init_log(); impl INetCallBack<(String, Vec)> for MyClient { fn data_callback( self: ugly_smart_lib::smart_pub::StdArc, data: Vec, handle: String, _serv_type: ugly_smart_lib::net_pub::ServiceType, ) { let sender_arc = self.sender.clone(); let sender_mutex = sender_arc.lock().unwrap(); match &*sender_mutex { Some(sender) => { let _ = tokio::time::sleep(std::time::Duration::from_secs(1)); sender.send((handle, data)).unwrap(); } None => {} } } fn state_callback( self: ugly_smart_lib::smart_pub::StdArc, handle: String, state: ugly_smart_lib::net_pub::EConnectionState<(String, Vec)>, _serv_type: ugly_smart_lib::net_pub::ServiceType, ) { match state { ugly_smart_lib::net_pub::EConnectionState::Running(sender) => { let sender_arc = self.sender.clone(); let mut sender_mutex = sender_arc.lock().unwrap(); *sender_mutex = Some(sender.clone()); let _ = sender.clone().send((handle, "123".into())); } _ => {} } } } let my_client = StdArc::new(MyClient { sender: ugly_smart_lib::smart_pub::StdArc::new(ugly_smart_lib::smart_pub::StdMutex::new(None)), }); let client_params = SUdpClientParams { remote_addr: "127.0.0.1:28686".into(), call_back: Box::new(my_client.clone()), extra_params: "127.0.0.1:8866".into(), local_addr: "127.0.0.1:5550".into(), }; let client = StdArc::new(CUdpClient::new(client_params)); ugly_smart_lib::net_pub::spawn_and_log_error(client.clone().start()); tokio::time::sleep(std::time::Duration::from_secs(10)).await; tracing::error!("will stop"); ugly_smart_lib::net_pub::spawn_and_log_error(client.clone().stop()); tokio::time::sleep(std::time::Duration::from_secs(1)).await; }