Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
fix(test): account for relocations in test_startup_section_bootstrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam authored and dirvine committed Dec 9, 2020
1 parent fffc946 commit 53196a5
Showing 1 changed file with 33 additions and 42 deletions.
75 changes: 33 additions & 42 deletions tests/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use anyhow::{Error, Result};
use ed25519_dalek::Keypair;
use futures::future;
use sn_routing::{Config, Event, ELDER_SIZE};
use std::collections::HashSet;
use tokio::time;
use utils::*;

#[tokio::test]
Expand Down Expand Up @@ -66,58 +68,47 @@ async fn test_node_bootstrapping() -> Result<()> {

#[tokio::test]
async fn test_startup_section_bootstrapping() -> Result<()> {
// Create the genesis node.
let (genesis_node, mut event_stream) = create_node(Config {
first: true,
..Default::default()
})
.await?;
let other_node_count = ELDER_SIZE - 1;

// spawn genesis node events listener
let genesis_handler = tokio::spawn(async move {
let mut joined_nodes = vec![];
// expect events for all nodes
while let Some(event) = event_stream.next().await {
if let Event::MemberJoined { name, .. } = event {
joined_nodes.push(name)
}

if joined_nodes.len() == other_node_count {
break;
}
}

joined_nodes
});

// bootstrap several nodes with genesis to form a section
// Then add more nodes to form a section. Because there is only `ELDER_SIZE` nodes in total,
// we expect every one to be promoted to elder.
let genesis_contact = genesis_node.our_connection_info().await?;
let nodes_joining_tasks: Vec<_> = (0..other_node_count)
.map(|_| async {
let (node, mut event_stream) =
create_node(config_with_contact(genesis_contact)).await?;

assert_event!(event_stream, Event::PromotedToElder);

Ok::<_, Error>(node)
})
.collect();

let nodes = future::try_join_all(nodes_joining_tasks).await?;

// just await for genesis node to finish receiving all events
let joined_nodes = genesis_handler.await?;

for node in nodes {
let name = node.name().await;

// assert names of nodes joined match
assert!(joined_nodes.contains(&name));

verify_invariants_for_node(&node, ELDER_SIZE).await?;
let nodes_joining_tasks = (0..other_node_count).map(|_| async {
let (node, mut event_stream) = create_node(config_with_contact(genesis_contact)).await?;
assert_event!(event_stream, Event::PromotedToElder);
Ok::<_, Error>(node)
});
let other_nodes = future::try_join_all(nodes_joining_tasks).await?;

// Keep track of the joined nodes the genesis node knows about.
let mut joined_names = HashSet::new();

// Keep listening to the events from the genesis node until it becomes aware of all the other
// nodes in the section.
while let Some(event) = time::timeout(TIMEOUT, event_stream.next()).await? {
let _ = match event {
Event::MemberJoined { name, .. } => joined_names.insert(name),
Event::MemberLeft { name, .. } => joined_names.remove(&name),
_ => false,
};

let actual_names: HashSet<_> = future::join_all(other_nodes.iter().map(|node| node.name()))
.await
.into_iter()
.collect();

if joined_names == actual_names {
return Ok(());
}
}

Ok(())
panic!("event stream unexpectedly closed")
}

// Test that the first `ELDER_SIZE` nodes in the network are promoted to elders.
Expand Down

0 comments on commit 53196a5

Please sign in to comment.