Skip to content

Commit 2664768

Browse files
committed
fix(tcp_client): properly increase unsuccessful attempts counter
1 parent 2b52890 commit 2664768

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/toxcore/tcp/client/client.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ impl Client {
335335
ref mut status => *status = ClientStatus::Disconnected,
336336
}
337337
if res.is_err() {
338-
self_c.connection_attempts.write().saturating_add(1);
338+
let mut connection_attempts = self_c.connection_attempts.write();
339+
*connection_attempts = connection_attempts.saturating_add(1);
339340
}
340341
*self_c.connected_time.write() = None;
341342
self_c.links.write().clear();
@@ -1229,4 +1230,40 @@ pub mod tests {
12291230

12301231
tokio::run(future);
12311232
}
1233+
1234+
#[test]
1235+
fn spawn_unsuccessful() {
1236+
// run server
1237+
let (_server_pk, server_sk) = gen_keypair();
1238+
1239+
let addr = "127.0.0.1:0".parse().unwrap();
1240+
let listener = TcpListener::bind(&addr).unwrap();
1241+
let addr = listener.local_addr().unwrap();
1242+
1243+
let server = Server::new();
1244+
let stats = Stats::new();
1245+
let server_future = server.run(listener, server_sk, stats, 2)
1246+
.map_err(|e| Error::new(ErrorKind::Other, e.compat()));
1247+
1248+
// run a client with invalid server's pk
1249+
let (client_pk_1, client_sk_1) = gen_keypair();
1250+
let (invalid_server_pk, _invalid_server_sk) = gen_keypair();
1251+
let (incoming_tx_1, _incoming_rx_1) = mpsc::unbounded();
1252+
let client = Client::new(invalid_server_pk, addr, incoming_tx_1);
1253+
let client_future = client.clone().spawn(client_sk_1, client_pk_1);
1254+
1255+
let future = server_future
1256+
.select(client_future)
1257+
.then(|r| {
1258+
assert!(r.is_ok());
1259+
r
1260+
})
1261+
.map(|_| ())
1262+
.map_err(|_| ());
1263+
1264+
tokio::run(future);
1265+
1266+
// connection_attempts should be increased
1267+
assert_eq!(*client.connection_attempts.read(), 1);
1268+
}
12321269
}

0 commit comments

Comments
 (0)