From 275959e301fa353f6c1c984091f95b7f90a1d07d Mon Sep 17 00:00:00 2001 From: ljedrz Date: Wed, 9 Sep 2020 16:29:32 +0200 Subject: [PATCH] test: add Topology::Star and use it --- tests/bitswap_cleanup.rs | 46 +++++++++++++++++--------------------- tests/common/mod.rs | 48 ++++++++++++++++++++++++++-------------- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/tests/bitswap_cleanup.rs b/tests/bitswap_cleanup.rs index 80985ba3..e1e3ed32 100644 --- a/tests/bitswap_cleanup.rs +++ b/tests/bitswap_cleanup.rs @@ -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()); } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index c8af4db2..6053606c 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -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 { + 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