Skip to content

Commit

Permalink
tests: add a crawling test
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Mar 9, 2022
1 parent 4117648 commit 4a694f5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .crawler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ version = "1"

[dependencies.tracing]
version = "0.1"

[dev-dependencies.snarkos-integration]
path = "../.integration"
version = "2.0.2"

[dev-dependencies.tracing-subscriber]
version = "0.3"
76 changes: 76 additions & 0 deletions .crawler/tests/peering.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (C) 2019-2022 Aleo Systems Inc.
// This file is part of the snarkOS library.

// The snarkOS library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The snarkOS library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.

use std::time::{Duration, Instant};

use pea2pea::Pea2Pea;
use snarkos_crawler::crawler::{Crawler, Opts};
use snarkos_integration::TestNode;

const NUM_NODES: usize = 10;

#[tokio::test]
async fn basics() {
// tracing_subscriber::fmt::init();

// Prepare some collections we'll be using.
let mut test_nodes = Vec::with_capacity(NUM_NODES);
let mut test_node_addrs = Vec::with_capacity(NUM_NODES);

// Start the test nodes.
for _ in 0..NUM_NODES {
let test_node = TestNode::default().await;
let test_node_addr = test_node.node().listening_addr().unwrap();

test_nodes.push(test_node);
test_node_addrs.push(test_node_addr);
}

// Connect the test nodes into a linear topology.
for (node, prev_node_addr) in test_nodes.iter().skip(1).zip(&test_node_addrs) {
node.node().connect(*prev_node_addr).await.unwrap();
}

// Double-check the topology.
for (i, node) in test_nodes.iter().enumerate() {
if i == 0 || i == NUM_NODES - 1 {
assert_eq!(node.node().num_connected(), 1);
} else {
assert_eq!(node.node().num_connected(), 2);
}
}

// Start the crawler.
let opts = Opts {
node: "127.0.0.1:0".parse().unwrap(),
};
let crawler = Crawler::new(opts).await;

// "Seed" the crawler with the address of the first node.
crawler.known_network.add_node(test_node_addrs[0]);

// Initialize the crawler.
crawler.run_periodic_tasks();

let now = Instant::now();
loop {
if crawler.node().num_connected() == NUM_NODES {
break;
}
tokio::time::sleep(Duration::from_millis(1)).await;
assert!(now.elapsed() <= Duration::from_secs(5), "timed out!");
}
}

0 comments on commit 4a694f5

Please sign in to comment.