add test to ensure that client and server agree on socket address

This commit is contained in:
Joseph Montanaro 2024-12-28 07:36:38 -05:00
parent ee495478ff
commit efbf6c687c
2 changed files with 9 additions and 9 deletions

View File

@ -9,8 +9,7 @@ pub use cli::{
invoke_shortcut, invoke_shortcut,
}; };
pub(crate) use platform::connect; pub use platform::{connect, server_addr};
pub use platform::server_addr;
pub mod proto; pub mod proto;

View File

@ -4,6 +4,7 @@ use tauri::{
AppHandle, AppHandle,
async_runtime as rt, async_runtime as rt,
Manager, Manager,
Runtime,
}; };
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use tokio::sync::oneshot; use tokio::sync::oneshot;
@ -80,9 +81,11 @@ impl<'s> CloseWaiter<'s> {
} }
fn serve<H, F>(sock_name: &str, app_handle: AppHandle, handler: H) -> std::io::Result<()> // note: AppHandle is generic over `Runtime` for testing
where H: Copy + Send + Fn(Stream, AppHandle, u32) -> F + 'static, fn serve<H, F, R>(sock_name: &str, app_handle: AppHandle<R>, handler: H) -> std::io::Result<()>
where H: Copy + Send + Fn(Stream, AppHandle<R>, u32) -> F + 'static,
F: Send + Future<Output = Result<(), HandlerError>>, F: Send + Future<Output = Result<(), HandlerError>>,
R: Runtime
{ {
let (mut listener, addr) = platform::bind(sock_name)?; let (mut listener, addr) = platform::bind(sock_name)?;
rt::spawn(async move { rt::spawn(async move {
@ -233,23 +236,21 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_server_connect() { async fn test_server_connect() {
let app = tauri::test::mock_app(); let app = tauri::test::mock_app();
serve("creddy_server_test", app.app_handle().clone(), |stream, _handle, _pid| { serve("creddy_server_test", app.app_handle().clone(), |mut stream, _handle, _pid| {
async move { async move {
let buf = serde_json::to_vec(&CliResponse::Empty).unwrap(); let buf = serde_json::to_vec(&CliResponse::Empty).unwrap();
stream.write_all(&buf).await.unwrap(); stream.write_all(&buf).await.unwrap();
Ok(()) Ok(())
} }
}); }).unwrap();
let addr = creddy_cli::server_addr("creddy_server_test"); let addr = creddy_cli::server_addr("creddy_server_test");
let mut stream = creddy_cli::connect(Some(addr)).await.unwrap(); let mut stream = creddy_cli::connect(Some(addr)).await.unwrap();
let req = CliRequest::InvokeShortcut{ action: ShortcutAction::ShowWindow };
let req_bytes = serde_json::to_vec(&req).unwrap();
stream.write_all(&req_bytes).await.unwrap();
let mut buf = Vec::new(); let mut buf = Vec::new();
stream.read_to_end(&mut buf).await.unwrap(); stream.read_to_end(&mut buf).await.unwrap();
let resp: CliResponse = serde_json::from_slice(&buf).unwrap(); let resp: CliResponse = serde_json::from_slice(&buf).unwrap();
assert!(matches!(resp, CliResponse::Empty)) assert!(matches!(resp, CliResponse::Empty))
} }
} }