Skip to content

Commit

Permalink
Merge rs-ipfs#265
Browse files Browse the repository at this point in the history
265: Use async macro for tests r=ljedrz a=c410-f3r

Fixes rs-ipfs#248 

Co-authored-by: Caio <c410.f3r@gmail.com>
  • Loading branch information
bors[bot] and c410-f3r committed Jul 27, 2020
2 parents 6bee0b5 + 1107b29 commit 1367a30
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 225 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions http/Cargo.toml
Expand Up @@ -10,6 +10,7 @@ prost-build = "0.6.1"
vergen = "3.1.0"

[dependencies]
async-std = { default-features = false, features = ["attributes"], version = "1.6" }
base64 = "0.12.0"
cid = "0.5"
env_logger = "0.7.1"
Expand Down
47 changes: 24 additions & 23 deletions http/src/v0/pubsub.rs
Expand Up @@ -543,7 +543,6 @@ impl<'a> Iterator for QueryAsRawPartsParser<'a> {
#[cfg(test)]
mod tests {
use super::{publish_args, PublishArgs};
use futures::executor::block_on;
use futures::future::ready;
use std::str;
use warp::reply::json;
Expand All @@ -561,32 +560,34 @@ mod tests {
})
}

#[test]
fn url_hacked_args() {
let response = block_on(
request()
.path("/pubsub/pub?arg=some_channel&arg=foobar")
.reply(&publish_args_as_json("arg")),
);

#[async_std::test]
async fn url_hacked_args() {
let response = request()
.path("/pubsub/pub?arg=some_channel&arg=foobar")
.reply(&publish_args_as_json("arg"))
.await;
let body = str::from_utf8(response.body()).unwrap();
assert_eq!(body, r#"{"message":"foobar","topic":"some_channel"}"#);
}

#[test]
fn message_in_body() {
let response = block_on(
request()
.path("/pubsub/pub?arg=some_channel")
.header("content-type", "multipart/form-data; boundary=-----------------------------Z0oYi6XyTm7_x2L4ty8JL")
.body(&b"-------------------------------Z0oYi6XyTm7_x2L4ty8JL\r\n\
Content-Disposition: form-data; name=\"file\"; filename=\"\"\r\n\
Content-Type: application/octet-stream\r\n\
\r\n\
aedFIxDJZ2jS1eVB6Pkbv\
\r\n-------------------------------Z0oYi6XyTm7_x2L4ty8JL--\r\n"[..])
.reply(&publish_args_as_json("arg")),
);
#[async_std::test]
async fn message_in_body() {
let response = request()
.path("/pubsub/pub?arg=some_channel")
.header(
"content-type",
"multipart/form-data; boundary=-----------------------------Z0oYi6XyTm7_x2L4ty8JL",
)
.body(
&b"-------------------------------Z0oYi6XyTm7_x2L4ty8JL\r\n\
Content-Disposition: form-data; name=\"file\"; filename=\"\"\r\n\
Content-Type: application/octet-stream\r\n\
\r\n\
aedFIxDJZ2jS1eVB6Pkbv\
\r\n-------------------------------Z0oYi6XyTm7_x2L4ty8JL--\r\n"[..],
)
.reply(&publish_args_as_json("arg"))
.await;

let body = str::from_utf8(response.body()).unwrap();
assert_eq!(
Expand Down
7 changes: 3 additions & 4 deletions src/p2p/swarm.rs
Expand Up @@ -231,16 +231,15 @@ fn connection_point_addr(cp: &ConnectedPoint) -> &Multiaddr {
mod tests {
use super::*;
use crate::p2p::transport::{build_transport, TTransport};
use async_std::task;
use futures::channel::mpsc;
use futures::future::{select, FutureExt};
use futures::sink::SinkExt;
use futures::stream::StreamExt;
use libp2p::identity::Keypair;
use libp2p::swarm::Swarm;

#[test]
fn swarm_api() {
#[async_std::test]
async fn swarm_api() {
env_logger::init();

let (peer1_id, trans) = mk_transport();
Expand Down Expand Up @@ -277,7 +276,7 @@ mod tests {
};

let result = select(Box::pin(peer1), Box::pin(peer2));
task::block_on(result);
result.await;
}

fn mk_transport() -> (PeerId, TTransport) {
Expand Down
171 changes: 81 additions & 90 deletions tests/connect_two.rs
@@ -1,10 +1,8 @@
use async_std::task;

/// Make sure two instances of ipfs can be connected.
#[test]
fn connect_two_nodes() {
// env_logger::init();

// 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 = task::spawn(async move {
Expand All @@ -25,96 +23,89 @@ fn connect_two_nodes() {
tx.send((pk, addrs, ipfs, jh)).unwrap();
});

task::block_on(async move {
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();
let (ipfs, fut) = ipfs::UninitializedIpfs::new(opts)
.await
.start()
.await
.unwrap();
let jh = task::spawn(fut);

let _other_peerid = other_pk.into_peer_id();

let mut connected = None;

for addr in other_addrs {
println!("trying {}", addr);
match ipfs.connect(addr.clone()).await {
Ok(_) => {
connected = Some(addr);
break;
}
Err(e) => {
println!("Failed connecting to {}: {}", addr, e);
}
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();
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 {
println!("trying {}", addr);
match ipfs.connect(addr.clone()).await {
Ok(_) => {
connected = Some(addr);
break;
}
Err(e) => {
println!("Failed connecting to {}: {}", addr, e);
}
}
}

let connected = connected.expect("Failed to connect to anything");
println!("connected to {}", connected);
let connected = connected.expect("Failed to connect to anything");
println!("connected to {}", connected);

other_ipfs.exit_daemon().await;
other_jh.await;
node_a.await;
other_ipfs.exit_daemon().await;
other_jh.await;
node_a.await;

ipfs.exit_daemon().await;
jh.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.
#[test]
fn connect_two_nodes_with_two_connections_doesnt_panic() {
task::block_on(async move {
let node_a = ipfs::Node::new().await;
let node_b = ipfs::Node::new().await;

node_a
.add_listening_address(libp2p::build_multiaddr!(Ip4([127, 0, 0, 1]), Tcp(0u16)))
.await
.unwrap();

let addresses = node_a.addrs_local().await.unwrap();
assert_eq!(
addresses.len(),
2,
"there should had been two local addresses, found {:?}",
addresses
);

for addr in addresses {
node_b.connect(addr).await.unwrap();
}

// not too sure on this, since there'll be a single peer but two connections; the return
// type is `Vec<Connection>` but it's peer with any connection.
let mut peers = node_a.peers().await.unwrap();
assert_eq!(
peers.len(),
1,
"there should had been one peer, found {:?}",
peers
);

// sadly we are unable to currently verify that there exists two connections for the node_b
// peer..

node_a
.disconnect(peers.remove(0).address)
.await
.expect("failed to disconnect peer_b at peer_a");

let peers = node_a.peers().await.unwrap();
assert!(
peers.is_empty(),
"node_b was still connected after disconnect: {:?}",
peers
);
});
// 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().await;
let node_b = ipfs::Node::new().await;

node_a
.add_listening_address(libp2p::build_multiaddr!(Ip4([127, 0, 0, 1]), Tcp(0u16)))
.await
.unwrap();

let addresses = node_a.addrs_local().await.unwrap();
assert_eq!(
addresses.len(),
2,
"there should had been two local addresses, found {:?}",
addresses
);

for addr in addresses {
node_b.connect(addr).await.unwrap();
}

// not too sure on this, since there'll be a single peer but two connections; the return
// type is `Vec<Connection>` but it's peer with any connection.
let mut peers = node_a.peers().await.unwrap();
assert_eq!(
peers.len(),
1,
"there should had been one peer, found {:?}",
peers
);

// sadly we are unable to currently verify that there exists two connections for the node_b
// peer..

node_a
.disconnect(peers.remove(0).address)
.await
.expect("failed to disconnect peer_b at peer_a");

let peers = node_a.peers().await.unwrap();
assert!(
peers.is_empty(),
"node_b was still connected after disconnect: {:?}",
peers
);
}

0 comments on commit 1367a30

Please sign in to comment.