From 3d2cc1f0d3c320df4d9d990f45698103812789eb Mon Sep 17 00:00:00 2001 From: Tim Kuehn Date: Thu, 11 Feb 2016 22:53:13 -0800 Subject: [PATCH 1/2] Organize macro module tests --- tarpc/src/macros.rs | 159 +++++++++++++++++--------------------------- 1 file changed, 60 insertions(+), 99 deletions(-) diff --git a/tarpc/src/macros.rs b/tarpc/src/macros.rs index e7f9e26..3bb22f6 100644 --- a/tarpc/src/macros.rs +++ b/tarpc/src/macros.rs @@ -321,106 +321,23 @@ macro_rules! service_inner { #[cfg(test)] #[allow(dead_code)] // because we're testing that the macro expansion compiles -mod test { - extern crate env_logger; - use std::time::Duration; - - fn test_timeout() -> Option { - Some(Duration::from_secs(5)) - } - - #[derive(PartialEq, Debug, Serialize, Deserialize)] - pub struct Foo { - pub message: String - } - mod my_server { - use super::Foo; - service! { - rpc hello(foo: Foo) -> Foo; - rpc add(x: i32, y: i32) -> i32; - } - } - - struct Server; - impl my_server::Service for Server { - fn hello(&self, s: Foo) -> Foo { - Foo { message: format!("Hello, {}", &s.message) } - } - fn add(&self, x: i32, y: i32) -> i32 { - x + y - } - } - - #[test] - fn serve_arc_server() { - my_server::serve("localhost:0", ::std::sync::Arc::new(Server), None) - .unwrap() - .shutdown(); - } - - #[test] - fn simple() { - let handle = my_server::serve( "localhost:0", Server, test_timeout()).unwrap(); - let client = my_server::Client::new(handle.local_addr(), None).unwrap(); - assert_eq!(3, client.add(1, 2).unwrap()); - let foo = Foo { message: "Adam".into() }; - let want = Foo { message: format!("Hello, {}", &foo.message) }; - assert_eq!(want, client.hello(Foo { message: "Adam".into() }).unwrap()); - drop(client); - handle.shutdown(); - } - - #[test] - fn simple_async() { - let handle = my_server::serve("localhost:0", Server, test_timeout()).unwrap(); - let client = my_server::AsyncClient::new(handle.local_addr(), None).unwrap(); - assert_eq!(3, client.add(1, 2).get().unwrap()); - let foo = Foo { message: "Adam".into() }; - let want = Foo { message: format!("Hello, {}", &foo.message) }; - assert_eq!(want, client.hello(Foo { message: "Adam".into() }).get().unwrap()); - drop(client); - handle.shutdown(); - } - - /// Tests a service definition with a fn that takes no args +mod syntax_tests { + // Tests a service definition with a fn that takes no args mod qux { service! { rpc hello() -> String; } } - /// Tests a service definition with an import - mod foo { - use std::collections::HashMap; - service! { - #[doc="Hello bob"] - #[inline(always)] - rpc baz(s: String) -> HashMap; - } - } - - /// Tests a service definition with an attribute but no doc comment + // Tests a service definition with an attribute. mod bar { - use std::collections::HashMap; - service! { - #[inline(always)] - rpc baz(s: String) -> HashMap; - } - } - - /// Tests a service definition with an attribute and a doc comment - #[deny(missing_docs)] - #[allow(unused)] - mod baz { - use std::collections::HashMap; - pub struct Debuggable; service! { #[doc="Hello bob"] - #[inline(always)] - rpc baz(s: String) -> HashMap; + rpc baz(s: String) -> String; } } + // Tests a service with implicit return types. mod no_return { service! { rpc ack(); @@ -431,35 +348,79 @@ mod test { } #[cfg(test)] -#[allow(dead_code)] // because we're not using all expanded types -mod benchmarks { +mod functional_tests { + extern crate env_logger; + use std::time::Duration; + + fn test_timeout() -> Option { + Some(Duration::from_secs(5)) + } + + service! { + rpc add(x: i32, y: i32) -> i32; + } + + struct Server; + + impl Service for Server { + fn add(&self, x: i32, y: i32) -> i32 { + x + y + } + } + + #[test] + fn simple() { + let handle = serve( "localhost:0", Server, test_timeout()).unwrap(); + let client = Client::new(handle.local_addr(), None).unwrap(); + assert_eq!(3, client.add(1, 2).unwrap()); + drop(client); + handle.shutdown(); + } + + #[test] + fn simple_async() { + let handle = serve("localhost:0", Server, test_timeout()).unwrap(); + let client = AsyncClient::new(handle.local_addr(), None).unwrap(); + assert_eq!(3, client.add(1, 2).get().unwrap()); + drop(client); + handle.shutdown(); + } + + // Tests that a server can be wrapped in an Arc; no need to run, just compile + #[allow(dead_code)] + fn serve_arc_server() { + let _ = serve("localhost:0", ::std::sync::Arc::new(Server), None); + } +} + +#[cfg(test)] +#[allow(dead_code)] // generated Client isn't used in this benchmark +mod benchmark { extern crate env_logger; use ServeHandle; use std::sync::{Arc, Mutex}; use test::Bencher; - mod hi { - service! { - rpc hello(s: String) -> String; - } + service! { + rpc hello(s: String) -> String; } struct HelloServer; - - impl hi::Service for HelloServer { + impl Service for HelloServer { fn hello(&self, s: String) -> String { format!("Hello, {}!", s) } } + // Prevents resource exhaustion when benching lazy_static! { static ref HANDLE: Arc> = { - let handle = hi::serve("localhost:0", HelloServer, None).unwrap(); + let handle = serve("localhost:0", HelloServer, None).unwrap(); Arc::new(Mutex::new(handle)) }; - static ref CLIENT: Arc> = { + static ref CLIENT: Arc> = { let addr = HANDLE.lock().unwrap().local_addr().clone(); - let client = hi::AsyncClient::new(addr, None).unwrap(); + let client = AsyncClient::new(addr, None).unwrap(); Arc::new(Mutex::new(client)) }; } From 7cfca8f721b47a817060d02db1a977d6e4c19bcc Mon Sep 17 00:00:00 2001 From: Tim Kuehn Date: Thu, 11 Feb 2016 23:02:35 -0800 Subject: [PATCH 2/2] Singular module names --- tarpc/src/macros.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tarpc/src/macros.rs b/tarpc/src/macros.rs index 3bb22f6..6f62da1 100644 --- a/tarpc/src/macros.rs +++ b/tarpc/src/macros.rs @@ -321,7 +321,7 @@ macro_rules! service_inner { #[cfg(test)] #[allow(dead_code)] // because we're testing that the macro expansion compiles -mod syntax_tests { +mod syntax_test { // Tests a service definition with a fn that takes no args mod qux { service! { @@ -348,7 +348,7 @@ mod syntax_tests { } #[cfg(test)] -mod functional_tests { +mod functional_test { extern crate env_logger; use std::time::Duration;