Files
oak-editor/crates/oaktask/tests/customcache_test.rs
T
Mike-Solar 013a175707 refactor: workspace layout — crates/, app at root, legacy C++ removed
Single mechanical restructure commit:
- root Cargo.toml = oakapp bin + workspace; one cargo build produces
  oakapp, oak-cli, oak-worker, liboakengine.dylib
- app/rust/src -> src/ (app at repo root, no rust/ nesting)
- src/<mod>/rust -> crates/oak<mod>; src/oakcore-rs -> crates/oakcore;
  src/bindings/oakotio -> crates/oakotio; src/engine/rust ->
  crates/oakengine (keeps cdylib+staticlib+rlib)
- public C headers include/<mod>/ -> crates/oakengine/include/<mod>/
- OFX SDK headers vendored into crates/oakplugin/ofx/ (HostSupport gone)
- legacy deleted: old src/ C++ modules, engine/, core/, ffmpeg_bridge/,
  app/ (Qt), cli/worker C++, root CMakeLists, third_party/KDDockWidgets
  submodule, otio-install, all build-* output (~40GB)
- oakstorage kept but excluded from the workspace (skeleton w/ todos);
  gpui excluded (own workspace)
- verified: cargo build green, cargo test --workspace 1845/0
  (with the documented OCIO_RS_* env override for the homebrew OCIO)
2026-08-10 20:24:25 +08:00

110 lines
3.9 KiB
Rust

// Oak Video Editor - Non-Linear Video Editor
// Copyright (C) 2026 Oak Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//! CustomCacheTask lifecycle tests: the task parks until `finish()` (success)
//! or a real cancellation (`Err(Cancelled)`); the cancelled callback fires
//! only for real cancellations.
mod common;
use common::*;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use oaktask::customcache::CustomCacheTask;
use oaktask::task::Task;
fn spawn_parked(cc: CustomCacheTask) -> (std::thread::JoinHandle<oaktask::error::Result<()>>, Arc<oaktask::customcache::CustomCacheState>) {
let state = cc.state();
let atom = cc.base.get_cancel_atom();
let title = cc.base.title().to_string();
let mut outer = Task::new(&title, atom);
outer.set_behavior(Box::new(cc));
let handle = std::thread::spawn(move || outer.start());
// Let the worker park on the condvar.
std::thread::sleep(std::time::Duration::from_millis(50));
(handle, state)
}
/// The task title mirrors the C++ constructor; finishing wakes the parked
/// task which then reports success.
#[test]
fn finish_completes_parked_task() {
let _guard = MANAGER_LOCK.lock().unwrap();
reset_stubs();
let cc = CustomCacheTask::new("my-sequence");
assert_eq!(cc.base.title(), "Caching custom range for \"my-sequence\"");
let (handle, state) = spawn_parked(cc);
state.finish();
let result = handle.join().expect("worker panicked");
assert!(result.is_ok(), "finish should complete the task successfully");
// Re-run: a real cancel (no finish) reports Err(Cancelled).
let cc = CustomCacheTask::new("cancel-seq");
let (handle, state) = spawn_parked(cc);
state.cancel();
let result = handle.join().expect("worker panicked");
assert!(result.is_err(), "real cancellation should fail the task");
}
/// The cancelled callback fires only for a real cancellation, not for
/// `finish()`.
#[test]
fn cancelled_callback_fires_only_on_real_cancel() {
let _guard = MANAGER_LOCK.lock().unwrap();
reset_stubs();
// finish(): the callback must NOT fire.
let fired_on_finish = Arc::new(AtomicBool::new(false));
let mut cc = CustomCacheTask::new("finish-seq");
cc.set_cancelled_callback(Box::new({
let flag = fired_on_finish.clone();
move || flag.store(true, Ordering::SeqCst)
}));
let (handle, state) = spawn_parked(cc);
state.finish();
assert!(handle.join().expect("worker panicked").is_ok());
assert!(!fired_on_finish.load(Ordering::SeqCst), "finish must not fire the cancelled callback");
// real cancel: the callback fires.
let fired_on_cancel = Arc::new(AtomicBool::new(false));
let mut cc = CustomCacheTask::new("cancel-seq");
cc.set_cancelled_callback(Box::new({
let flag = fired_on_cancel.clone();
move || flag.store(true, Ordering::SeqCst)
}));
let (handle, state) = spawn_parked(cc);
state.cancel();
assert!(handle.join().expect("worker panicked").is_err());
assert!(fired_on_cancel.load(Ordering::SeqCst), "real cancel must fire the cancelled callback");
}
/// The cancel hook wakes the parked task even without finish().
#[test]
fn cancel_hook_wakes_parked_task() {
let _guard = MANAGER_LOCK.lock().unwrap();
reset_stubs();
let cc = CustomCacheTask::new("wake-seq");
let (handle, state) = spawn_parked(cc);
// Simulate the manager's cancel path (base.cancel -> atom + hook).
state.cancel();
let result = handle.join().expect("worker panicked");
assert!(result.is_err());
}