397 lines
12 KiB
Rust
397 lines
12 KiB
Rust
use std::collections::BTreeMap;
|
|
use std::time::Duration;
|
|
|
|
use rcom::domain::{HostAlias, Location, SourceId};
|
|
use rcom::operations::{
|
|
CollisionPolicy, CommitOutcome, EventDisposition, ItemId, ItemOutcome, ItemPhase, MutationKind,
|
|
OperationEvent, OperationEventKind, OperationKind, OperationPlan, OperationStart,
|
|
PlanGeneration, PlanId, PlanValidationError, ProgressUpdate, SourceCleanupOutcome, SourceItem,
|
|
TaskManager, TaskResult, TaskStatus, WorkKind, WorkPlan,
|
|
};
|
|
|
|
const ALL_WORK_KINDS: [WorkKind; 7] = [
|
|
WorkKind::CopyMove,
|
|
WorkKind::Create,
|
|
WorkKind::Rename,
|
|
WorkKind::Trash,
|
|
WorkKind::Delete,
|
|
WorkKind::Undo,
|
|
WorkKind::EditUpload,
|
|
];
|
|
|
|
fn source(id: u64, path: &str) -> SourceItem {
|
|
SourceItem::new(ItemId::new(id), Location::local(path))
|
|
}
|
|
|
|
fn operation(id: u64, kind: OperationKind) -> OperationPlan {
|
|
OperationPlan::new(
|
|
PlanId::new(id),
|
|
PlanGeneration::new(id + 100),
|
|
kind,
|
|
vec![source(1, "/source/one")],
|
|
Location::local("/destination"),
|
|
CollisionPolicy::Skip,
|
|
Some(1),
|
|
Some(128),
|
|
BTreeMap::new(),
|
|
)
|
|
.expect("test operation has one source")
|
|
}
|
|
|
|
fn work(id: u64, kind: WorkKind) -> WorkPlan {
|
|
work_with_sources(id, kind, vec![source(1, "/source/one")])
|
|
}
|
|
|
|
fn work_with_sources(id: u64, kind: WorkKind, sources: Vec<SourceItem>) -> WorkPlan {
|
|
let expected_items = sources.len() as u64;
|
|
WorkPlan::new(
|
|
PlanId::new(id),
|
|
PlanGeneration::new(id + 100),
|
|
kind,
|
|
sources,
|
|
Location::local("/destination"),
|
|
CollisionPolicy::Skip,
|
|
Some(expected_items),
|
|
Some(128),
|
|
BTreeMap::new(),
|
|
)
|
|
.expect("test work has valid sources")
|
|
}
|
|
|
|
fn event(start: OperationStart, kind: OperationEventKind) -> OperationEvent {
|
|
OperationEvent::new(start.operation_id, start.plan_id, kind)
|
|
}
|
|
|
|
fn succeeded(item: u64) -> ItemOutcome {
|
|
ItemOutcome::new(
|
|
ItemId::new(item),
|
|
ItemPhase::Succeeded,
|
|
CommitOutcome::Committed,
|
|
SourceCleanupOutcome::NotRequired,
|
|
)
|
|
}
|
|
|
|
fn apply(manager: &mut TaskManager, start: OperationStart, kind: OperationEventKind) {
|
|
assert_eq!(
|
|
manager.apply_event(event(start, kind)),
|
|
EventDisposition::Applied
|
|
);
|
|
}
|
|
|
|
fn complete_active(manager: &mut TaskManager) {
|
|
let start = manager.active_operation().expect("an active work item");
|
|
apply(
|
|
manager,
|
|
start,
|
|
OperationEventKind::ItemStarted(ItemId::new(1)),
|
|
);
|
|
apply(
|
|
manager,
|
|
start,
|
|
OperationEventKind::ItemFinished(succeeded(1)),
|
|
);
|
|
apply(manager, start, OperationEventKind::Completed);
|
|
}
|
|
|
|
#[test]
|
|
fn every_work_variant_delegates_the_complete_common_snapshot() {
|
|
let source_endpoint = SourceId::Ssh(HostAlias::new("source"));
|
|
let destination_endpoint = SourceId::Ssh(HostAlias::new("destination"));
|
|
let sources = vec![
|
|
SourceItem::new(
|
|
ItemId::new(1),
|
|
Location::ssh(HostAlias::new("source"), "/source/one"),
|
|
),
|
|
SourceItem::new(
|
|
ItemId::new(2),
|
|
Location::ssh(HostAlias::new("source"), "/source/two"),
|
|
),
|
|
];
|
|
let destination = Location::ssh(HostAlias::new("destination"), "/destination");
|
|
let epochs = BTreeMap::from([
|
|
(source_endpoint.clone(), 11),
|
|
(destination_endpoint.clone(), 12),
|
|
]);
|
|
let expected_epochs = epochs.clone();
|
|
|
|
for work_kind in ALL_WORK_KINDS {
|
|
let mut input_epochs = epochs.clone();
|
|
let plan = WorkPlan::new(
|
|
PlanId::new(7),
|
|
PlanGeneration::new(3),
|
|
work_kind,
|
|
sources.clone(),
|
|
destination.clone(),
|
|
CollisionPolicy::Ask,
|
|
Some(2),
|
|
None,
|
|
input_epochs.clone(),
|
|
)
|
|
.expect("common source invariants accept this snapshot");
|
|
input_epochs.insert(SourceId::Ssh(HostAlias::new("later")), 99);
|
|
|
|
assert_eq!(plan.id(), PlanId::new(7));
|
|
assert_eq!(plan.generation(), PlanGeneration::new(3));
|
|
assert_eq!(plan.work_kind(), work_kind);
|
|
assert_eq!(plan.sources(), sources);
|
|
assert_eq!(plan.destination(), &destination);
|
|
assert_eq!(plan.policy(), CollisionPolicy::Ask);
|
|
assert_eq!(plan.expected_items(), Some(2));
|
|
assert_eq!(plan.expected_bytes(), None);
|
|
assert_eq!(plan.endpoint_epochs(), &expected_epochs);
|
|
assert_eq!(plan.endpoint_epoch(&source_endpoint), Some(11));
|
|
assert_eq!(plan.endpoint_epoch(&destination_endpoint), Some(12));
|
|
assert_eq!(
|
|
plan.endpoint_epoch(&SourceId::Ssh(HostAlias::new("later"))),
|
|
None
|
|
);
|
|
assert_eq!(plan.operation_plan().id(), plan.id());
|
|
assert_eq!(plan.clone().into_operation_plan(), *plan.operation_plan());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn copy_move_wrapper_preserves_legacy_kind_and_collision_decisions() {
|
|
for kind in [OperationKind::Copy, OperationKind::Move] {
|
|
let policies = BTreeMap::from([(ItemId::new(1), CollisionPolicy::Overwrite)]);
|
|
let operation = operation(9, kind).with_collision_policies(policies.clone());
|
|
let expected = operation.clone();
|
|
|
|
let plan = WorkPlan::from_operation(operation);
|
|
assert_eq!(plan.work_kind(), WorkKind::CopyMove);
|
|
assert_eq!(plan.kind(), kind);
|
|
assert_eq!(plan.collision_policies(), &policies);
|
|
assert_eq!(
|
|
plan.collision_policy_for(ItemId::new(1)),
|
|
CollisionPolicy::Overwrite
|
|
);
|
|
assert_eq!(plan.operation_plan(), &expected);
|
|
assert_eq!(plan.into_operation_plan(), expected);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn every_work_variant_reuses_operation_source_validation() {
|
|
for work_kind in ALL_WORK_KINDS {
|
|
let empty = WorkPlan::new(
|
|
PlanId::new(1),
|
|
PlanGeneration::new(1),
|
|
work_kind,
|
|
Vec::new(),
|
|
Location::local("/destination"),
|
|
CollisionPolicy::Skip,
|
|
Some(0),
|
|
Some(0),
|
|
BTreeMap::new(),
|
|
);
|
|
assert_eq!(empty.unwrap_err(), PlanValidationError::EmptySources);
|
|
|
|
let duplicate = WorkPlan::new(
|
|
PlanId::new(1),
|
|
PlanGeneration::new(1),
|
|
work_kind,
|
|
vec![source(1, "/source/a"), source(1, "/source/b")],
|
|
Location::local("/destination"),
|
|
CollisionPolicy::Skip,
|
|
Some(2),
|
|
None,
|
|
BTreeMap::new(),
|
|
);
|
|
assert_eq!(duplicate.unwrap_err(), PlanValidationError::DuplicateItemId);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn task_history_replays_each_work_variant_and_never_restarts_a_terminal_plan() {
|
|
for work_kind in ALL_WORK_KINDS {
|
|
let mut manager = TaskManager::new();
|
|
let expected = work(1, work_kind);
|
|
let task_id = manager.start_work(expected.clone());
|
|
let start = manager.active_operation().expect("work starts immediately");
|
|
assert_eq!(start.task_id, task_id);
|
|
assert_eq!(start.plan_id, expected.id());
|
|
let task = manager.task(task_id).expect("accepted work is retained");
|
|
|
|
assert_eq!(task.plan(), &expected);
|
|
assert_eq!(task.operation_plan(), expected.operation_plan());
|
|
assert_eq!(task.plan().work_kind(), work_kind);
|
|
assert_eq!(task.progress().total_items(), Some(1));
|
|
assert_eq!(task.progress().total_bytes(), Some(128));
|
|
|
|
complete_active(&mut manager);
|
|
assert_eq!(
|
|
manager.task(task_id).unwrap().status(),
|
|
TaskStatus::Succeeded
|
|
);
|
|
assert_eq!(
|
|
manager.task(task_id).unwrap().result(),
|
|
Some(TaskResult::Succeeded)
|
|
);
|
|
assert_eq!(manager.start_work(expected), task_id);
|
|
assert_eq!(manager.len(), 1);
|
|
assert_eq!(manager.active_task(), None);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn mixed_start_apis_are_idempotent_and_keep_one_active_writer() {
|
|
let mut manager = TaskManager::new();
|
|
let first_plan = operation(1, OperationKind::Move);
|
|
let first_id = manager.start(first_plan);
|
|
let first_start = manager.active_operation().unwrap();
|
|
let second_plan = work(2, WorkKind::Create);
|
|
let second_id = manager.start_work(second_plan.clone());
|
|
|
|
assert_eq!(
|
|
manager.task(first_id).unwrap().plan().kind(),
|
|
OperationKind::Move
|
|
);
|
|
assert_eq!(
|
|
manager.task(first_id).unwrap().plan().work_kind(),
|
|
WorkKind::CopyMove
|
|
);
|
|
assert_eq!(
|
|
manager.task(second_id).unwrap().status(),
|
|
TaskStatus::Queued
|
|
);
|
|
assert_eq!(manager.queued_len(), 1);
|
|
|
|
assert_eq!(manager.start(operation(1, OperationKind::Copy)), first_id);
|
|
assert_eq!(manager.start_work(work(1, WorkKind::Delete)), first_id);
|
|
assert_eq!(
|
|
manager.start_work(work_with_sources(
|
|
2,
|
|
WorkKind::Trash,
|
|
vec![source(1, "/changed"), source(2, "/changed-too")],
|
|
)),
|
|
second_id
|
|
);
|
|
assert_eq!(manager.len(), 2);
|
|
assert_eq!(manager.active_operation(), Some(first_start));
|
|
assert_eq!(manager.task(second_id).unwrap().plan(), &second_plan);
|
|
|
|
complete_active(&mut manager);
|
|
assert_eq!(manager.active_task(), Some(second_id));
|
|
assert_eq!(
|
|
manager.task(second_id).unwrap().status(),
|
|
TaskStatus::Running
|
|
);
|
|
complete_active(&mut manager);
|
|
assert_eq!(manager.active_task(), None);
|
|
|
|
assert_eq!(manager.start_work(second_plan), second_id);
|
|
assert_eq!(manager.start(operation(1, OperationKind::Move)), first_id);
|
|
assert_eq!(manager.len(), 2);
|
|
assert_eq!(manager.active_task(), None);
|
|
}
|
|
|
|
#[test]
|
|
fn non_copy_work_rejects_stale_events_and_preserves_cancellation_gates() {
|
|
let mut manager = TaskManager::new();
|
|
let old_id = manager.start_work(work(1, WorkKind::Rename));
|
|
let old = manager.active_operation().unwrap();
|
|
complete_active(&mut manager);
|
|
|
|
let current_id = manager.start_work(work_with_sources(
|
|
2,
|
|
WorkKind::Delete,
|
|
vec![source(1, "/source/one"), source(2, "/source/two")],
|
|
));
|
|
let current = manager.active_operation().unwrap();
|
|
let next_id = manager.start_work(work(3, WorkKind::EditUpload));
|
|
apply(
|
|
&mut manager,
|
|
current,
|
|
OperationEventKind::Progress(ProgressUpdate::new(
|
|
1,
|
|
50,
|
|
Some(ItemId::new(1)),
|
|
Duration::from_secs(4),
|
|
)),
|
|
);
|
|
apply(
|
|
&mut manager,
|
|
current,
|
|
OperationEventKind::ItemStarted(ItemId::new(1)),
|
|
);
|
|
|
|
let stale_events = [
|
|
event(
|
|
old,
|
|
OperationEventKind::Progress(ProgressUpdate::new(
|
|
2,
|
|
999,
|
|
Some(ItemId::new(2)),
|
|
Duration::from_secs(99),
|
|
)),
|
|
),
|
|
event(old, OperationEventKind::ItemStarted(ItemId::new(2))),
|
|
OperationEvent::new(
|
|
current.operation_id,
|
|
old.plan_id,
|
|
OperationEventKind::ItemFinished(succeeded(1)),
|
|
),
|
|
];
|
|
for stale in stale_events {
|
|
assert_eq!(manager.apply_event(stale), EventDisposition::Stale);
|
|
}
|
|
assert!(
|
|
!manager
|
|
.task(current_id)
|
|
.unwrap()
|
|
.has_started(ItemId::new(2))
|
|
);
|
|
assert_eq!(
|
|
manager.task(old_id).unwrap().status(),
|
|
TaskStatus::Succeeded
|
|
);
|
|
assert_eq!(manager.task(next_id).unwrap().status(), TaskStatus::Queued);
|
|
|
|
assert!(manager.mutation_permitted(
|
|
current.operation_id,
|
|
current.plan_id,
|
|
ItemId::new(1),
|
|
MutationKind::Commit,
|
|
));
|
|
assert!(manager.request_cancellation(current_id));
|
|
for kind in [
|
|
MutationKind::CreatePart,
|
|
MutationKind::Commit,
|
|
MutationKind::DeleteSource,
|
|
] {
|
|
assert!(!manager.mutation_permitted(
|
|
current.operation_id,
|
|
current.plan_id,
|
|
ItemId::new(1),
|
|
kind,
|
|
));
|
|
}
|
|
assert_eq!(
|
|
manager.apply_event(event(
|
|
current,
|
|
OperationEventKind::ItemStarted(ItemId::new(2)),
|
|
)),
|
|
EventDisposition::Rejected
|
|
);
|
|
apply(
|
|
&mut manager,
|
|
current,
|
|
OperationEventKind::CancellationAcknowledged,
|
|
);
|
|
|
|
let cancelled = manager.task(current_id).unwrap();
|
|
assert_eq!(cancelled.status(), TaskStatus::Cancelled);
|
|
assert_eq!(
|
|
cancelled.outcome(ItemId::new(1)).unwrap().commit(),
|
|
CommitOutcome::Unknown
|
|
);
|
|
assert_eq!(
|
|
cancelled.outcome(ItemId::new(2)).unwrap().commit(),
|
|
CommitOutcome::NotAttempted
|
|
);
|
|
assert_eq!(manager.active_task(), Some(next_id));
|
|
assert_eq!(
|
|
manager.apply_event(event(current, OperationEventKind::Completed)),
|
|
EventDisposition::Stale
|
|
);
|
|
}
|