Client can now perform requests asynchronously

This commit is contained in:
2024-06-21 11:51:33 +02:00
parent b4f1e1b092
commit b9128a465c
2 changed files with 81 additions and 14 deletions

View File

@@ -30,7 +30,7 @@ enum TestProtocol {
async fn main() {
let (qtx, qrx) = mpsc::channel(16);
let (atx, arx) = mpsc::channel(16);
let mut client = TestProtocolClient::new(qtx, arx);
let client = TestProtocolClient::new(qtx, arx);
let server = tokio::spawn(server_loop(qrx, atx));
let result = client.addition(2, 5).await.unwrap();
assert_eq!(result, 7);
@@ -86,3 +86,23 @@ async fn server_loop(
}
}
}
#[tokio::test]
async fn heavy_async() {
let (qtx, qrx) = mpsc::channel(16);
let (atx, arx) = mpsc::channel(16);
let client = TestProtocolClient::new(qtx, arx);
let server = tokio::spawn(server_loop(qrx, atx));
let mut tasks = Vec::new();
for i in 0..100 {
let client = client.clone();
tasks.push(tokio::spawn(async move {
let result = client.addition(i, i).await.unwrap();
assert_eq!(result, i + i);
}));
}
for task in tasks {
task.await.unwrap();
}
server.abort();
}