Skip to content

Commit

Permalink
I wrote some client and server test example to solve the issue:Write …
Browse files Browse the repository at this point in the history
…integration tests between server/client #7. I hope my version can be accepted
  • Loading branch information
kzchenguanzhou committed Jul 18, 2024
1 parent c2b18b9 commit c62893c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
24 changes: 12 additions & 12 deletions tests/client.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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[..]);
Expand All @@ -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[..]);
}

Expand All @@ -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();
Expand All @@ -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()
});

Expand All @@ -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()
});

Expand All @@ -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()
});

Expand All @@ -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
Expand Down
15 changes: 7 additions & 8 deletions tests/server.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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 {
Expand Down

0 comments on commit c62893c

Please sign in to comment.