Skip to content

Commit

Permalink
Merge #2216
Browse files Browse the repository at this point in the history
2216: Fix clippy lints r=yvan-sraka a=yvan-sraka



Co-authored-by: Yvan Sraka <[email protected]>
  • Loading branch information
bors[bot] and yvan-sraka authored Jan 26, 2022
2 parents 4a21df2 + ed5c72c commit 249cc0c
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 164 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated from 5d53f4 to c7b61b
2 changes: 1 addition & 1 deletion massa-client/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl CommandHistory {
}
}

fn write_to_saved_history(command: &String) {
fn write_to_saved_history(command: &str) {
if let Ok(mut file) = OpenOptions::new()
.write(true)
.append(true)
Expand Down
120 changes: 51 additions & 69 deletions massa-network/src/peer_info_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,10 @@ mod tests {
#[tokio::test]
#[serial]
async fn test_try_new_in_connection_in_connection_closed() {
let mut network_settings = NetworkSettings::default();
network_settings.target_out_nonbootstrap_connections = 5;
let network_settings = NetworkSettings {
target_out_nonbootstrap_connections: 5,
..Default::default()
};
let mut peers: HashMap<IpAddr, PeerInfo> = HashMap::new();

// add peers
Expand All @@ -906,14 +908,8 @@ mod tests {
let wakeup_interval = network_settings.wakeup_interval;
let (saver_watch_tx, mut saver_watch_rx) = watch::channel(peers.clone());

let saver_join_handle = tokio::spawn(async move {
loop {
match saver_watch_rx.changed().await {
Ok(()) => (),
_ => break,
}
}
});
let saver_join_handle =
tokio::spawn(async move { while let Ok(()) = saver_watch_rx.changed().await {} });

let mut db = PeerInfoDatabase {
network_settings,
Expand All @@ -937,7 +933,7 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)), ip_err);
} else {
assert!(false, "ToManyConnectionAttempt error not return");
panic!("ToManyConnectionAttempt error not return");
}

let res = db
Expand Down Expand Up @@ -966,7 +962,7 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 12)), ip_err);
} else {
assert!(false, "ToManyConnectionAttempt error not return");
panic!("ToManyConnectionAttempt error not return");
}

// test with a not connected peer
Expand All @@ -977,7 +973,7 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 13)), ip_err);
} else {
assert!(false, "PeerInfoNotFoundError error not return");
panic!("PeerInfoNotFoundError error not return");
}

db.in_connection_closed(&IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)))
Expand All @@ -989,15 +985,17 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)), ip_err);
} else {
assert!(false, "ToManyConnectionAttempt error not return");
panic!("ToManyConnectionAttempt error not return");
}
}

#[tokio::test]
#[serial]
async fn test_out_connection_attempt_failed() {
let mut network_settings = NetworkSettings::default();
network_settings.target_out_nonbootstrap_connections = 5;
let network_settings = NetworkSettings {
target_out_nonbootstrap_connections: 5,
..Default::default()
};
let mut peers: HashMap<IpAddr, PeerInfo> = HashMap::new();

// add peers
Expand All @@ -1014,14 +1012,8 @@ mod tests {
let wakeup_interval = network_settings.wakeup_interval;
let (saver_watch_tx, mut saver_watch_rx) = watch::channel(peers.clone());

let saver_join_handle = tokio::spawn(async move {
loop {
match saver_watch_rx.changed().await {
Ok(()) => (),
_ => break,
}
}
});
let saver_join_handle =
tokio::spawn(async move { while let Ok(()) = saver_watch_rx.changed().await {} });

let mut db = PeerInfoDatabase {
network_settings,
Expand All @@ -1047,7 +1039,7 @@ mod tests {
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)), ip_err);
} else {
println!("res: {:?}", res);
assert!(false, "ToManyConnectionFailure error not return");
panic!("ToManyConnectionFailure error not return");
}

db.new_out_connection_attempt(&IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)))
Expand All @@ -1063,7 +1055,7 @@ mod tests {
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 13)), ip_err);
} else {
println!("res: {:?}", res);
assert!(false, "PeerInfoNotFoundError error not return");
panic!("PeerInfoNotFoundError error not return");
}
// peer with no attempt.
let res =
Expand All @@ -1075,7 +1067,7 @@ mod tests {
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 12)), ip_err);
} else {
println!("res: {:?}", res);
assert!(false, "ToManyConnectionFailure error not return");
panic!("ToManyConnectionFailure error not return");
}

