summaryrefslogtreecommitdiff
path: root/crates/utils
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-09-17 13:08:51 +0800
committer魏曹先生 <1992414357@qq.com>2025-09-17 13:08:51 +0800
commitb877bd1b0e35bcd11399a27190049f0f9d56f33b (patch)
tree29030eb137204983aad0404446163ba75f93ce93 /crates/utils
parent15832a893383a850c9d032ab1620661b0121f247 (diff)
Finished test of `tcp_connection` 1. Merge example_handle.rs into
test_connection.rs
Diffstat (limited to 'crates/utils')
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs35
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/lib.rs3
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs85
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs7
4 files changed, 91 insertions, 39 deletions
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<ExampleServerHandle> for ExampleClientHandle {
- fn process(
- mut instance: ConnectionInstance,
- ) -> impl std::future::Future<Output = ()> + 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<ExampleClientHandle> for ExampleServerHandle {
- fn process(
- mut instance: ConnectionInstance,
- ) -> impl std::future::Future<Output = ()> + 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<ExampleServerHandle> for ExampleClientHandle {
+ fn process(
+ mut instance: ConnectionInstance,
+ ) -> impl std::future::Future<Output = ()> + 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<ExampleClientHandle> for ExampleServerHandle {
+ fn process(
+ mut instance: ConnectionInstance,
+ ) -> impl std::future::Future<Output = ()> + 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::<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;
+}
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::<ExampleClientHandle, ExampleServerHandle>::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::<ExampleClientHandle, ExampleServerHandle>::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