test: add Topology::Star and use it

This commit is contained in:
ljedrz 2020-09-09 16:29:32 +02:00
parent cdc07e93c6
commit 275959e301
2 changed files with 51 additions and 43 deletions

View File

@ -1,39 +1,33 @@
use ipfs::Node;
use std::time::Duration;
use tokio::time;
async fn wait(millis: u64) {
time::delay_for(std::time::Duration::from_millis(millis)).await;
}
mod common;
use common::{spawn_connected_nodes, Topology};
// Ensure that the Bitswap object doesn't leak.
#[tokio::test(max_threads = 1)]
async fn check_bitswap_cleanups() {
// create a few nodes
let node_a = Node::new("a").await;
let node_b = Node::new("b").await;
let node_c = Node::new("c").await;
// create a few nodes and connect the first one to others
let mut nodes = spawn_connected_nodes(3, Topology::Star).await;
// connect node a to node b...
let (_, mut b_addrs) = node_b.identity().await.unwrap();
node_a.connect(b_addrs.pop().unwrap()).await.unwrap();
let bitswap_peers = node_a.get_bitswap_peers().await.unwrap();
assert_eq!(bitswap_peers.len(), 1);
// ...and to node c
let (_, mut c_addrs) = node_c.identity().await.unwrap();
node_a.connect(c_addrs.pop().unwrap()).await.unwrap();
let bitswap_peers = node_a.get_bitswap_peers().await.unwrap();
let bitswap_peers = nodes[0].get_bitswap_peers().await.unwrap();
assert_eq!(bitswap_peers.len(), 2);
// node b says goodbye; check the number of bitswap peers
node_b.shutdown().await;
wait(200).await;
let bitswap_peers = node_a.get_bitswap_peers().await.unwrap();
// last node says goodbye; check the number of bitswap peers
if let Some(node) = nodes.pop() {
node.shutdown().await;
time::delay_for(Duration::from_millis(200)).await;
}
let bitswap_peers = nodes[0].get_bitswap_peers().await.unwrap();
assert_eq!(bitswap_peers.len(), 1);
// node c says goodbye; check the number of bitswap peers
node_c.shutdown().await;
wait(200).await;
let bitswap_peers = node_a.get_bitswap_peers().await.unwrap();
// another node says goodbye; check the number of bitswap peers
if let Some(node) = nodes.pop() {
node.shutdown().await;
time::delay_for(Duration::from_millis(200)).await;
}
let bitswap_peers = nodes[0].get_bitswap_peers().await.unwrap();
assert!(bitswap_peers.is_empty());
}

View File

@ -2,12 +2,18 @@ pub mod interop;
use ipfs::Node;
/// The way in which nodes are connected to each other; to be used with spawn_connected_nodes.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Topology {
/// a > b > c
Line,
/// a > b > c > a
Ring,
/// a <> b <> c <> a
Mesh,
/// a > b, a > c
Star,
}
#[allow(dead_code)]
@ -19,27 +25,35 @@ pub async fn spawn_connected_nodes(count: usize, topology: Topology) -> Vec<Node
nodes.push(node);
}
if topology != Topology::Mesh {
for i in 0..(count - 1) {
nodes[i]
.connect(nodes[i + 1].addrs[0].clone())
.await
.unwrap();
match topology {
Topology::Line | Topology::Ring => {
for i in 0..(count - 1) {
nodes[i]
.connect(nodes[i + 1].addrs[0].clone())
.await
.unwrap();
}
if topology == Topology::Ring {
nodes[count - 1]
.connect(nodes[0].addrs[0].clone())
.await
.unwrap();
}
}
if topology == Topology::Ring {
nodes[count - 1]
.connect(nodes[0].addrs[0].clone())
.await
.unwrap();
}
} else {
for i in 0..count {
for (j, peer) in nodes.iter().enumerate() {
if i != j {
nodes[i].connect(peer.addrs[0].clone()).await.unwrap();
Topology::Mesh => {
for i in 0..count {
for (j, peer) in nodes.iter().enumerate() {
if i != j {
nodes[i].connect(peer.addrs[0].clone()).await.unwrap();
}
}
}
}
Topology::Star => {
for node in nodes.iter().skip(1) {
nodes[0].connect(node.addrs[0].clone()).await.unwrap();
}
}
}
nodes