team-ljyzipelo

공개
287줄8.9 KB
1use crate::{fixtures::*, support::*};
2use job::{Event, FileJob, Limits, Outcome, Report, Stop, gzip, publication, snapshot};
3use std::{fs, rc::Rc};
4
5#[derive(Clone, Copy)]
6enum Target {
7 Snapshot(snapshot::Stage),
8 Decode(gzip::Stage),
9 Publication(publication::Stage),
10}
11
12impl Target {
13 fn matches(self, event: Event) -> bool {
14 match (self, event) {
15 (Self::Snapshot(stage), Event::Snapshot(point)) => stage == point.stage,
16 (Self::Decode(stage), Event::Decode(point)) => stage == point.stage,
17 (Self::Publication(stage), Event::Publication(point)) => stage == point.stage,
18 _ => false,
19 }
20 }
21}
22
23fn cancelled(report: &Report<()>, target: Target) {
24 match (&report.outcome, target) {
25 (Outcome::SnapshotFailed(failure), Target::Snapshot(_)) => {
26 assert!(matches!(failure.cause, snapshot::Error::Cancelled))
27 }
28 (
29 Outcome::DecodeFailed(gzip::Error::Reader(gzip::Failure::Cancelled)),
30 Target::Decode(_),
31 ) => {}
32 (Outcome::Publication(native), Target::Publication(stage)) => {
33 assert!(matches!(
34 native.outcome,
35 publication::Outcome::NotCommitted(publication::Error::Cancelled)
36 ));
37 assert_eq!(native.usage.link_calls, 0);
38 cleanup(
39 &native.cleanup,
40 match stage {
41 publication::Stage::Opening => 0,
42 publication::Stage::BeforeCommit => 4,
43 _ => 2,
44 },
45 );
46 }
47 _ => panic!("취소 원인이 해당 단계에 보존되지 않음"),
48 }
49}
50
51fn cancel_at(target: Target, local_stop: bool) {
52 let sandbox = Sandbox::new();
53 let archive = stored(b"abc");
54 let input = sandbox.file("input.gz", &archive);
55 let before = identity(&input);
56 let parent = sandbox.parent();
57 let (task, cancel) = FileJob::new();
58 let mut trace = Trace::default();
59 let mut reached = false;
60 let report = task.run(request(&input, &parent, Limits::default()), |point| {
61 trace.push(point);
62 if target.matches(point.event) {
63 reached = true;
64 if local_stop {
65 return Err(Stop::<()>::Cancelled);
66 }
67 assert_eq!(cancel.request(), publication::CancelDecision::Requested);
68 assert_eq!(
69 cancel.clone().request(),
70 publication::CancelDecision::AlreadyRequested
71 );
72 }
73 Ok(())
74 });
75 assert!(reached);
76 trace.finish(&report);
77 assert!(target.matches(trace.points.last().unwrap().event));
78 assert_eq!(report.usage, trace.points.last().unwrap().usage);
79 cancelled(&report, target);
80 assert_eq!(cancel.request(), publication::CancelDecision::Finished);
81 assert!(names(&parent).is_empty());
82 assert_eq!(identity(&input), before);
83 sandbox.finish();
84}
85
86#[test]
87fn pre_cancel_stops_before_snapshot_open() {
88 let sandbox = Sandbox::new();
89 let input = sandbox.file("input.gz", &stored(b"abc"));
90 let (task, cancel) = FileJob::new();
91 assert_eq!(cancel.request(), publication::CancelDecision::Requested);
92 assert_eq!(
93 cancel.clone().request(),
94 publication::CancelDecision::AlreadyRequested
95 );
96 let mut trace = Trace::default();
97 let report = task.run(
98 request(&input, &sandbox.parent(), Limits::default()),
99 |point| {
100 trace.push(point);
101 Ok::<_, Stop<()>>(())
102 },
103 );
104 trace.finish(&report);
105 assert_eq!(trace.points.len(), 1);
106 assert_eq!(report.usage, job::Usage::default());
107 cancelled(&report, Target::Snapshot(snapshot::Stage::Opening));
108 assert_eq!(cancel.request(), publication::CancelDecision::Finished);
109 assert!(names(&sandbox.parent()).is_empty());
110 sandbox.finish();
111}
112
113fn observer_error(target: Target) {
114 let sandbox = Sandbox::new();
115 let input = sandbox.file("input.gz", &stored(b"abc"));
116 let parent = sandbox.parent();
117 let marker = Rc::new(String::from("원래 observer 오류"));
118 let (task, cancel) = FileJob::new();
119 let mut trace = Trace::default();
120 let report = task.run(request(&input, &parent, Limits::default()), |point| {
121 trace.push(point);
122 if target.matches(point.event) {
123 assert_eq!(cancel.request(), publication::CancelDecision::Requested);
124 Err(Stop::Callback(marker.clone()))
125 } else {
126 Ok(())
127 }
128 });
129 trace.finish(&report);
130 let actual = match &report.outcome {
131 Outcome::SnapshotFailed(snapshot::Failure {
132 cause: snapshot::Error::Callback(value),
133 ..
134 }) => value,
135 Outcome::DecodeFailed(gzip::Error::Callback(value)) => value,
136 Outcome::Publication(native) => {
137 cleanup(&native.cleanup, 4);
138 assert_eq!(native.usage.link_calls, 0);
139 match &native.outcome {
140 publication::Outcome::NotCommitted(publication::Error::Callback(value)) => value,
141 _ => panic!("게시 callback 오류 원형이 사라짐"),
142 }
143 }
144 _ => panic!("callback 오류 원형이 사라짐"),
145 };
146 assert!(Rc::ptr_eq(actual, &marker));
147 assert_eq!(report.usage, trace.points.last().unwrap().usage);
148 assert!(target.matches(trace.points.last().unwrap().event));
149 assert_eq!(cancel.request(), publication::CancelDecision::Finished);
150 assert!(names(&parent).is_empty());
151 sandbox.finish();
152}
153
154#[test]
155fn snapshot_observer_error_beats_accepted_cancel() {
156 observer_error(Target::Snapshot(snapshot::Stage::Finalizing));
157}
158#[test]
159fn decode_observer_error_beats_accepted_cancel() {
160 observer_error(Target::Decode(gzip::Stage::AfterOutput));
161}
162#[test]
163fn publication_observer_error_beats_accepted_cancel() {
164 observer_error(Target::Publication(publication::Stage::BeforeCommit));
165}
166
167#[test]
168fn local_snapshot_stop_does_not_need_cancel_request() {
169 cancel_at(Target::Snapshot(snapshot::Stage::Comparing), true);
170}
171#[test]
172fn local_decode_stop_does_not_need_cancel_request() {
173 cancel_at(Target::Decode(gzip::Stage::AfterOutput), true);
174}
175#[test]
176fn local_publication_stop_does_not_need_cancel_request() {
177 cancel_at(Target::Publication(publication::Stage::BeforeCommit), true);
178}
179
180#[test]
181fn cancellation_after_return_preserves_committed_bytes() {
182 let sandbox = Sandbox::new();
183 let archive = stored(b"abc");
184 let input = sandbox.file("input.gz", &archive);
185 let parent = sandbox.parent();
186 let (task, cancel) = FileJob::new();
187 let report = task.run(request(&input, &parent, Limits::default()), |_| {
188 Ok::<_, Stop<()>>(())
189 });
190 let before = identity(&parent.join("output"));
191 assert_eq!(cancel.request(), publication::CancelDecision::Finished);
192 assert_eq!(
193 cancel.clone().request(),
194 publication::CancelDecision::Finished
195 );
196 committed(&report, &parent, b"abc");
197 assert_eq!(identity(&parent.join("output")), before);
198 assert_eq!(fs::read(&input).unwrap(), archive);
199 sandbox.finish();
200}
201
202#[test]
203fn dropping_unused_job_finishes_its_gate() {
204 let (task, cancel) = FileJob::new();
205 drop(task);
206 assert_eq!(cancel.request(), publication::CancelDecision::Finished);
207}
208
209#[test]
210fn cancel_snapshot_opening() {
211 cancel_at(Target::Snapshot(snapshot::Stage::Opening), false);
212}
213
214#[test]
215fn cancel_snapshot_pinned() {
216 cancel_at(Target::Snapshot(snapshot::Stage::Pinned), false);
217}
218
219#[test]
220fn cancel_snapshot_copying() {
221 cancel_at(Target::Snapshot(snapshot::Stage::Copying), false);
222}
223
224#[test]
225fn cancel_snapshot_comparing() {
226 cancel_at(Target::Snapshot(snapshot::Stage::Comparing), false);
227}
228
229#[test]
230fn cancel_snapshot_finalizing() {
231 cancel_at(Target::Snapshot(snapshot::Stage::Finalizing), false);
232}
233
234#[test]
235fn cancel_decode_start() {
236 cancel_at(Target::Decode(gzip::Stage::Start), false);
237}
238
239#[test]
240fn cancel_decode_header() {
241 cancel_at(Target::Decode(gzip::Stage::Header), false);
242}
243
244#[test]
245fn cancel_decode_before_inflate() {
246 cancel_at(Target::Decode(gzip::Stage::BeforeInflate), false);
247}
248
249#[test]
250fn cancel_decode_after_inflate() {
251 cancel_at(Target::Decode(gzip::Stage::AfterInflate), false);
252}
253
254#[test]
255fn cancel_decode_after_output() {
256 cancel_at(Target::Decode(gzip::Stage::AfterOutput), false);
257}
258
259#[test]
260fn cancel_decode_trailer() {
261 cancel_at(Target::Decode(gzip::Stage::Trailer), false);
262}
263
264#[test]
265fn cancel_decode_before_return() {
266 cancel_at(Target::Decode(gzip::Stage::BeforeReturn), false);
267}
268
269#[test]
270fn cancel_publication_opening() {
271 cancel_at(Target::Publication(publication::Stage::Opening), false);
272}
273
274#[test]
275fn cancel_publication_writing() {
276 cancel_at(Target::Publication(publication::Stage::Writing), false);
277}
278
279#[test]
280fn cancel_publication_verifying() {
281 cancel_at(Target::Publication(publication::Stage::Verifying), false);
282}
283
284#[test]
285fn cancel_publication_before_commit() {
286 cancel_at(Target::Publication(publication::Stage::BeforeCommit), false);
287}