From 15bbc9babb39f39b650feb6d7f7b916246e3d072 Mon Sep 17 00:00:00 2001 From: Peter Bjorklund Date: Fri, 12 Apr 2024 16:07:16 +0200 Subject: [PATCH] feat: set_debug_name --- crates/session/src/lib.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/session/src/lib.rs b/crates/session/src/lib.rs index 4032397..fa64386 100644 --- a/crates/session/src/lib.rs +++ b/crates/session/src/lib.rs @@ -62,11 +62,12 @@ pub struct Connection { pub state: ConnectionState, pub last_reported_term: Option, pub has_connection_host: ConnectionToLeader, + pub debug_name: Option, } impl fmt::Display for Connection { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "[conn id:{}, knowledge:{}, connectedToHost:{:?}, knownTerm:{:?}, quality:{}]", self.id, self.knowledge, self.has_connection_host, self.last_reported_term, self.quality) + write!(f, "[conn id:{} (name:{:?}) knowledge:{}, connectedToHost:{:?}, knownTerm:{:?}, quality:{}]", self.id, self.debug_name, self.knowledge, self.has_connection_host, self.last_reported_term, self.quality) } } @@ -83,6 +84,7 @@ impl Connection { quality: ConnectionQuality::new(pings_per_second_threshold, time), knowledge: Knowledge(0), state: ConnectionState::Online, + debug_name: None, } } @@ -256,7 +258,6 @@ impl Room { } let leader_connection = self.connections.get(&self.leader_index.unwrap()).unwrap(); - if leader_connection.assessment() == QualityAssessment::RecommendDisconnect && self.is_possible_to_switch_leader() { @@ -416,12 +417,17 @@ impl Room { } self.connections.remove(&connection_index); } + + pub fn set_debug_name(&mut self, connection_index: ConnectionIndex, name: &str) { + self.connections.get_mut(&connection_index).unwrap().debug_name = Some(name.to_string()); + } } #[cfg(test)] mod tests { use std::time::{Duration, Instant}; + use log::info; use test_log::test; use conclave_types::{ConnectionToLeader, Knowledge, Term}; @@ -648,4 +654,13 @@ mod tests { assert_eq!(room.connection_knows_about_current_term(connection_id), true); } + + #[test] + fn check_set_debug_name() { + let mut room = Room::new(); + let now = Instant::now(); + let connection_id = room.create_connection(now); + room.set_debug_name(connection_id, "Hello"); + info!("connection: {}", room.get(connection_id)) + } }