peer.rs

  1use super::{
  2    proto::{self, AnyTypedEnvelope, EnvelopedMessage, MessageStream, RequestMessage},
  3    Connection,
  4};
  5use anyhow::{anyhow, Context, Result};
  6use collections::HashMap;
  7use futures::{
  8    channel::{mpsc, oneshot},
  9    stream::BoxStream,
 10    FutureExt, SinkExt, StreamExt,
 11};
 12use parking_lot::{Mutex, RwLock};
 13use serde::{ser::SerializeStruct, Serialize};
 14use std::sync::atomic::Ordering::SeqCst;
 15use std::{
 16    fmt,
 17    future::Future,
 18    marker::PhantomData,
 19    sync::{
 20        atomic::{self, AtomicU32},
 21        Arc,
 22    },
 23    time::Duration,
 24};
 25use tracing::instrument;
 26
 27#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize)]
 28pub struct ConnectionId(pub u32);
 29
 30impl fmt::Display for ConnectionId {
 31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 32        self.0.fmt(f)
 33    }
 34}
 35
 36#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
 37pub struct PeerId(pub u32);
 38
 39impl fmt::Display for PeerId {
 40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 41        self.0.fmt(f)
 42    }
 43}
 44
 45pub struct Receipt<T> {
 46    pub sender_id: ConnectionId,
 47    pub message_id: u32,
 48    payload_type: PhantomData<T>,
 49}
 50
 51impl<T> Clone for Receipt<T> {
 52    fn clone(&self) -> Self {
 53        Self {
 54            sender_id: self.sender_id,
 55            message_id: self.message_id,
 56            payload_type: PhantomData,
 57        }
 58    }
 59}
 60
 61impl<T> Copy for Receipt<T> {}
 62
 63pub struct TypedEnvelope<T> {
 64    pub sender_id: ConnectionId,
 65    pub original_sender_id: Option<PeerId>,
 66    pub message_id: u32,
 67    pub payload: T,
 68}
 69
 70impl<T> TypedEnvelope<T> {
 71    pub fn original_sender_id(&self) -> Result<PeerId> {
 72        self.original_sender_id
 73            .ok_or_else(|| anyhow!("missing original_sender_id"))
 74    }
 75}
 76
 77impl<T: RequestMessage> TypedEnvelope<T> {
 78    pub fn receipt(&self) -> Receipt<T> {
 79        Receipt {
 80            sender_id: self.sender_id,
 81            message_id: self.message_id,
 82            payload_type: PhantomData,
 83        }
 84    }
 85}
 86
 87pub struct Peer {
 88    pub connections: RwLock<HashMap<ConnectionId, ConnectionState>>,
 89    next_connection_id: AtomicU32,
 90}
 91
 92#[derive(Clone, Serialize)]
 93pub struct ConnectionState {
 94    #[serde(skip)]
 95    outgoing_tx: mpsc::UnboundedSender<proto::Message>,
 96    next_message_id: Arc<AtomicU32>,
 97    #[allow(clippy::type_complexity)]
 98    #[serde(skip)]
 99    response_channels:
100        Arc<Mutex<Option<HashMap<u32, oneshot::Sender<(proto::Envelope, oneshot::Sender<()>)>>>>>,
101}
102
103const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(1);
104const WRITE_TIMEOUT: Duration = Duration::from_secs(2);
105pub const RECEIVE_TIMEOUT: Duration = Duration::from_secs(5);
106
107impl Peer {
108    pub fn new() -> Arc<Self> {
109        Arc::new(Self {
110            connections: Default::default(),
111            next_connection_id: Default::default(),
112        })
113    }
114
115    #[instrument(skip_all)]
116    pub fn add_connection<F, Fut, Out>(
117        self: &Arc<Self>,
118        connection: Connection,
119        create_timer: F,
120    ) -> (
121        ConnectionId,
122        impl Future<Output = anyhow::Result<()>> + Send,
123        BoxStream<'static, Box<dyn AnyTypedEnvelope>>,
124    )
125    where
126        F: Send + Fn(Duration) -> Fut,
127        Fut: Send + Future<Output = Out>,
128        Out: Send,
129    {
130        // For outgoing messages, use an unbounded channel so that application code
131        // can always send messages without yielding. For incoming messages, use a
132        // bounded channel so that other peers will receive backpressure if they send
133        // messages faster than this peer can process them.
134        #[cfg(any(test, feature = "test-support"))]
135        const INCOMING_BUFFER_SIZE: usize = 1;
136        #[cfg(not(any(test, feature = "test-support")))]
137        const INCOMING_BUFFER_SIZE: usize = 64;
138        let (mut incoming_tx, incoming_rx) = mpsc::channel(INCOMING_BUFFER_SIZE);
139        let (outgoing_tx, mut outgoing_rx) = mpsc::unbounded();
140
141        let connection_id = ConnectionId(self.next_connection_id.fetch_add(1, SeqCst));
142        let connection_state = ConnectionState {
143            outgoing_tx,
144            next_message_id: Default::default(),
145            response_channels: Arc::new(Mutex::new(Some(Default::default()))),
146        };
147        let mut writer = MessageStream::new(connection.tx);
148        let mut reader = MessageStream::new(connection.rx);
149
150        let this = self.clone();
151        let response_channels = connection_state.response_channels.clone();
152        let handle_io = async move {
153            tracing::debug!(%connection_id, "handle io future: start");
154
155            let _end_connection = util::defer(|| {
156                response_channels.lock().take();
157                this.connections.write().remove(&connection_id);
158                tracing::debug!(%connection_id, "handle io future: end");
159            });
160
161            // Send messages on this frequency so the connection isn't closed.
162            let keepalive_timer = create_timer(KEEPALIVE_INTERVAL).fuse();
163            futures::pin_mut!(keepalive_timer);
164
165            // Disconnect if we don't receive messages at least this frequently.
166            let receive_timeout = create_timer(RECEIVE_TIMEOUT).fuse();
167            futures::pin_mut!(receive_timeout);
168
169            loop {
170                tracing::debug!(%connection_id, "outer loop iteration start");
171                let read_message = reader.read().fuse();
172                futures::pin_mut!(read_message);
173
174                loop {
175                    tracing::debug!(%connection_id, "inner loop iteration start");
176                    futures::select_biased! {
177                        outgoing = outgoing_rx.next().fuse() => match outgoing {
178                            Some(outgoing) => {
179                                tracing::debug!(%connection_id, "outgoing rpc message: writing");
180                                futures::select_biased! {
181                                    result = writer.write(outgoing).fuse() => {
182                                        tracing::debug!(%connection_id, "outgoing rpc message: done writing");
183                                        result.context("failed to write RPC message")?;
184                                        tracing::debug!(%connection_id, "keepalive interval: resetting after sending message");
185                                        keepalive_timer.set(create_timer(KEEPALIVE_INTERVAL).fuse());
186                                    }
187                                    _ = create_timer(WRITE_TIMEOUT).fuse() => {
188                                        tracing::debug!(%connection_id, "outgoing rpc message: writing timed out");
189                                        Err(anyhow!("timed out writing message"))?;
190                                    }
191                                }
192                            }
193                            None => {
194                                tracing::debug!(%connection_id, "outgoing rpc message: channel closed");
195                                return Ok(())
196                            },
197                        },
198                        _ = keepalive_timer => {
199                            tracing::debug!(%connection_id, "keepalive interval: pinging");
200                            futures::select_biased! {
201                                result = writer.write(proto::Message::Ping).fuse() => {
202                                    tracing::debug!(%connection_id, "keepalive interval: done pinging");
203                                    result.context("failed to send keepalive")?;
204                                    tracing::debug!(%connection_id, "keepalive interval: resetting after pinging");
205                                    keepalive_timer.set(create_timer(KEEPALIVE_INTERVAL).fuse());
206                                }
207                                _ = create_timer(WRITE_TIMEOUT).fuse() => {
208                                    tracing::debug!(%connection_id, "keepalive interval: pinging timed out");
209                                    Err(anyhow!("timed out sending keepalive"))?;
210                                }
211                            }
212                        }
213                        incoming = read_message => {
214                            let incoming = incoming.context("error reading rpc message from socket")?;
215                            tracing::debug!(%connection_id, "incoming rpc message: received");
216                            tracing::debug!(%connection_id, "receive timeout: resetting");
217                            receive_timeout.set(create_timer(RECEIVE_TIMEOUT).fuse());
218                            if let proto::Message::Envelope(incoming) = incoming {
219                                tracing::debug!(%connection_id, "incoming rpc message: processing");
220                                futures::select_biased! {
221                                    result = incoming_tx.send(incoming).fuse() => match result {
222                                        Ok(_) => {
223                                            tracing::debug!(%connection_id, "incoming rpc message: processed");
224                                        }
225                                        Err(_) => {
226                                            tracing::debug!(%connection_id, "incoming rpc message: channel closed");
227                                            return Ok(())
228                                        }
229                                    },
230                                    _ = create_timer(WRITE_TIMEOUT).fuse() => {
231                                        tracing::debug!(%connection_id, "incoming rpc message: processing timed out");
232                                        Err(anyhow!("timed out processing incoming message"))?
233                                    }
234                                }
235                            }
236                            break;
237                        },
238                        _ = receive_timeout => {
239                            tracing::debug!(%connection_id, "receive timeout: delay between messages too long");
240                            Err(anyhow!("delay between messages too long"))?
241                        }
242                    }
243                }
244            }
245        };
246
247        let response_channels = connection_state.response_channels.clone();
248        self.connections
249            .write()
250            .insert(connection_id, connection_state);
251
252        let incoming_rx = incoming_rx.filter_map(move |incoming| {
253            let response_channels = response_channels.clone();
254            async move {
255                let message_id = incoming.id;
256                tracing::debug!(?incoming, "incoming message future: start");
257                let _end = util::defer(move || {
258                    tracing::debug!(
259                        %connection_id,
260                        message_id,
261                        "incoming message future: end"
262                    );
263                });
264
265                if let Some(responding_to) = incoming.responding_to {
266                    tracing::debug!(
267                        %connection_id,
268                        message_id,
269                        responding_to,
270                        "incoming response: received"
271                    );
272                    let channel = response_channels.lock().as_mut()?.remove(&responding_to);
273                    if let Some(tx) = channel {
274                        let requester_resumed = oneshot::channel();
275                        if let Err(error) = tx.send((incoming, requester_resumed.0)) {
276                            tracing::debug!(
277                                %connection_id,
278                                message_id,
279                                responding_to = responding_to,
280                                ?error,
281                                "incoming response: request future dropped",
282                            );
283                        }
284
285                        tracing::debug!(
286                            %connection_id,
287                            message_id,
288                            responding_to,
289                            "incoming response: waiting to resume requester"
290                        );
291                        let _ = requester_resumed.1.await;
292                        tracing::debug!(
293                            %connection_id,
294                            message_id,
295                            responding_to,
296                            "incoming response: requester resumed"
297                        );
298                    } else {
299                        tracing::warn!(
300                            %connection_id,
301                            message_id,
302                            responding_to,
303                            "incoming response: unknown request"
304                        );
305                    }
306
307                    None
308                } else {
309                    tracing::debug!(
310                        %connection_id,
311                        message_id,
312                        "incoming message: received"
313                    );
314                    proto::build_typed_envelope(connection_id, incoming).or_else(|| {
315                        tracing::error!(
316                            %connection_id,
317                            message_id,
318                            "unable to construct a typed envelope"
319                        );
320                        None
321                    })
322                }
323            }
324        });
325        (connection_id, handle_io, incoming_rx.boxed())
326    }
327
328    #[cfg(any(test, feature = "test-support"))]
329    pub fn add_test_connection(
330        self: &Arc<Self>,
331        connection: Connection,
332        executor: Arc<gpui::executor::Background>,
333    ) -> (
334        ConnectionId,
335        impl Future<Output = anyhow::Result<()>> + Send,
336        BoxStream<'static, Box<dyn AnyTypedEnvelope>>,
337    ) {
338        let executor = executor.clone();
339        self.add_connection(connection, move |duration| executor.timer(duration))
340    }
341
342    pub fn disconnect(&self, connection_id: ConnectionId) {
343        self.connections.write().remove(&connection_id);
344    }
345
346    pub fn reset(&self) {
347        self.connections.write().clear();
348    }
349
350    pub fn request<T: RequestMessage>(
351        &self,
352        receiver_id: ConnectionId,
353        request: T,
354    ) -> impl Future<Output = Result<T::Response>> {
355        self.request_internal(None, receiver_id, request)
356    }
357
358    pub fn forward_request<T: RequestMessage>(
359        &self,
360        sender_id: ConnectionId,
361        receiver_id: ConnectionId,
362        request: T,
363    ) -> impl Future<Output = Result<T::Response>> {
364        self.request_internal(Some(sender_id), receiver_id, request)
365    }
366
367    pub fn request_internal<T: RequestMessage>(
368        &self,
369        original_sender_id: Option<ConnectionId>,
370        receiver_id: ConnectionId,
371        request: T,
372    ) -> impl Future<Output = Result<T::Response>> {
373        let (tx, rx) = oneshot::channel();
374        let send = self.connection_state(receiver_id).and_then(|connection| {
375            let message_id = connection.next_message_id.fetch_add(1, SeqCst);
376            connection
377                .response_channels
378                .lock()
379                .as_mut()
380                .ok_or_else(|| anyhow!("connection was closed"))?
381                .insert(message_id, tx);
382            connection
383                .outgoing_tx
384                .unbounded_send(proto::Message::Envelope(request.into_envelope(
385                    message_id,
386                    None,
387                    original_sender_id.map(|id| id.0),
388                )))
389                .map_err(|_| anyhow!("connection was closed"))?;
390            Ok(())
391        });
392        async move {
393            send?;
394            let (response, _barrier) = rx.await.map_err(|_| anyhow!("connection was closed"))?;
395            if let Some(proto::envelope::Payload::Error(error)) = &response.payload {
396                Err(anyhow!(
397                    "RPC request {} failed - {}",
398                    T::NAME,
399                    error.message
400                ))
401            } else {
402                T::Response::from_envelope(response)
403                    .ok_or_else(|| anyhow!("received response of the wrong type"))
404            }
405        }
406    }
407
408    pub fn send<T: EnvelopedMessage>(&self, receiver_id: ConnectionId, message: T) -> Result<()> {
409        let connection = self.connection_state(receiver_id)?;
410        let message_id = connection
411            .next_message_id
412            .fetch_add(1, atomic::Ordering::SeqCst);
413        connection
414            .outgoing_tx
415            .unbounded_send(proto::Message::Envelope(
416                message.into_envelope(message_id, None, None),
417            ))?;
418        Ok(())
419    }
420
421    pub fn forward_send<T: EnvelopedMessage>(
422        &self,
423        sender_id: ConnectionId,
424        receiver_id: ConnectionId,
425        message: T,
426    ) -> Result<()> {
427        let connection = self.connection_state(receiver_id)?;
428        let message_id = connection
429            .next_message_id
430            .fetch_add(1, atomic::Ordering::SeqCst);
431        connection
432            .outgoing_tx
433            .unbounded_send(proto::Message::Envelope(message.into_envelope(
434                message_id,
435                None,
436                Some(sender_id.0),
437            )))?;
438        Ok(())
439    }
440
441    pub fn respond<T: RequestMessage>(
442        &self,
443        receipt: Receipt<T>,
444        response: T::Response,
445    ) -> Result<()> {
446        let connection = self.connection_state(receipt.sender_id)?;
447        let message_id = connection
448            .next_message_id
449            .fetch_add(1, atomic::Ordering::SeqCst);
450        connection
451            .outgoing_tx
452            .unbounded_send(proto::Message::Envelope(response.into_envelope(
453                message_id,
454                Some(receipt.message_id),
455                None,
456            )))?;
457        Ok(())
458    }
459
460    pub fn respond_with_error<T: RequestMessage>(
461        &self,
462        receipt: Receipt<T>,
463        response: proto::Error,
464    ) -> Result<()> {
465        let connection = self.connection_state(receipt.sender_id)?;
466        let message_id = connection
467            .next_message_id
468            .fetch_add(1, atomic::Ordering::SeqCst);
469        connection
470            .outgoing_tx
471            .unbounded_send(proto::Message::Envelope(response.into_envelope(
472                message_id,
473                Some(receipt.message_id),
474                None,
475            )))?;
476        Ok(())
477    }
478
479    fn connection_state(&self, connection_id: ConnectionId) -> Result<ConnectionState> {
480        let connections = self.connections.read();
481        let connection = connections
482            .get(&connection_id)
483            .ok_or_else(|| anyhow!("no such connection: {}", connection_id))?;
484        Ok(connection.clone())
485    }
486}
487
488impl Serialize for Peer {
489    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
490    where
491        S: serde::Serializer,
492    {
493        let mut state = serializer.serialize_struct("Peer", 2)?;
494        state.serialize_field("connections", &*self.connections.read())?;
495        state.end()
496    }
497}
498
499#[cfg(test)]
500mod tests {
501    use super::*;
502    use crate::TypedEnvelope;
503    use async_tungstenite::tungstenite::Message as WebSocketMessage;
504    use gpui::TestAppContext;
505
506    #[ctor::ctor]
507    fn init_logger() {
508        if std::env::var("RUST_LOG").is_ok() {
509            env_logger::init();
510        }
511    }
512
513    #[gpui::test(iterations = 50)]
514    async fn test_request_response(cx: &mut TestAppContext) {
515        let executor = cx.foreground();
516
517        // create 2 clients connected to 1 server
518        let server = Peer::new();
519        let client1 = Peer::new();
520        let client2 = Peer::new();
521
522        let (client1_to_server_conn, server_to_client_1_conn, _kill) =
523            Connection::in_memory(cx.background());
524        let (client1_conn_id, io_task1, client1_incoming) =
525            client1.add_test_connection(client1_to_server_conn, cx.background());
526        let (_, io_task2, server_incoming1) =
527            server.add_test_connection(server_to_client_1_conn, cx.background());
528
529        let (client2_to_server_conn, server_to_client_2_conn, _kill) =
530            Connection::in_memory(cx.background());
531        let (client2_conn_id, io_task3, client2_incoming) =
532            client2.add_test_connection(client2_to_server_conn, cx.background());
533        let (_, io_task4, server_incoming2) =
534            server.add_test_connection(server_to_client_2_conn, cx.background());
535
536        executor.spawn(io_task1).detach();
537        executor.spawn(io_task2).detach();
538        executor.spawn(io_task3).detach();
539        executor.spawn(io_task4).detach();
540        executor
541            .spawn(handle_messages(server_incoming1, server.clone()))
542            .detach();
543        executor
544            .spawn(handle_messages(client1_incoming, client1.clone()))
545            .detach();
546        executor
547            .spawn(handle_messages(server_incoming2, server.clone()))
548            .detach();
549        executor
550            .spawn(handle_messages(client2_incoming, client2.clone()))
551            .detach();
552
553        assert_eq!(
554            client1
555                .request(client1_conn_id, proto::Ping {},)
556                .await
557                .unwrap(),
558            proto::Ack {}
559        );
560
561        assert_eq!(
562            client2
563                .request(client2_conn_id, proto::Ping {},)
564                .await
565                .unwrap(),
566            proto::Ack {}
567        );
568
569        assert_eq!(
570            client1
571                .request(client1_conn_id, proto::Test { id: 1 },)
572                .await
573                .unwrap(),
574            proto::Test { id: 1 }
575        );
576
577        assert_eq!(
578            client2
579                .request(client2_conn_id, proto::Test { id: 2 })
580                .await
581                .unwrap(),
582            proto::Test { id: 2 }
583        );
584
585        client1.disconnect(client1_conn_id);
586        client2.disconnect(client1_conn_id);
587
588        async fn handle_messages(
589            mut messages: BoxStream<'static, Box<dyn AnyTypedEnvelope>>,
590            peer: Arc<Peer>,
591        ) -> Result<()> {
592            while let Some(envelope) = messages.next().await {
593                let envelope = envelope.into_any();
594                if let Some(envelope) = envelope.downcast_ref::<TypedEnvelope<proto::Ping>>() {
595                    let receipt = envelope.receipt();
596                    peer.respond(receipt, proto::Ack {})?
597                } else if let Some(envelope) = envelope.downcast_ref::<TypedEnvelope<proto::Test>>()
598                {
599                    peer.respond(envelope.receipt(), envelope.payload.clone())?
600                } else {
601                    panic!("unknown message type");
602                }
603            }
604
605            Ok(())
606        }
607    }
608
609    #[gpui::test(iterations = 50)]
610    async fn test_order_of_response_and_incoming(cx: &mut TestAppContext) {
611        let executor = cx.foreground();
612        let server = Peer::new();
613        let client = Peer::new();
614
615        let (client_to_server_conn, server_to_client_conn, _kill) =
616            Connection::in_memory(cx.background());
617        let (client_to_server_conn_id, io_task1, mut client_incoming) =
618            client.add_test_connection(client_to_server_conn, cx.background());
619        let (server_to_client_conn_id, io_task2, mut server_incoming) =
620            server.add_test_connection(server_to_client_conn, cx.background());
621
622        executor.spawn(io_task1).detach();
623        executor.spawn(io_task2).detach();
624
625        executor
626            .spawn(async move {
627                let request = server_incoming
628                    .next()
629                    .await
630                    .unwrap()
631                    .into_any()
632                    .downcast::<TypedEnvelope<proto::Ping>>()
633                    .unwrap();
634
635                server
636                    .send(
637                        server_to_client_conn_id,
638                        proto::Error {
639                            message: "message 1".to_string(),
640                        },
641                    )
642                    .unwrap();
643                server
644                    .send(
645                        server_to_client_conn_id,
646                        proto::Error {
647                            message: "message 2".to_string(),
648                        },
649                    )
650                    .unwrap();
651                server.respond(request.receipt(), proto::Ack {}).unwrap();
652
653                // Prevent the connection from being dropped
654                server_incoming.next().await;
655            })
656            .detach();
657
658        let events = Arc::new(Mutex::new(Vec::new()));
659
660        let response = client.request(client_to_server_conn_id, proto::Ping {});
661        let response_task = executor.spawn({
662            let events = events.clone();
663            async move {
664                response.await.unwrap();
665                events.lock().push("response".to_string());
666            }
667        });
668
669        executor
670            .spawn({
671                let events = events.clone();
672                async move {
673                    let incoming1 = client_incoming
674                        .next()
675                        .await
676                        .unwrap()
677                        .into_any()
678                        .downcast::<TypedEnvelope<proto::Error>>()
679                        .unwrap();
680                    events.lock().push(incoming1.payload.message);
681                    let incoming2 = client_incoming
682                        .next()
683                        .await
684                        .unwrap()
685                        .into_any()
686                        .downcast::<TypedEnvelope<proto::Error>>()
687                        .unwrap();
688                    events.lock().push(incoming2.payload.message);
689
690                    // Prevent the connection from being dropped
691                    client_incoming.next().await;
692                }
693            })
694            .detach();
695
696        response_task.await;
697        assert_eq!(
698            &*events.lock(),
699            &[
700                "message 1".to_string(),
701                "message 2".to_string(),
702                "response".to_string()
703            ]
704        );
705    }
706
707    #[gpui::test(iterations = 50)]
708    async fn test_dropping_request_before_completion(cx: &mut TestAppContext) {
709        let executor = cx.foreground();
710        let server = Peer::new();
711        let client = Peer::new();
712
713        let (client_to_server_conn, server_to_client_conn, _kill) =
714            Connection::in_memory(cx.background());
715        let (client_to_server_conn_id, io_task1, mut client_incoming) =
716            client.add_test_connection(client_to_server_conn, cx.background());
717        let (server_to_client_conn_id, io_task2, mut server_incoming) =
718            server.add_test_connection(server_to_client_conn, cx.background());
719
720        executor.spawn(io_task1).detach();
721        executor.spawn(io_task2).detach();
722
723        executor
724            .spawn(async move {
725                let request1 = server_incoming
726                    .next()
727                    .await
728                    .unwrap()
729                    .into_any()
730                    .downcast::<TypedEnvelope<proto::Ping>>()
731                    .unwrap();
732                let request2 = server_incoming
733                    .next()
734                    .await
735                    .unwrap()
736                    .into_any()
737                    .downcast::<TypedEnvelope<proto::Ping>>()
738                    .unwrap();
739
740                server
741                    .send(
742                        server_to_client_conn_id,
743                        proto::Error {
744                            message: "message 1".to_string(),
745                        },
746                    )
747                    .unwrap();
748                server
749                    .send(
750                        server_to_client_conn_id,
751                        proto::Error {
752                            message: "message 2".to_string(),
753                        },
754                    )
755                    .unwrap();
756                server.respond(request1.receipt(), proto::Ack {}).unwrap();
757                server.respond(request2.receipt(), proto::Ack {}).unwrap();
758
759                // Prevent the connection from being dropped
760                server_incoming.next().await;
761            })
762            .detach();
763
764        let events = Arc::new(Mutex::new(Vec::new()));
765
766        let request1 = client.request(client_to_server_conn_id, proto::Ping {});
767        let request1_task = executor.spawn(request1);
768        let request2 = client.request(client_to_server_conn_id, proto::Ping {});
769        let request2_task = executor.spawn({
770            let events = events.clone();
771            async move {
772                request2.await.unwrap();
773                events.lock().push("response 2".to_string());
774            }
775        });
776
777        executor
778            .spawn({
779                let events = events.clone();
780                async move {
781                    let incoming1 = client_incoming
782                        .next()
783                        .await
784                        .unwrap()
785                        .into_any()
786                        .downcast::<TypedEnvelope<proto::Error>>()
787                        .unwrap();
788                    events.lock().push(incoming1.payload.message);
789                    let incoming2 = client_incoming
790                        .next()
791                        .await
792                        .unwrap()
793                        .into_any()
794                        .downcast::<TypedEnvelope<proto::Error>>()
795                        .unwrap();
796                    events.lock().push(incoming2.payload.message);
797
798                    // Prevent the connection from being dropped
799                    client_incoming.next().await;
800                }
801            })
802            .detach();
803
804        // Allow the request to make some progress before dropping it.
805        cx.background().simulate_random_delay().await;
806        drop(request1_task);
807
808        request2_task.await;
809        assert_eq!(
810            &*events.lock(),
811            &[
812                "message 1".to_string(),
813                "message 2".to_string(),
814                "response 2".to_string()
815            ]
816        );
817    }
818
819    #[gpui::test(iterations = 50)]
820    async fn test_disconnect(cx: &mut TestAppContext) {
821        let executor = cx.foreground();
822
823        let (client_conn, mut server_conn, _kill) = Connection::in_memory(cx.background());
824
825        let client = Peer::new();
826        let (connection_id, io_handler, mut incoming) =
827            client.add_test_connection(client_conn, cx.background());
828
829        let (io_ended_tx, io_ended_rx) = oneshot::channel();
830        executor
831            .spawn(async move {
832                io_handler.await.ok();
833                io_ended_tx.send(()).unwrap();
834            })
835            .detach();
836
837        let (messages_ended_tx, messages_ended_rx) = oneshot::channel();
838        executor
839            .spawn(async move {
840                incoming.next().await;
841                messages_ended_tx.send(()).unwrap();
842            })
843            .detach();
844
845        client.disconnect(connection_id);
846
847        let _ = io_ended_rx.await;
848        let _ = messages_ended_rx.await;
849        assert!(server_conn
850            .send(WebSocketMessage::Binary(vec![]))
851            .await
852            .is_err());
853    }
854
855    #[gpui::test(iterations = 50)]
856    async fn test_io_error(cx: &mut TestAppContext) {
857        let executor = cx.foreground();
858        let (client_conn, mut server_conn, _kill) = Connection::in_memory(cx.background());
859
860        let client = Peer::new();
861        let (connection_id, io_handler, mut incoming) =
862            client.add_test_connection(client_conn, cx.background());
863        executor.spawn(io_handler).detach();
864        executor
865            .spawn(async move { incoming.next().await })
866            .detach();
867
868        let response = executor.spawn(client.request(connection_id, proto::Ping {}));
869        let _request = server_conn.rx.next().await.unwrap().unwrap();
870
871        drop(server_conn);
872        assert_eq!(
873            response.await.unwrap_err().to_string(),
874            "connection was closed"
875        );
876    }
877}