| 1 | use crate::support::*; |
| 2 | use snapshot::{ |
| 3 | Checkpoint, Error, Failure, FileSnapshot, Identity, LimitKind, Limits, Request, Stage, Stop, |
| 4 | Usage, capture, |
| 5 | }; |
| 6 | use std::{ |
| 7 | cell::Cell, |
| 8 | fs, |
| 9 | panic::{AssertUnwindSafe, catch_unwind}, |
| 10 | path::Path, |
| 11 | rc::Rc, |
| 12 | time::Duration, |
| 13 | }; |
| 14 | |
| 15 | fn cancel(stage: Stage, reads: u64, actual: u64) { |
| 16 | let sandbox = Sandbox::new(); |
| 17 | let path = sandbox.file("input", b"abc"); |
| 18 | let mut stopped = false; |
| 19 | let failure = capture( |
| 20 | Request { |
| 21 | path: &path, |
| 22 | limits: Limits::default(), |
| 23 | }, |
| 24 | |point| { |
| 25 | if point.stage == stage { |
| 26 | stopped = true; |
| 27 | Err(Stop::<()>::Cancelled) |
| 28 | } else { |
| 29 | Ok(()) |
| 30 | } |
| 31 | }, |
| 32 | ) |
| 33 | .unwrap_err(); |
| 34 | assert!(stopped); |
| 35 | assert!(matches!(failure.cause, Error::Cancelled)); |
| 36 | assert_eq!(failure.usage.read_calls, reads); |
| 37 | assert_eq!(failure.usage.actual_read_bytes, actual); |
| 38 | assert_eq!(fs::read(&path).unwrap(), b"abc"); |
| 39 | sandbox.finish(); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn cancel_at_opening() { |
| 44 | cancel(Stage::Opening, 0, 0); |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn cancel_at_pinned() { |
| 49 | cancel(Stage::Pinned, 0, 0); |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn cancel_at_copying() { |
| 54 | cancel(Stage::Copying, 0, 0); |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn cancel_at_comparing() { |
| 59 | cancel(Stage::Comparing, 2, 3); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn cancel_at_finalizing() { |
| 64 | cancel(Stage::Finalizing, 4, 6); |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn cancel_after_first_chunk_preserves_accumulated_usage() { |
| 69 | let sandbox = Sandbox::new(); |
| 70 | let path = sandbox.file("input", &pattern(65_537)); |
| 71 | let failure = capture( |
| 72 | Request { |
| 73 | path: &path, |
| 74 | limits: Limits::default(), |
| 75 | }, |
| 76 | |point| { |
| 77 | if point.stage == Stage::Copying && point.offset == 65_536 { |
| 78 | Err(Stop::<()>::Cancelled) |
| 79 | } else { |
| 80 | Ok(()) |
| 81 | } |
| 82 | }, |
| 83 | ) |
| 84 | .unwrap_err(); |
| 85 | assert!(matches!(failure.cause, Error::Cancelled)); |
| 86 | assert_eq!(failure.usage.read_calls, 1); |
| 87 | assert_eq!(failure.usage.actual_read_bytes, 65_536); |
| 88 | assert_eq!(failure.usage.reserved_read_bytes, 65_536); |
| 89 | sandbox.finish(); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn callback_preserves_non_send_error_identity() { |
| 94 | let sandbox = Sandbox::new(); |
| 95 | let path = sandbox.file("input", b"abc"); |
| 96 | let marker = Rc::new(String::from("typed")); |
| 97 | let failure = capture( |
| 98 | Request { |
| 99 | path: &path, |
| 100 | limits: Limits::default(), |
| 101 | }, |
| 102 | |point| { |
| 103 | if point.stage == Stage::Comparing { |
| 104 | Err(Stop::Callback(marker.clone())) |
| 105 | } else { |
| 106 | Ok(()) |
| 107 | } |
| 108 | }, |
| 109 | ) |
| 110 | .unwrap_err(); |
| 111 | match failure.cause { |
| 112 | Error::Callback(value) => assert!(Rc::ptr_eq(&value, &marker)), |
| 113 | other => panic!("unexpected: {other:?}"), |
| 114 | } |
| 115 | assert_eq!(failure.usage.actual_read_bytes, 3); |
| 116 | sandbox.finish(); |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn callback_error_precedes_next_observed_source_change() { |
| 121 | let sandbox = Sandbox::new(); |
| 122 | let path = sandbox.file("input", b"abc"); |
| 123 | let failure = capture( |
| 124 | Request { |
| 125 | path: &path, |
| 126 | limits: Limits::default(), |
| 127 | }, |
| 128 | |point| { |
| 129 | if point.stage == Stage::Comparing { |
| 130 | fs::write(&path, b"xyz").unwrap(); |
| 131 | Err(Stop::Callback(73_u16)) |
| 132 | } else { |
| 133 | Ok(()) |
| 134 | } |
| 135 | }, |
| 136 | ) |
| 137 | .unwrap_err(); |
| 138 | assert!(matches!(failure.cause, Error::Callback(73))); |
| 139 | assert_eq!(failure.usage.actual_read_bytes, 3); |
| 140 | assert_eq!(failure.usage.compared_bytes, 0); |
| 141 | sandbox.finish(); |
| 142 | } |
| 143 | |
| 144 | fn expired(stop: Option<Stop<u16>>) -> Failure<u16> { |
| 145 | let mut stop = Some(stop); |
| 146 | capture( |
| 147 | Request { |
| 148 | path: Path::new("invalid"), |
| 149 | limits: Limits { |
| 150 | max_duration: Duration::from_millis(1), |
| 151 | ..Limits::default() |
| 152 | }, |
| 153 | }, |
| 154 | |_| { |
| 155 | std::thread::sleep(Duration::from_millis(10)); |
| 156 | match stop.take().unwrap() { |
| 157 | Some(value) => Err(value), |
| 158 | None => Ok(()), |
| 159 | } |
| 160 | }, |
| 161 | ) |
| 162 | .unwrap_err() |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn natural_elapsed_deadline_precedes_path_validation() { |
| 167 | let failure = expired(None); |
| 168 | assert!(matches!(failure.cause, Error::Limit(LimitKind::Deadline))); |
| 169 | assert_eq!(failure.usage, Usage::default()); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn cancellation_precedes_expired_checkpoint_deadline() { |
| 174 | let failure = expired(Some(Stop::Cancelled)); |
| 175 | assert!(matches!(failure.cause, Error::Cancelled)); |
| 176 | assert_eq!(failure.usage, Usage::default()); |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn callback_error_precedes_expired_checkpoint_deadline() { |
| 181 | let failure = expired(Some(Stop::Callback(91))); |
| 182 | assert!(matches!(failure.cause, Error::Callback(91))); |
| 183 | assert_eq!(failure.usage, Usage::default()); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn checkpoint_order_offsets_and_usage_are_observable() { |
| 188 | let sandbox = Sandbox::new(); |
| 189 | let path = sandbox.file("input", b"abc"); |
| 190 | let mut points: Vec<Checkpoint> = Vec::new(); |
| 191 | let result = capture( |
| 192 | Request { |
| 193 | path: &path, |
| 194 | limits: Limits::default(), |
| 195 | }, |
| 196 | |point| { |
| 197 | points.push(point); |
| 198 | Ok::<_, Stop<()>>(()) |
| 199 | }, |
| 200 | ) |
| 201 | .unwrap(); |
| 202 | assert_eq!( |
| 203 | points |
| 204 | .iter() |
| 205 | .map(|p| (p.stage, p.offset)) |
| 206 | .collect::<Vec<_>>(), |
| 207 | vec![ |
| 208 | (Stage::Opening, 0), |
| 209 | (Stage::Pinned, 0), |
| 210 | (Stage::Copying, 0), |
| 211 | (Stage::Copying, 0), |
| 212 | (Stage::Copying, 3), |
| 213 | (Stage::Comparing, 0), |
| 214 | (Stage::Comparing, 3), |
| 215 | (Stage::Finalizing, 3), |
| 216 | ] |
| 217 | ); |
| 218 | assert_eq!(points[0].length, None); |
| 219 | assert!(points[1..].iter().all(|p| p.length == Some(3))); |
| 220 | for pair in points.windows(2) { |
| 221 | assert!(pair[0].usage.open_calls <= pair[1].usage.open_calls); |
| 222 | assert!(pair[0].usage.metadata_calls <= pair[1].usage.metadata_calls); |
| 223 | assert!(pair[0].usage.read_calls <= pair[1].usage.read_calls); |
| 224 | assert!(pair[0].usage.reserved_read_bytes <= pair[1].usage.reserved_read_bytes); |
| 225 | assert!(pair[0].usage.actual_read_bytes <= pair[1].usage.actual_read_bytes); |
| 226 | assert!(pair[0].usage.compared_bytes <= pair[1].usage.compared_bytes); |
| 227 | } |
| 228 | expect_usage(&result, 3); |
| 229 | sandbox.finish(); |
| 230 | } |
| 231 | |
| 232 | #[test] |
| 233 | fn callback_panic_propagates_without_snapshot() { |
| 234 | let sandbox = Sandbox::new(); |
| 235 | let path = sandbox.file("input", b"abc"); |
| 236 | let result = catch_unwind(AssertUnwindSafe(|| { |
| 237 | let _: Result<FileSnapshot, Failure<()>> = capture( |
| 238 | Request { |
| 239 | path: &path, |
| 240 | limits: Limits::default(), |
| 241 | }, |
| 242 | |point| { |
| 243 | if point.stage == Stage::Copying { |
| 244 | panic!("intentional-checkpoint-panic"); |
| 245 | } |
| 246 | Ok(()) |
| 247 | }, |
| 248 | ); |
| 249 | })); |
| 250 | assert!(result.is_err()); |
| 251 | assert_eq!(fs::read(&path).unwrap(), b"abc"); |
| 252 | sandbox.finish(); |
| 253 | } |
| 254 | |
| 255 | #[test] |
| 256 | fn cancellation_requested_after_return_does_not_change_owned_result() { |
| 257 | let sandbox = Sandbox::new(); |
| 258 | let path = sandbox.file("input", b"abc"); |
| 259 | let requested = Cell::new(false); |
| 260 | let result = capture( |
| 261 | Request { |
| 262 | path: &path, |
| 263 | limits: Limits::default(), |
| 264 | }, |
| 265 | |_| { |
| 266 | if requested.get() { |
| 267 | Err(Stop::<()>::Cancelled) |
| 268 | } else { |
| 269 | Ok(()) |
| 270 | } |
| 271 | }, |
| 272 | ) |
| 273 | .unwrap(); |
| 274 | requested.set(true); |
| 275 | assert_eq!(result.bytes(), b"abc"); |
| 276 | expect_usage(&result, 3); |
| 277 | sandbox.finish(); |
| 278 | } |
| 279 | |
| 280 | #[test] |
| 281 | fn selected_public_types_have_send_bounds() { |
| 282 | fn send<T: Send>() {} |
| 283 | send::<FileSnapshot>(); |
| 284 | send::<Identity>(); |
| 285 | send::<Request<'static>>(); |
| 286 | send::<Failure<String>>(); |
| 287 | send::<Stop<String>>(); |
| 288 | } |