From fbe06d3f2f85275cc6ec4c9c3b17344a3778def0 Mon Sep 17 00:00:00 2001 From: arkpar Date: Tue, 16 Feb 2016 21:25:01 +0100 Subject: [PATCH] More tests --- util/src/network/discovery.rs | 22 +++++++++++++++++----- util/src/network/host.rs | 10 ++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/util/src/network/discovery.rs b/util/src/network/discovery.rs index c15dbbbc4..0064a34db 100644 --- a/util/src/network/discovery.rs +++ b/util/src/network/discovery.rs @@ -445,13 +445,13 @@ impl Discovery { Ok(Some(TableUpdates { added: added, removed: HashSet::new() })) } - fn check_expired(&mut self) -> HashSet { + fn check_expired(&mut self, force: bool) -> HashSet { let now = time::precise_time_ns(); let mut removed: HashSet = HashSet::new(); for bucket in &mut self.node_buckets { bucket.nodes.retain(|node| { if let Some(timeout) = node.timeout { - if now - timeout < PING_TIMEOUT_MS * 1000_0000 { + if !force && now - timeout < PING_TIMEOUT_MS * 1000_0000 { true } else { @@ -466,7 +466,7 @@ impl Discovery { } pub fn round(&mut self) -> Option { - let removed = self.check_expired(); + let removed = self.check_expired(false); self.discover(); if !removed.is_empty() { Some(TableUpdates { added: HashMap::new(), removed: removed }) @@ -512,8 +512,8 @@ mod tests { let node1 = Node::from_str("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@127.0.0.1:7770").unwrap(); let node2 = Node::from_str("enode://b979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@127.0.0.1:7771").unwrap(); - discovery1.add_node(NodeEntry { id: node1.id.clone(), endpoint: node1. endpoint.clone() }); - discovery1.add_node(NodeEntry { id: node2.id.clone(), endpoint: node2. endpoint.clone() }); + discovery1.add_node(NodeEntry { id: node1.id.clone(), endpoint: node1.endpoint.clone() }); + discovery1.add_node(NodeEntry { id: node2.id.clone(), endpoint: node2.endpoint.clone() }); discovery2.add_node(NodeEntry { id: key1.public().clone(), endpoint: ep1.clone() }); discovery2.refresh(); @@ -535,4 +535,16 @@ mod tests { } assert_eq!(Discovery::nearest_node_entries(&NodeId::new(), &discovery2.node_buckets).len(), 3) } + + #[test] + fn removes_expired() { + let key = KeyPair::create().unwrap(); + let ep = NodeEndpoint { address: SocketAddr::from_str("127.0.0.1:40446").unwrap(), udp_port: 40444 }; + let mut discovery = Discovery::new(&key, ep.address.clone(), ep.clone(), 0); + for _ in 0..1200 { + discovery.add_node(NodeEntry { id: NodeId::random(), endpoint: ep.clone() }); + } + let removed = discovery.check_expired(true).len(); + assert!(removed > 0); + } } diff --git a/util/src/network/host.rs b/util/src/network/host.rs index 4afb790ae..080a8b5cc 100644 --- a/util/src/network/host.rs +++ b/util/src/network/host.rs @@ -987,3 +987,13 @@ fn load_key(path: &Path) -> Option { } } } + +#[test] +fn key_save_load() { + use tests::helpers::RandomTempPath; + let temp_path = RandomTempPath::create_dir(); + let key = H256::random(); + save_key(temp_path.as_path(), &key); + let r = load_key(temp_path.as_path()); + assert_eq!(key, r.unwrap()); +}