Skip to content

Commit 96dfa44

Browse files
dht: fix flaky test
1 parent 588f62e commit 96dfa44

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

mtorrent-cli/tests/test_dht_node.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use local_async_utils::sec;
1+
use local_async_utils::{millisec, sec};
22
use mtorrent::app::dht;
33
use mtorrent_dht::Command;
44
use serde::Deserialize;
@@ -106,10 +106,13 @@ fn test_two_dht_nodes_discover_and_announce() {
106106
})
107107
.unwrap();
108108

109-
node1_cmds.blocking_send(Command::AddNode { addr: node2_addr }).unwrap();
109+
// wait for both nodes to start up and bind to their ports
110+
std::thread::sleep(sec!(1));
110111

112+
node1_cmds.blocking_send(Command::AddNode { addr: node2_addr }).unwrap();
111113
node2_cmds.blocking_send(Command::AddNode { addr: node1_addr }).unwrap();
112114

115+
// wait for the nodes to discover each other
113116
std::thread::sleep(sec!(2));
114117

115118
let (result_sender, mut result_receiver1) = mpsc::channel(10);
@@ -147,7 +150,7 @@ fn test_two_dht_nodes_discover_and_announce() {
147150
if start_time.elapsed() > sec!(5) {
148151
panic!("Timeout waiting for nodes to discover each other");
149152
}
150-
std::thread::sleep(sec!(1));
153+
std::thread::sleep(millisec!(100));
151154
}
152155

153156
assert!(discovered_by_node1.contains(&node2_addr), "Node 1 did not discover Node 2");

mtorrent-dht/src/processor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ impl Processor {
147147
}
148148

149149
fn handle_node_event(&mut self, event: NodeEvent) {
150+
log::trace!("Handling node event: {event:?}");
150151
match event {
151152
NodeEvent::Discovered(node) => {
152153
if self.node_table.can_insert(&node.id) && self.known_nodes.insert(node.addr) {

mtorrent-dht/src/tasks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fn validate_discovered_node(discovered: &Node, source: &Node, ctx: &Ctx) -> bool
3737

3838
pub type Result<T> = std::result::Result<T, Error>;
3939

40+
#[derive(Debug, Clone)]
4041
pub enum NodeEvent {
4142
Discovered(Node),
4243
Connected(Node),

0 commit comments

Comments
 (0)