| 1 | use crate::{fixtures::*, support::*}; |
| 2 | use job::{Event, FileJob, Limits, Outcome, Stop, publication}; |
| 3 | use std::{sync::mpsc, thread, time::Duration}; |
| 4 | |
| 5 | const WAIT: Duration = Duration::from_secs(5); |
| 6 | |
| 7 | #[test] |
| 8 | fn two_file_jobs_for_one_name_have_one_complete_winner() { |
| 9 | let sandbox = Sandbox::new(); |
| 10 | let parent = sandbox.parent(); |
| 11 | let (ready_tx, ready_rx) = mpsc::sync_channel(2); |
| 12 | let (done_tx, done_rx) = mpsc::sync_channel(2); |
| 13 | let mut releases = Vec::new(); |
| 14 | let mut handles = Vec::new(); |
| 15 | let mut inputs = Vec::new(); |
| 16 | for marker in [65_u8, 66_u8] { |
| 17 | let archive = stored(&vec![marker; 65_537]); |
| 18 | let input = sandbox.file(format!("input-{marker}.gz"), &archive); |
| 19 | inputs.push((input.clone(), identity(&input), archive)); |
| 20 | let (release_tx, release_rx) = mpsc::sync_channel(1); |
| 21 | releases.push(release_tx); |
| 22 | let ready = ready_tx.clone(); |
| 23 | let done = done_tx.clone(); |
| 24 | let parent = parent.clone(); |
| 25 | handles.push(thread::spawn(move || { |
| 26 | let (task, cancel) = FileJob::new(); |
| 27 | let mut trace = Trace::default(); |
| 28 | let report = task.run(request(&input, &parent, Limits::default()), |point| { |
| 29 | trace.push(point); |
| 30 | if matches!(point.event, Event::Publication(native) if native.stage == publication::Stage::BeforeCommit) { |
| 31 | ready.send(marker).unwrap(); |
| 32 | release_rx.recv_timeout(WAIT).expect("게시 해제 신호 시간 초과"); |
| 33 | } |
| 34 | Ok::<_, Stop<()>>(()) |
| 35 | }); |
| 36 | trace.finish(&report); |
| 37 | assert_eq!(cancel.request(), publication::CancelDecision::Finished); |
| 38 | assert!(done.send((marker, report)).is_ok()); |
| 39 | })); |
| 40 | } |
| 41 | drop(ready_tx); |
| 42 | drop(done_tx); |
| 43 | let mut ready = vec![ |
| 44 | ready_rx.recv_timeout(WAIT).unwrap(), |
| 45 | ready_rx.recv_timeout(WAIT).unwrap(), |
| 46 | ]; |
| 47 | ready.sort(); |
| 48 | assert_eq!(ready, [65, 66]); |
| 49 | assert!(names(&parent).is_empty()); |
| 50 | for release in releases { |
| 51 | release.send(()).unwrap(); |
| 52 | } |
| 53 | let results = [ |
| 54 | done_rx.recv_timeout(WAIT).unwrap(), |
| 55 | done_rx.recv_timeout(WAIT).unwrap(), |
| 56 | ]; |
| 57 | for handle in handles { |
| 58 | handle.join().expect("파일 작업 스레드 실패"); |
| 59 | } |
| 60 | assert_eq!(results.iter().filter(|(_, report)| matches!( |
| 61 | &report.outcome, Outcome::Publication(native) if matches!(&native.outcome, publication::Outcome::Committed { .. }) |
| 62 | )).count(), 1); |
| 63 | for (marker, report) in results { |
| 64 | let (_, before, archive) = &inputs[(marker - 65) as usize]; |
| 65 | observations( |
| 66 | &report, |
| 67 | (before.0, before.1), |
| 68 | archive, |
| 69 | &vec![marker; 65_537], |
| 70 | 10, |
| 71 | ); |
| 72 | if matches!(&report.outcome, Outcome::Publication(native) if matches!(&native.outcome, publication::Outcome::Committed { .. })) |
| 73 | { |
| 74 | committed(&report, &parent, &vec![marker; 65_537]); |
| 75 | } else { |
| 76 | conflict(&report, 65_537); |
| 77 | } |
| 78 | } |
| 79 | for (input, before, archive) in inputs { |
| 80 | assert_eq!(identity(&input), before); |
| 81 | assert_eq!(std::fs::read(input).unwrap(), archive); |
| 82 | } |
| 83 | sandbox.finish(); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn acknowledged_external_cancel_before_commit_prevents_publication() { |
| 88 | let sandbox = Sandbox::new(); |
| 89 | let input = sandbox.file("input.gz", &stored(b"abc")); |
| 90 | let parent = sandbox.parent(); |
| 91 | let worker_parent = parent.clone(); |
| 92 | let (task, cancel) = FileJob::new(); |
| 93 | let (ready_tx, ready_rx) = mpsc::sync_channel(1); |
| 94 | let (release_tx, release_rx) = mpsc::sync_channel(1); |
| 95 | let (done_tx, done_rx) = mpsc::sync_channel(1); |
| 96 | let handle = thread::spawn(move || { |
| 97 | let mut trace = Trace::default(); |
| 98 | let report = task.run(request(&input, &worker_parent, Limits::default()), |point| { |
| 99 | trace.push(point); |
| 100 | if matches!(point.event, Event::Publication(native) if native.stage == publication::Stage::BeforeCommit) { |
| 101 | ready_tx.send(()).unwrap(); |
| 102 | release_rx.recv_timeout(WAIT).expect("취소 확인 신호 시간 초과"); |
| 103 | } |
| 104 | Ok::<_, Stop<()>>(()) |
| 105 | }); |
| 106 | trace.finish(&report); |
| 107 | assert!(done_tx.send(report).is_ok()); |
| 108 | }); |
| 109 | ready_rx.recv_timeout(WAIT).unwrap(); |
| 110 | assert_eq!(cancel.request(), publication::CancelDecision::Requested); |
| 111 | assert_eq!( |
| 112 | cancel.clone().request(), |
| 113 | publication::CancelDecision::AlreadyRequested |
| 114 | ); |
| 115 | release_tx.send(()).unwrap(); |
| 116 | let report = done_rx.recv_timeout(WAIT).unwrap(); |
| 117 | handle.join().expect("취소 스레드 실패"); |
| 118 | match &report.outcome { |
| 119 | Outcome::Publication(native) => { |
| 120 | assert!(matches!( |
| 121 | native.outcome, |
| 122 | publication::Outcome::NotCommitted(publication::Error::Cancelled) |
| 123 | )); |
| 124 | assert_eq!(native.usage.link_calls, 0); |
| 125 | assert_eq!(native.usage.actual_written_bytes, 3); |
| 126 | assert_eq!(native.usage.actual_read_bytes, 3); |
| 127 | cleanup(&native.cleanup, 4); |
| 128 | } |
| 129 | _ => panic!("게시 전 취소가 보존되지 않음"), |
| 130 | } |
| 131 | assert_eq!(cancel.request(), publication::CancelDecision::Finished); |
| 132 | assert!(names(&parent).is_empty()); |
| 133 | sandbox.finish(); |
| 134 | } |