//
// Copyright (C) 2024 Automated Design Corp.. All Rights Reserved.
// Created Date: 2024-10-03 09:15:21
// -----
// Last Modified: 2024-10-08 20:55:19
// -----
//
//

use mechutil::async_channel::async_pipeline;
use std::time::Instant;
use tokio::time::Duration;

#[tokio::test]
async fn test_pipeline_latency() {
    // Set up the pipeline with a buffer size of 32
    let (mut chan1, mut chan2) = async_pipeline::<String, String>(32);

    // Message to be sent through the channel
    let message = "This is a test.".to_string();

    // Start the clock
    let start_time = Instant::now();

    // Send a message from chan1 to chan2
    chan1.send(message.clone()).await.unwrap();

    // Receive the message from chan2
    let received_message = chan2.recv().await.unwrap();

    // Stop the clock
    let elapsed_time = start_time.elapsed();

    // Assert that the received message matches the sent message
    assert_eq!(received_message, message);
    assert!(
        elapsed_time.as_micros() < 1000,
        "Latency was too high: {} µs",
        elapsed_time.as_micros()
    );

    // Print the latency in microseconds
    println!("Latency: {} µs", elapsed_time.as_micros());
}
