Skip to content

Commit

Permalink
tests: Adjust testing
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv committed Aug 14, 2024
1 parent dd45588 commit c9e7aff
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/crypto/noise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ mod tests {
#[test]
fn invalid_peer_id_schema() {
match parse_peer_id(&vec![1, 2, 3, 4]).unwrap_err() {
crate::Error::ParseError(_) => {}
NegotiationError::ParseError(_) => {}
_ => panic!("invalid error"),
}
}
Expand Down
12 changes: 0 additions & 12 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,18 +428,6 @@ mod tests {

#[tokio::test]
async fn try_from_errors() {
tracing::trace!("{:?}", NotificationError::InvalidState);
tracing::trace!("{:?}", DialError::AlreadyConnected);
tracing::trace!(
"{:?}",
SubstreamError::YamuxError(crate::yamux::ConnectionError::Closed)
);
tracing::trace!("{:?}", AddressError::PeerIdMissing);
tracing::trace!(
"{:?}",
ParseError::InvalidMultihash(Multihash::from(PeerId::random()))
);

let (tx, rx) = channel(1);
drop(rx);

Expand Down
8 changes: 2 additions & 6 deletions src/multistream_select/dialer_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,9 +818,7 @@ mod tests {
DialerState::propose(ProtocolName::from("/13371338/proto/1"), vec![]).unwrap();

match dialer_state.register_response(bytes.freeze().to_vec()) {
Err(Error::NegotiationError(error::NegotiationError::MultistreamSelectError(
NegotiationError::Failed,
))) => {}
Err(error::NegotiationError::MultistreamSelectError(NegotiationError::Failed)) => {}
event => panic!("invalid event: {event:?}"),
}
}
Expand All @@ -839,9 +837,7 @@ mod tests {
DialerState::propose(ProtocolName::from("/13371338/proto/1"), vec![]).unwrap();

match dialer_state.register_response(bytes.freeze().to_vec()) {
Err(Error::NegotiationError(error::NegotiationError::MultistreamSelectError(
NegotiationError::Failed,
))) => {}
Err(error::NegotiationError::MultistreamSelectError(NegotiationError::Failed)) => {}
event => panic!("invalid event: {event:?}"),
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/protocol/notification/tests/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
ConnectionState, InboundState, NotificationProtocol, OutboundState, PeerContext,
PeerState, ValidationResult,
},
InnerTransportEvent, ProtocolCommand,
InnerTransportEvent, ProtocolCommand, SubstreamError,
},
substream::Substream,
transport::Endpoint,
Expand Down Expand Up @@ -225,7 +225,9 @@ async fn substream_open_failure_for_unknown_substream() {

let (mut notif, _handle, _sender, _tx) = make_notification_protocol();

notif.on_substream_open_failure(SubstreamId::new(), Error::Unknown).await;
notif
.on_substream_open_failure(SubstreamId::new(), SubstreamError::ConnectionClosed)
.await;
}

#[tokio::test]
Expand Down Expand Up @@ -313,7 +315,9 @@ async fn substream_open_failure_for_unknown_peer() {
let substream_id = SubstreamId::from(1337usize);

notif.pending_outbound.insert(substream_id, peer);
notif.on_substream_open_failure(substream_id, Error::Unknown).await;
notif
.on_substream_open_failure(substream_id, SubstreamError::ConnectionClosed)
.await;
}

#[tokio::test]
Expand Down Expand Up @@ -895,7 +899,10 @@ async fn open_failure_reported_once() {
notif.pending_outbound.insert(SubstreamId::from(1337usize), peer);

notif
.on_substream_open_failure(SubstreamId::from(1337usize), Error::Unknown)
.on_substream_open_failure(
SubstreamId::from(1337usize),
SubstreamError::ConnectionClosed,
)
.await;

match handle.next().await {
Expand Down
7 changes: 5 additions & 2 deletions src/protocol/request_response/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
ConfigBuilder, DialOptions, RequestResponseError, RequestResponseEvent,
RequestResponseHandle, RequestResponseProtocol,
},
InnerTransportEvent, TransportService,
InnerTransportEvent, SubstreamError, TransportService,
},
substream::Substream,
transport::manager::{limits::ConnectionLimitsConfig, TransportManager},
Expand Down Expand Up @@ -134,7 +134,10 @@ async fn unknown_substream_open_failure() {
let (mut protocol, _handle, _manager, _tx) = protocol();

match protocol
.on_substream_open_failure(SubstreamId::from(1338usize), Error::Unknown)
.on_substream_open_failure(
SubstreamId::from(1338usize),
SubstreamError::ConnectionClosed,
)
.await
{
Err(Error::InvalidState) => {}
Expand Down
4 changes: 2 additions & 2 deletions src/transport/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Transport for DummyTransport {
#[cfg(test)]
mod tests {
use super::*;
use crate::{transport::Endpoint, Error, PeerId};
use crate::{error::DialError, transport::Endpoint, PeerId};
use futures::StreamExt;

#[tokio::test]
Expand All @@ -114,7 +114,7 @@ mod tests {
transport.inject_event(TransportEvent::DialFailure {
connection_id: ConnectionId::from(1338usize),
address: Multiaddr::empty(),
error: Error::Unknown,
error: DialError::Timeout,
});

let peer = PeerId::random();
Expand Down
30 changes: 10 additions & 20 deletions src/transport/quic/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,10 @@ mod tests {
let crypto_config =
Arc::new(make_client_config(&Keypair::generate(), Some(peer)).expect("to succeed"));
let client_config = ClientConfig::new(crypto_config);
let client = Endpoint::client(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0))
.map_err(|error| Error::Other(error.to_string()))
.unwrap();
let client =
Endpoint::client(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)).unwrap();
let connection = client
.connect_with(client_config, format!("[::1]:{port}").parse().unwrap(), "l")
.map_err(|error| Error::Other(error.to_string()))
.unwrap();

let (res1, res2) = tokio::join!(
Expand Down Expand Up @@ -320,31 +318,27 @@ mod tests {
let crypto_config1 =
Arc::new(make_client_config(&Keypair::generate(), Some(peer)).expect("to succeed"));
let client_config1 = ClientConfig::new(crypto_config1);
let client1 = Endpoint::client(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0))
.map_err(|error| Error::Other(error.to_string()))
.unwrap();
let client1 =
Endpoint::client(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)).unwrap();
let connection1 = client1
.connect_with(
client_config1,
format!("[::1]:{port1}").parse().unwrap(),
"l",
)
.map_err(|error| Error::Other(error.to_string()))
.unwrap();

let crypto_config2 =
Arc::new(make_client_config(&Keypair::generate(), Some(peer)).expect("to succeed"));
let client_config2 = ClientConfig::new(crypto_config2);
let client2 = Endpoint::client(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0))
.map_err(|error| Error::Other(error.to_string()))
.unwrap();
let client2 =
Endpoint::client(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0)).unwrap();
let connection2 = client2
.connect_with(
client_config2,
format!("127.0.0.1:{port2}").parse().unwrap(),
"l",
)
.map_err(|error| Error::Other(error.to_string()))
.unwrap();

tokio::spawn(async move {
Expand Down Expand Up @@ -393,31 +387,27 @@ mod tests {
let crypto_config1 =
Arc::new(make_client_config(&Keypair::generate(), Some(peer)).expect("to succeed"));
let client_config1 = ClientConfig::new(crypto_config1);
let client1 = Endpoint::client(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0))
.map_err(|error| Error::Other(error.to_string()))
.unwrap();
let client1 =
Endpoint::client(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)).unwrap();
let connection1 = client1
.connect_with(
client_config1,
format!("[::1]:{port}").parse().unwrap(),
"l",
)
.map_err(|error| Error::Other(error.to_string()))
.unwrap();

let crypto_config2 =
Arc::new(make_client_config(&Keypair::generate(), Some(peer)).expect("to succeed"));
let client_config2 = ClientConfig::new(crypto_config2);
let client2 = Endpoint::client(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0))
.map_err(|error| Error::Other(error.to_string()))
.unwrap();
let client2 =
Endpoint::client(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)).unwrap();
let connection2 = client2
.connect_with(
client_config2,
format!("[::1]:{port}").parse().unwrap(),
"l",
)
.map_err(|error| Error::Other(error.to_string()))
.unwrap();

tokio::spawn(async move {
Expand Down
36 changes: 18 additions & 18 deletions src/transport/tcp/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,11 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::NegotiationError(NegotiationError::MultistreamSelectError(
Err(NegotiationError::MultistreamSelectError(
crate::multistream_select::NegotiationError::ProtocolError(
crate::multistream_select::ProtocolError::InvalidMessage,
),
))) => {}
)) => {}
Err(error) => panic!("invalid error: {error:?}"),
}
}
Expand Down Expand Up @@ -760,11 +760,11 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::NegotiationError(NegotiationError::MultistreamSelectError(
Err(NegotiationError::MultistreamSelectError(
crate::multistream_select::NegotiationError::ProtocolError(
crate::multistream_select::ProtocolError::InvalidMessage,
),
))) => {}
)) => {}
Err(error) => panic!("invalid error: {error:?}"),
}
}
Expand Down Expand Up @@ -813,9 +813,9 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::NegotiationError(NegotiationError::MultistreamSelectError(
Err(NegotiationError::MultistreamSelectError(
crate::multistream_select::NegotiationError::Failed,
))) => {}
)) => {}
Err(error) => panic!("invalid error: {error:?}"),
}
}
Expand Down Expand Up @@ -857,9 +857,9 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::NegotiationError(NegotiationError::MultistreamSelectError(
Err(NegotiationError::MultistreamSelectError(
crate::multistream_select::NegotiationError::Failed,
))) => {}
)) => {}
Err(error) => panic!("invalid error: {error:?}"),
}
}
Expand Down Expand Up @@ -904,7 +904,7 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::Timeout) => {}
Err(NegotiationError::Timeout) => {}
Err(error) => panic!("invalid error: {error:?}"),
}
}
Expand Down Expand Up @@ -955,7 +955,7 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::Timeout) => {}
Err(NegotiationError::Timeout) => {}
Err(error) => panic!("invalid error: {error:?}"),
}
}
Expand Down Expand Up @@ -1001,7 +1001,7 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::Timeout) => {}
Err(NegotiationError::Timeout) => {}
Err(error) => panic!("invalid error: {error:?}"),
}
}
Expand Down Expand Up @@ -1041,7 +1041,7 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::Timeout) => {}
Err(NegotiationError::Timeout) => {}
Err(error) => panic!("invalid error: {error:?}"),
}
}
Expand Down Expand Up @@ -1096,9 +1096,9 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::NegotiationError(NegotiationError::MultistreamSelectError(
Err(NegotiationError::MultistreamSelectError(
crate::multistream_select::NegotiationError::Failed,
))) => {}
)) => {}
Err(error) => panic!("{error:?}"),
}
}
Expand Down Expand Up @@ -1156,9 +1156,9 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::NegotiationError(NegotiationError::MultistreamSelectError(
Err(NegotiationError::MultistreamSelectError(
crate::multistream_select::NegotiationError::Failed,
))) => {}
)) => {}
Err(error) => panic!("{error:?}"),
}
}
Expand Down Expand Up @@ -1209,7 +1209,7 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::Timeout) => {}
Err(NegotiationError::Timeout) => {}
Err(error) => panic!("invalid error: {error:?}"),
}
}
Expand Down Expand Up @@ -1266,7 +1266,7 @@ mod tests {
.await
{
Ok(_) => panic!("connection was supposed to fail"),
Err(Error::Timeout) => {}
Err(NegotiationError::Timeout) => {}
Err(error) => panic!("invalid error: {error:?}"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/transport/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ mod tests {

assert!(!transport.pending_dials.is_empty());
transport.pending_connections.push(Box::pin(async move {
Err((ConnectionId::from(0usize), Error::Unknown))
Err((ConnectionId::from(0usize), DialError::Timeout))
}));

assert!(std::matches!(
Expand Down
11 changes: 9 additions & 2 deletions tests/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use litep2p::{
config::ConfigBuilder,
crypto::ed25519::Keypair,
error::Error,
error::{DialError, Error, NegotiationError},
protocol::libp2p::ping::{Config as PingConfig, PingEvent},
transport::tcp::config::Config as TcpConfig,
Litep2p, Litep2pEvent, PeerId,
Expand Down Expand Up @@ -366,7 +366,14 @@ async fn connection_timeout(transport: Transport, address: Multiaddr) {

assert_eq!(dial_address, address);
println!("{error:?}");
assert!(std::matches!(error, Error::Timeout));
match error {
DialError::Timeout => {}
DialError::NegotiationError(NegotiationError::Timeout) => {}
DialError::Quic(litep2p::error::QuicError::ConnectionError(
quinn::ConnectionError::TimedOut,
)) => {}
_ => panic!("unexpected error {error:?}"),
}
}

#[cfg(feature = "quic")]
Expand Down

0 comments on commit c9e7aff

Please sign in to comment.