refactor: improve node init in some tests

Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
ljedrz 2020-07-30 15:59:14 +02:00
parent e0d43ed861
commit 0abe1b4aa4
5 changed files with 28 additions and 69 deletions

View File

@ -626,14 +626,14 @@ mod tests {
use super::{ipld_links, local, refs_paths, Edge, IpfsPath};
use cid::{self, Cid};
use futures::stream::TryStreamExt;
use ipfs::{Block, Ipfs};
use ipfs::{Block, Node};
use libipld::block::{decode_ipld, validate};
use std::collections::HashSet;
use std::convert::TryFrom;
#[tokio::test]
async fn test_inner_local() {
let filter = local(&preloaded_testing_ipfs().await);
let filter = local(&*preloaded_testing_ipfs().await);
let response = warp::test::request()
.path("/refs/local")
@ -680,7 +680,10 @@ mod tests {
#[tokio::test]
async fn all_refs_from_root() {
let ipfs = preloaded_testing_ipfs().await;
let Node {
ipfs,
background_task: _bt,
} = preloaded_testing_ipfs().await;
let (root, dag0, unixfs0, dag1, unixfs1) = (
// this is the dag with content: [dag0, unixfs0, dag1, unixfs1]
@ -721,7 +724,10 @@ mod tests {
#[tokio::test]
async fn all_unique_refs_from_root() {
let ipfs = preloaded_testing_ipfs().await;
let Node {
ipfs,
background_task: _bt,
} = preloaded_testing_ipfs().await;
let (root, dag0, unixfs0, dag1, unixfs1) = (
// this is the dag with content: [dag0, unixfs0, dag1, unixfs1]
@ -805,13 +811,8 @@ mod tests {
assert!(diff.is_empty(), "{:#?}", diff);
}
async fn preloaded_testing_ipfs() -> Ipfs<ipfs::TestTypes> {
let options = ipfs::IpfsOptions::inmemory_with_generated_keys("test_node");
let (ipfs, _) = ipfs::UninitializedIpfs::new(options)
.await
.start()
.await
.unwrap();
async fn preloaded_testing_ipfs() -> Node {
let ipfs = Node::new("test_node").await;
let blocks = [
(

View File

@ -282,7 +282,7 @@ impl std::error::Error for GetError {
mod tests {
use futures::stream::{FuturesOrdered, TryStreamExt};
use hex_literal::hex;
use ipfs::{Block, Ipfs, IpfsTypes};
use ipfs::{Block, Ipfs, IpfsTypes, Node};
use libipld::cid::Cid;
use multihash::Sha2_256;
use std::convert::TryFrom;
@ -334,12 +334,7 @@ mod tests {
#[tokio::test]
async fn very_long_file_and_symlink_names() {
let options = ipfs::IpfsOptions::inmemory_with_generated_keys("test_node");
let (ipfs, _) = ipfs::UninitializedIpfs::new(options)
.await
.start()
.await
.unwrap();
let ipfs = Node::new("test_node").await;
let blocks: &[&[u8]] = &[
// the root, QmdKuCuXDuVTsnGpzPgZEuJmiCEn6LZhGHHHwWPQH28DeD
@ -395,12 +390,7 @@ mod tests {
#[tokio::test]
async fn get_multiblock_file() {
let options = ipfs::IpfsOptions::inmemory_with_generated_keys("test_node");
let (ipfs, _) = ipfs::UninitializedIpfs::new(options)
.await
.start()
.await
.unwrap();
let ipfs = Node::new("test_node").await;
let blocks: &[&[u8]] = &[
// the root, QmRJHYTNvC3hmd9gJQARxLR1QMEincccBV53bBw524yyq6

View File

@ -169,7 +169,7 @@ mod tests {
#[tokio::test]
async fn add_single_block_file() {
let ipfs = testing_ipfs().await;
let ipfs = tokio_ipfs().await;
// this is from interface-ipfs-core, pretty much simplest add a buffer test case
// but the body content is from the pubsub test case I copied this from
@ -198,7 +198,7 @@ mod tests {
);
}
async fn testing_ipfs() -> ipfs::Ipfs<ipfs::TestTypes> {
async fn tokio_ipfs() -> ipfs::Ipfs<ipfs::TestTypes> {
let options = ipfs::IpfsOptions::inmemory_with_generated_keys("test_node");
let (ipfs, fut) = ipfs::UninitializedIpfs::new(options)
.await

View File

@ -971,8 +971,8 @@ mod node {
/// Node encapsulates everything to setup a testing instance so that multi-node tests become
/// easier.
pub struct Node {
ipfs: Ipfs<TestTypes>,
background_task: async_std::task::JoinHandle<()>,
pub ipfs: Ipfs<TestTypes>,
pub background_task: async_std::task::JoinHandle<()>,
}
impl Node {

View File

@ -1,44 +1,19 @@
use async_std::task;
use ipfs::Node;
// Make sure two instances of ipfs can be connected.
#[async_std::test]
async fn connect_two_nodes() {
let (tx, rx) = futures::channel::oneshot::channel();
let node_a = Node::new("a").await;
let node_b = Node::new("b").await;
let node_a = task::spawn(async move {
let opts = ipfs::IpfsOptions::inmemory_with_generated_keys("test_node");
let (ipfs, fut) = ipfs::UninitializedIpfs::new(opts)
.await
.start()
.await
.unwrap();
let (_, b_addrs) = node_b.identity().await.unwrap();
assert!(!b_addrs.is_empty());
let jh = task::spawn(fut);
let (pk, addrs) = ipfs
.identity()
.await
.expect("failed to read identity() on node_a");
assert!(!addrs.is_empty());
tx.send((pk, addrs, ipfs, jh)).unwrap();
});
let (_other_pk, other_addrs, other_ipfs, other_jh) = rx.await.unwrap();
println!("got back from the other node: {:?}", other_addrs);
let opts = ipfs::IpfsOptions::inmemory_with_generated_keys("test_node");
let (ipfs, fut) = ipfs::UninitializedIpfs::new(opts)
.await
.start()
.await
.unwrap();
let jh = task::spawn(fut);
let mut connected = None;
for addr in other_addrs {
for addr in b_addrs {
println!("trying {}", addr);
match ipfs.connect(addr.clone()).await {
match node_a.connect(addr.clone()).await {
Ok(_) => {
connected = Some(addr);
break;
@ -51,21 +26,14 @@ async fn connect_two_nodes() {
let connected = connected.expect("Failed to connect to anything");
println!("connected to {}", connected);
other_ipfs.exit_daemon().await;
other_jh.await;
node_a.await;
ipfs.exit_daemon().await;
jh.await;
}
// More complicated one to the above; first node will have two listening addresses and the second
// one should dial both of the addresses, resulting in two connections.
#[async_std::test]
async fn connect_two_nodes_with_two_connections_doesnt_panic() {
let node_a = ipfs::Node::new("a").await;
let node_b = ipfs::Node::new("b").await;
let node_a = Node::new("a").await;
let node_b = Node::new("b").await;
node_a
.add_listening_address(libp2p::build_multiaddr!(Ip4([127, 0, 0, 1]), Tcp(0u16)))