[Clean] safer unwrap & except handling
Some checks failed
CI / checks (push) Failing after 1m51s

This commit is contained in:
Alex Emmet 2026-07-15 19:11:01 +02:00
commit 5f11d476b6
17 changed files with 475 additions and 348 deletions

View file

@ -3,18 +3,17 @@ use std::net::{IpAddr, Ipv4Addr};
use mtp_client::{ClientConfig, MTPClient};
use mtp_host::{HostConfig, MTPHost};
fn generate_self_signed_cert() -> (Vec<u8>, Vec<u8>) {
let key_pair = rcgen::KeyPair::generate().unwrap();
let params =
rcgen::CertificateParams::new(vec!["localhost".into(), "127.0.0.1".into()]).unwrap();
let cert = params.self_signed(&key_pair).unwrap();
async fn generate_self_signed_cert() -> Result<(Vec<u8>, Vec<u8>), Box<dyn std::error::Error>> {
let key_pair = rcgen::KeyPair::generate()?;
let params = rcgen::CertificateParams::new(vec!["localhost".into(), "127.0.0.1".into()])?;
let cert = params.self_signed(&key_pair)?;
let cert_pem = cert.pem();
let key_pem = key_pair.serialize_pem();
(cert_pem.into_bytes(), key_pem.into_bytes())
Ok((cert_pem.into_bytes(), key_pem.into_bytes()))
}
async fn start_host(send_pongs: bool) -> (MTPHost, Vec<u8>) {
let (cert_pem, key_pem) = generate_self_signed_cert();
async fn start_host(send_pongs: bool) -> Result<(MTPHost, Vec<u8>), Box<dyn std::error::Error>> {
let (cert_pem, key_pem) = generate_self_signed_cert().await?;
let host = MTPHost::new(
HostConfig::new(
IpAddr::V4(Ipv4Addr::LOCALHOST),
@ -24,14 +23,13 @@ async fn start_host(send_pongs: bool) -> (MTPHost, Vec<u8>) {
)
.with_pongs(send_pongs),
)
.await
.unwrap();
(host, cert_pem)
.await?;
Ok((host, cert_pem))
}
#[tokio::test]
async fn test_ping_rtt_and_missed_ping_teardown() {
let (mut host, cert_pem) = start_host(true).await;
async fn test_ping_rtt_and_missed_ping_teardown() -> Result<(), Box<dyn std::error::Error>> {
let (mut host, cert_pem) = start_host(true).await?;
let url = format!("https://127.0.0.1:{}", host.local_addr().port());
let client = MTPClient::connect(
@ -40,10 +38,9 @@ async fn test_ping_rtt_and_missed_ping_teardown() {
.with_ping_interval(std::time::Duration::from_millis(25))
.with_max_missed_pings(3),
)
.await
.unwrap();
.await?;
let _accepted = host.accept().await.unwrap().unwrap();
let _accepted = host.accept().await?;
let ping = tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
@ -53,12 +50,11 @@ async fn test_ping_rtt_and_missed_ping_teardown() {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
})
.await
.unwrap();
.await?;
assert!(ping > std::time::Duration::ZERO);
let (mut silent_host, silent_cert_pem) = start_host(false).await;
let (mut silent_host, silent_cert_pem) = start_host(false).await?;
let silent_url = format!("https://127.0.0.1:{}", silent_host.local_addr().port());
let silent_client = MTPClient::connect(
ClientConfig::new(silent_url)
@ -66,10 +62,9 @@ async fn test_ping_rtt_and_missed_ping_teardown() {
.with_ping_interval(std::time::Duration::from_millis(25))
.with_max_missed_pings(2),
)
.await
.unwrap();
.await?;
let _accepted = silent_host.accept().await.unwrap().unwrap();
let _accepted = silent_host.accept().await?;
let closed = tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
@ -82,4 +77,5 @@ async fn test_ping_rtt_and_missed_ping_teardown() {
.await;
assert!(closed.is_ok(), "client should close after missed pings");
Ok(())
}