diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-01-12 04:28:28 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-01-12 04:51:34 +0800 |
| commit | c5fb22694e95f12c24b8d8af76999be7aea3fcec (patch) | |
| tree | 399d8a24ce491fb635f3d09f2123290fe784059e /utils/tcp_connection/tcp_connection_test/src/test_connection.rs | |
| parent | 444754489aca0454eb54e15a49fb8a6db0b68a07 (diff) | |
Reorganize crate structure and move documentation files
Diffstat (limited to 'utils/tcp_connection/tcp_connection_test/src/test_connection.rs')
| -rw-r--r-- | utils/tcp_connection/tcp_connection_test/src/test_connection.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/utils/tcp_connection/tcp_connection_test/src/test_connection.rs b/utils/tcp_connection/tcp_connection_test/src/test_connection.rs new file mode 100644 index 0000000..8c3ab01 --- /dev/null +++ b/utils/tcp_connection/tcp_connection_test/src/test_connection.rs @@ -0,0 +1,78 @@ +use std::time::Duration; + +use tcp_connection::instance::ConnectionInstance; +use tokio::{join, time::sleep}; + +use crate::test_utils::{ + handle::{ClientHandle, ServerHandle}, + target::TcpServerTarget, + target_configure::ServerTargetConfig, +}; + +pub(crate) struct ExampleClientHandle; + +impl ClientHandle<ExampleServerHandle> for ExampleClientHandle { + async fn process(mut instance: ConnectionInstance) { + // Write name + let Ok(_) = instance.write_text("Peter").await else { + panic!("Write text failed!"); + }; + // Read msg + let Ok(result) = instance.read_text().await else { + return; + }; + assert_eq!("Hello Peter!", result); + } +} + +pub(crate) struct ExampleServerHandle; + +impl ServerHandle<ExampleClientHandle> for ExampleServerHandle { + async fn process(mut instance: ConnectionInstance) { + // Read name + let Ok(name) = instance.read_text().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:5012"; + + // Server setup + let Ok(server_target) = + TcpServerTarget::<ExampleClientHandle, ExampleServerHandle>::from_domain(host).await + else { + panic!("Test target built failed from a domain named `{}`", host); + }; + + // Client setup + let Ok(client_target) = + TcpServerTarget::<ExampleClientHandle, ExampleServerHandle>::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; +} |
