//! Tests for cases that would case the client to return //! `Error::ServerDisappeared`: //! - When a client issues a request after the server end-point has already //! been dropped. //! - If a server end-point is dropped while there are requests in the queue, //! all those requests should abort with a `ServerDisappeared` error. use std::{thread, time}; use ump::{channel, Error}; /// Issuing a request should fail immediately if the server end-point has been /// dropped. #[test] fn sync_expect_err_if_server_dropped() { let (server, client) = channel::(); // nuke the server end-point drop(server); let msg = String::from("Client"); let reply = client.req(msg); let Err(Error::ServerDisappeared) = reply else { panic!("Unexpected return value"); }; } /// Issuing a request should fail immediately if the server end-point has been /// dropped. #[test] fn async_expect_err_if_server_dropped() { let tokrt = tokio::runtime::Runtime::new().unwrap(); let (server, client) = channel::(); // nuke the server end-point drop(server); tokrt.block_on(async { let msg = String::from("Client"); let reply = client.areq(msg).await; let Err(Error::ServerDisappeared) = reply else { panic!("Unexpected return value"); }; }); } /// If a request is still in the queue when the server end-point is dropped, /// the client shoull return `Error::ServerDisappeared` #[test] fn sync_expect_err_if_queue_dropped() { let (server, client) = channel::(); // Don't actually take any requests off queue -- just terminate the server // end-point. let server_thread = thread::spawn(move || { // Should be doing something more robust .. let one_second = time::Duration::from_secs(1); thread::sleep(one_second); drop(server); }); let msg = String::from("Client"); let reply = client.req(msg); let Err(Error::ServerDisappeared) = reply else { panic!("Unexpected return value"); }; server_thread.join().unwrap(); } /// If a request is still in the queue when the server end-point is dropped, /// the client shoull return `Error::ServerDisappeared` #[test] fn async_expect_err_if_queue_dropped() { let tokrt = tokio::runtime::Runtime::new().unwrap(); let (server, client) = channel::(); // Don't actually take any requests off queue -- just terminate the server // end-point. let server_thread = thread::spawn(move || { // Should be doing something more robust .. let one_second = time::Duration::from_secs(1); thread::sleep(one_second); drop(server); }); tokrt.block_on(async { let msg = String::from("Client"); let reply = client.areq(msg).await; let Err(Error::ServerDisappeared) = reply else { panic!("Unexpected return value"); }; }); server_thread.join().unwrap(); } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :