Fixed tests

This commit is contained in:
arkpar 2016-01-22 00:47:45 +01:00
parent ccf1cc4d54
commit d431854421
2 changed files with 63 additions and 18 deletions

View File

@ -8,27 +8,28 @@
/// ///
/// struct MyHandler; /// struct MyHandler;
/// ///
/// #[derive(Clone)]
/// struct MyMessage { /// struct MyMessage {
/// data: u32 /// data: u32
/// } /// }
/// ///
/// impl IoHandler<MyMessage> for MyHandler { /// impl IoHandler<MyMessage> for MyHandler {
/// fn initialize(&mut self, io: &mut IoContext<MyMessage>) { /// fn initialize(&self, io: &IoContext<MyMessage>) {
/// io.register_timer(1000).unwrap(); /// io.register_timer(0, 1000).unwrap();
/// } /// }
/// ///
/// fn timeout(&mut self, _io: &mut IoContext<MyMessage>, timer: TimerToken) { /// fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
/// println!("Timeout {}", timer); /// println!("Timeout {}", timer);
/// } /// }
/// ///
/// fn message(&mut self, _io: &mut IoContext<MyMessage>, message: &mut MyMessage) { /// fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
/// println!("Message {}", message.data); /// println!("Message {}", message.data);
/// } /// }
/// } /// }
/// ///
/// fn main () { /// fn main () {
/// let mut service = IoService::<MyMessage>::start().expect("Error creating network service"); /// let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
/// service.register_handler(Box::new(MyHandler)).unwrap(); /// service.register_handler(Arc::new(MyHandler)).unwrap();
/// ///
/// // Wait for quit condition /// // Wait for quit condition
/// // ... /// // ...
@ -93,24 +94,26 @@ pub use io::service::TOKENS_PER_HANDLER;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::sync::Arc;
use io::*; use io::*;
struct MyHandler; struct MyHandler;
#[derive(Clone)]
struct MyMessage { struct MyMessage {
data: u32 data: u32
} }
impl IoHandler<MyMessage> for MyHandler { impl IoHandler<MyMessage> for MyHandler {
fn initialize(&mut self, io: &mut IoContext<MyMessage>) { fn initialize(&self, io: &IoContext<MyMessage>) {
io.register_timer(1000).unwrap(); io.register_timer(0, 1000).unwrap();
} }
fn timeout(&mut self, _io: &mut IoContext<MyMessage>, timer: TimerToken) { fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
println!("Timeout {}", timer); println!("Timeout {}", timer);
} }
fn message(&mut self, _io: &mut IoContext<MyMessage>, message: &mut MyMessage) { fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
println!("Message {}", message.data); println!("Message {}", message.data);
} }
} }
@ -118,7 +121,7 @@ mod tests {
#[test] #[test]
fn test_service_register_handler () { fn test_service_register_handler () {
let mut service = IoService::<MyMessage>::start().expect("Error creating network service"); let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
service.register_handler(Box::new(MyHandler)).unwrap(); service.register_handler(Arc::new(MyHandler)).unwrap();
} }
} }

View File

@ -8,39 +8,40 @@
/// ///
/// struct MyHandler; /// struct MyHandler;
/// ///
/// #[derive(Clone)]
/// struct MyMessage { /// struct MyMessage {
/// data: u32 /// data: u32
/// } /// }
/// ///
/// impl NetworkProtocolHandler<MyMessage> for MyHandler { /// impl NetworkProtocolHandler<MyMessage> for MyHandler {
/// fn initialize(&mut self, io: &mut NetworkContext<MyMessage>) { /// fn initialize(&self, io: &NetworkContext<MyMessage>) {
/// io.register_timer(1000); /// io.register_timer(0, 1000);
/// } /// }
/// ///
/// fn read(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) { /// fn read(&self, io: &NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
/// println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer); /// println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
/// } /// }
/// ///
/// fn connected(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId) { /// fn connected(&self, io: &NetworkContext<MyMessage>, peer: &PeerId) {
/// println!("Connected {}", peer); /// println!("Connected {}", peer);
/// } /// }
/// ///
/// fn disconnected(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId) { /// fn disconnected(&self, io: &NetworkContext<MyMessage>, peer: &PeerId) {
/// println!("Disconnected {}", peer); /// println!("Disconnected {}", peer);
/// } /// }
/// ///
/// fn timeout(&mut self, io: &mut NetworkContext<MyMessage>, timer: TimerToken) { /// fn timeout(&self, io: &NetworkContext<MyMessage>, timer: TimerToken) {
/// println!("Timeout {}", timer); /// println!("Timeout {}", timer);
/// } /// }
/// ///
/// fn message(&mut self, io: &mut NetworkContext<MyMessage>, message: &MyMessage) { /// fn message(&self, io: &NetworkContext<MyMessage>, message: &MyMessage) {
/// println!("Message {}", message.data); /// println!("Message {}", message.data);
/// } /// }
/// } /// }
/// ///
/// fn main () { /// fn main () {
/// let mut service = NetworkService::<MyMessage>::start().expect("Error creating network service"); /// let mut service = NetworkService::<MyMessage>::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 /// // Wait for quit condition
/// // ... /// // ...
@ -91,3 +92,44 @@ pub trait NetworkProtocolHandler<Message>: Sync + Send where Message: Send + Syn
fn message(&self, _io: &NetworkContext<Message>, _message: &Message) {} fn message(&self, _io: &NetworkContext<Message>, _message: &Message) {}
} }
#[test]
fn test_net_service() {
use std::sync::Arc;
struct MyHandler;
#[derive(Clone)]
struct MyMessage {
data: u32
}
impl NetworkProtocolHandler<MyMessage> for MyHandler {
fn initialize(&self, io: &NetworkContext<MyMessage>) {
io.register_timer(0, 1000).unwrap();
}
fn read(&self, _io: &NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
}
fn connected(&self, _io: &NetworkContext<MyMessage>, peer: &PeerId) {
println!("Connected {}", peer);
}
fn disconnected(&self, _io: &NetworkContext<MyMessage>, peer: &PeerId) {
println!("Disconnected {}", peer);
}
fn timeout(&self, _io: &NetworkContext<MyMessage>, timer: TimerToken) {
println!("Timeout {}", timer);
}
fn message(&self, _io: &NetworkContext<MyMessage>, message: &MyMessage) {
println!("Message {}", message.data);
}
}
let mut service = NetworkService::<MyMessage>::start().expect("Error creating network service");
service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]).unwrap();
}