Merge pull request #180 from zed-industries/peer-test-io-error-flaky

Fix flaky `zrpc::tests::peer::test_io_error` test
This commit is contained in:
Antonio Scandurra 2021-09-23 17:56:40 +02:00 committed by GitHub
commit 969bdb2390
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -87,7 +87,7 @@ pub struct Peer {
struct ConnectionState { struct ConnectionState {
outgoing_tx: mpsc::Sender<proto::Envelope>, outgoing_tx: mpsc::Sender<proto::Envelope>,
next_message_id: Arc<AtomicU32>, next_message_id: Arc<AtomicU32>,
response_channels: Arc<Mutex<HashMap<u32, mpsc::Sender<proto::Envelope>>>>, response_channels: Arc<Mutex<Option<HashMap<u32, mpsc::Sender<proto::Envelope>>>>>,
} }
impl Peer { impl Peer {
@ -115,7 +115,7 @@ impl Peer {
let connection_state = ConnectionState { let connection_state = ConnectionState {
outgoing_tx, outgoing_tx,
next_message_id: Default::default(), next_message_id: Default::default(),
response_channels: Default::default(), response_channels: Arc::new(Mutex::new(Some(Default::default()))),
}; };
let mut writer = MessageStream::new(connection.tx); let mut writer = MessageStream::new(connection.tx);
let mut reader = MessageStream::new(connection.rx); let mut reader = MessageStream::new(connection.rx);
@ -123,7 +123,7 @@ impl Peer {
let this = self.clone(); let this = self.clone();
let response_channels = connection_state.response_channels.clone(); let response_channels = connection_state.response_channels.clone();
let handle_io = async move { let handle_io = async move {
loop { let result = 'outer: loop {
let read_message = reader.read_message().fuse(); let read_message = reader.read_message().fuse();
futures::pin_mut!(read_message); futures::pin_mut!(read_message);
loop { loop {
@ -131,7 +131,7 @@ impl Peer {
incoming = read_message => match incoming { incoming = read_message => match incoming {
Ok(incoming) => { Ok(incoming) => {
if let Some(responding_to) = incoming.responding_to { if let Some(responding_to) = incoming.responding_to {
let channel = response_channels.lock().await.remove(&responding_to); let channel = response_channels.lock().await.as_mut().unwrap().remove(&responding_to);
if let Some(mut tx) = channel { if let Some(mut tx) = channel {
tx.send(incoming).await.ok(); tx.send(incoming).await.ok();
} else { } else {
@ -140,9 +140,7 @@ impl Peer {
} else { } else {
if let Some(envelope) = proto::build_typed_envelope(connection_id, incoming) { if let Some(envelope) = proto::build_typed_envelope(connection_id, incoming) {
if incoming_tx.send(envelope).await.is_err() { if incoming_tx.send(envelope).await.is_err() {
response_channels.lock().await.clear(); break 'outer Ok(())
this.connections.write().await.remove(&connection_id);
return Ok(())
} }
} else { } else {
log::error!("unable to construct a typed envelope"); log::error!("unable to construct a typed envelope");
@ -152,28 +150,24 @@ impl Peer {
break; break;
} }
Err(error) => { Err(error) => {
response_channels.lock().await.clear(); break 'outer Err(error).context("received invalid RPC message")
this.connections.write().await.remove(&connection_id);
Err(error).context("received invalid RPC message")?;
} }
}, },
outgoing = outgoing_rx.recv().fuse() => match outgoing { outgoing = outgoing_rx.recv().fuse() => match outgoing {
Some(outgoing) => { Some(outgoing) => {
if let Err(result) = writer.write_message(&outgoing).await { if let Err(result) = writer.write_message(&outgoing).await {
response_channels.lock().await.clear(); break 'outer Err(result).context("failed to write RPC message")
this.connections.write().await.remove(&connection_id);
Err(result).context("failed to write RPC message")?;
} }
} }
None => { None => break 'outer Ok(()),
response_channels.lock().await.clear();
this.connections.write().await.remove(&connection_id);
return Ok(())
}
} }
} }
} }
} };
response_channels.lock().await.take();
this.connections.write().await.remove(&connection_id);
result
}; };
self.connections self.connections
@ -226,6 +220,8 @@ impl Peer {
.response_channels .response_channels
.lock() .lock()
.await .await
.as_mut()
.ok_or_else(|| anyhow!("connection was closed"))?
.insert(message_id, tx); .insert(message_id, tx);
connection connection
.outgoing_tx .outgoing_tx
@ -520,8 +516,7 @@ mod tests {
#[test] #[test]
fn test_io_error() { fn test_io_error() {
smol::block_on(async move { smol::block_on(async move {
let (client_conn, server_conn, _) = Connection::in_memory(); let (client_conn, mut server_conn, _) = Connection::in_memory();
drop(server_conn);
let client = Peer::new(); let client = Peer::new();
let (connection_id, io_handler, mut incoming) = let (connection_id, io_handler, mut incoming) =
@ -529,11 +524,14 @@ mod tests {
smol::spawn(io_handler).detach(); smol::spawn(io_handler).detach();
smol::spawn(async move { incoming.next().await }).detach(); smol::spawn(async move { incoming.next().await }).detach();
let err = client let response = smol::spawn(client.request(connection_id, proto::Ping {}));
.request(connection_id, proto::Ping {}) let _request = server_conn.rx.next().await.unwrap().unwrap();
.await
.unwrap_err(); drop(server_conn);
assert_eq!(err.to_string(), "connection was closed"); assert_eq!(
response.await.unwrap_err().to_string(),
"connection was closed"
);
}); });
} }
} }