diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-05-22 15:16:25 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-05-23 08:53:41 +0200 |
| commit | 8865e99f1f647434ef72f90afcd39e96686b82b6 (patch) | |
| tree | b36b8cf626a2bb6b7951ccba8d43a1bed0c29095 | |
| parent | 0175fb089f8fea8283c9c8577a705fe052983672 (diff) | |
| download | mullvadvpn-8865e99f1f647434ef72f90afcd39e96686b82b6.tar.xz mullvadvpn-8865e99f1f647434ef72f90afcd39e96686b82b6.zip | |
Add integration tests for IPC client/server
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | talpid_ipc/Cargo.toml | 1 | ||||
| -rw-r--r-- | talpid_ipc/tests/ipc-client-server.rs | 67 |
3 files changed, 69 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock index 2715f496a2..28a6b15e0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -610,6 +610,7 @@ dependencies = [ "env_logger 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 7.0.0 (git+https://github.com/faern/jsonrpc?branch=bind-zero)", + "jsonrpc-macros 7.0.0 (git+https://github.com/faern/jsonrpc?branch=bind-zero)", "jsonrpc-ws-server 7.0.0 (git+https://github.com/faern/jsonrpc?branch=bind-zero)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/talpid_ipc/Cargo.toml b/talpid_ipc/Cargo.toml index d9b0b2dd77..a3bfea8c50 100644 --- a/talpid_ipc/Cargo.toml +++ b/talpid_ipc/Cargo.toml @@ -20,3 +20,4 @@ zmq = "0.8" [dev-dependencies] assert_matches = "1.0" env_logger = "0.4" +jsonrpc-macros = { git = "https://github.com/faern/jsonrpc", branch = "bind-zero" } diff --git a/talpid_ipc/tests/ipc-client-server.rs b/talpid_ipc/tests/ipc-client-server.rs new file mode 100644 index 0000000000..a41bce282a --- /dev/null +++ b/talpid_ipc/tests/ipc-client-server.rs @@ -0,0 +1,67 @@ +extern crate talpid_ipc; +extern crate jsonrpc_core; +#[macro_use] +extern crate jsonrpc_macros; +extern crate env_logger; +#[macro_use] +extern crate assert_matches; + +use jsonrpc_core::{Error, IoHandler}; +use std::sync::{Mutex, mpsc}; +use std::time::Duration; + +build_rpc_trait! { + pub trait TestApi { + #[rpc(name = "foo")] + fn foo(&self, i64) -> Result<(), Error>; + } +} + +struct ApiImpl { + tx: Mutex<mpsc::Sender<i64>>, +} + +impl TestApi for ApiImpl { + fn foo(&self, i: i64) -> Result<(), Error> { + self.tx.lock().unwrap().send(i).unwrap(); + Ok(()) + } +} + +#[test] +fn ipc_client_server() { + env_logger::init().unwrap(); + + let (server, rx) = create_server(); + let server_id = server.address().to_owned(); + let mut client = create_client(server_id); + + client.call("foo", &[97]).unwrap(); + assert_eq!(Ok(97), rx.recv_timeout(Duration::from_millis(500))); +} + +#[test] +#[should_panic] +fn ipc_client_invalid_url() { + create_client("INVALID ID".to_owned()); +} + +#[test] +fn ipc_client_invalid_method() { + let mut client = create_client("ws://127.0.0.1:9876".to_owned()); + assert_matches!(client.call("invalid_method", &[0]), Err(_)); +} + +fn create_server() -> (talpid_ipc::IpcServer, mpsc::Receiver<i64>) { + let (tx, rx) = mpsc::channel(); + let rpc = ApiImpl { tx: Mutex::new(tx) }; + let mut io = IoHandler::new(); + io.extend_with(rpc.to_delegate()); + + let server = talpid_ipc::IpcServer::start(io.into()).unwrap(); + (server, rx) +} + +fn create_client(id: talpid_ipc::IpcServerId) -> talpid_ipc::WsIpcClient { + talpid_ipc::WsIpcClient::new(id).unwrap() +} |
