zpinger 0.7.0

Async, protocol-agnostic latency probe library: TCP / UDP / HTTP(S) / WebSocket / DNS / MQTT / gRPC / HLS / TLS / NTP / STUN / TURN / RTSP / RTMP / QUIC
Documentation
use super::*;
use async_trait::async_trait;
use std::io::{Error, ErrorKind, Result};
use std::time::Duration;

struct OkPinger;
#[async_trait]
impl Pinger for OkPinger {
    async fn ping(&self) -> Result<()> {
        Ok(())
    }
}

struct ErrPinger;
#[async_trait]
impl Pinger for ErrPinger {
    async fn ping(&self) -> Result<()> {
        Err(Error::other("Test fail"))
    }
}

struct SleepPinger {
    duration: Duration,
}
#[async_trait]
impl Pinger for SleepPinger {
    async fn ping(&self) -> Result<()> {
        tokio::time::sleep(self.duration).await;
        Ok(())
    }
}

#[tokio::test]
async fn test_timed_ok() {
    let elapsed = timed(&OkPinger).await.unwrap();
    assert!(elapsed < Duration::from_millis(100));
}

#[tokio::test]
async fn test_timed_err() {
    assert_eq!(
        Err(ErrorKind::Other),
        timed(&ErrPinger).await.map_err(|e| e.kind())
    );
}

#[tokio::test]
async fn test_timed_measures_duration() {
    let p = SleepPinger {
        duration: Duration::from_millis(20),
    };
    let elapsed = timed(&p).await.unwrap();
    assert!(elapsed >= Duration::from_millis(20));
}

#[tokio::test]
async fn test_timed_via_dyn_trait() {
    let p: Box<dyn Pinger> = Box::new(OkPinger);
    let elapsed = timed(p.as_ref()).await.unwrap();
    assert!(elapsed < Duration::from_millis(100));
}