From c62893cf88d40d0366221d3d37a144c1410f3750 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=86=A0=E6=B4=B2?= Date: Thu, 18 Jul 2024 18:30:01 +0800 Subject: [PATCH] I wrote some client and server test example to solve the issue:Write integration tests between server/client #7. I hope my version can be accepted --- tests/client.rs | 24 ++++++++++++------------ tests/server.rs | 15 +++++++-------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/tests/client.rs b/tests/client.rs index 154384d..3e25c5b 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -1,4 +1,4 @@ -use mini_redis::{clients::Client, server}; +use mini_redis::{client, server}; use std::net::SocketAddr; use tokio::net::TcpListener; use tokio::task::JoinHandle; @@ -8,7 +8,7 @@ use tokio::task::JoinHandle; #[tokio::test] async fn ping_pong_without_message() { let (addr, _) = start_server().await; - let mut client = Client::connect(addr).await.unwrap(); + let mut client = client::connect(addr).await.unwrap(); let pong = client.ping(None).await.unwrap(); assert_eq!(b"PONG", &pong[..]); @@ -19,9 +19,9 @@ async fn ping_pong_without_message() { #[tokio::test] async fn ping_pong_with_message() { let (addr, _) = start_server().await; - let mut client = Client::connect(addr).await.unwrap(); + let mut client = client::connect(addr).await.unwrap(); - let pong = client.ping(Some("你好世界".into())).await.unwrap(); + let pong = client.ping(Some("你好世界".to_string())).await.unwrap(); assert_eq!("你好世界".as_bytes(), &pong[..]); } @@ -32,7 +32,7 @@ async fn ping_pong_with_message() { async fn key_value_get_set() { let (addr, _) = start_server().await; - let mut client = Client::connect(addr).await.unwrap(); + let mut client = client::connect(addr).await.unwrap(); client.set("hello", "world".into()).await.unwrap(); let value = client.get("hello").await.unwrap().unwrap(); @@ -45,11 +45,11 @@ async fn key_value_get_set() { async fn receive_message_subscribed_channel() { let (addr, _) = start_server().await; - let client = Client::connect(addr).await.unwrap(); + let client = client::connect(addr).await.unwrap(); let mut subscriber = client.subscribe(vec!["hello".into()]).await.unwrap(); tokio::spawn(async move { - let mut client = Client::connect(addr).await.unwrap(); + let mut client = client::connect(addr).await.unwrap(); client.publish("hello", "world".into()).await.unwrap() }); @@ -63,14 +63,14 @@ async fn receive_message_subscribed_channel() { async fn receive_message_multiple_subscribed_channels() { let (addr, _) = start_server().await; - let client = Client::connect(addr).await.unwrap(); + let client = client::connect(addr).await.unwrap(); let mut subscriber = client .subscribe(vec!["hello".into(), "world".into()]) .await .unwrap(); tokio::spawn(async move { - let mut client = Client::connect(addr).await.unwrap(); + let mut client = client::connect(addr).await.unwrap(); client.publish("hello", "world".into()).await.unwrap() }); @@ -79,7 +79,7 @@ async fn receive_message_multiple_subscribed_channels() { assert_eq!(b"world", &message1.content[..]); tokio::spawn(async move { - let mut client = Client::connect(addr).await.unwrap(); + let mut client = client::connect(addr).await.unwrap(); client.publish("world", "howdy?".into()).await.unwrap() }); @@ -88,13 +88,13 @@ async fn receive_message_multiple_subscribed_channels() { assert_eq!(b"howdy?", &message2.content[..]) } -/// test that a client accurately removes its own subscribed channel list +/// test that a client accurately removes its own subscribed chanel list /// when unsubscribing to all subscribed channels by submitting an empty vec #[tokio::test] async fn unsubscribes_from_channels() { let (addr, _) = start_server().await; - let client = Client::connect(addr).await.unwrap(); + let client = client::connect(addr).await.unwrap(); let mut subscriber = client .subscribe(vec!["hello".into(), "world".into()]) .await diff --git a/tests/server.rs b/tests/server.rs index 488cb58..4949f39 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -1,10 +1,11 @@ -use mini_redis::server; - use std::net::SocketAddr; + use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::{TcpListener, TcpStream}; use tokio::time::{self, Duration}; +use mini_redis::server; + /// A basic "hello world" style test. A server instance is started in a /// background task. A client TCP connection is then established and raw redis /// commands are sent to the server. The response is evaluated at the byte @@ -66,8 +67,6 @@ async fn key_value_get_set() { /// to advance to the application. #[tokio::test] async fn key_value_timeout() { - tokio::time::pause(); - let addr = start_server().await; // Establish a connection to the server @@ -103,7 +102,7 @@ async fn key_value_timeout() { assert_eq!(b"$5\r\nworld\r\n", &response); // Wait for the key to expire - time::advance(Duration::from_secs(1)).await; + time::sleep(Duration::from_secs(1)).await; // Get a key, data is missing stream @@ -350,7 +349,7 @@ async fn send_error_unknown_command() { stream.read_exact(&mut response).await.unwrap(); - assert_eq!(b"-ERR unknown command \'foo\'\r\n", &response); + assert_eq!(b"-err unknown command \'foo\'\r\n", &response); } // In this case we test that server Responds with an Error message if a client @@ -384,7 +383,7 @@ async fn send_error_get_set_after_subscribe() { let mut response = [0; 28]; stream.read_exact(&mut response).await.unwrap(); - assert_eq!(b"-ERR unknown command \'set\'\r\n", &response); + assert_eq!(b"-err unknown command \'set\'\r\n", &response); stream .write_all(b"*2\r\n$3\r\nGET\r\n$5\r\nhello\r\n") @@ -394,7 +393,7 @@ async fn send_error_get_set_after_subscribe() { let mut response = [0; 28]; stream.read_exact(&mut response).await.unwrap(); - assert_eq!(b"-ERR unknown command \'get\'\r\n", &response); + assert_eq!(b"-err unknown command \'get\'\r\n", &response); } async fn start_server() -> SocketAddr {