Skip to content

Commit

Permalink
fix: deterministic location generation in the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iduartgomez committed Jan 3, 2025
1 parent 7e0ffab commit 956d0cf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ dashmap = "^6.1"
either = "1"
futures = "0.3"
rand = { version = "0.8" }
rand_chacha = "0.8"
semver = { version = "1", features = ["serde"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
52 changes: 40 additions & 12 deletions crates/core/src/ring/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ pub struct Location(pub(crate) f64);
impl Location {
#[cfg(all(not(feature = "local-simulation"), not(test)))]
pub fn from_address(addr: &std::net::SocketAddr) -> Self {
Self::deterministic_loc(addr)
}

#[cfg(any(feature = "local-simulation", test))]
pub fn from_address(_addr: &std::net::SocketAddr) -> Self {
let random_component: f64 = rand::random();
Location(random_component)
}

#[allow(unused)]
fn deterministic_loc(addr: &std::net::SocketAddr) -> Self {
match addr.ip() {
std::net::IpAddr::V4(ipv4) => {
let octets = ipv4.octets();
Expand All @@ -30,12 +41,6 @@ impl Location {
}
}

#[cfg(any(feature = "local-simulation", test))]
pub fn from_address(_addr: &std::net::SocketAddr) -> Self {
let random_component: f64 = rand::random();
Location(random_component)
}

pub fn new(location: f64) -> Self {
debug_assert!(
(0.0..=1.0).contains(&location),
Expand Down Expand Up @@ -220,24 +225,30 @@ impl Display for Distance {

#[cfg(test)]
mod test {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use super::*;

#[test]
fn test_ipv4_address_location() {
fn test_ipv4_address_location_distribution() {
use rand::prelude::*;
use rand::rngs::StdRng;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

// Use seeded RNG for reproducible tests
let mut rng = StdRng::seed_from_u64(12345);

// Generate 100 random IP addresses with seeded RNG
let locations: Vec<f64> = (0..100)
// Generate 100 random IP addresses wih seeded RNG
let ips = (0..100)
.map(|_| {
let ip = Ipv4Addr::new(rng.gen(), rng.gen(), rng.gen(), rng.gen());
let addr = SocketAddr::new(IpAddr::V4(ip), 12345);
Location::from_address(&addr).0
SocketAddr::new(IpAddr::V4(ip), 12345)
})
.collect::<Vec<_>>();

// Compute locations for each IP address
let locations: Vec<f64> = ips
.into_iter()
.map(|addr| Location::deterministic_loc(&addr).0)
.collect();

// Verify all locations are between 0 and 1
Expand Down Expand Up @@ -271,6 +282,23 @@ mod test {
);
}

#[test]
fn test_ipv4_address_location() {
let addresses = [
SocketAddr::new(IpAddr::V4([86, 38, 75, 158].into()), 12345),
SocketAddr::new(IpAddr::V4([103, 169, 0, 130].into()), 12345),
SocketAddr::new(IpAddr::V4([20, 5, 226, 4].into()), 12345),
];

let locations: Vec<Location> = addresses.iter().map(Location::deterministic_loc).collect();
let expected_locations = vec![
Location(0.336521824390997),
Location(0.40492250948682484),
Location(0.07821476925699528),
];
assert_eq!(locations, expected_locations);
}

#[test]
fn location_dist() {
let l0 = Location(0.);
Expand Down

0 comments on commit 956d0cf

Please sign in to comment.