Skip to content

Commit 7303b52

Browse files
committed
fix(tcp_client): remove assert!(err.is_err())
1 parent 142f8c0 commit 7303b52

File tree

3 files changed

+55
-73
lines changed

3 files changed

+55
-73
lines changed

src/toxcore/tcp/client/client.rs

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ pub mod tests {
508508
use super::*;
509509

510510
use std::time::{Duration, Instant};
511-
use std::io::{Error, ErrorKind};
511+
use failure::Error;
512+
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
512513

513514
use tokio::net::TcpListener;
514515
use tokio::timer::Interval;
@@ -542,9 +543,8 @@ pub mod tests {
542543
pk: gen_keypair().0,
543544
});
544545

545-
let res = client.handle_packet(route_request).wait();
546-
assert!(res.is_err());
547-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::MustNotSend);
546+
let error = client.handle_packet(route_request).wait().err().unwrap();
547+
assert_eq!(*error.kind(), HandlePacketErrorKind::MustNotSend);
548548
}
549549

550550
#[test]
@@ -603,9 +603,8 @@ pub mod tests {
603603
pk: gen_keypair().0,
604604
});
605605

606-
let res = client.handle_packet(route_response).wait();
607-
assert!(res.is_err());
608-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::UnexpectedRouteResponse);
606+
let error = client.handle_packet(route_response).wait().err().unwrap();
607+
assert_eq!(*error.kind(), HandlePacketErrorKind::UnexpectedRouteResponse);
609608

610609
assert!(client.links.read().by_id(index).is_none());
611610
}
@@ -619,9 +618,8 @@ pub mod tests {
619618
pk: gen_keypair().0,
620619
});
621620

622-
let res = client.handle_packet(route_response).wait();
623-
assert!(res.is_err());
624-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::InvalidConnectionId);
621+
let error = client.handle_packet(route_response).wait().err().unwrap();
622+
assert_eq!(*error.kind(), HandlePacketErrorKind::InvalidConnectionId);
625623
}
626624

627625
#[test]
@@ -654,9 +652,8 @@ pub mod tests {
654652
connection_id: ConnectionId::from_index(index),
655653
});
656654

657-
let res = client.handle_packet(connect_notification).wait();
658-
assert!(res.is_err());
659-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::AlreadyLinked);
655+
let error = client.handle_packet(connect_notification).wait().err().unwrap();
656+
assert_eq!(*error.kind(), HandlePacketErrorKind::AlreadyLinked);
660657

661658
assert!(client.links.read().by_id(index).is_none());
662659
}
@@ -669,9 +666,8 @@ pub mod tests {
669666
connection_id: ConnectionId::zero(),
670667
});
671668

672-
let res = client.handle_packet(connect_notification).wait();
673-
assert!(res.is_err());
674-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::InvalidConnectionId);
669+
let error = client.handle_packet(connect_notification).wait().err().unwrap();
670+
assert_eq!(*error.kind(), HandlePacketErrorKind::InvalidConnectionId);
675671
}
676672

677673
#[test]
@@ -705,9 +701,8 @@ pub mod tests {
705701
connection_id: ConnectionId::from_index(index),
706702
});
707703

708-
let res = client.handle_packet(disconnect_notification).wait();
709-
assert!(res.is_err());
710-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::AlreadyLinked);
704+
let error = client.handle_packet(disconnect_notification).wait().err().unwrap();
705+
assert_eq!(*error.kind(), HandlePacketErrorKind::AlreadyLinked);
711706

712707
assert!(client.links.read().by_id(index).is_none());
713708
}
@@ -720,9 +715,8 @@ pub mod tests {
720715
connection_id: ConnectionId::zero(),
721716
});
722717

723-
let res = client.handle_packet(disconnect_notification).wait();
724-
assert!(res.is_err());
725-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::InvalidConnectionId);
718+
let error = client.handle_packet(disconnect_notification).wait().err().unwrap();
719+
assert_eq!(*error.kind(), HandlePacketErrorKind::InvalidConnectionId);
726720
}
727721

728722
#[test]
@@ -762,9 +756,8 @@ pub mod tests {
762756
data: vec![42; 123],
763757
});
764758

765-
let res = client.handle_packet(oob_send).wait();
766-
assert!(res.is_err());
767-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::MustNotSend);
759+
let error = client.handle_packet(oob_send).wait().err().unwrap();
760+
assert_eq!(*error.kind(), HandlePacketErrorKind::MustNotSend);
768761
}
769762

