From d431854421d024da69c22ac3bc1ed0725f79aaf2 Mon Sep 17 00:00:00 2001 From: arkpar Date: Fri, 22 Jan 2016 00:47:45 +0100 Subject: [PATCH] Fixed tests --- util/src/io/mod.rs | 23 +++++++++------- util/src/network/mod.rs | 58 +++++++++++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/util/src/io/mod.rs b/util/src/io/mod.rs index 4406c751f..48c02f6ee 100644 --- a/util/src/io/mod.rs +++ b/util/src/io/mod.rs @@ -8,27 +8,28 @@ /// /// struct MyHandler; /// +/// #[derive(Clone)] /// struct MyMessage { /// data: u32 /// } /// /// impl IoHandler for MyHandler { -/// fn initialize(&mut self, io: &mut IoContext) { -/// io.register_timer(1000).unwrap(); +/// fn initialize(&self, io: &IoContext) { +/// io.register_timer(0, 1000).unwrap(); /// } /// -/// fn timeout(&mut self, _io: &mut IoContext, timer: TimerToken) { +/// fn timeout(&self, _io: &IoContext, timer: TimerToken) { /// println!("Timeout {}", timer); /// } /// -/// fn message(&mut self, _io: &mut IoContext, message: &mut MyMessage) { +/// fn message(&self, _io: &IoContext, message: &MyMessage) { /// println!("Message {}", message.data); /// } /// } /// /// fn main () { /// let mut service = IoService::::start().expect("Error creating network service"); -/// service.register_handler(Box::new(MyHandler)).unwrap(); +/// service.register_handler(Arc::new(MyHandler)).unwrap(); /// /// // Wait for quit condition /// // ... @@ -93,24 +94,26 @@ pub use io::service::TOKENS_PER_HANDLER; #[cfg(test)] mod tests { + use std::sync::Arc; use io::*; struct MyHandler; + #[derive(Clone)] struct MyMessage { data: u32 } impl IoHandler for MyHandler { - fn initialize(&mut self, io: &mut IoContext) { - io.register_timer(1000).unwrap(); + fn initialize(&self, io: &IoContext) { + io.register_timer(0, 1000).unwrap(); } - fn timeout(&mut self, _io: &mut IoContext, timer: TimerToken) { + fn timeout(&self, _io: &IoContext, timer: TimerToken) { println!("Timeout {}", timer); } - fn message(&mut self, _io: &mut IoContext, message: &mut MyMessage) { + fn message(&self, _io: &IoContext, message: &MyMessage) { println!("Message {}", message.data); } } @@ -118,7 +121,7 @@ mod tests { #[test] fn test_service_register_handler () { let mut service = IoService::::start().expect("Error creating network service"); - service.register_handler(Box::new(MyHandler)).unwrap(); + service.register_handler(Arc::new(MyHandler)).unwrap(); } } diff --git a/util/src/network/mod.rs b/util/src/network/mod.rs index 32296d476..0c734442d 100644 --- a/util/src/network/mod.rs +++ b/util/src/network/mod.rs @@ -8,39 +8,40 @@ /// /// struct MyHandler; /// +/// #[derive(Clone)] /// struct MyMessage { /// data: u32 /// } /// /// impl NetworkProtocolHandler for MyHandler { -/// fn initialize(&mut self, io: &mut NetworkContext) { -/// io.register_timer(1000); +/// fn initialize(&self, io: &NetworkContext) { +/// io.register_timer(0, 1000); /// } /// -/// fn read(&mut self, io: &mut NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) { +/// fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) { /// println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer); /// } /// -/// fn connected(&mut self, io: &mut NetworkContext, peer: &PeerId) { +/// fn connected(&self, io: &NetworkContext, peer: &PeerId) { /// println!("Connected {}", peer); /// } /// -/// fn disconnected(&mut self, io: &mut NetworkContext, peer: &PeerId) { +/// fn disconnected(&self, io: &NetworkContext, peer: &PeerId) { /// println!("Disconnected {}", peer); /// } /// -/// fn timeout(&mut self, io: &mut NetworkContext, timer: TimerToken) { +/// fn timeout(&self, io: &NetworkContext, timer: TimerToken) { /// println!("Timeout {}", timer); /// } /// -/// fn message(&mut self, io: &mut NetworkContext, message: &MyMessage) { +/// fn message(&self, io: &NetworkContext, message: &MyMessage) { /// println!("Message {}", message.data); /// } /// } /// /// fn main () { /// let mut service = NetworkService::::start().expect("Error creating network service"); -/// service.register_protocol(Box::new(MyHandler), "myproto", &[1u8]); +/// service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]); /// /// // Wait for quit condition /// // ... @@ -91,3 +92,44 @@ pub trait NetworkProtocolHandler: Sync + Send where Message: Send + Syn fn message(&self, _io: &NetworkContext, _message: &Message) {} } + +#[test] +fn test_net_service() { + + use std::sync::Arc; + struct MyHandler; + + #[derive(Clone)] + struct MyMessage { + data: u32 + } + + impl NetworkProtocolHandler for MyHandler { + fn initialize(&self, io: &NetworkContext) { + io.register_timer(0, 1000).unwrap(); + } + + fn read(&self, _io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) { + println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer); + } + + fn connected(&self, _io: &NetworkContext, peer: &PeerId) { + println!("Connected {}", peer); + } + + fn disconnected(&self, _io: &NetworkContext, peer: &PeerId) { + println!("Disconnected {}", peer); + } + + fn timeout(&self, _io: &NetworkContext, timer: TimerToken) { + println!("Timeout {}", timer); + } + + fn message(&self, _io: &NetworkContext, message: &MyMessage) { + println!("Message {}", message.data); + } + } + + let mut service = NetworkService::::start().expect("Error creating network service"); + service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]).unwrap(); +}