Fix FakeServer to expect new GetPrivateUserInfo request

This commit is contained in:
Max Brunsfeld 2022-09-29 13:51:17 -07:00
parent 5d09083a7d
commit 35a537dae0
2 changed files with 54 additions and 21 deletions

View file

@ -6,7 +6,10 @@ use anyhow::{anyhow, Result};
use futures::{future::BoxFuture, stream::BoxStream, Future, StreamExt}; use futures::{future::BoxFuture, stream::BoxStream, Future, StreamExt};
use gpui::{executor, ModelHandle, TestAppContext}; use gpui::{executor, ModelHandle, TestAppContext};
use parking_lot::Mutex; use parking_lot::Mutex;
use rpc::{proto, ConnectionId, Peer, Receipt, TypedEnvelope}; use rpc::{
proto::{self, GetPrivateUserInfo, GetPrivateUserInfoResponse},
ConnectionId, Peer, Receipt, TypedEnvelope,
};
use std::{fmt, rc::Rc, sync::Arc}; use std::{fmt, rc::Rc, sync::Arc};
pub struct FakeServer { pub struct FakeServer {
@ -93,6 +96,7 @@ impl FakeServer {
.authenticate_and_connect(false, &cx.to_async()) .authenticate_and_connect(false, &cx.to_async())
.await .await
.unwrap(); .unwrap();
server server
} }
@ -126,26 +130,44 @@ impl FakeServer {
#[allow(clippy::await_holding_lock)] #[allow(clippy::await_holding_lock)]
pub async fn receive<M: proto::EnvelopedMessage>(&self) -> Result<TypedEnvelope<M>> { pub async fn receive<M: proto::EnvelopedMessage>(&self) -> Result<TypedEnvelope<M>> {
self.executor.start_waiting(); self.executor.start_waiting();
let message = self
.state loop {
.lock() let message = self
.incoming .state
.as_mut() .lock()
.expect("not connected") .incoming
.next() .as_mut()
.await .expect("not connected")
.ok_or_else(|| anyhow!("other half hung up"))?; .next()
self.executor.finish_waiting(); .await
let type_name = message.payload_type_name(); .ok_or_else(|| anyhow!("other half hung up"))?;
Ok(*message self.executor.finish_waiting();
.into_any() let type_name = message.payload_type_name();
.downcast::<TypedEnvelope<M>>() let message = message.into_any();
.unwrap_or_else(|_| {
panic!( if message.is::<TypedEnvelope<M>>() {
"fake server received unexpected message type: {:?}", return Ok(*message.downcast().unwrap());
type_name }
);
})) if message.is::<TypedEnvelope<GetPrivateUserInfo>>() {
self.respond(
message
.downcast::<TypedEnvelope<GetPrivateUserInfo>>()
.unwrap()
.receipt(),
GetPrivateUserInfoResponse {
metrics_id: "the-metrics-id".into(),
},
)
.await;
continue;
}
panic!(
"fake server received unexpected message type: {:?}",
type_name
);
}
} }
pub async fn respond<T: proto::RequestMessage>( pub async fn respond<T: proto::RequestMessage>(

View file

@ -1220,6 +1220,17 @@ mod tests {
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx)); let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
let project_store = cx.add_model(|_| ProjectStore::new(project::Db::open_fake())); let project_store = cx.add_model(|_| ProjectStore::new(project::Db::open_fake()));
let server = FakeServer::for_client(current_user_id, &client, cx).await; let server = FakeServer::for_client(current_user_id, &client, cx).await;
let request = server.receive::<proto::GetPrivateUserInfo>().await.unwrap();
server
.respond(
request.receipt(),
proto::GetPrivateUserInfoResponse {
metrics_id: "the-metrics-id".into(),
},
)
.await;
let fs = FakeFs::new(cx.background()); let fs = FakeFs::new(cx.background());
fs.insert_tree("/private_dir", json!({ "one.rs": "" })) fs.insert_tree("/private_dir", json!({ "one.rs": "" }))
.await; .await;