// call ok.
Expand All @@ -1090,15 +1082,17 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)), ip_err);
} else {
assert!(false, "ToManyConnectionFailure error not return");
panic!("ToManyConnectionFailure error not return");
}
}

#[tokio::test]
#[serial]
async fn test_try_out_connection_attempt_success() {
let mut network_settings = NetworkSettings::default();
network_settings.target_out_nonbootstrap_connections = 5;
let network_settings = NetworkSettings {
target_out_nonbootstrap_connections: 5,
..Default::default()
};
let mut peers: HashMap<IpAddr, PeerInfo> = HashMap::new();

// add peers
Expand All @@ -1115,14 +1109,8 @@ mod tests {
let wakeup_interval = network_settings.wakeup_interval;
let (saver_watch_tx, mut saver_watch_rx) = watch::channel(peers.clone());

let saver_join_handle = tokio::spawn(async move {
loop {
match saver_watch_rx.changed().await {
Ok(()) => (),
_ => break,
}
}
});
let saver_join_handle =
tokio::spawn(async move { while let Ok(()) = saver_watch_rx.changed().await {} });

let mut db = PeerInfoDatabase {
network_settings,
Expand All @@ -1148,7 +1136,7 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)), ip_err);
} else {
assert!(false, "ToManyConnectionAttempt error not return");
panic!("ToManyConnectionAttempt error not return");
}

db.new_out_connection_attempt(&IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)))
Expand All @@ -1165,7 +1153,7 @@ mod tests {
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 13)), ip_err);
} else {
println!("res: {:?}", res);
assert!(false, "PeerInfoNotFoundError error not return");
panic!("PeerInfoNotFoundError error not return");
}

let res = db
Expand All @@ -1184,7 +1172,7 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 12)), ip_err);
} else {
assert!(false, "PeerInfoNotFoundError error not return");
panic!("PeerInfoNotFoundError error not return");
}

db.new_out_connection_attempt(&IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 12)))
Expand All @@ -1200,8 +1188,10 @@ mod tests {
#[tokio::test]
#[serial]
async fn test_new_out_connection_closed() {
let mut network_settings = NetworkSettings::default();
network_settings.max_out_nonbootstrap_connection_attempts = 5;
let network_settings = NetworkSettings {
max_out_nonbootstrap_connection_attempts: 5,
..Default::default()
};
let mut peers: HashMap<IpAddr, PeerInfo> = HashMap::new();

// add peers
Expand All @@ -1211,14 +1201,8 @@ mod tests {
peers.insert(connected_peers1.ip, connected_peers1);
let wakeup_interval = network_settings.wakeup_interval;
let (saver_watch_tx, mut saver_watch_rx) = watch::channel(peers.clone());
let saver_join_handle = tokio::spawn(async move {
loop {
match saver_watch_rx.changed().await {
Ok(()) => (),
_ => break,
}
}
});
let saver_join_handle =
tokio::spawn(async move { while let Ok(()) = saver_watch_rx.changed().await {} });

let mut db = PeerInfoDatabase {
network_settings,
Expand All @@ -1242,10 +1226,7 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)), ip_err);
} else {
assert!(
false,
"CloseConnectionWithNoConnectionToClose error not return"
);
panic!("CloseConnectionWithNoConnectionToClose error not return");
}

// add a new connection attempt
Expand All @@ -1265,7 +1246,7 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 12)), ip_err);
} else {
assert!(false, "PeerInfoNotFoundError error not return");
panic!("PeerInfoNotFoundError error not return");
}

db.out_connection_closed(&IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)))
Expand All @@ -1277,18 +1258,17 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)), ip_err);
} else {
assert!(
false,
"CloseConnectionWithNoConnectionToClose error not return"
);
panic!("CloseConnectionWithNoConnectionToClose error not return");
}
}

