More tests

This commit is contained in:
arkpar 2016-02-16 21:25:01 +01:00
parent 4f73d63f90
commit fbe06d3f2f
2 changed files with 27 additions and 5 deletions

View File

@ -445,13 +445,13 @@ impl Discovery {
Ok(Some(TableUpdates { added: added, removed: HashSet::new() })) Ok(Some(TableUpdates { added: added, removed: HashSet::new() }))
} }
fn check_expired(&mut self) -> HashSet<NodeId> { fn check_expired(&mut self, force: bool) -> HashSet<NodeId> {
let now = time::precise_time_ns(); let now = time::precise_time_ns();
let mut removed: HashSet<NodeId> = HashSet::new(); let mut removed: HashSet<NodeId> = HashSet::new();
for bucket in &mut self.node_buckets { for bucket in &mut self.node_buckets {
bucket.nodes.retain(|node| { bucket.nodes.retain(|node| {
if let Some(timeout) = node.timeout { if let Some(timeout) = node.timeout {
if now - timeout < PING_TIMEOUT_MS * 1000_0000 { if !force && now - timeout < PING_TIMEOUT_MS * 1000_0000 {
true true
} }
else { else {
@ -466,7 +466,7 @@ impl Discovery {
} }
pub fn round(&mut self) -> Option<TableUpdates> { pub fn round(&mut self) -> Option<TableUpdates> {
let removed = self.check_expired(); let removed = self.check_expired(false);
self.discover(); self.discover();
if !removed.is_empty() { if !removed.is_empty() {
Some(TableUpdates { added: HashMap::new(), removed: removed }) Some(TableUpdates { added: HashMap::new(), removed: removed })
@ -535,4 +535,16 @@ mod tests {
} }
assert_eq!(Discovery::nearest_node_entries(&NodeId::new(), &discovery2.node_buckets).len(), 3) 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);
}
} }

View File

@ -987,3 +987,13 @@ fn load_key(path: &Path) -> Option<Secret> {
} }
} }
} }
#[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());
}