Skip to content

Commit

Permalink
[JSON-RPC] Add timeouts to websocket test
Browse files Browse the repository at this point in the history
  • Loading branch information
CapCap authored and bors-libra committed Aug 13, 2021
1 parent 272f9e1 commit 077295e
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions json-rpc/src/stream_rpc/tests/websockets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{
stream_rpc::tests::util::{
close_ws, connect_to_ws, get_latest_client, next_message, num_clients, num_tasks,
close_ws, connect_to_ws, get_latest_client, next_message, num_clients, num_tasks, timeout,
verify_ok, ws_test_setup,
},
tests::utils::create_db_and_runtime,
Expand Down Expand Up @@ -182,6 +182,8 @@ fn test_websocket_requests() {
json!({"jsonrpc": "2.0", "id": "client-generated-id-1", "result": {"status": "OK", "transaction_version": mock_db.version}}),
)];

let _guard = runtime.enter();

for (name, request, expected) in calls {
let http_request = http::Request::builder()
.method(http::method::Method::GET)
Expand All @@ -191,22 +193,30 @@ fn test_websocket_requests() {
.body(())
.unwrap();

let ws_stream = runtime.handle().block_on(async move {
let (ws_stream, _) = connect_async(http_request)
.await
.expect("Could not create websocket connection");
ws_stream
});
let ws_stream = runtime.handle().block_on(timeout(
500,
async move {
let (ws_stream, _) = connect_async(http_request)
.await
.expect("Could not create websocket connection");
ws_stream
},
"create ws connection",
));

let (mut tx, mut rx) = ws_stream.split();
runtime
.handle()
.block_on(tx.send(Message::text(request.to_string())))
.block_on(timeout(
500,
tx.send(Message::text(request.to_string())),
"send request",
))
.unwrap_or_else(|e| panic!("{}: Could not send websocket request. {:?}", &name, e));

let response = runtime
.handle()
.block_on(rx.next())
.block_on(timeout(500, rx.next(), "get next message"))
.unwrap_or_else(|| panic!("{}: Expected 'Some' response", &name))
.unwrap_or_else(|_| panic!("{}: Expected no error response", &name))
.to_string();
Expand Down

0 comments on commit 077295e

Please sign in to comment.