| 1 | use std::{ |
| 2 | cell::RefCell, |
| 3 | fs, |
| 4 | path::{Path, PathBuf}, |
| 5 | rc::Rc, |
| 6 | sync::atomic::{AtomicU64, Ordering}, |
| 7 | time::{Duration, Instant}, |
| 8 | }; |
| 9 | use zipelo_ui_memory_app::{archive_process::ProcessFactory, archive_ui::*}; |
| 10 | static NEXT: AtomicU64 = AtomicU64::new(0); |
| 11 | struct Scratch(PathBuf); |
| 12 | impl Scratch { |
| 13 | fn new() -> Self { |
| 14 | let root = std::env::temp_dir().join("app-real-worker"); |
| 15 | fs::create_dir_all(&root).unwrap(); |
| 16 | let path = root.join(format!( |
| 17 | "{}-{}", |
| 18 | std::process::id(), |
| 19 | NEXT.fetch_add(1, Ordering::Relaxed) |
| 20 | )); |
| 21 | Self(make_directory(&path)) |
| 22 | } |
| 23 | fn path(&self, name: &str) -> PathBuf { |
| 24 | self.0.join(name) |
| 25 | } |
| 26 | } |
| 27 | impl Drop for Scratch { |
| 28 | fn drop(&mut self) { |
| 29 | fs::remove_dir_all(&self.0).unwrap(); |
| 30 | } |
| 31 | } |
| 32 | fn make_directory(path: &Path) -> PathBuf { |
| 33 | let mut builder = fs::DirBuilder::new(); |
| 34 | #[cfg(unix)] |
| 35 | { |
| 36 | use std::os::unix::fs::DirBuilderExt; |
| 37 | builder.mode(0o700); |
| 38 | } |
| 39 | builder.create(path).unwrap(); |
| 40 | path.canonicalize().unwrap() |
| 41 | } |
| 42 | fn binary(name: &str) -> PathBuf { |
| 43 | let current = std::env::current_exe().unwrap(); |
| 44 | let path = current |
| 45 | .parent() |
| 46 | .unwrap() |
| 47 | .parent() |
| 48 | .unwrap() |
| 49 | .join(format!("{name}{}", std::env::consts::EXE_SUFFIX)); |
| 50 | assert!( |
| 51 | path.is_file(), |
| 52 | "실제 작업자와 시험 probe를 먼저 빌드해야 함: {}", |
| 53 | path.display() |
| 54 | ); |
| 55 | path |
| 56 | } |
| 57 | #[derive(Default)] |
| 58 | struct Observation { |
| 59 | jobs: Vec<Vec<&'static str>>, |
| 60 | starts: Vec<Start>, |
| 61 | } |
| 62 | struct ObservedFactory { |
| 63 | process: ProcessFactory, |
| 64 | observed: Rc<RefCell<Observation>>, |
| 65 | } |
| 66 | struct ObservedConnection { |
| 67 | connection: Box<dyn Connection>, |
| 68 | index: usize, |
| 69 | observed: Rc<RefCell<Observation>>, |
| 70 | } |
| 71 | impl Factory for ObservedFactory { |
| 72 | fn connect(&mut self, job: u64) -> Result<Box<dyn Connection>, String> { |
| 73 | let connection = self.process.connect(job)?; |
| 74 | let index = self.observed.borrow().jobs.len(); |
| 75 | self.observed.borrow_mut().jobs.push(Vec::new()); |
| 76 | Ok(Box::new(ObservedConnection { |
| 77 | connection, |
| 78 | index, |
| 79 | observed: self.observed.clone(), |
| 80 | })) |
| 81 | } |
| 82 | } |
| 83 | impl Connection for ObservedConnection { |
| 84 | fn send(&mut self, request: Request) -> Result<(), String> { |
| 85 | if let Command::Start(start) = &request.command { |
| 86 | self.observed.borrow_mut().starts.push(start.clone()); |
| 87 | } |
| 88 | self.connection.send(request) |
| 89 | } |
| 90 | fn poll(&mut self) -> Option<HostEvent> { |
| 91 | let event = self.connection.poll()?; |
| 92 | let label = match &event { |
| 93 | HostEvent::StdoutEof => "stdout", |
| 94 | HostEvent::StderrEof => "stderr", |
| 95 | HostEvent::Exited { .. } => "wait", |
| 96 | HostEvent::CleanupComplete => "cleanup", |
| 97 | HostEvent::Fault(_) => "fault", |
| 98 | HostEvent::Frame { |
| 99 | event: Event::Terminal(_), |
| 100 | .. |
| 101 | } => "terminal", |
| 102 | _ => "frame", |
| 103 | }; |
| 104 | self.observed.borrow_mut().jobs[self.index].push(label); |
| 105 | Some(event) |
| 106 | } |
| 107 | } |
| 108 | impl Drop for ObservedConnection { |
| 109 | fn drop(&mut self) { |
| 110 | self.observed.borrow_mut().jobs[self.index].push("drop"); |
| 111 | } |
| 112 | } |
| 113 | fn observed_factory() -> (ObservedFactory, Rc<RefCell<Observation>>) { |
| 114 | let observed = Rc::new(RefCell::new(Observation::default())); |
| 115 | ( |
| 116 | ObservedFactory { |
| 117 | process: ProcessFactory::new(binary("zipelo-archive-worker")), |
| 118 | observed: observed.clone(), |
| 119 | }, |
| 120 | observed, |
| 121 | ) |
| 122 | } |
| 123 | fn assert_reaped(observed: &Rc<RefCell<Observation>>) { |
| 124 | for events in &observed.borrow().jobs { |
| 125 | for label in ["terminal", "stdout", "stderr", "cleanup", "wait", "drop"] { |
| 126 | assert_eq!( |
| 127 | events.iter().filter(|value| **value == label).count(), |
| 128 | 1, |
| 129 | "{events:?}" |
| 130 | ); |
| 131 | } |
| 132 | assert!(!events.contains(&"fault")); |
| 133 | assert_eq!(&events[events.len() - 2..], &["wait", "drop"]); |
| 134 | } |
| 135 | } |
| 136 | fn wait(model: &mut Model) { |
| 137 | let deadline = Instant::now() + Duration::from_secs(10); |
| 138 | while model.busy() { |
| 139 | model.poll(); |
| 140 | assert!( |
| 141 | Instant::now() < deadline, |
| 142 | "작업자 종료 시간 초과: {}", |
| 143 | model.status() |
| 144 | ); |
| 145 | std::thread::sleep(Duration::from_millis(2)); |
| 146 | } |
| 147 | } |
| 148 | fn completed(model: &Model) { |
| 149 | assert!(model.confirmed, "{}", model.status()); |
| 150 | assert_eq!( |
| 151 | model.terminal.as_ref().unwrap().outcome, |
| 152 | Outcome::Completed, |
| 153 | "terminal: {:?}", |
| 154 | model.terminal |
| 155 | ); |
| 156 | } |
| 157 | #[cfg(feature = "ui")] |
| 158 | #[path = "../rust-app-ui/dialogs.rs"] |
| 159 | mod dialogs; |
| 160 | #[cfg(feature = "ui")] |
| 161 | mod screen { |
| 162 | use super::dialogs::TestDialogs; |
| 163 | use super::*; |
| 164 | use eframe::egui::{self, epaint::Shape}; |
| 165 | use zipelo_ui_memory_app::archive_screen::ArchiveApp; |
| 166 | struct Screen { |
| 167 | context: egui::Context, |
| 168 | app: ArchiveApp, |
| 169 | dialogs: TestDialogs, |
| 170 | } |
| 171 | impl Screen { |
| 172 | fn new(factory: ObservedFactory) -> Self { |
| 173 | let dialogs = TestDialogs::default(); |
| 174 | let mut app = ArchiveApp::new(Box::new(factory), None, false); |
| 175 | app.set_dialogs(Box::new(dialogs.clone())); |
| 176 | Self { |
| 177 | context: egui::Context::default(), |
| 178 | app, |
| 179 | dialogs, |
| 180 | } |
| 181 | } |
| 182 | fn render(&mut self, events: Vec<egui::Event>) -> Vec<(String, egui::Pos2)> { |
| 183 | fn text(shape: &Shape, output: &mut Vec<(String, egui::Pos2)>) { |
| 184 | match shape { |
| 185 | Shape::Text(value) => output.push(( |
| 186 | value.galley.job.text.clone(), |
| 187 | value.pos + value.galley.rect.size() / 2.0, |
| 188 | )), |
| 189 | Shape::Vec(values) => { |
| 190 | for value in values { |
| 191 | text(value, output); |
| 192 | } |
| 193 | } |
| 194 | _ => {} |
| 195 | } |
| 196 | } |
| 197 | let mut frame = self.context.run_ui( |
| 198 | egui::RawInput { |
| 199 | screen_rect: Some(egui::Rect::from_min_size( |
| 200 | egui::Pos2::ZERO, |
| 201 | egui::vec2(1060.0, 720.0), |
| 202 | )), |
| 203 | events, |
| 204 | ..Default::default() |
| 205 | }, |
| 206 | |ui| self.app.show(ui), |
| 207 | ); |
| 208 | frame.textures_delta.clear(); |
| 209 | let mut output = Vec::new(); |
| 210 | for shape in frame.shapes { |
| 211 | text(&shape.shape, &mut output); |
| 212 | } |
| 213 | output |
| 214 | } |
| 215 | fn locate(&mut self, label: &str) -> egui::Pos2 { |
| 216 | self.render(vec![]); |
| 217 | self.render(vec![]) |
| 218 | .into_iter() |
| 219 | .filter(|(text, _)| text == label) |
| 220 | .max_by(|a, b| a.1.y.total_cmp(&b.1.y)) |
| 221 | .unwrap_or_else(|| panic!("화면 항목 누락: {label}")) |
| 222 | .1 |
| 223 | } |
| 224 | fn click_at(&mut self, point: egui::Pos2) { |
| 225 | self.render(vec![ |
| 226 | egui::Event::PointerMoved(point), |
| 227 | egui::Event::PointerButton { |
| 228 | pos: point, |
| 229 | button: egui::PointerButton::Primary, |
| 230 | pressed: true, |
| 231 | modifiers: Default::default(), |
| 232 | }, |
| 233 | ]); |
| 234 | self.render(vec![egui::Event::PointerButton { |
| 235 | pos: point, |
| 236 | button: egui::PointerButton::Primary, |
| 237 | pressed: false, |
| 238 | modifiers: Default::default(), |
| 239 | }]); |
| 240 | } |
| 241 | fn click(&mut self, label: &str) { |
| 242 | let point = self.locate(label); |
| 243 | self.click_at(point); |
| 244 | } |
| 245 | fn set_text(&mut self, id: &str, value: &str) { |
| 246 | if id == "zip-name" { |
| 247 | self.dialogs.0.borrow_mut().save_name = Some(value.to_owned()); |
| 248 | return; |
| 249 | } |
| 250 | self.render(vec![]); |
| 251 | let point = self |
| 252 | .context |
| 253 | .read_response(egui::Id::unique(id)) |
| 254 | .unwrap() |
| 255 | .rect |
| 256 | .center(); |
| 257 | self.click_at(point); |
| 258 | self.context |
| 259 | .memory_mut(|memory| memory.request_focus(egui::Id::unique(id))); |
| 260 | self.render(vec![ |
| 261 | egui::Event::Key { |
| 262 | key: egui::Key::A, |
| 263 | physical_key: None, |
| 264 | pressed: true, |
| 265 | repeat: false, |
| 266 | modifiers: egui::Modifiers::COMMAND, |
| 267 | }, |
| 268 | egui::Event::Text(value.into()), |
| 269 | ]); |
| 270 | } |
| 271 | fn escape(&mut self) { |
| 272 | self.render(vec![egui::Event::Key { |
| 273 | key: egui::Key::Escape, |
| 274 | physical_key: None, |
| 275 | pressed: true, |
| 276 | repeat: false, |
| 277 | modifiers: egui::Modifiers::NONE, |
| 278 | }]); |
| 279 | } |
| 280 | fn settle(&mut self) { |
| 281 | let deadline = Instant::now() + Duration::from_secs(10); |
| 282 | while self.app.model.busy() { |
| 283 | self.render(vec![]); |
| 284 | assert!(Instant::now() < deadline, "{}", self.app.model.status()); |
| 285 | std::thread::sleep(Duration::from_millis(2)); |
| 286 | } |
| 287 | completed(&self.app.model); |
| 288 | } |
| 289 | fn open(&mut self, archive: &Path) { |
| 290 | self.app.open_path(archive.to_path_buf()); |
| 291 | assert!(self.app.model.busy()); |
| 292 | self.settle(); |
| 293 | } |
| 294 | fn extract(&mut self, destination: &Path) { |
| 295 | self.app.choose_destination(destination.to_path_buf()); |
| 296 | self.dialogs |
| 297 | .0 |
| 298 | .borrow_mut() |
| 299 | .answers |
| 300 | .push_back(Some(vec![destination.to_path_buf()])); |
| 301 | self.click("압축해제"); |
| 302 | assert!(self.app.model.busy()); |
| 303 | self.settle(); |
| 304 | assert_eq!( |
| 305 | self.app.model.terminal.as_ref().unwrap().publication, |
| 306 | if self.app.model.terminal.as_ref().unwrap().created_count == 0 { |
| 307 | Publication::None |
| 308 | } else { |
| 309 | Publication::Complete |
| 310 | } |
| 311 | ); |
| 312 | } |
| 313 | } |
| 314 | include!("password_flows.rs"); |
| 315 | include!("selection_flows.rs"); |
| 316 | include!("conflict_flows.rs"); |
| 317 | include!("native_dialog_flows.rs"); |
| 318 | include!("compressed_tar_flows.rs"); |
| 319 | |
| 320 | fn archive_flow(name: &str, format: Format) { |
| 321 | let scratch = Scratch::new(); |
| 322 | let fixture = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 323 | .join("../../tests/rust-app-real-worker/fixtures") |
| 324 | .join(name); |
| 325 | let archive = scratch.path(name); |
| 326 | fs::copy(fixture, &archive).unwrap(); |
| 327 | let original = fs::read(&archive).unwrap(); |
| 328 | let output = make_directory(&scratch.path("output")); |
| 329 | let (factory, observed) = observed_factory(); |
| 330 | let mut screen = Screen::new(factory); |
| 331 | screen.open(&archive); |
| 332 | assert_eq!(screen.app.model.list_id.as_ref().unwrap().format, format); |
| 333 | assert_eq!(screen.app.model.entries[0].name, "content.txt"); |
| 334 | screen.extract(&output); |
| 335 | assert_eq!( |
| 336 | fs::read(output.join("content.txt")).unwrap(), |
| 337 | b"Zipelo real worker flow\n" |
| 338 | ); |
| 339 | assert_eq!(fs::read(archive).unwrap(), original); |
| 340 | assert_eq!(observed.borrow().jobs.len(), 2); |
| 341 | assert_reaped(&observed); |
| 342 | } |
| 343 | #[test] |
| 344 | fn seven_zip_open_and_extract_via_actual_widgets_and_worker() { |
| 345 | for name in ["lzma.7z", "lzma2.7z"] { |
| 346 | let scratch = Scratch::new(); |
| 347 | let archive = scratch.path(name); |
| 348 | fs::copy( |
| 349 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 350 | .join("../../tests/rust-archive-worker/fixtures/7z") |
| 351 | .join(name), |
| 352 | &archive, |
| 353 | ) |
| 354 | .unwrap(); |
| 355 | let original = fs::read(&archive).unwrap(); |
| 356 | let output = make_directory(&scratch.path("output")); |
| 357 | let (factory, observed) = observed_factory(); |
| 358 | let mut screen = Screen::new(factory); |
| 359 | screen.open(&archive); |
| 360 | assert_eq!( |
| 361 | screen.app.model.list_id.as_ref().unwrap().format, |
| 362 | Format::SevenZip |
| 363 | ); |
| 364 | assert!(screen.app.model.can_extract()); |
| 365 | screen.extract(&output); |
| 366 | assert_eq!( |
| 367 | fs::read(output.join("folder/hello.txt")).unwrap(), |
| 368 | b"Zipelo 7z interoperability\n".repeat(100) |
| 369 | ); |
| 370 | assert_eq!(fs::read(archive).unwrap(), original); |
| 371 | assert_eq!(observed.borrow().jobs.len(), 2); |
| 372 | assert_reaped(&observed); |
| 373 | } |
| 374 | } |
| 375 | #[test] |
| 376 | fn zip_open_and_extract_via_actual_widgets_and_worker() { |
| 377 | archive_flow("sample.zip", Format::Zip); |
| 378 | } |
| 379 | #[test] |
| 380 | fn targz_open_and_extract_via_actual_widgets_and_worker() { |
| 381 | archive_flow("sample.tar.gz", Format::TarGzip); |
| 382 | } |
| 383 | |
| 384 | #[test] |
| 385 | fn empty_directory_zip_extracts_successfully_with_existing_directory_reuse() { |
| 386 | let scratch = Scratch::new(); |
| 387 | let source = make_directory(&scratch.path("empty-source")); |
| 388 | fs::create_dir(source.join("child")).unwrap(); |
| 389 | let archive = scratch.path("empty-source.zip"); |
| 390 | let output = make_directory(&scratch.path("output")); |
| 391 | let (factory, observed) = observed_factory(); |
| 392 | let mut screen = Screen::new(factory); |
| 393 | screen.app.add_sources(vec![source]); |
| 394 | screen.render(vec![]); |
| 395 | screen.click("압축하기"); |
| 396 | screen.settle(); |
| 397 | assert!(archive.is_file()); |
| 398 | screen.open(&archive); |
| 399 | assert!( |
| 400 | screen |
| 401 | .app |
| 402 | .model |
| 403 | .entries |
| 404 | .iter() |
| 405 | .all(|entry| entry.kind == Kind::Directory) |
| 406 | ); |
| 407 | for expected in [2, 0] { |
| 408 | screen.extract(&output); |
| 409 | assert!(output.join("empty-source/child").is_dir()); |
| 410 | assert_eq!( |
| 411 | screen.app.model.terminal.as_ref().unwrap().created_count, |
| 412 | expected |
| 413 | ); |
| 414 | let shown = screen.render(vec![]); |
| 415 | assert!(shown.iter().any(|(text, _)| text == "압축을 풀었습니다")); |
| 416 | assert!( |
| 417 | !shown |
| 418 | .iter() |
| 419 | .any(|(text, _)| text.contains("저장이 끝났는지")) |
| 420 | ); |
| 421 | screen.click("확인"); |
| 422 | } |
| 423 | assert_reaped(&observed); |
| 424 | } |
| 425 | |
| 426 | #[test] |
| 427 | fn create_zip_reopen_and_extract_via_actual_widgets_and_worker() { |
| 428 | let scratch = Scratch::new(); |
| 429 | let source = scratch.path("one.txt"); |
| 430 | fs::write(&source, b"ZIP creation through real worker\n").unwrap(); |
| 431 | let archive = scratch.path("new.zip"); |
| 432 | let output = make_directory(&scratch.path("output")); |
| 433 | let (factory, observed) = observed_factory(); |
| 434 | let mut screen = Screen::new(factory); |
| 435 | screen.app.add_sources(vec![source.clone()]); |
| 436 | screen.render(vec![]); |
| 437 | screen.set_text("zip-name", archive.file_name().unwrap().to_str().unwrap()); |
| 438 | screen.click("압축하기"); |
| 439 | assert!(screen.app.model.busy()); |
| 440 | screen.settle(); |
| 441 | assert!(archive.is_file()); |
| 442 | screen.open(&archive); |
| 443 | assert_eq!(screen.app.model.entries[0].name, "one.txt"); |
| 444 | screen.extract(&output); |
| 445 | assert_eq!( |
| 446 | fs::read(output.join("one.txt")).unwrap(), |
| 447 | fs::read(source).unwrap() |
| 448 | ); |
| 449 | assert_eq!(observed.borrow().jobs.len(), 3); |
| 450 | assert_reaped(&observed); |
| 451 | } |
| 452 | } |
| 453 | #[test] |
| 454 | fn missing_worker_is_an_error_without_a_fake_result() { |
| 455 | let scratch = Scratch::new(); |
| 456 | let mut factory = ProcessFactory::new(scratch.path("not-installed")); |
| 457 | let mut model = Model::default(); |
| 458 | assert!(!model.begin( |
| 459 | Start::List { |
| 460 | archive: scratch.path("sample.zip") |
| 461 | }, |
| 462 | &mut factory |
| 463 | )); |
| 464 | assert!(!model.busy()); |
| 465 | assert!(model.error.is_some()); |
| 466 | assert!(!model.confirmed); |
| 467 | } |
| 468 | #[test] |
| 469 | fn broken_or_incomplete_worker_output_is_not_success() { |
| 470 | for name in ["truncated", "wrong-action", "early-exit"] { |
| 471 | let scratch = Scratch::new(); |
| 472 | let probe = scratch.path(&format!("{name}{}", std::env::consts::EXE_SUFFIX)); |
| 473 | fs::copy(binary("zipelo-real-worker-probe"), &probe).unwrap(); |
| 474 | let mut factory = ProcessFactory::new(probe); |
| 475 | let mut model = Model::default(); |
| 476 | assert!( |
| 477 | model.begin( |
| 478 | Start::List { |
| 479 | archive: scratch.path("sample.zip") |
| 480 | }, |
| 481 | &mut factory |
| 482 | ), |
| 483 | "{:?}", |
| 484 | model.error |
| 485 | ); |
| 486 | model.request_close(); |
| 487 | model.confirm_close(); |
| 488 | assert_eq!(model.close, Close::Waiting); |
| 489 | wait(&mut model); |
| 490 | assert!(!model.confirmed); |
| 491 | assert!(model.error.is_some()); |
| 492 | assert_eq!(model.close, Close::Review); |
| 493 | model.acknowledge_close(); |
| 494 | assert_eq!(model.close, Close::Allowed); |
| 495 | } |
| 496 | } |
| 497 | #[cfg(target_os = "linux")] |
| 498 | #[test] |
| 499 | fn dropping_live_connection_kills_and_reaps_owned_child() { |
| 500 | let scratch = Scratch::new(); |
| 501 | let probe = scratch.path("hang"); |
| 502 | fs::copy(binary("zipelo-real-worker-probe"), &probe).unwrap(); |
| 503 | let mut factory = ProcessFactory::new(probe); |
| 504 | let mut model = Model::default(); |
| 505 | assert!( |
| 506 | model.begin( |
| 507 | Start::List { |
| 508 | archive: scratch.path("sample.zip") |
| 509 | }, |
| 510 | &mut factory |
| 511 | ), |
| 512 | "{:?}", |
| 513 | model.error |
| 514 | ); |
| 515 | let deadline = Instant::now() + Duration::from_secs(5); |
| 516 | while !scratch.path("probe.pid").exists() { |
| 517 | assert!(Instant::now() < deadline); |
| 518 | std::thread::sleep(Duration::from_millis(2)); |
| 519 | } |
| 520 | let pid = fs::read_to_string(scratch.path("probe.pid")).unwrap(); |
| 521 | drop(model); |
| 522 | assert!(!Path::new("/proc").join(pid).exists()); |
| 523 | } |
| 524 | |
| 525 | #[test] |
| 526 | fn failed_start_serialization_reaps_the_real_worker_without_terminal() { |
| 527 | let scratch = Scratch::new(); |
| 528 | let mut factory = ProcessFactory::new(binary("zipelo-archive-worker")); |
| 529 | let mut model = Model::default(); |
| 530 | let archive = scratch.path(&"a".repeat(40000)); |
| 531 | assert!(!model.begin(Start::List { archive }, &mut factory)); |
| 532 | assert!(model.busy()); |
| 533 | model.request_close(); |
| 534 | model.confirm_close(); |
| 535 | assert_eq!(model.close, Close::Waiting); |
| 536 | wait(&mut model); |
| 537 | assert!(!model.confirmed); |
| 538 | assert!(model.terminal.is_none()); |
| 539 | assert!(model.error.is_some()); |
| 540 | assert_eq!(model.close, Close::Review); |
| 541 | } |
| 542 | |
| 543 | #[test] |
| 544 | fn late_cancel_after_child_reap_keeps_the_original_terminal_result() { |
| 545 | let scratch = Scratch::new(); |
| 546 | let archive = scratch.path("sample.zip"); |
| 547 | fs::copy( |
| 548 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 549 | .join("../../tests/rust-app-real-worker/fixtures/sample.zip"), |
| 550 | &archive, |
| 551 | ) |
| 552 | .unwrap(); |
| 553 | let mut factory = ProcessFactory::new(binary("zipelo-archive-worker")); |
| 554 | let mut connection = factory.connect(1).unwrap(); |
| 555 | connection |
| 556 | .send(Request { |
| 557 | job_id: 1, |
| 558 | sequence: 0, |
| 559 | command: Command::Start(Start::List { archive }), |
| 560 | }) |
| 561 | .unwrap(); |
| 562 | let deadline = Instant::now() + Duration::from_secs(10); |
| 563 | let mut terminal = false; |
| 564 | loop { |
| 565 | assert!(Instant::now() < deadline); |
| 566 | match connection.poll() { |
| 567 | Some(HostEvent::CleanupComplete) => break, |
| 568 | Some(HostEvent::Frame { |
| 569 | event: Event::Terminal(value), |
| 570 | .. |
| 571 | }) => { |
| 572 | assert_eq!(value.outcome, Outcome::Completed); |
| 573 | terminal = true; |
| 574 | } |
| 575 | Some(HostEvent::Fault(message)) => panic!("{message}"), |
| 576 | _ => std::thread::sleep(Duration::from_millis(1)), |
| 577 | } |
| 578 | } |
| 579 | assert!(terminal); |
| 580 | assert!( |
| 581 | connection |
| 582 | .send(Request { |
| 583 | job_id: 1, |
| 584 | sequence: 1, |
| 585 | command: Command::Cancel |
| 586 | }) |
| 587 | .is_ok() |
| 588 | ); |
| 589 | loop { |
| 590 | assert!(Instant::now() < deadline); |
| 591 | match connection.poll() { |
| 592 | Some(HostEvent::Exited { success }) => { |
| 593 | assert!(success); |
| 594 | break; |
| 595 | } |
| 596 | Some(HostEvent::Fault(message)) => panic!("{message}"), |
| 597 | _ => std::thread::sleep(Duration::from_millis(1)), |
| 598 | } |
| 599 | } |
| 600 | assert!(connection.poll().is_none()); |
| 601 | } |
| 602 | |
| 603 | #[test] |
| 604 | fn cancel_racing_child_exit_preserves_completed_result_before_poll() { |
| 605 | struct CompletedFactory(ProcessFactory); |
| 606 | struct BufferedConnection { |
| 607 | inner: Box<dyn Connection>, |
| 608 | pending: std::collections::VecDeque<HostEvent>, |
| 609 | } |
| 610 | impl Factory for CompletedFactory { |
| 611 | fn connect(&mut self, job_id: u64) -> Result<Box<dyn Connection>, String> { |
| 612 | Ok(Box::new(BufferedConnection { |
| 613 | inner: self.0.connect(job_id)?, |
| 614 | pending: Default::default(), |
| 615 | })) |
| 616 | } |
| 617 | } |
| 618 | impl Connection for BufferedConnection { |
| 619 | fn send(&mut self, request: Request) -> Result<(), String> { |
| 620 | let starting = matches!(&request.command, Command::Start(_)); |
| 621 | self.inner.send(request)?; |
| 622 | if starting { |
| 623 | let deadline = Instant::now() + Duration::from_secs(20); |
| 624 | loop { |
| 625 | assert!(Instant::now() < deadline, "작업자 종료 대기 시간 초과"); |
| 626 | if let Some(event) = self.inner.poll() { |
| 627 | let exited = matches!(&event, HostEvent::Exited { .. }); |
| 628 | self.pending.push_back(event); |
| 629 | if exited { |
| 630 | break; |
| 631 | } |
| 632 | } else { |
| 633 | std::thread::sleep(Duration::from_millis(1)); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | Ok(()) |
| 638 | } |
| 639 | fn poll(&mut self) -> Option<HostEvent> { |
| 640 | self.pending.pop_front().or_else(|| self.inner.poll()) |
| 641 | } |
| 642 | } |
| 643 | let archive = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 644 | .join("../../tests/rust-app-real-worker/fixtures/sample.zip") |
| 645 | .canonicalize() |
| 646 | .unwrap(); |
| 647 | for iteration in 0..100 { |
| 648 | let mut factory = CompletedFactory(ProcessFactory::new(binary("zipelo-archive-worker"))); |
| 649 | let mut model = Model::default(); |
| 650 | assert!( |
| 651 | model.begin( |
| 652 | Start::List { |
| 653 | archive: archive.clone() |
| 654 | }, |
| 655 | &mut factory |
| 656 | ), |
| 657 | "{:?}", |
| 658 | model.error |
| 659 | ); |
| 660 | assert!(model.terminal.is_none()); |
| 661 | model.cancel(); |
| 662 | wait(&mut model); |
| 663 | assert!(model.confirmed, "iteration={iteration}: {:?}", model.error); |
| 664 | assert_eq!(model.terminal.as_ref().unwrap().outcome, Outcome::Completed); |
| 665 | assert!(model.error.is_none()); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | #[test] |
| 670 | fn cancel_racing_exit_without_terminal_remains_unconfirmed() { |
| 671 | let scratch = Scratch::new(); |
| 672 | let probe = scratch.path("early-exit"); |
| 673 | fs::copy(binary("zipelo-real-worker-probe"), &probe).unwrap(); |
| 674 | let mut factory = ProcessFactory::new(probe); |
| 675 | let mut model = Model::default(); |
| 676 | assert!( |
| 677 | model.begin( |
| 678 | Start::List { |
| 679 | archive: scratch.path("sample.zip") |
| 680 | }, |
| 681 | &mut factory |
| 682 | ), |
| 683 | "{:?}", |
| 684 | model.error |
| 685 | ); |
| 686 | std::thread::sleep(Duration::from_millis(2)); |
| 687 | model.cancel(); |
| 688 | model.request_close(); |
| 689 | model.confirm_close(); |
| 690 | assert_eq!(model.close, Close::Waiting); |
| 691 | wait(&mut model); |
| 692 | assert!(!model.confirmed); |
| 693 | assert!(model.terminal.is_none()); |
| 694 | assert!(model.error.is_some()); |
| 695 | assert_eq!(model.close, Close::Review); |
| 696 | } |
| 697 | |
| 698 | mod guarded_flows; |
| 699 | |
| 700 | mod preview_catalog; |
| 701 | |
| 702 | #[test] |
| 703 | fn recovery_adapter_confirms_complete_partial_and_impossible_without_changing_source() { |
| 704 | use sha2::{Digest, Sha256}; |
| 705 | let fixtures = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/zip-recovery-fixtures"); |
| 706 | for (name, expected, count) in [ |
| 707 | ("broken-local-header.zip", None, 1), |
| 708 | ( |
| 709 | "missing-directory.zip", |
| 710 | Some(ErrorCode::RecoveryIncomplete), |
| 711 | 1, |
| 712 | ), |
| 713 | ("body-corrupt.zip", Some(ErrorCode::RecoveryIncomplete), 1), |
| 714 | ("unrecoverable.zip", Some(ErrorCode::RecoveryImpossible), 0), |
| 715 | ] { |
| 716 | let scratch = Scratch::new(); |
| 717 | let source = scratch.path("source.zip"); |
| 718 | fs::copy(fixtures.join(name), &source).unwrap(); |
| 719 | let before = Sha256::digest(fs::read(&source).unwrap()); |
| 720 | let output = scratch.path("recovered.zip"); |
| 721 | let mut model = Model::default(); |
| 722 | let (mut factory, observed) = observed_factory(); |
| 723 | assert!(model.begin( |
| 724 | Start::RecoverZip { |
| 725 | archive: source.clone(), |
| 726 | output_file: output.clone() |
| 727 | }, |
| 728 | &mut factory |
| 729 | )); |
| 730 | wait(&mut model); |
| 731 | assert!(model.confirmed, "{name}: {:?}", model.error); |
| 732 | let terminal = model.terminal.as_ref().unwrap(); |
| 733 | assert_eq!(terminal.error, expected, "{name}"); |
| 734 | assert_eq!(terminal.created_count, count); |
| 735 | assert_eq!(output.exists(), count == 1); |
| 736 | assert_eq!(Sha256::digest(fs::read(source).unwrap()), before); |
| 737 | assert_reaped(&observed); |
| 738 | if count == 1 { |
| 739 | assert!(model.begin(Start::List { archive: output }, &mut factory)); |
| 740 | wait(&mut model); |
| 741 | completed(&model); |
| 742 | assert!(model.list_id.is_some()); |
| 743 | } |
| 744 | } |
| 745 | } |