| 1 | use crate::support::*; |
| 2 | use publication::{ |
| 3 | CancelDecision, Checkpoint, Error, Limits, Publication, Request, Stage, Stop, Usage, |
| 4 | }; |
| 5 | use std::{ |
| 6 | ffi::OsStr, |
| 7 | panic::{AssertUnwindSafe, catch_unwind}, |
| 8 | path::Path, |
| 9 | rc::Rc, |
| 10 | time::Duration, |
| 11 | }; |
| 12 | |
| 13 | #[test] |
| 14 | fn pre_requested_cancel_is_idempotent_and_stops_before_open() { |
| 15 | let (task, cancel) = Publication::new(); |
| 16 | assert_eq!(cancel.request(), CancelDecision::Requested); |
| 17 | assert_eq!(cancel.clone().request(), CancelDecision::AlreadyRequested); |
| 18 | let mut callbacks = 0; |
| 19 | let (error, usage, closed) = not_committed(task.publish( |
| 20 | Request { |
| 21 | parent: Path::new("/owned"), |
| 22 | leaf: OsStr::new("unused"), |
| 23 | bytes: b"x", |
| 24 | limits: Limits::default(), |
| 25 | }, |
| 26 | |_| { |
| 27 | callbacks += 1; |
| 28 | Ok::<_, Stop<()>>(()) |
| 29 | }, |
| 30 | )); |
| 31 | assert!(matches!(error, Error::Cancelled)); |
| 32 | assert_eq!(callbacks, 1); |
| 33 | assert_eq!(usage, Usage::default()); |
| 34 | cleanup(&closed, 0); |
| 35 | assert_eq!(cancel.request(), CancelDecision::Finished); |
| 36 | } |
| 37 | |
| 38 | fn cancel_at(stage: Stage, occurrence: usize, closes: u8, writes: u64, reads: u64) { |
| 39 | let sandbox = Sandbox::new(); |
| 40 | let parent = sandbox.parent(); |
| 41 | let (task, cancel) = Publication::new(); |
| 42 | let mut count = 0; |
| 43 | let mut requested = false; |
| 44 | let (error, usage, closed) = not_committed(task.publish( |
| 45 | Request { |
| 46 | parent: &parent, |
| 47 | leaf: OsStr::new("output"), |
| 48 | bytes: b"abc", |
| 49 | limits: Limits::default(), |
| 50 | }, |
| 51 | |point| { |
| 52 | if point.stage == stage { |
| 53 | count += 1; |
| 54 | if count == occurrence { |
| 55 | assert_eq!(cancel.request(), CancelDecision::Requested); |
| 56 | requested = true; |
| 57 | } |
| 58 | } |
| 59 | Ok::<_, Stop<()>>(()) |
| 60 | }, |
| 61 | )); |
| 62 | assert!(requested); |
| 63 | assert!(matches!(error, Error::Cancelled)); |
| 64 | assert_eq!(usage.link_calls, 0); |
| 65 | assert_eq!(usage.write_calls, writes); |
| 66 | assert_eq!(usage.read_calls, reads); |
| 67 | cleanup(&closed, closes); |
| 68 | assert!(names(&parent).is_empty()); |
| 69 | assert_eq!(cancel.request(), CancelDecision::Finished); |
| 70 | sandbox.finish(); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn cancel_first_opening_has_no_handles() { |
| 75 | cancel_at(Stage::Opening, 1, 0, 0, 0); |
| 76 | } |
| 77 | #[test] |
| 78 | fn cancel_second_opening_closes_parent() { |
| 79 | cancel_at(Stage::Opening, 2, 1, 0, 0); |
| 80 | } |
| 81 | #[test] |
| 82 | fn cancel_before_write_closes_anonymous_temp() { |
| 83 | cancel_at(Stage::Writing, 1, 2, 0, 0); |
| 84 | } |
| 85 | #[test] |
| 86 | fn cancel_before_sync_preserves_written_usage() { |
| 87 | cancel_at(Stage::Verifying, 1, 2, 1, 0); |
| 88 | } |
| 89 | #[test] |
| 90 | fn cancel_before_commit_closes_four_handles_without_link() { |
| 91 | cancel_at(Stage::BeforeCommit, 1, 4, 1, 2); |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn cancel_after_first_chunk_preserves_reservations() { |
| 96 | let sandbox = Sandbox::new(); |
| 97 | let parent = sandbox.parent(); |
| 98 | let (task, _) = Publication::new(); |
| 99 | let bytes = pattern(65_537); |
| 100 | let (error, usage, closed) = not_committed(task.publish( |
| 101 | Request { |
| 102 | parent: &parent, |
| 103 | leaf: OsStr::new("output"), |
| 104 | bytes: &bytes, |
| 105 | limits: Limits::default(), |
| 106 | }, |
| 107 | |point| { |
| 108 | if point.stage == Stage::Writing && point.offset == 65_536 { |
| 109 | Err(Stop::<()>::Cancelled) |
| 110 | } else { |
| 111 | Ok(()) |
| 112 | } |
| 113 | }, |
| 114 | )); |
| 115 | assert!(matches!(error, Error::Cancelled)); |
| 116 | assert_eq!(usage.write_calls, 1); |
| 117 | assert_eq!(usage.reserved_io_bytes, 65_536); |
| 118 | assert_eq!(usage.actual_written_bytes, 65_536); |
| 119 | assert_eq!(usage.read_calls + usage.link_calls + usage.sync_calls, 0); |
| 120 | cleanup(&closed, 2); |
| 121 | assert!(names(&parent).is_empty()); |
| 122 | sandbox.finish(); |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn callback_preserves_non_send_error_and_wins_over_cancel() { |
| 127 | let sandbox = Sandbox::new(); |
| 128 | let parent = sandbox.parent(); |
| 129 | let marker = Rc::new(String::from("typed-callback")); |
| 130 | let (task, cancel) = Publication::new(); |
| 131 | let (error, usage, closed) = not_committed(task.publish( |
| 132 | Request { |
| 133 | parent: &parent, |
| 134 | leaf: OsStr::new("output"), |
| 135 | bytes: b"abc", |
| 136 | limits: Limits::default(), |
| 137 | }, |
| 138 | |point| { |
| 139 | if point.stage == Stage::BeforeCommit { |
| 140 | assert_eq!(cancel.request(), CancelDecision::Requested); |
| 141 | Err(Stop::Callback(marker.clone())) |
| 142 | } else { |
| 143 | Ok(()) |
| 144 | } |
| 145 | }, |
| 146 | )); |
| 147 | match error { |
| 148 | Error::Callback(value) => assert!(Rc::ptr_eq(&value, &marker)), |
| 149 | other => panic!("callback error replaced: {other:?}"), |
| 150 | } |
| 151 | assert_eq!(usage.link_calls, 0); |
| 152 | assert_eq!(usage.actual_read_bytes, 3); |
| 153 | assert_eq!(usage.actual_written_bytes, 3); |
| 154 | cleanup(&closed, 4); |
| 155 | assert!(names(&parent).is_empty()); |
| 156 | assert_eq!(cancel.request(), CancelDecision::Finished); |
| 157 | sandbox.finish(); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn callback_panic_has_no_report_or_named_output() { |
| 162 | let sandbox = Sandbox::new(); |
| 163 | let parent = sandbox.parent(); |
| 164 | let (task, cancel) = Publication::new(); |
| 165 | let panicked = catch_unwind(AssertUnwindSafe(|| { |
| 166 | task.publish( |
| 167 | Request { |
| 168 | parent: &parent, |
| 169 | leaf: OsStr::new("output"), |
| 170 | bytes: b"abc", |
| 171 | limits: Limits::default(), |
| 172 | }, |
| 173 | |point| { |
| 174 | if point.stage == Stage::BeforeCommit { |
| 175 | panic!("intentional-publication-callback-panic"); |
| 176 | } |
| 177 | Ok::<_, Stop<()>>(()) |
| 178 | }, |
| 179 | ) |
| 180 | })); |
| 181 | assert!(panicked.is_err()); |
| 182 | assert_eq!(cancel.request(), CancelDecision::Finished); |
| 183 | assert!(names(&parent).is_empty()); |
| 184 | assert!(owned_descriptors(&sandbox.root).is_empty()); |
| 185 | sandbox.finish(); |
| 186 | } |
| 187 | |
| 188 | fn expired(stop: Option<Stop<u16>>) -> Error<u16> { |
| 189 | let (task, _) = Publication::new(); |
| 190 | let mut stop = Some(stop); |
| 191 | let (error, usage, closed) = not_committed(task.publish( |
| 192 | Request { |
| 193 | parent: Path::new("/owned"), |
| 194 | leaf: OsStr::new("unused"), |
| 195 | bytes: &[], |
| 196 | limits: Limits { |
| 197 | max_duration: Duration::from_millis(1), |
| 198 | ..Limits::default() |
| 199 | }, |
| 200 | }, |
| 201 | |_| { |
| 202 | std::thread::sleep(Duration::from_millis(10)); |
| 203 | match stop.take().unwrap() { |
| 204 | Some(value) => Err(value), |
| 205 | None => Ok(()), |
| 206 | } |
| 207 | }, |
| 208 | )); |
| 209 | assert_eq!(usage, Usage::default()); |
| 210 | cleanup(&closed, 0); |
| 211 | error |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn natural_elapsed_deadline_stops_before_open() { |
| 216 | assert!(matches!(expired(None), Error::DeadlineExceeded)); |
| 217 | } |
| 218 | #[test] |
| 219 | fn callback_cancel_precedes_expired_deadline() { |
| 220 | assert!(matches!(expired(Some(Stop::Cancelled)), Error::Cancelled)); |
| 221 | } |
| 222 | #[test] |
| 223 | fn callback_error_precedes_expired_deadline() { |
| 224 | assert!(matches!( |
| 225 | expired(Some(Stop::Callback(91))), |
| 226 | Error::Callback(91) |
| 227 | )); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn cancellation_after_return_preserves_committed_output() { |
| 232 | let sandbox = Sandbox::new(); |
| 233 | let parent = sandbox.parent(); |
| 234 | let (task, cancel) = Publication::new(); |
| 235 | let report = task.publish( |
| 236 | Request { |
| 237 | parent: &parent, |
| 238 | leaf: OsStr::new("output"), |
| 239 | bytes: b"abc", |
| 240 | limits: Limits::default(), |
| 241 | }, |
| 242 | |_| Ok::<_, Stop<()>>(()), |
| 243 | ); |
| 244 | assert_eq!(cancel.request(), CancelDecision::Finished); |
| 245 | assert_eq!(cancel.request(), CancelDecision::Finished); |
| 246 | committed(report, &parent, OsStr::new("output"), b"abc"); |
| 247 | sandbox.finish(); |
| 248 | } |
| 249 | |
| 250 | #[test] |
| 251 | fn checkpoints_keep_output_absent_and_close_owned_descriptors() { |
| 252 | let sandbox = Sandbox::new(); |
| 253 | let parent = sandbox.parent(); |
| 254 | let (task, _) = Publication::new(); |
| 255 | let mut points: Vec<Checkpoint> = Vec::new(); |
| 256 | let report = task.publish( |
| 257 | Request { |
| 258 | parent: &parent, |
| 259 | leaf: OsStr::new("output"), |
| 260 | bytes: b"abc", |
| 261 | limits: Limits::default(), |
| 262 | }, |
| 263 | |point| { |
| 264 | assert!(names(&parent).is_empty()); |
| 265 | if point.stage == Stage::BeforeCommit { |
| 266 | assert_eq!(owned_descriptors(&sandbox.root).len(), 2); |
| 267 | assert_eq!(point.usage.link_calls, 0); |
| 268 | assert_eq!(point.usage.reserved_io_bytes, 7); |
| 269 | } |
| 270 | points.push(point); |
| 271 | Ok::<_, Stop<()>>(()) |
| 272 | }, |
| 273 | ); |
| 274 | assert_eq!( |
| 275 | points |
| 276 | .iter() |
| 277 | .map(|p| (p.stage, p.offset)) |
| 278 | .collect::<Vec<_>>(), |
| 279 | vec![ |
| 280 | (Stage::Opening, 0), |
| 281 | (Stage::Opening, 0), |
| 282 | (Stage::Writing, 0), |
| 283 | (Stage::Verifying, 0), |
| 284 | (Stage::Verifying, 0), |
| 285 | (Stage::Verifying, 3), |
| 286 | (Stage::BeforeCommit, 3), |
| 287 | ] |
| 288 | ); |
| 289 | assert!(points.iter().all(|p| p.length == 3)); |
| 290 | for pair in points.windows(2) { |
| 291 | assert!(pair[0].usage.reserved_io_bytes <= pair[1].usage.reserved_io_bytes); |
| 292 | assert!(pair[0].usage.actual_written_bytes <= pair[1].usage.actual_written_bytes); |
| 293 | assert!(pair[0].usage.actual_read_bytes <= pair[1].usage.actual_read_bytes); |
| 294 | } |
| 295 | assert!(owned_descriptors(&sandbox.root).is_empty()); |
| 296 | committed(report, &parent, OsStr::new("output"), b"abc"); |
| 297 | sandbox.finish(); |
| 298 | } |
| 299 | |
| 300 | #[test] |
| 301 | fn dropping_unused_publication_marks_finished_without_io() { |
| 302 | let (task, cancel) = Publication::new(); |
| 303 | drop(task); |
| 304 | assert_eq!(cancel.request(), CancelDecision::Finished); |
| 305 | } |