GitCorner

Code

ossartcrafttexturecraft🌐
d84914ef1e5a
//! A bake job belongs to the mesh it started on: when `file.changeMesh` (or a reimport) swaps
//! the mesh meanwhile, the job is cancelled and its result is dropped.

use std::sync::Arc;
use std::sync::atomic::Ordering;

use egui_kittest::Harness;
use texturecraft_engine::bake::Progress;
use texturecraft_ui_egui::{BakeJob, RunningBake, TextureCraftApp};

/// An app with a running bake job (its worker never answers) started on mesh `mesh_id`.
fn harness_with_job(mesh_id: impl FnOnce(u64) -> u64) -> (Harness<'static, TextureCraftApp>, Arc<Progress>) {
    let session = texturecraft_engine::sample_session(256, 64).unwrap();
    let mut h = Harness::builder().with_size(egui::vec2(1600.0, 960.0)).build_eframe(|cc| TextureCraftApp::new(cc, session));
    h.run_steps(4);
    let progress = Arc::new(Progress::new());
    let (tx, receiver) = std::sync::mpsc::channel();
    let (maps_tx, maps) = std::sync::mpsc::channel();
    let id = mesh_id(h.state().session.mesh_id);
    // Leak the sender so the channel stays empty instead of closed.
    std::mem::forget(tx);
    std::mem::forget(maps_tx);
    let running =
        RunningBake { set_index: 0, progress: progress.clone(), receiver, maps, maps_done: Arc::new(std::sync::atomic::AtomicUsize::new(0)), maps_total: 1 };
    h.state_mut().bake = Some(BakeJob { queue: vec![], current: Some(running), total: 1, mesh_id: id });
    (h, progress)
}

#[test]
fn a_job_on_a_swapped_mesh_is_cancelled_and_dropped() {
    let (mut h, progress) = harness_with_job(|current| current + 1);
    h.step();
    assert!(h.state().bake.is_none(), "the job is dropped");
    assert!(progress.cancel.load(Ordering::Relaxed), "and its worker is told to stop");
}

#[test]
fn a_job_on_the_current_mesh_keeps_running() {
    let (mut h, progress) = harness_with_job(|current| current);
    h.step();
    assert!(h.state().bake.is_some(), "the job stays");
    assert!(!progress.cancel.load(Ordering::Relaxed));
}