crisol-core 1.1.0

Crisol core: an egui-free data model, themes, metrics, localization, commands, state, and storage.
Documentation
// spec: ROB-04
use super::*;
use std::sync::atomic::AtomicUsize;

struct Work(u64);
impl Job for Work {
    type Output = u64;
    fn run(&self, context: &JobContext) -> Result<u64, JobError> {
        context.check()?;
        context.progress().set_total(1);
        context.progress().step();
        Ok(self.0 + 1)
    }
}

struct Slow;
impl Job for Slow {
    type Output = ();
    fn run(&self, context: &JobContext) -> Result<(), JobError> {
        while !context.token().is_cancelled() {
            std::hint::spin_loop();
        }
        Err(JobError::Cancelled)
    }
}

struct Boom;
impl Job for Boom {
    type Output = ();
    fn run(&self, _context: &JobContext) -> Result<(), JobError> {
        panic!("boom");
    }
}

#[test]
fn progress_is_shared_and_bounded() {
    let progress = Progress::new();
    assert_eq!(progress.fraction(), None);
    progress.set_total(4);
    progress.set_current(2);
    assert_eq!(progress.fraction(), Some(0.5));
    progress.set_current(9);
    assert_eq!(progress.fraction(), Some(1.0));
    progress.set_label("indexing");
    assert_eq!(progress.label(), "indexing");
}

#[test]
fn a_token_cancels_and_checks() {
    let token = CancellationToken::new();
    assert!(token.check().is_ok());
    token.cancel();
    assert!(token.is_cancelled());
    assert_eq!(token.check(), Err(JobError::Cancelled));
}

#[test]
fn the_queue_runs_reports_and_joins() {
    let mut queue = JobQueue::new();
    let handle = queue.spawn(Work(41));
    assert_eq!(handle.progress().total(), None);
    #[cfg(all(feature = "threads", not(target_family = "wasm")))]
    let results = queue.wait();
    #[cfg(not(all(feature = "threads", not(target_family = "wasm"))))]
    let results = {
        assert_eq!(queue.run_next(), Some(handle.id()));
        queue.drain()
    };
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].1, Ok(42));
    assert_eq!(handle.status(), JobStatus::Finished);
}

#[test]
#[cfg(not(target_family = "wasm"))]
fn a_panic_is_reported_as_an_error() {
    let mut queue = JobQueue::new();
    let _ = queue.spawn(Boom);
    #[cfg(all(feature = "threads", not(target_family = "wasm")))]
    let results = queue.wait();
    #[cfg(not(all(feature = "threads", not(target_family = "wasm"))))]
    let results = {
        queue.run_next();
        queue.drain()
    };
    assert!(matches!(results[0].1, Err(JobError::Panicked(_))));
}

#[test]
fn cancelling_stops_a_job_and_drop_joins() {
    let mut queue = JobQueue::new();
    let handle = queue.spawn(Slow);
    handle.cancel();
    #[cfg(all(feature = "threads", not(target_family = "wasm")))]
    let results = queue.wait();
    #[cfg(not(all(feature = "threads", not(target_family = "wasm"))))]
    let results = {
        queue.run_next();
        queue.drain()
    };
    assert!(matches!(results[0].1, Err(JobError::Cancelled)));
    assert_eq!(handle.status(), JobStatus::Cancelled);
    drop(queue);
}

#[test]
fn the_event_queue_wakes_and_drains() {
    let wakes = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&wakes);
    let queue = EventQueue::with_waker(CallbackWaker::new(move || {
        counter.fetch_add(1, Ordering::SeqCst);
    }));
    queue.push(1);
    queue.push(2);
    assert_eq!(queue.len(), 2);
    assert_eq!(queue.drain(), [1, 2]);
    assert!(queue.is_empty());
    assert_eq!(wakes.load(Ordering::SeqCst), 2);
}