From b877bd1b0e35bcd11399a27190049f0f9d56f33b Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 17 Sep 2025 13:08:51 +0800 Subject: Finished test of `tcp_connection` 1. Merge example_handle.rs into test_connection.rs --- .../tcp_connection_test/src/example_handle.rs | 35 --------- .../tcp_connection/tcp_connection_test/src/lib.rs | 3 +- .../tcp_connection_test/src/test_connection.rs | 85 ++++++++++++++++++++++ .../src/test_tcp_target_build.rs | 7 +- 4 files changed, 91 insertions(+), 39 deletions(-) delete mode 100644 crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs create mode 100644 crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs (limited to 'crates/utils') diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs b/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs deleted file mode 100644 index 1f02133..0000000 --- a/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs +++ /dev/null @@ -1,35 +0,0 @@ -use tcp_connection::{ - handle::{ClientHandle, ServerHandle}, - instance::ConnectionInstance, -}; - -pub(crate) struct ExampleClientHandle; - -impl ClientHandle for ExampleClientHandle { - fn process( - mut instance: ConnectionInstance, - ) -> impl std::future::Future + Send + Sync { - async move { - let _ = instance.write_text("Hello, World!").await; - let Ok(result) = instance.read_text(512 as u32).await else { - return; - }; - println!("Received: `{}`", result); - } - } -} - -pub(crate) struct ExampleServerHandle; - -impl ServerHandle for ExampleServerHandle { - fn process( - mut instance: ConnectionInstance, - ) -> impl std::future::Future + Send + Sync { - async move { - let Ok(_) = instance.read_text(512 as u32).await else { - return; - }; - let _ = instance.write_text("Hello!").await; - } - } -} diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs b/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs index 697e847..cc585d1 100644 --- a/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs +++ b/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs @@ -1,4 +1,5 @@ #[cfg(test)] pub mod test_tcp_target_build; -pub(crate) mod example_handle; +#[cfg(test)] +pub mod test_connection; diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs new file mode 100644 index 0000000..d9fa123 --- /dev/null +++ b/crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs @@ -0,0 +1,85 @@ +use std::time::Duration; + +use tcp_connection::{ + handle::{ClientHandle, ServerHandle}, + instance::ConnectionInstance, + target::TcpServerTarget, + target_configure::ServerTargetConfig, +}; +use tokio::{join, time::sleep}; + +pub(crate) struct ExampleClientHandle; + +impl ClientHandle for ExampleClientHandle { + fn process( + mut instance: ConnectionInstance, + ) -> impl std::future::Future + Send + Sync { + async move { + // Write name + let Ok(_) = instance.write_text("Peter").await else { + panic!("Write text failed!"); + }; + // Read msg + let Ok(result) = instance.read_text(512 as u32).await else { + return; + }; + assert_eq!("Hello Peter!", result); + } + } +} + +pub(crate) struct ExampleServerHandle; + +impl ServerHandle for ExampleServerHandle { + fn process( + mut instance: ConnectionInstance, + ) -> impl std::future::Future + Send + Sync { + async move { + // Read name + let Ok(name) = instance.read_text(512 as u32).await else { + return; + }; + // Write msg + let Ok(_) = instance.write_text(format!("Hello {}!", name)).await else { + panic!("Write text failed!"); + }; + } + } +} + +#[tokio::test] +async fn test_connection_with_example_handle() { + let host = "localhost"; + + // Server setup + let Ok(server_target) = + TcpServerTarget::::from_domain(host).await + else { + panic!("Test target built failed from a domain named `{}`", host); + }; + + // Client setup + let Ok(client_target) = + TcpServerTarget::::from_domain(host).await + else { + panic!("Test target built failed from a domain named `{}`", host); + }; + + let future_server = async move { + // Only process once + let configured_server = server_target.server_cfg(ServerTargetConfig::default().once()); + + // Listen here + let _ = configured_server.listen().await; + }; + + let future_client = async move { + // Wait for server start + let _ = sleep(Duration::from_secs_f32(1.5)).await; + + // Connect here + let _ = client_target.connect().await; + }; + + let _ = async { join!(future_client, future_server) }.await; +} diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs index c108e42..0e1ed67 100644 --- a/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs +++ b/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs @@ -1,6 +1,7 @@ -use crate::example_handle::{ExampleClientHandle, ExampleServerHandle}; use tcp_connection::target::TcpServerTarget; +use crate::test_connection::{ExampleClientHandle, ExampleServerHandle}; + #[test] fn test_tcp_test_target_build() { let host = "127.0.0.1:8080"; @@ -8,7 +9,7 @@ fn test_tcp_test_target_build() { // Test build target by string let Ok(target) = TcpServerTarget::::from_str(host) else { - panic!("Test target built from a target addr `{}`", host); + panic!("Test target built failed from a target addr `{}`", host); }; assert_eq!(target.to_string(), "127.0.0.1:8080"); } @@ -21,7 +22,7 @@ async fn test_tcp_test_target_build_domain() { let Ok(target) = TcpServerTarget::::from_domain(host).await else { - panic!("Test target built from a domain named `{}`", host); + panic!("Test target built failed from a domain named `{}`", host); }; // Test into string -- cgit