Skip to content

Commit

Permalink
Simulate multiple calls to same server
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Jun 2, 2024
1 parent ac639c8 commit d28e74a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions ahnlich/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ itertools = "0.10.0"
clap = { version = "4.5.4", features = ["derive"] }
env_logger = "0.11.3"
log = "0.4"
futures = "0.3.30"
1 change: 1 addition & 0 deletions ahnlich/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ loom = "0.7.2"
reqwest = "0.12.4"
serde_json = "1.0.116"
tokio = { version = "1.37.0", features = ["io-util", "sync"] }
futures.workspace = true
36 changes: 28 additions & 8 deletions ahnlich/server/tests/server_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use futures::future::join_all;
use server::cli::ServerConfig;
use std::net::SocketAddr;
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpStream;
use tokio::time::{timeout, Duration};
Expand All @@ -18,14 +20,36 @@ async fn test_run_server_echos() {

// Allow some time for the server to start
tokio::time::sleep(Duration::from_millis(100)).await;
let tasks = vec![
tokio::spawn(async move {
let message = ServerQuery::from_queries(&[Query::InfoServer, Query::Ping]);
let mut expected = ServerResult::with_capacity(2);
expected.push(Ok(ServerResponse::Unit));
expected.push(Ok(ServerResponse::Pong));
query_server_assert_result(address, message, expected).await
}),
tokio::spawn(async move {
let message = ServerQuery::from_queries(&[Query::Ping, Query::InfoServer]);
let mut expected = ServerResult::with_capacity(2);
expected.push(Ok(ServerResponse::Pong));
expected.push(Ok(ServerResponse::Unit));
query_server_assert_result(address, message, expected).await
}),
];
join_all(tasks).await;
}

async fn query_server_assert_result(
server_addr: SocketAddr,
query: ServerQuery,
expected_result: ServerResult,
) {
// Connect to the server
let stream = TcpStream::connect(address).await.unwrap();
let stream = TcpStream::connect(server_addr).await.unwrap();
let mut reader = BufReader::new(stream);

// Message to send
let message = ServerQuery::from_queries(&[Query::InfoServer, Query::Ping]);
let serialized_message = message.serialize().unwrap();
let serialized_message = query.serialize().unwrap();

// Send the message
reader.write_all(&serialized_message).await.unwrap();
Expand All @@ -47,11 +71,7 @@ async fn test_run_server_echos() {
.unwrap()
.unwrap();

let mut expected = ServerResult::with_capacity(2);
expected.push(Ok(ServerResponse::Unit));
expected.push(Ok(ServerResponse::Pong));

let response = ServerResult::deserialize(false, &response).unwrap();

assert_eq!(response, expected);
assert_eq!(response, expected_result);
}

0 comments on commit d28e74a

Please sign in to comment.