tenshift-core 0.1.2

Thread-safe, backpressure-aware data loading pipeline for iterative processing
Documentation
#![allow(clippy::pedantic)]

use tenshift_core::error::Result;
use tenshift_core::pipeline::ErrorPolicy;
use tenshift_core::sample::Sample;
use tenshift_core::source::{Source, SourceIterator};
use tenshift_core::Pipeline;

struct RapidBurstSource {
    target: usize,
}

impl Source for RapidBurstSource {
    fn open(&self) -> Result<Box<dyn SourceIterator>> {
        Ok(Box::new(RapidBurstIterator {
            target: self.target,
            idx: 0,
        }))
    }
    fn name(&self) -> &str {
        "burst"
    }
}

struct RapidBurstIterator {
    target: usize,
    idx: usize,
}

impl SourceIterator for RapidBurstIterator {
    fn next_sample(&mut self) -> Option<Result<Sample>> {
        if self.idx >= self.target {
            return None;
        }
        self.idx += 1;
        Some(Ok(Sample::new()))
    }
}

#[test]
fn test_sqlite_massive_oversubscription_and_cancellation() {
    // Inject massive thread contention:
    // Hundreds of worker threads competing for tiny chunks and tiny prefetch buffers.
    // Tests lock-free channel queue wrapping around and ABA anomalies in Crossbeam.

    let pipeline = Pipeline::from_source(RapidBurstSource { target: 10_000 })
        .workers(128) // Extreme OS thread oversubscription (standard CPUs have 8-32 cores)
        .prefetch(1) // Force immediate backpressure blocking across 128 workers simultaneously
        .chunk_size(1) // Force an allocation/channel switch every single input
        .on_error(ErrorPolicy::Fail)
        .map(Ok)
        .batch(1);

    let mut iter = pipeline
        .start()
        .expect("Pipeline should not fail 128 thread init");

    // Let the first 50 items squeeze through the massive 128-thread pipeline
    for _ in 0..50 {
        let _ = iter.next();
    }

    // Now trigger cancellation while 128 hungry threads are blocking on bounded(1)
    drop(iter);

    // If Tenshift survives this, the channels successfully unblocked 128 threads via
    // proper `AtomicBool` propagation without losing tracking or hitting a kernel deadlock.
}