From a217df8a498af32bbd5c23a2c60bccc13a5e4dea Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Thu, 4 Sep 2014 16:59:13 -0700 Subject: [PATCH 1/3] Clean warnings --- Cargo.toml | 3 +-- src/buf/byte.rs | 2 +- src/os/epoll.rs | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b7abc20f3..22f2117d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,8 +6,7 @@ authors = ["Carl Lerche "] [dependencies.nix] -# git = "https://github.com/carllerche/nix-rust" -path = "libs/nix-rust" +git = "https://github.com/carllerche/nix-rust" [[test]] diff --git a/src/buf/byte.rs b/src/buf/byte.rs index 68640836a..f1475c573 100644 --- a/src/buf/byte.rs +++ b/src/buf/byte.rs @@ -8,7 +8,7 @@ pub struct ByteBuf { } impl ByteBuf { - pub fn new(mut capacity: uint) -> ByteBuf { + pub fn new(capacity: uint) -> ByteBuf { // Handle 0 capacity case if capacity == 0 { return ByteBuf { diff --git a/src/os/epoll.rs b/src/os/epoll.rs index 5af0afd3c..1f747708b 100644 --- a/src/os/epoll.rs +++ b/src/os/epoll.rs @@ -43,7 +43,7 @@ impl Selector { impl Drop for Selector { fn drop(&mut self) { - close(self.epfd); + let _ = close(self.epfd); } } From 877c400f88e388ba3652c00330fe396c79733d45 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Thu, 4 Sep 2014 18:10:32 -0700 Subject: [PATCH 2/3] Fix a few slab bugs and test --- src/slab.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/src/slab.rs b/src/slab.rs index 34302ce87..8c7f0e561 100644 --- a/src/slab.rs +++ b/src/slab.rs @@ -39,7 +39,7 @@ impl Slab { } self.nxt = idx + 1; - self.len = idx; + self.len = idx + 1; Ok(idx) } else { @@ -85,7 +85,7 @@ impl Slab { #[inline] fn validate(&self, idx: uint) { if idx >= self.len { - fail!("invalid index"); + fail!("invalid index {} >= {}", idx, self.len); } } } @@ -97,7 +97,7 @@ impl Index for Slab { let e = self.entry(idx); - if e.nxt == 0 { + if e.nxt != 0 { fail!("invalid index"); } @@ -139,3 +139,61 @@ struct Entry { nxt: uint, // Next available slot when available, 0 when in use val: T // Value at slot } + +#[cfg(test)] +mod tests { + use super::Slab; + + #[test] + fn test_insertion() { + let mut slab = Slab::new(1); + let token = slab.insert(10u).ok().expect("Failed to insert"); + assert_eq!(slab[token], 10u); + } + + #[test] + fn test_repeated_insertion() { + let mut slab = Slab::new(10); + + for i in range(0u, 10u) { + let token = slab.insert(i + 10u).ok().expect("Failed to insert"); + assert_eq!(slab[token], i + 10u); + } + + slab.insert(20).err().expect("Inserted when full"); + } + + #[test] + fn test_repeated_insertion_and_removal() { + let mut slab = Slab::new(10); + let mut tokens = vec![]; + + for i in range(0u, 10u) { + let token = slab.insert(i + 10u).ok().expect("Failed to insert"); + tokens.push(token); + assert_eq!(slab[token], i + 10u); + } + + for &i in tokens.iter() { + slab.remove(i); + } + + slab.insert(20).ok().expect("Failed to insert in newly empty slab"); + } + + #[test] + fn test_insertion_when_full() { + let mut slab = Slab::new(1); + slab.insert(10u).ok().expect("Failed to insert"); + slab.insert(10u).err().expect("Inserted into a full slab"); + } + + #[test] + fn test_removal_is_successful() { + let mut slab = Slab::new(1); + let t1 = slab.insert(10u).ok().expect("Failed to insert"); + slab.remove(t1); + let t2 = slab.insert(20u).ok().expect("Failed to insert"); + assert_eq!(slab[t2], 20u); + } +} From 971641a0cf5d3d8343feac6616b7822987934c9e Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Thu, 4 Sep 2014 18:10:44 -0700 Subject: [PATCH 3/3] Get Echo tests compiling --- test/test_echo_server.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/test/test_echo_server.rs b/test/test_echo_server.rs index 847700dcf..a33e74f2d 100644 --- a/test/test_echo_server.rs +++ b/test/test_echo_server.rs @@ -1,5 +1,5 @@ use mio::*; -use mio::buf::RingBuf; +use mio::buf::{RingBuf, SliceBuf}; #[deriving(Show)] struct EchoConn { @@ -99,14 +99,14 @@ struct EchoClient { // Sends a message and expects to receive the same exact message, one at a time impl EchoClient { - fn new(sock: TcpSocket, msgs: Vec<&'static str>) { - let curr = msgs.remove(0); + fn new(sock: TcpSocket, mut msgs: Vec<&'static str>) -> EchoClient { + let curr = msgs.remove(0).unwrap(); EchoClient { sock: sock, msgs: msgs, - tx: SliceBuf::wrap(curr), - rx: SliceBuf::wrap(curr) + tx: SliceBuf::wrap(curr.as_bytes()), + rx: SliceBuf::wrap(curr.as_bytes()) } } @@ -133,11 +133,8 @@ impl EchoHandler { sock: srv, conns: Slab::new(128) }, - client: EchoClient { - sock: client, - msgs: msgs, - curr: None - } + + client: EchoClient::new(client, msgs) } } }