770763
#[test]
@@ -829,9 +822,8 @@ pub mod tests {
829822
}),
830823
});
831824

832-
let res = client.handle_packet(data_packet).wait();
833-
assert!(res.is_err());
834-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::AlreadyLinked);
825+
let error = client.handle_packet(data_packet).wait().err().unwrap();
826+
assert_eq!(*error.kind(), HandlePacketErrorKind::AlreadyLinked);
835827

836828
// Necessary to drop tx so that rx.collect() can be finished
837829
drop(client);
@@ -851,9 +843,8 @@ pub mod tests {
851843
}),
852844
});
853845

854-
let res = client.handle_packet(data_packet).wait();
855-
assert!(res.is_err());
856-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::InvalidConnectionId);
846+
let error = client.handle_packet(data_packet).wait().err().unwrap();
847+
assert_eq!(*error.kind(), HandlePacketErrorKind::InvalidConnectionId);
857848

858849
// Necessary to drop tx so that rx.collect() can be finished
859850
drop(client);
@@ -876,9 +867,8 @@ pub mod tests {
876867
payload: vec![42; 123],
877868
});
878869

879-
let res = client.handle_packet(onion_request).wait();
880-
assert!(res.is_err());
881-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::MustNotSend);
870+
let error = client.handle_packet(onion_request).wait().err().unwrap();
871+
assert_eq!(*error.kind(), HandlePacketErrorKind::MustNotSend);
882872
}
883873

884874
#[test]
@@ -935,9 +925,8 @@ pub mod tests {
935925
payload: vec![42; 123],
936926
});
937927

938-
let res = client.send_data(destination_pk, data.clone()).wait();
939-
assert!(res.is_err());
940-
assert_eq!(*res.err().unwrap().kind(), SendPacketErrorKind::NotLinked);
928+
let error = client.send_data(destination_pk, data.clone()).wait().err().unwrap();
929+
assert_eq!(*error.kind(), SendPacketErrorKind::NotLinked);
941930

