Skip to content

Commit

Permalink
Merge pull request #22 from jeromefroe/jerome/implement-hash-for-tests
Browse files Browse the repository at this point in the history
Implement Hash for VNode in tests
  • Loading branch information
jeromefroe authored Nov 12, 2023
2 parents d4f749f + 31c24d2 commit 1f2b7ad
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ use siphasher::sip::SipHasher;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::hash::BuildHasher;
use std::hash::{Hash, Hasher};
use std::hash::Hash;

pub struct DefaultHashBuilder;

Expand Down Expand Up @@ -283,31 +283,37 @@ 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)]
mod tests {
use std::net::{IpAddr, SocketAddr};
use std::hash::Hash;
use std::hash::Hasher;
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 Hash for VNode {
fn hash<H: Hasher>(&self, s: &mut H) {
(self.id, self.addr.port(), self.addr.ip()).hash(s)
}
}

#[test]
fn add_and_remove_nodes() {
let mut ring: HashRing<VNode> = HashRing::new();
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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]
);
}

Expand All @@ -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"
);
}
Expand All @@ -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());
}
Expand Down

0 comments on commit 1f2b7ad

Please sign in to comment.