Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed Jul 18, 2020
1 parent 6d6f0a1 commit 26de8e6
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 40 deletions.
6 changes: 3 additions & 3 deletions core/src/connection/listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ mod tests {

let address2 = address.clone();
async_std::task::spawn(async move {
mem_transport.dial(address2).unwrap().await.unwrap();
mem_transport.dial(Multiaddr::empty(), address2).unwrap().await.unwrap();
});

match listeners.next().await.unwrap() {
Expand Down Expand Up @@ -427,7 +427,7 @@ mod tests {
})))
}

fn dial(self, _: Multiaddr) -> Result<Self::Dial, transport::TransportError<Self::Error>> {
fn dial(self, _: Multiaddr, _: Multiaddr) -> Result<Self::Dial, transport::TransportError<Self::Error>> {
panic!()
}
}
Expand Down Expand Up @@ -465,7 +465,7 @@ mod tests {
})))
}

fn dial(self, _: Multiaddr) -> Result<Self::Dial, transport::TransportError<Self::Error>> {
fn dial(self, _: Multiaddr, _: Multiaddr) -> Result<Self::Dial, transport::TransportError<Self::Error>> {
panic!()
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/transport/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ mod tests {
#[test]
fn port_not_in_use() {
let transport = MemoryTransport::default();
assert!(transport.dial("/memory/810172461024613".parse().unwrap()).is_err());
assert!(transport.dial(Multiaddr::empty(), "/memory/810172461024613".parse().unwrap()).is_err());
let _listener = transport.listen_on("/memory/810172461024613".parse().unwrap()).unwrap();
assert!(transport.dial("/memory/810172461024613".parse().unwrap()).is_ok());
assert!(transport.dial(Multiaddr::empty(), "/memory/810172461024613".parse().unwrap()).is_ok());
}

#[test]
Expand Down Expand Up @@ -342,7 +342,7 @@ mod tests {

let t2 = MemoryTransport::default();
let dialer = async move {
let mut socket = t2.dial(cloned_t1_addr).unwrap().await.unwrap();
let mut socket = t2.dial(Multiaddr::empty(), cloned_t1_addr).unwrap().await.unwrap();
socket.write_all(&msg).await.unwrap();
};

Expand Down
14 changes: 7 additions & 7 deletions core/tests/network_dial_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn deny_incoming_connec() {

swarm2
.peer(swarm1.local_peer_id().clone())
.dial(address.clone(), Vec::new(), TestHandler())
.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), address.clone(), Vec::new(), TestHandler())
.unwrap();

async_std::task::block_on(future::poll_fn(|cx| -> Poll<Result<(), io::Error>> {
Expand Down Expand Up @@ -144,7 +144,7 @@ fn dial_self() {
}))
.unwrap();

swarm.dial(&local_address, TestHandler()).unwrap();
swarm.dial(&"/ip4/127.0.0.1/tcp/0".parse().unwrap(), &local_address, TestHandler()).unwrap();

let mut got_dial_err = false;
let mut got_inc_err = false;
Expand Down Expand Up @@ -215,7 +215,7 @@ fn multiple_addresses_err() {

let target = PeerId::random();
swarm.peer(target.clone())
.dial(first, rest, TestHandler())
.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), first, rest, TestHandler())
.unwrap();

async_std::task::block_on(future::poll_fn(|cx| -> Poll<Result<(), io::Error>> {
Expand Down Expand Up @@ -257,13 +257,13 @@ fn connection_limit() {
let target = PeerId::random();
for _ in 0 .. outgoing_per_peer_limit {
network.peer(target.clone())
.dial(Multiaddr::empty(), Vec::new(), TestHandler())
.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), Multiaddr::empty(), Vec::new(), TestHandler())
.ok()
.expect("Unexpected connection limit.");
}

let err = network.peer(target)
.dial(Multiaddr::empty(), Vec::new(), TestHandler())
.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), Multiaddr::empty(), Vec::new(), TestHandler())
.expect_err("Unexpected dialing success.");

assert_eq!(err.current, outgoing_per_peer_limit);
Expand All @@ -272,13 +272,13 @@ fn connection_limit() {
let target2 = PeerId::random();
for _ in outgoing_per_peer_limit .. outgoing_limit {
network.peer(target2.clone())
.dial(Multiaddr::empty(), Vec::new(), TestHandler())
.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), Multiaddr::empty(), Vec::new(), TestHandler())
.ok()
.expect("Unexpected connection limit.");
}

let err = network.peer(target2)
.dial(Multiaddr::empty(), Vec::new(), TestHandler())
.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), Multiaddr::empty(), Vec::new(), TestHandler())
.expect_err("Unexpected dialing success.");

assert_eq!(err.current, outgoing_limit);
Expand Down
6 changes: 4 additions & 2 deletions core/tests/transport_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ fn upgrade_pipeline() {
};

let client = async move {
let (peer, _mplex) = dialer_transport.dial(listen_addr2).unwrap().await.unwrap();
let (peer, _mplex) = dialer_transport.dial(
"/ip4/127.0.0.1/tcp/0".parse().unwrap(),
listen_addr2,
).unwrap().await.unwrap();
assert_eq!(peer, listener_id);
};

