use std::{
    io,
    sync::mpsc,
};

use qubit_thread_pool::ExecutorService;

use super::mod_tests::{
    create_single_worker_pool,
    wait_started,
};

#[test]
fn test_thread_pool_state_reports_queue_saturation_and_shutdown_cancellation() {
    let pool = create_single_worker_pool();
    let (started_tx, started_rx) = mpsc::channel();
    let (release_tx, release_rx) = mpsc::channel();
    let running = pool
        .submit_tracked(move || {
            started_tx.send(()).unwrap();
            release_rx
                .recv()
                .map_err(|err| io::Error::other(err.to_string()))?;
            Ok::<(), io::Error>(())
        })
        .unwrap();
    wait_started(started_rx);
    let queued = pool.submit_tracked(|| Ok::<_, io::Error>(())).unwrap();
    let report = pool.stop();

    assert_eq!(1, report.queued);
    assert_eq!(1, report.cancelled);
    assert!(queued.is_done());
    release_tx.send(()).unwrap();
    running.get().unwrap();
    pool.wait_termination();
}
