Skip to content

Commit

Permalink
wip: fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iduartgomez committed Nov 6, 2023
1 parent 007ceab commit 2701b5e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
1 change: 0 additions & 1 deletion crates/core/src/node/event_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,6 @@ pub(super) mod test {

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn event_register_read_write() -> Result<(), DynError> {
crate::config::set_logger();
let event_log_path = crate::config::Config::conf().event_log();
// truncate the log if it exists
std::fs::File::create(event_log_path).unwrap();
Expand Down
17 changes: 9 additions & 8 deletions crates/core/src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,14 +766,11 @@ impl Ring {
connections_by_location: impl Iterator<Item = (Location, &'a PeerKeyLocation)>,
skip_list: &[&PeerKey],
) -> Option<connect::ConnectMsg> {
const MAX_DEVIATION_FROM_IDEAL: f64 = 0.05;

// filter possible peers by proximity to the ideal location
let acceptable_range = ideal_location.0 * MAX_DEVIATION_FROM_IDEAL;
// sort possible peers by proximity to the ideal location
let mut request_to = connections_by_location
.filter_map(|(loc, peer)| {
let dist: Distance = loc.distance(ideal_location);
let is_valid = (dist.0 < acceptable_range) && !skip_list.contains(&&peer.peer);
let is_valid = !skip_list.contains(&&peer.peer);
is_valid.then_some((dist, peer))
})
.collect::<ArrayVec<_, { Ring::MAX_CONNECTIONS }>>();
Expand Down Expand Up @@ -1037,7 +1034,7 @@ mod test {
},
),
(
Location::new(0.5),
Location::new(0.6),
PeerKeyLocation {
location: Some(Location::new(0.6)),
peer: PeerKey::random(),
Expand All @@ -1052,13 +1049,17 @@ mod test {
)
.expect("find query target");
let connect::ConnectMsg::Request {
msg: connect::ConnectRequest::FindOptimalPeer { ideal_location, .. },
msg:
connect::ConnectRequest::FindOptimalPeer {
query_target: PeerKeyLocation { location, .. },
..
},
..
} = result
else {
panic!()
};
assert_eq!(ideal_location, Location::new(0.6));
assert_eq!(location, Some(Location::new(0.6)));
}

#[test]
Expand Down
11 changes: 8 additions & 3 deletions crates/core/src/topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
collections::BTreeMap,
time::{Duration, Instant},
};
use tracing::{debug, error, info};
use tracing::{debug, error};

mod connection_evaluator;
mod request_density_tracker;
Expand Down Expand Up @@ -52,7 +52,6 @@ pub(crate) struct TopologyManager {
impl TopologyManager {
/// Create a new TopologyManager specifying the peer's own Location
pub(crate) fn new(this_peer_location: Location) -> Self {
info!("Creating a new TopologyManager instance");
TopologyManager {
slow_connection_evaluator: connection_evaluator::ConnectionEvaluator::new(
SLOW_CONNECTION_EVALUATOR_WINDOW_DURATION,
Expand Down Expand Up @@ -175,7 +174,6 @@ impl TopologyManager {
}
}

// FIXME
pub(crate) enum AcquisitionStrategy {
/// Acquire new connections slowly, be picky
Slow,
Expand All @@ -199,6 +197,13 @@ mod tests {
current_neighbors.insert(Location::new(i as f64 / 10.0), 0);
}

topology_manager
.cached_density_map
.create(
&topology_manager.request_density_tracker,
&current_neighbors,
)
.unwrap();
let mut requests = vec![];
// Simulate a bunch of random requests clustered around 0.35
for _ in 0..1000 {
Expand Down
7 changes: 5 additions & 2 deletions crates/core/src/topology/request_density_tracker/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ fn test_drop() {
}

#[test]
#[should_panic(expected = "assertion failed: !neighbors.is_empty()")]
fn test_empty_neighbors_error() {
let sw = RequestDensityTracker::new(10);
let empty_neighbors = BTreeMap::new();
let result = sw.create_density_map(&empty_neighbors);
assert!(matches!(result, Err(DensityMapError::EmptyNeighbors)));
matches!(
sw.create_density_map(&empty_neighbors),
Err(DensityMapError::EmptyNeighbors)
);
}

#[test]
Expand Down

0 comments on commit 2701b5e

Please sign in to comment.