942931
// Necessary to drop tx so that rx.collect() can be finished
943932
drop(client);
@@ -958,9 +947,8 @@ pub mod tests {
958947
let connection_id = 42;
959948
client.links.write().insert_by_id(&destination_pk, connection_id - 16);
960949

961-
let res = client.send_data(destination_pk, data.clone()).wait();
962-
assert!(res.is_err());
963-
assert_eq!(*res.err().unwrap().kind(), SendPacketErrorKind::NotOnline);
950+
let error = client.send_data(destination_pk, data.clone()).wait().err().unwrap();
951+
assert_eq!(*error.kind(), SendPacketErrorKind::NotOnline);
964952

965953
// Necessary to drop tx so that rx.collect() can be finished
966954
drop(client);
@@ -1050,9 +1038,8 @@ pub mod tests {
10501038

10511039
let (connection_pk, _connection_sk) = gen_keypair();
10521040

1053-
let res = client.remove_connection(connection_pk).wait();
1054-
assert!(res.is_err());
1055-
assert_eq!(*res.err().unwrap().kind(), SendPacketErrorKind::NoSuchConnection);
1041+
let error = client.remove_connection(connection_pk).wait().err().unwrap();
1042+
assert_eq!(*error.kind(), SendPacketErrorKind::NoSuchConnection);
10561043
}
10571044

10581045
#[test]
@@ -1144,11 +1131,11 @@ pub mod tests {
11441131
// waits until the relay becomes connected
11451132
fn on_connected(client: Client) -> impl Future<Item = (), Error = Error> + Send {
11461133
Interval::new(Instant::now(), Duration::from_millis(10))
1147-
.map_err(|e| Error::new(ErrorKind::Other, e))
1134+
.map_err(Error::from)
11481135
.skip_while(move |_| match *client.status.read() {
11491136
ClientStatus::Connecting => future::ok(true),
11501137
ClientStatus::Connected(_) => future::ok(false),
1151-
ref other => future::err(Error::new(ErrorKind::Other, format!("Invalid status: {:?}", other))),
1138+
ref other => future::err(Error::from(IoError::new(IoErrorKind::Other, format!("Invalid status: {:?}", other)))),
11521139
})
11531140
.into_future()
11541141
.map(|_| ())
@@ -1158,7 +1145,7 @@ pub mod tests {
11581145
// waits until link with PublicKey becomes online
11591146
fn on_online(client: Client, pk: PublicKey) -> impl Future<Item = (), Error = Error> + Send {
11601147
Interval::new(Instant::now(), Duration::from_millis(10))
1161-
.map_err(|e| Error::new(ErrorKind::Other, e))
1148+
.map_err(Error::from)
11621149
.skip_while(move |_| {
11631150
let links = client.links.read();
11641151
if let Some(index) = links.id_by_pk(&pk) {
@@ -1183,7 +1170,7 @@ pub mod tests {
11831170
let server = Server::new();
11841171
let stats = Stats::new();
11851172
let server_future = server.run(listener, server_sk, stats, 2)
1186-
.map_err(|e| Error::new(ErrorKind::Other, e.compat()));
1173+
.map_err(Error::from);
11871174

11881175
// run first client
11891176
let (client_pk_1, client_sk_1) = gen_keypair();
@@ -1192,7 +1179,7 @@ pub mod tests {
11921179
// connection attempts should be set to 0 after successful connection
11931180
set_connection_attempts(&client_1, 3);
11941181
let client_future_1 = client_1.clone().spawn(client_sk_1, client_pk_1)
1195-
.map_err(|e| Error::new(ErrorKind::Other, e.compat()));
1182+
.map_err(Error::from);
11961183

11971184
// run second client
11981185
let (client_pk_2, client_sk_2) = gen_keypair();
@@ -1201,11 +1188,11 @@ pub mod tests {
12011188
// connection attempts should be set to 0 after successful connection
12021189
set_connection_attempts(&client_2, 3);
12031190
let client_future_2 = client_2.clone().spawn(client_sk_2, client_pk_2)
1204-
.map_err(|e| Error::new(ErrorKind::Other, e.compat()));
1191+
.map_err(Error::from);
12051192

12061193
// add connection immediately
12071194
let connection_future = client_2.add_connection(client_pk_1)
1208-
.map_err(|e| Error::new(ErrorKind::Other, e.compat()));
1195+
.map_err(Error::from);
12091196

12101197
// wait until connections are established
12111198
let client_1_c = client_1.clone();
@@ -1217,7 +1204,7 @@ pub mod tests {
12171204
assert_eq!(client_2_c.connection_attempts(), 0);
12181205
// add connection when relay is connected
12191206
client_1_c.add_connection(client_pk_2)
1220-
.map_err(|e| Error::new(ErrorKind::Other, e.compat()))
1207+
.map_err(Error::from)
12211208
});
12221209

12231210
let data_1 = DataPayload::CryptoData(CryptoData {
@@ -1237,9 +1224,9 @@ pub mod tests {
12371224
let on_online_future = on_online(client_1.clone(), client_pk_2).join(on_online(client_2.clone(), client_pk_1))
12381225
// and then send data packets to each other
12391226
.and_then(move |_| client_1_c.send_data(client_pk_2, data_1_c)
1240-
.map_err(|e| Error::new(ErrorKind::Other, e.compat())))
1227+
.map_err(Error::from))
12411228
.and_then(move |_| client_2_c.send_data(client_pk_1, data_2_c)
1242-
.map_err(|e| Error::new(ErrorKind::Other, e.compat())))
1229+
.map_err(Error::from))
12431230
// and then get them
12441231
.and_then(move |_| incoming_rx_1.map_err(|()| unreachable!("rx can't fail")).into_future().map(move |(packet, _)| {
12451232
let (relay_pk, packet) = packet.unwrap();
@@ -1288,15 +1275,15 @@ pub mod tests {
12881275
let server = Server::new();
12891276
let stats = Stats::new();
12901277
let server_future = server.run(listener, server_sk, stats, 2)
1291-
.map_err(|e| Error::new(ErrorKind::Other, e.compat()));
1278+
.map_err(Error::from);
12921279

12931280
// run a client with invalid server's pk
12941281
let (client_pk_1, client_sk_1) = gen_keypair();
12951282
let (invalid_server_pk, _invalid_server_sk) = gen_keypair();
12961283
let (incoming_tx_1, _incoming_rx_1) = mpsc::unbounded();
12971284
let client = Client::new(invalid_server_pk, addr, incoming_tx_1);
12981285
let client_future = client.clone().spawn(client_sk_1, client_pk_1)
1299-
.map_err(|e| Error::new(ErrorKind::Other, e.compat()));
1286+
.map_err(Error::from);
13001287

13011288
let future = server_future
13021289
.select(client_future)

src/toxcore/tcp/client/connections.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl Connections {
172172
Either::A(self.add_connection_inner(client, node_pk))
173173
} else {
174174
Either::B( future::err(
175-
ConnectionErrorKind::NOSuchRelay.into()
175+
ConnectionErrorKind::NoSuchRelay.into()
176176
))
177177
}
178178
}
@@ -279,7 +279,7 @@ impl Connections {
279279
future
280280
} else {
281281
Either::B( future::err(
282-
ConnectionErrorKind::NOSuchRelay.into()
282+
ConnectionErrorKind::NoSuchRelay.into()
283283
))
284284
}
285285
}
@@ -541,9 +541,8 @@ mod tests {
541541
let (relay_pk, _relay_sk) = gen_keypair();
542542
let (node_pk, _node_sk) = gen_keypair();
543543

544-
let res = connections.add_connection(relay_pk, node_pk).wait();
545-
assert!(res.is_err());
546-
assert_eq!(*res.err().unwrap().kind(), ConnectionErrorKind::NOSuchRelay);
544+
let error = connections.add_connection(relay_pk, node_pk).wait().err().unwrap();
545+
assert_eq!(*error.kind(), ConnectionErrorKind::NoSuchRelay);
547546
}
548547

549548
#[test]
@@ -585,9 +584,8 @@ mod tests {
585584

586585
let (node_pk, _node_sk) = gen_keypair();
587586

588-
let res = connections.remove_connection(node_pk).wait();
589-
assert!(res.is_err());
590-
assert_eq!(*res.err().unwrap().kind(), ConnectionErrorKind::NoConnection);
587+
let error = connections.remove_connection(node_pk).wait().err().unwrap();
588+
assert_eq!(*error.kind(), ConnectionErrorKind::NoConnection);
591589
}
592590

593591
#[test]
@@ -702,9 +700,8 @@ mod tests {
702700
let (destination_pk, _destination_sk) = gen_keypair();
703701
let (relay_pk, _relay_sk) = gen_keypair();
704702

705-
let res = connections.send_oob(relay_pk, destination_pk, vec![42; 123]).wait();
706-
assert!(res.is_err());
707-
assert_eq!(*res.err().unwrap().kind(), ConnectionErrorKind::NotConnected);
703+
let error = connections.send_oob(relay_pk, destination_pk, vec![42; 123]).wait().err().unwrap();
704+
assert_eq!(*error.kind(), ConnectionErrorKind::NotConnected);
708705
}
709706

710707
#[test]
@@ -757,9 +754,8 @@ mod tests {
757754
payload: vec![42; 123],
758755
};
759756

760-
let res = connections.send_onion(relay_pk, onion_request.clone()).wait();
761-
assert!(res.is_err());
762-
assert_eq!(*res.err().unwrap().kind(), ConnectionErrorKind::NotConnected);
757+
let error = connections.send_onion(relay_pk, onion_request.clone()).wait().err().unwrap();
758+
assert_eq!(*error.kind(), ConnectionErrorKind::NotConnected);
763759
}
764760

765761
#[test]
@@ -790,9 +786,8 @@ mod tests {
790786

791787
let (node_pk, _node_sk) = gen_keypair();
792788

793-
let res = connections.set_connection_status(node_pk, NodeConnectionStatus::UDP).wait();
794-
assert!(res.is_err());
795-
assert_eq!(*res.err().unwrap().kind(), ConnectionErrorKind::NOSuchRelay);
789+
let error = connections.set_connection_status(node_pk, NodeConnectionStatus::UDP).wait().err().unwrap();
790+
assert_eq!(*error.kind(), ConnectionErrorKind::NoSuchRelay);
796791
}
797792

798793
#[test]

src/toxcore/tcp/client/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ error_kind! {
8585
Spawn,
8686
#[doc = "Search relay by relay's PK, but no such relay."]
8787
#[fail(display = "Search relay by relay's PK, but no such relay")]
88-
NOSuchRelay,
88+
NoSuchRelay,
8989
#[doc = "Send packet(s) error."]
9090
#[fail(display = "Send packet(s) error")]
9191
SendTo,

0 commit comments

Comments
 (0)