#[tokio::test]
#[serial]
async fn test_new_out_connection_attempt() {
let mut network_settings = NetworkSettings::default();
network_settings.max_out_nonbootstrap_connection_attempts = 5;
let network_settings = NetworkSettings {
max_out_nonbootstrap_connection_attempts: 5,
..Default::default()
};
let mut peers: HashMap<IpAddr, PeerInfo> = HashMap::new();

// add peers
Expand Down Expand Up @@ -1320,7 +1300,7 @@ mod tests {
if let Err(NetworkError::InvalidIpError(ip_err)) = res {
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 0, 11)), ip_err);
} else {
assert!(false, "InvalidIpError not return");
panic!("InvalidIpError not return");
}

let res =
Expand All @@ -1331,7 +1311,7 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 12)), ip_err);
} else {
assert!(false, "PeerInfoNotFoundError error not return");
panic!("PeerInfoNotFoundError error not return");
}

(0..5).for_each(|_| {
Expand All @@ -1346,7 +1326,7 @@ mod tests {
{
assert_eq!(IpAddr::V4(std::net::Ipv4Addr::new(169, 202, 0, 11)), ip_err);
} else {
assert!(false, "ToManyConnectionAttempt error not return");
panic!("ToManyConnectionAttempt error not return");
}
}

Expand Down Expand Up @@ -1551,9 +1531,11 @@ mod tests {
#[tokio::test]
#[serial]
async fn test_cleanup_peers() {
let mut network_settings = NetworkSettings::default();
network_settings.max_banned_peers = 1;
network_settings.max_idle_peers = 1;
let mut network_settings = NetworkSettings {
max_banned_peers: 1,
max_idle_peers: 1,
..Default::default()
};
let mut peers = HashMap::new();

// Call with empty db.
Expand Down Expand Up @@ -1679,7 +1661,7 @@ mod tests {
async fn test() {
let peer_db = PeerInfoDatabase::from(5);
let p = peer_db.peers.values().next().unwrap();
assert_eq!(p.is_active(), false);
assert!(!p.is_active());
}

fn default_peer_info_connected(ip: IpAddr) -> PeerInfo {
Expand Down
29 changes: 14 additions & 15 deletions massa-network/src/tests/mock_establisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ pub struct MockListener {

impl MockListener {
pub async fn accept(&mut self) -> std::io::Result<(ReadHalf, WriteHalf, SocketAddr)> {
let (addr, sender) = self
.connection_listener_rx
.recv()
.await
.ok_or(io::Error::new(
let (addr, sender) = self.connection_listener_rx.recv().await.ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
"MockListener accept channel from Establisher closed".to_string(),
))?;
)
})?;
let (duplex_controller, duplex_mock) = tokio::io::duplex(MAX_DUPLEX_BUFFER_SIZE);
let (duplex_mock_read, duplex_mock_write) = tokio::io::split(duplex_controller);
let (duplex_controller_read, duplex_controller_write) = tokio::io::split(duplex_mock);
Expand Down Expand Up @@ -151,10 +149,12 @@ impl MockEstablisherInterface {
&self,
addr: &SocketAddr,
) -> io::Result<(ReadHalf, WriteHalf)> {
let sender = self.connection_listener_tx.as_ref().ok_or(io::Error::new(
io::ErrorKind::Other,
"mock connect_to_controller_listener channel not initialized".to_string(),
))?;
let sender = self.connection_listener_tx.as_ref().ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
"mock connect_to_controller_listener channel not initialized".to_string(),
)
})?;
let (response_tx, response_rx) = oneshot::channel::<(ReadHalf, WriteHalf)>();
sender.send((*addr, response_tx)).await.map_err(|_err| {
io::Error::new(
Expand All @@ -175,13 +175,12 @@ impl MockEstablisherInterface {
pub async fn wait_connection_attempt_from_controller(
&mut self,
) -> io::Result<(ReadHalf, WriteHalf, SocketAddr, oneshot::Sender<bool>)> {
self.connection_connector_rx
.recv()
.await
.ok_or(io::Error::new(
self.connection_connector_rx.recv().await.ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
"MockListener get_connect_stream channel from connector closed".to_string(),
))
)
})
}

/*
Expand Down
Loading

0 comments on commit 249cc0c

Please sign in to comment.