async_std::task::spawn(server);
async_std::task::block_on(client);
}

2 changes: 1 addition & 1 deletion muxers/mplex/tests/async_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn async_write() {
let transport = TcpConfig::new().and_then(move |c, e|
upgrade::apply(c, mplex, e, upgrade::Version::V1));

let client = Arc::new(transport.dial(rx.await.unwrap()).unwrap().await.unwrap());
let client = Arc::new(transport.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), rx.await.unwrap()).unwrap().await.unwrap());
let mut inbound = loop {
if let Some(s) = muxing::event_from_ref_and_wrap(client.clone()).await.unwrap()
.into_inbound_substream() {
Expand Down
10 changes: 8 additions & 2 deletions muxers/mplex/tests/two_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ fn client_to_server_outbound() {
let transport = TcpConfig::new().and_then(move |c, e|
upgrade::apply(c, mplex, e, upgrade::Version::V1));

let client = Arc::new(transport.dial(rx.await.unwrap()).unwrap().await.unwrap());
let client = Arc::new(transport.dial(
"/ip4/127.0.0.1/tcp/0".parse().unwrap(),
rx.await.unwrap(),
).unwrap().await.unwrap());
let mut inbound = loop {
if let Some(s) = muxing::event_from_ref_and_wrap(client.clone()).await.unwrap()
.into_inbound_substream() {
Expand Down Expand Up @@ -126,7 +129,10 @@ fn client_to_server_inbound() {
let transport = TcpConfig::new().and_then(move |c, e|
upgrade::apply(c, mplex, e, upgrade::Version::V1));

let client = transport.dial(rx.await.unwrap()).unwrap().await.unwrap();
let client = transport.dial(
"/ip4/127.0.0.1/tcp/0".parse().unwrap(),
rx.await.unwrap(),
).unwrap().await.unwrap();
let mut outbound = muxing::outbound_from_ref_and_wrap(Arc::new(client)).await.unwrap();
outbound.write_all(b"hello world").await.unwrap();
outbound.close().await.unwrap();
Expand Down
3 changes: 2 additions & 1 deletion protocols/deflate/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ async fn run(message1: Vec<u8>) {
conn.close().await.expect("close")
});

let mut conn = transport.dial(listen_addr).expect("dialer").await.expect("connection");
let mut conn = transport.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), listen_addr)
.expect("dialer").await.expect("connection");
conn.write_all(&message1).await.expect("write_all");
conn.close().await.expect("close");

Expand Down
3 changes: 2 additions & 1 deletion protocols/identify/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ mod tests {
async_std::task::block_on(async move {
let transport = TcpConfig::new();

let socket = transport.dial(rx.await.unwrap()).unwrap().await.unwrap();
let socket = transport.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), rx.await.unwrap())
.unwrap().await.unwrap();
let RemoteInfo { info, observed_addr, .. } =
apply_outbound(socket, IdentifyProtocolConfig, upgrade::Version::V1).await.unwrap();
assert_eq!(observed_addr, "/ip4/100.101.102.103/tcp/5000".parse().unwrap());
Expand Down
5 changes: 4 additions & 1 deletion protocols/noise/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ where

let outbound_msgs = messages.clone();
let client_fut = async {
let mut client_session = client_transport.dial(server_address.clone())
let mut client_session = client_transport.dial(
"/ip4/127.0.0.1/tcp/0".parse().unwrap(),
server_address.clone(),
)
.unwrap()
.await
.map(|(_, session)| session)
Expand Down
4 changes: 2 additions & 2 deletions protocols/ping/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ mod tests {
use futures::prelude::*;
use libp2p_core::{
upgrade,
multiaddr::multiaddr,
multiaddr::{multiaddr, Multiaddr},
transport::{
Transport,
ListenerEvent,
Expand Down Expand Up @@ -138,7 +138,7 @@ mod tests {
});

async_std::task::block_on(async move {
let c = MemoryTransport.dial(listener_addr).unwrap().await.unwrap();
let c = MemoryTransport.dial(Multiaddr::empty(), listener_addr).unwrap().await.unwrap();
let rtt = upgrade::apply_outbound(c, Ping::default(), upgrade::Version::V1).await.unwrap();
assert!(rtt > Duration::from_secs(0));
});
Expand Down
5 changes: 4 additions & 1 deletion protocols/plaintext/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ fn variable_msg_length() {

let client_fut = async {
debug!("dialing {:?}", server_address);
let (received_server_id, mut client_channel) = client_transport.dial(server_address).unwrap().await.unwrap();
let (received_server_id, mut client_channel) = client_transport.dial(
"/ip4/127.0.0.1/tcp/0".parse().unwrap(),
server_address,
).unwrap().await.unwrap();
assert_eq!(received_server_id, server_id.public().into_peer_id());

debug!("Client: writing message.");
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
//! use libp2p::{Multiaddr, Transport, tcp::TcpConfig};
//! let tcp = TcpConfig::new();
//! let addr: Multiaddr = "/ip4/98.97.96.95/tcp/20500".parse().expect("invalid multiaddr");
//! let _conn = tcp.dial(addr);
//! let _conn = tcp.dial("/ip4/0.0.0.0/tcp/0".parse().unwrap(), addr);
//! ```
//! In the above example, `_conn` is a [`Future`] that needs to be polled in order for
//! the dialing to take place and eventually resolve to a connection. Polling
Expand Down
8 changes: 4 additions & 4 deletions transports/dns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ mod tests {
unreachable!()
}

fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
fn dial(self, _local_addr: Multiaddr, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
let addr = addr.iter().collect::<Vec<_>>();
assert_eq!(addr.len(), 2);
match addr[1] {
Expand All @@ -296,20 +296,20 @@ mod tests {

let _ = transport
.clone()
.dial("/dns4/example.com/tcp/20000".parse().unwrap())
.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), "/dns4/example.com/tcp/20000".parse().unwrap())
.unwrap()
.await
.unwrap();

let _ = transport
.clone()
.dial("/dns6/example.com/tcp/20000".parse().unwrap())
.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), "/dns6/example.com/tcp/20000".parse().unwrap())
.unwrap()
.await
.unwrap();

let _ = transport
.dial("/ip4/1.2.3.4/tcp/20000".parse().unwrap())
.dial("/ip4/127.0.0.1/tcp/0".parse().unwrap(), "/ip4/1.2.3.4/tcp/20000".parse().unwrap())
.unwrap()
.await
.unwrap();
Expand Down
16 changes: 10 additions & 6 deletions transports/tcp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ mod tests {
#[test]
#[cfg(feature = "async-std")]
fn wildcard_expansion() {
fn test(addr: Multiaddr) {
let mut listener = TcpConfig::new().listen_on(addr).expect("listener");
fn test(default_addr: Multiaddr) {
let mut listener = TcpConfig::new().listen_on(default_addr.clone()).expect("listener");

// Get the first address.
let addr = futures::executor::block_on_stream(listener.by_ref())
Expand Down Expand Up @@ -554,7 +554,7 @@ mod tests {
})
.for_each(|_| futures::future::ready(()));

let client = TcpConfig::new().dial(addr).expect("dialer");
let client = TcpConfig::new().dial(default_addr, addr).expect("dialer");
async_std::task::block_on(futures::future::join(server, client)).1.unwrap();
}

Expand Down Expand Up @@ -614,13 +614,14 @@ mod tests {
#[test]
#[cfg(feature = "async-std")]
fn communicating_between_dialer_and_listener() {
fn test(addr: Multiaddr) {
fn test(default_addr: Multiaddr) {
let (ready_tx, ready_rx) = futures::channel::oneshot::channel();
let mut ready_tx = Some(ready_tx);

let default_addr2 = default_addr.clone();
async_std::task::spawn(async move {
let tcp = TcpConfig::new();
let mut listener = tcp.listen_on(addr).unwrap();
let mut listener = tcp.listen_on(default_addr2).unwrap();

loop {
match listener.next().await.unwrap().unwrap() {
Expand All @@ -644,7 +645,10 @@ mod tests {
let tcp = TcpConfig::new();

// Obtain a future socket through dialing
let mut socket = tcp.dial(addr.clone()).unwrap().await.unwrap();
let mut socket = tcp.dial(
default_addr,
addr.clone(),
).unwrap().await.unwrap();
socket.write_all(&[0x1, 0x2, 0x3]).await.unwrap();

let mut buf = [0u8; 3];
Expand Down
2 changes: 1 addition & 1 deletion transports/uds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ mod tests {
async_std::task::block_on(async move {
let uds = UdsConfig::new();
let addr = rx.await.unwrap();
let mut socket = uds.dial(addr).unwrap().await.unwrap();
let mut socket = uds.dial(Multiaddr::empty(), addr).unwrap().await.unwrap();
socket.write(&[1, 2, 3]).await.unwrap();
});
}
Expand Down
8 changes: 4 additions & 4 deletions transports/websocket/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,16 @@ mod tests {
#[test]
fn dialer_connects_to_listener_ipv4() {
let a = "/ip4/127.0.0.1/tcp/0/ws".parse().unwrap();
futures::executor::block_on(connect(a))
futures::executor::block_on(connect("/ip4/127.0.0.1/tcp/0".parse().unwrap(), a))
}

#[test]
fn dialer_connects_to_listener_ipv6() {
let a = "/ip6/::1/tcp/0/ws".parse().unwrap();
futures::executor::block_on(connect(a))
futures::executor::block_on(connect("/ip6/::1/tcp/0".parse().unwrap(), a))
}

async fn connect(listen_addr: Multiaddr) {
async fn connect(local_addr: Multiaddr, listen_addr: Multiaddr) {
let ws_config = WsConfig::new(tcp::TcpConfig::new());

let mut listener = ws_config.clone()
Expand All @@ -226,7 +226,7 @@ mod tests {
conn.await
};

let outbound = ws_config.dial(addr).unwrap();
let outbound = ws_config.dial(local_addr, addr).unwrap();

let (a, b) = futures::join!(inbound, outbound);
a.and(b).unwrap();
Expand Down

0 comments on commit 26de8e6

Please sign in to comment.