2023-03-01 00:26:27 +00:00
|
|
|
use ethers_core::types::Filter;
|
2022-12-06 06:05:33 +00:00
|
|
|
use ethers_providers::{Middleware, Provider, StreamExt, Ws};
|
2022-12-01 19:00:10 +00:00
|
|
|
use futures_util::SinkExt;
|
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::net::{TcpListener, TcpStream};
|
|
|
|
use tokio_tungstenite::{
|
2023-03-01 00:26:27 +00:00
|
|
|
accept_async, connect_async,
|
2022-12-01 19:00:10 +00:00
|
|
|
tungstenite::{
|
|
|
|
self,
|
|
|
|
protocol::{frame::coding::CloseCode, CloseFrame},
|
|
|
|
Error,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use tungstenite::protocol::Message;
|
|
|
|
|
|
|
|
const WS_ENDPOINT: &str = "127.0.0.1:9002";
|
|
|
|
|
2023-01-03 14:15:51 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn graceful_disconnect_on_ws_errors() {
|
|
|
|
// Spawn a fake Ws server that will drop our connection after a while
|
|
|
|
spawn_ws_server().await;
|
2022-12-01 19:00:10 +00:00
|
|
|
|
2023-01-03 14:15:51 +00:00
|
|
|
// Connect to the fake server
|
2023-02-20 23:55:36 +00:00
|
|
|
let (ws, _) = connect_async(format!("ws://{WS_ENDPOINT}")).await.unwrap();
|
2023-01-03 14:15:51 +00:00
|
|
|
let provider = Provider::new(Ws::new(ws));
|
|
|
|
let filter = Filter::new().event("Transfer(address,address,uint256)");
|
|
|
|
let mut stream = provider.subscribe_logs(&filter).await.unwrap();
|
2022-12-01 19:00:10 +00:00
|
|
|
|
2023-01-03 14:15:51 +00:00
|
|
|
assert!(stream.next().await.is_none());
|
2022-12-01 19:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn spawn_ws_server() {
|
|
|
|
let listener = TcpListener::bind(&WS_ENDPOINT).await.expect("Can't listen");
|
|
|
|
tokio::spawn(async move {
|
|
|
|
while let Ok((stream, _)) = listener.accept().await {
|
|
|
|
tokio::spawn(handle_conn(stream));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_conn(stream: TcpStream) -> Result<(), Error> {
|
|
|
|
let mut ws_stream = accept_async(stream).await?;
|
|
|
|
|
2023-01-03 14:15:51 +00:00
|
|
|
while ws_stream.next().await.is_some() {
|
2022-12-01 19:00:10 +00:00
|
|
|
let res: String =
|
|
|
|
"{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0xcd0c3e8af590364c09d0fa6a1210faf5\"}"
|
|
|
|
.into();
|
|
|
|
|
|
|
|
// Answer with a valid RPC response to keep the connection alive
|
|
|
|
ws_stream.send(Message::Text(res)).await?;
|
|
|
|
|
|
|
|
// Wait for a while
|
|
|
|
let timeout = Duration::from_secs(2);
|
|
|
|
tokio::time::sleep(timeout).await;
|
|
|
|
|
|
|
|
// Drop the connection
|
|
|
|
ws_stream
|
|
|
|
.send(Message::Close(Some(CloseFrame {
|
|
|
|
code: CloseCode::Error,
|
|
|
|
reason: "Upstream went away".into(),
|
|
|
|
})))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|