open-net 0.3.5

A cross-platform async networking library for Rust with HTTP and WebSocket clients, network status monitoring, configurable retries, WebSocket reconnection and heartbeats, and managed client lifecycles. Reuse common networking infrastructure across applications to avoid repeated implementation and debugging, helping save development time and AI coding tokens.
Documentation
#![cfg(test)]

use super::CommonEngine;
use crate::common::common_error::CommonError;
use crate::common::log::log_def::LogType;
use std::sync::{mpsc, Arc};
use std::time::Duration;
use tokio::runtime::{Handle, Id};

type TestResult<T = ()> = Result<T, Box<dyn std::error::Error + Send + Sync>>;
const WAIT: Duration = Duration::from_secs(5);

#[track_caller]
fn test_error(message: impl Into<String>) -> Box<dyn std::error::Error + Send + Sync> {
    let location = std::panic::Location::caller();
    let message = message.into();
    std::io::Error::other(format!("{location}: {message}")).into()
}

#[track_caller]
fn common_error(error: CommonError) -> Box<dyn std::error::Error + Send + Sync> {
    test_error(format!("common execution failed: {error:?}"))
}

fn engine() -> TestResult<Arc<CommonEngine>> {
    Ok(Arc::new(CommonEngine::new(8, 8).map_err(common_error)?))
}

fn deliver<T>(sender: mpsc::Sender<T>, value: T) {
    if sender.send(value).is_err() {
        crate::log_e!(LogType::Common; "execution_test", "error", "test_observation_receiver_closed");
    }
}

async fn observe_runtime(value: usize) -> TestResult<(Id, usize)> {
    // Exercise a real timer as well as an async scheduling point. Handle::block_on
    // must continue using the live engine's multithreaded I/O/time drivers.
    tokio::task::yield_now().await;
    tokio::time::sleep(Duration::from_millis(1)).await;
    Ok((Handle::try_current()?.id(), value))
}

#[track_caller]
fn check_observation(actual: (Id, usize), expected: (Id, usize)) -> TestResult {
    if actual != expected {
        return Err(test_error(
            "post/invoke changed runtime identity, return value, or queue order",
        ));
    }
    Ok(())
}

#[test]
fn post_and_invoke_from_plain_thread_preserve_engine_execution_and_async_queue_order() -> TestResult
{
    let engine = engine()?;
    let engine_id = engine.rt.handle().id();
    let (sender, receiver) = mpsc::channel();
    for value in [1, 2] {
        let sender = sender.clone();
        engine.post(async move {
            deliver(sender, observe_runtime(value).await);
        });
    }
    drop(sender);
    let invoked = engine.invoke(observe_runtime(3)).map_err(common_error)??;
    check_observation(invoked, (engine_id, 3))?;
    for value in [1, 2] {
        check_observation(receiver.recv_timeout(WAIT)??, (engine_id, value))?;
    }
    Ok(())
}

fn exercise_foreign_runtime() -> TestResult {
    let caller_id = Handle::try_current()?.id();
    let engine = engine()?;
    let engine_id = engine.rt.handle().id();
    if caller_id == engine_id {
        return Err(test_error(
            "foreign runtime test accidentally used the engine runtime",
        ));
    }
    let (sender, receiver) = mpsc::channel();
    engine.post(async move {
        deliver(sender, observe_runtime(4).await);
    });
    let invoked = engine.invoke(observe_runtime(5)).map_err(common_error)??;
    check_observation(invoked, (engine_id, 5))?;
    check_observation(receiver.recv_timeout(WAIT)??, (engine_id, 4))?;
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn post_and_invoke_from_foreign_current_thread_runtime_keep_engine_identity() -> TestResult {
    exercise_foreign_runtime()
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn post_and_invoke_from_foreign_multi_thread_runtime_keep_engine_identity() -> TestResult {
    exercise_foreign_runtime()
}

#[test]
fn invoke_from_engine_post_uses_the_existing_sync_queue_without_nested_runtime_failure(
) -> TestResult {
    let engine = engine()?;
    let engine_id = engine.rt.handle().id();
    let posted_engine = Arc::clone(&engine);
    let (sender, receiver) = mpsc::channel();
    engine.post(async move {
        let observed = posted_engine
            .invoke(observe_runtime(6))
            .map_err(common_error)
            .and_then(|result| result);
        deliver(sender, observed);
    });
    check_observation(receiver.recv_timeout(WAIT)??, (engine_id, 6))?;
    Ok(())
}

#[test]
fn explicit_shutdown_waits_for_a_foreign_runtime_bridge_thread() -> TestResult {
    let engine = engine()?;
    let (started, entry) = mpsc::channel();
    let (release, gate) = mpsc::channel();
    let bridged_engine = Arc::clone(&engine);
    let caller = std::thread::spawn(move || -> Result<(), String> {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .map_err(|error| error.to_string())?;
        runtime.block_on(async move {
            bridged_engine
                .run_blocking_on_engine_rt(move || {
                    let _ = started.send(());
                    let _ = gate.recv_timeout(WAIT);
                })
                .map_err(|error| format!("bridge failed: {error:?}"))
        })
    });
    entry.recv_timeout(WAIT)?;
    let (finished, completion) = mpsc::channel();
    let retiring = Arc::clone(&engine);
    let coordinator = std::thread::spawn(move || {
        let _ = finished.send(retiring.shutdown_blocking());
    });
    assert!(completion.recv_timeout(Duration::from_millis(50)).is_err());
    release.send(())?;
    completion.recv_timeout(WAIT)?.map_err(common_error)?;
    caller
        .join()
        .map_err(|_| test_error("foreign runtime caller panicked"))??;
    coordinator
        .join()
        .map_err(|_| test_error("bridge coordinator panicked"))?;
    let runtime = tokio::runtime::Builder::new_current_thread().build()?;
    assert_eq!(
        runtime.block_on(async { engine.run_blocking_on_engine_rt(|| ()) }),
        Err(CommonError::RuntimeError)
    );
    Ok(())
}