From a4d8b304d490017c61c2c3c7a01311e356120991 Mon Sep 17 00:00:00 2001 From: Jerome Froelich Date: Sun, 12 Nov 2023 13:04:11 -0500 Subject: [PATCH 1/3] Implement Hash for VNode in tests --- src/lib.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index db6c7fe..4b8596d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -290,24 +290,30 @@ where #[cfg(test)] mod tests { - use std::net::{IpAddr, SocketAddr}; + use std::net::{Ipv4Addr, SocketAddrV4}; use std::str::FromStr; use super::HashRing; - #[derive(Debug, Copy, Clone, Hash, PartialEq)] + #[derive(Debug, Copy, Clone, PartialEq)] struct VNode { id: usize, - addr: SocketAddr, + addr: SocketAddrV4, } impl VNode { fn new(ip: &str, port: u16, id: usize) -> Self { - let addr = SocketAddr::new(IpAddr::from_str(ip).unwrap(), port); + let addr = SocketAddrV4::new(Ipv4Addr::from_str(ip).unwrap(), port); VNode { id, addr } } } + impl std::hash::Hash for VNode { + fn hash(&self, s: &mut H) { + (self.id, self.addr.port(), self.addr.ip()).hash(s) + } + } + #[test] fn add_and_remove_nodes() { let mut ring: HashRing = HashRing::new(); @@ -362,17 +368,17 @@ mod tests { ring.add(vnode5); ring.add(vnode6); - assert_eq!(ring.get(&"foo"), Some(&vnode5)); - assert_eq!(ring.get(&"bar"), Some(&vnode3)); - assert_eq!(ring.get(&"baz"), Some(&vnode5)); + assert_eq!(ring.get(&"foo"), Some(&vnode6)); + assert_eq!(ring.get(&"bar"), Some(&vnode5)); + assert_eq!(ring.get(&"baz"), Some(&vnode4)); - assert_eq!(ring.get(&"abc"), Some(&vnode2)); - assert_eq!(ring.get(&"def"), Some(&vnode2)); + assert_eq!(ring.get(&"abc"), Some(&vnode1)); + assert_eq!(ring.get(&"def"), Some(&vnode1)); assert_eq!(ring.get(&"ghi"), Some(&vnode6)); - assert_eq!(ring.get(&"cat"), Some(&vnode1)); - assert_eq!(ring.get(&"dog"), Some(&vnode5)); - assert_eq!(ring.get(&"bird"), Some(&vnode5)); + assert_eq!(ring.get(&"cat"), Some(&vnode5)); + assert_eq!(ring.get(&"dog"), Some(&vnode4)); + assert_eq!(ring.get(&"bird"), Some(&vnode4)); // at least each node as a key let mut nodes = vec![0; 6]; @@ -424,12 +430,12 @@ mod tests { assert_eq!( ring.get_with_replicas(&"bar", 2).unwrap(), - vec![vnode6, vnode5, vnode2] + vec![vnode3, vnode1, vnode2] ); assert_eq!( ring.get_with_replicas(&"foo", 4).unwrap(), - vec![vnode3, vnode1, vnode6, vnode5, vnode2] + vec![vnode5, vnode4, vnode3, vnode1, vnode2] ); } @@ -456,7 +462,7 @@ mod tests { assert_eq!( ring.get_with_replicas(&"bar", 20).unwrap(), - vec![vnode6, vnode5, vnode2, vnode4, vnode3, vnode1], + vec![vnode3, vnode1, vnode2, vnode6, vnode5, vnode4], "too high of replicas causes the count to shrink to ring length" ); } @@ -477,8 +483,8 @@ mod tests { let mut iter = ring.into_iter(); - assert_eq!(Some(vnode1), iter.next()); assert_eq!(Some(vnode3), iter.next()); + assert_eq!(Some(vnode1), iter.next()); assert_eq!(Some(vnode2), iter.next()); assert_eq!(None, iter.next()); } From eb1bb791ed8a3e6c6633b8da11e43da71f39c2d6 Mon Sep 17 00:00:00 2001 From: Jerome Froelich Date: Sun, 12 Nov 2023 17:03:56 -0500 Subject: [PATCH 2/3] Fix lint --- src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4b8596d..ca2486a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,6 +84,7 @@ extern crate siphasher; use siphasher::sip::SipHasher; use std::cmp::Ordering; +use std::collections::hash_map::RandomState; use std::fmt::Debug; use std::hash::BuildHasher; use std::hash::{Hash, Hasher}; @@ -283,9 +284,7 @@ where S: BuildHasher, T: Hash, { - let mut hasher = hash_builder.build_hasher(); - input.hash(&mut hasher); - hasher.finish() + hash_builder.hash_one(input) } #[cfg(test)] From 31c24d25e8e90c76f97aa40ceeb8110484100eb3 Mon Sep 17 00:00:00 2001 From: Jerome Froelich Date: Sun, 12 Nov 2023 17:05:46 -0500 Subject: [PATCH 3/3] Fix lints --- src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ca2486a..b7bcbe1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,10 +84,9 @@ extern crate siphasher; use siphasher::sip::SipHasher; use std::cmp::Ordering; -use std::collections::hash_map::RandomState; use std::fmt::Debug; use std::hash::BuildHasher; -use std::hash::{Hash, Hasher}; +use std::hash::Hash; pub struct DefaultHashBuilder; @@ -289,6 +288,8 @@ where #[cfg(test)] mod tests { + use std::hash::Hash; + use std::hash::Hasher; use std::net::{Ipv4Addr, SocketAddrV4}; use std::str::FromStr; @@ -307,8 +308,8 @@ mod tests { } } - impl std::hash::Hash for VNode { - fn hash(&self, s: &mut H) { + impl Hash for VNode { + fn hash(&self, s: &mut H) { (self.id, self.addr.port(), self.addr.ip()).hash(s) } }