Skip to content

Commit f4f78cd

Browse files
nanoqshsdroege
authored andcommitted
Add concurrent_send test
1 parent e34c963 commit f4f78cd

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/communication.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,53 @@ async fn split_communication() {
116116
let messages = msg_rx.await.expect("Failed to receive messages");
117117
assert_eq!(messages.len(), 10);
118118
}
119+
120+
#[async_std::test]
121+
async fn concurrent_send() {
122+
let _ = env_logger::try_init();
123+
124+
let (con_tx, con_rx) = futures::channel::oneshot::channel();
125+
let (msg_tx, msg_rx) = futures::channel::oneshot::channel();
126+
127+
let f = async move {
128+
let listener = TcpListener::bind("127.0.0.1:12347").await.unwrap();
129+
info!("Server ready");
130+
con_tx.send(()).unwrap();
131+
info!("Waiting on next connection");
132+
let (connection, _) = listener.accept().await.expect("No connections to accept");
133+
let stream = accept_async(connection).await;
134+
let stream = stream.expect("Failed to handshake with connection");
135+
run_connection(stream, msg_tx).await;
136+
};
137+
138+
task::spawn(f);
139+
140+
info!("Waiting for server to be ready");
141+
142+
con_rx.await.expect("Server not ready");
143+
let tcp = TcpStream::connect("127.0.0.1:12347")
144+
.await
145+
.expect("Failed to connect");
146+
let url = url::Url::parse("ws://localhost:12347/").unwrap();
147+
let (stream, _) = client_async(url, tcp)
148+
.await
149+
.expect("Client failed to connect");
150+
151+
let (tx, _rx) = stream.split();
152+
153+
// the `WebSocketSender::send` takes a shared `&self`, so you can call it concurrently.
154+
// this test case checks that it works
155+
let results = futures::future::join_all((1..10).map(|i| {
156+
info!("Sending message");
157+
tx.send(Message::text(format!("{}", i)))
158+
}))
159+
.await;
160+
161+
assert!(results.iter().all(Result::is_ok));
162+
163+
tx.close(None).await.expect("Failed to close");
164+
165+
info!("Waiting for response messages");
166+
let messages = msg_rx.await.expect("Failed to receive messages");
167+
assert_eq!(messages.len(), 10);
168+
}

0 commit comments

Comments
 (0)