| 1 | use crate::support::*; |
| 2 | use std::{cell::Cell, convert::Infallible, fmt}; |
| 3 | use zipelo_memory_ustar_reader::{Checkpoint, Error, Failure, Stop, read}; |
| 4 | |
| 5 | #[derive(Debug)] |
| 6 | struct Marker { |
| 7 | payload: Box<u64>, |
| 8 | } |
| 9 | |
| 10 | impl fmt::Display for Marker { |
| 11 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 12 | write!(f, "선택 콜백 오류 {}", self.payload) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | impl std::error::Error for Marker {} |
| 17 | |
| 18 | fn trace() -> Vec<Checkpoint> { |
| 19 | let mut points = Vec::new(); |
| 20 | read( |
| 21 | request(normal()), |
| 22 | |_| Ok::<_, Infallible>(ids(&["2", "3"])), |
| 23 | |point| { |
| 24 | points.push(point); |
| 25 | Ok(()) |
| 26 | }, |
| 27 | ) |
| 28 | .expect("기준 순회는 성공해야 함"); |
| 29 | points |
| 30 | } |
| 31 | |
| 32 | #[test] |
| 33 | fn read_checkpoints_cover_both_passes_with_bounded_steps() { |
| 34 | let points = trace(); |
| 35 | assert_eq!(points.first(), Some(&Checkpoint::Start)); |
| 36 | assert_eq!(points.last(), Some(&Checkpoint::Finish)); |
| 37 | for pass in [1, 2] { |
| 38 | let offsets = points |
| 39 | .iter() |
| 40 | .filter_map(|point| match point { |
| 41 | Checkpoint::Read { |
| 42 | pass: current, |
| 43 | offset, |
| 44 | } if *current == pass => Some(*offset), |
| 45 | _ => None, |
| 46 | }) |
| 47 | .collect::<Vec<_>>(); |
| 48 | assert_eq!(offsets.first(), Some(&0)); |
| 49 | assert_eq!(offsets.last(), Some(&normal().len())); |
| 50 | assert!( |
| 51 | offsets |
| 52 | .windows(2) |
| 53 | .all(|pair| pair[1] >= pair[0] && pair[1] - pair[0] <= 65_536) |
| 54 | ); |
| 55 | assert!(offsets.windows(2).any(|pair| pair[1] - pair[0] == 65_536)); |
| 56 | } |
| 57 | assert_eq!( |
| 58 | points |
| 59 | .iter() |
| 60 | .filter(|p| **p == Checkpoint::BeforeSelection) |
| 61 | .count(), |
| 62 | 1 |
| 63 | ); |
| 64 | assert_eq!( |
| 65 | points |
| 66 | .iter() |
| 67 | .filter(|p| **p == Checkpoint::AfterSelection) |
| 68 | .count(), |
| 69 | 1 |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | fn stop_at_each_checkpoint(stop: Stop, expected: Failure) { |
| 74 | let reference = trace(); |
| 75 | for stop_index in 0..reference.len() { |
| 76 | let mut observed = Vec::new(); |
| 77 | let result = read( |
| 78 | request(normal()), |
| 79 | |_| Ok::<_, Infallible>(ids(&["2", "3"])), |
| 80 | |point| { |
| 81 | observed.push(point); |
| 82 | if observed.len() - 1 == stop_index { |
| 83 | Err(stop) |
| 84 | } else { |
| 85 | Ok(()) |
| 86 | } |
| 87 | }, |
| 88 | ); |
| 89 | failure(result, expected); |
| 90 | assert_eq!( |
| 91 | observed, |
| 92 | reference[..=stop_index], |
| 93 | "중단 뒤에 추가 체크가 실행됨" |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn cancellation_at_every_checkpoint_returns_no_completed() { |
| 100 | stop_at_each_checkpoint(Stop::Cancelled, Failure::Cancelled); |
| 101 | } |
| 102 | |
| 103 | // Deadline 신호 분류 시험이며 실제 시계 만료 시험이 아님. |
| 104 | #[test] |
| 105 | fn injected_deadline_at_every_checkpoint_is_budget() { |
| 106 | stop_at_each_checkpoint(Stop::Deadline, Failure::Budget); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn cancellation_before_selection_prevents_callback() { |
| 111 | let called = Cell::new(false); |
| 112 | let result = read( |
| 113 | request(normal()), |
| 114 | |_| { |
| 115 | called.set(true); |
| 116 | Ok::<_, Infallible>(ids(&["1"])) |
| 117 | }, |
| 118 | |point| { |
| 119 | if point == Checkpoint::BeforeSelection { |
| 120 | Err(Stop::Cancelled) |
| 121 | } else { |
| 122 | Ok(()) |
| 123 | } |
| 124 | }, |
| 125 | ); |
| 126 | failure(result, Failure::Cancelled); |
| 127 | assert!(!called.get()); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn selection_can_request_cancellation_before_second_pass() { |
| 132 | let cancelled = Cell::new(false); |
| 133 | let mut second = false; |
| 134 | let result = read( |
| 135 | request(normal()), |
| 136 | |_| { |
| 137 | cancelled.set(true); |
| 138 | Ok::<_, Infallible>(ids(&["1"])) |
| 139 | }, |
| 140 | |point| { |
| 141 | if matches!(point, Checkpoint::Read { pass: 2, .. }) { |
| 142 | second = true; |
| 143 | } |
| 144 | if cancelled.get() { |
| 145 | Err(Stop::Cancelled) |
| 146 | } else { |
| 147 | Ok(()) |
| 148 | } |
| 149 | }, |
| 150 | ); |
| 151 | failure(result, Failure::Cancelled); |
| 152 | assert!(!second); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn empty_selection_after_callback_cancellation_still_fails() { |
| 157 | let result = read( |
| 158 | request(normal()), |
| 159 | |_| Ok::<_, Infallible>(vec![]), |
| 160 | |point| { |
| 161 | if point == Checkpoint::AfterSelection { |
| 162 | Err(Stop::Cancelled) |
| 163 | } else { |
| 164 | Ok(()) |
| 165 | } |
| 166 | }, |
| 167 | ); |
| 168 | failure(result, Failure::Cancelled); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn final_checkpoint_failure_discards_accumulated_results() { |
| 173 | let mut saw_end = false; |
| 174 | let size = normal().len(); |
| 175 | let result = read( |
| 176 | request(normal()), |
| 177 | |_| Ok::<_, Infallible>(ids(&["1", "3"])), |
| 178 | |point| { |
| 179 | if point |
| 180 | == (Checkpoint::Read { |
| 181 | pass: 2, |
| 182 | offset: size, |
| 183 | }) |
| 184 | { |
| 185 | saw_end = true; |
| 186 | } |
| 187 | if point == Checkpoint::Finish { |
| 188 | Err(Stop::Cancelled) |
| 189 | } else { |
| 190 | Ok(()) |
| 191 | } |
| 192 | }, |
| 193 | ); |
| 194 | failure(result, Failure::Cancelled); |
| 195 | assert!(saw_end); |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn callback_error_preserves_owned_identity_and_source() { |
| 200 | let marker = Marker { |
| 201 | payload: Box::new(42), |
| 202 | }; |
| 203 | let pointer = marker.payload.as_ref() as *const u64; |
| 204 | let mut second = false; |
| 205 | let error = read( |
| 206 | request(normal()), |
| 207 | |_| Err(marker), |
| 208 | |point| { |
| 209 | if matches!(point, Checkpoint::Read { pass: 2, .. }) { |
| 210 | second = true; |
| 211 | } |
| 212 | Ok(()) |
| 213 | }, |
| 214 | ) |
| 215 | .expect_err("선택 오류가 성공으로 바뀌면 안 됨"); |
| 216 | let source = std::error::Error::source(&error).expect("원래 오류가 source로 남아야 함"); |
| 217 | assert_eq!( |
| 218 | source |
| 219 | .downcast_ref::<Marker>() |
| 220 | .expect("오류 형식이 보존되어야 함") |
| 221 | .payload |
| 222 | .as_ref() as *const u64, |
| 223 | pointer |
| 224 | ); |
| 225 | match error { |
| 226 | Error::Selection(marker) => assert_eq!(marker.payload.as_ref() as *const u64, pointer), |
| 227 | other => panic!("원래 선택 오류가 사라짐: {other:?}"), |
| 228 | } |
| 229 | assert!(!second); |
| 230 | } |
| 231 | |
| 232 | #[test] |
| 233 | fn callback_error_stops_before_after_checkpoint() { |
| 234 | let marker = Marker { |
| 235 | payload: Box::new(43), |
| 236 | }; |
| 237 | let pointer = marker.payload.as_ref() as *const u64; |
| 238 | let selection_failed = Cell::new(false); |
| 239 | let mut after_error = Vec::new(); |
| 240 | let error = read( |
| 241 | request(normal()), |
| 242 | |_| { |
| 243 | selection_failed.set(true); |
| 244 | Err(marker) |
| 245 | }, |
| 246 | |point| { |
| 247 | if selection_failed.get() { |
| 248 | after_error.push(point); |
| 249 | Err(Stop::Deadline) |
| 250 | } else { |
| 251 | Ok(()) |
| 252 | } |
| 253 | }, |
| 254 | ) |
| 255 | .expect_err("선택 오류를 반환해야 함"); |
| 256 | match error { |
| 257 | Error::Selection(marker) => assert_eq!(marker.payload.as_ref() as *const u64, pointer), |
| 258 | other => panic!("원래 오류가 교체됨: {other:?}"), |
| 259 | } |
| 260 | assert!( |
| 261 | after_error.is_empty(), |
| 262 | "선택 오류 뒤에 checkpoint가 호출됨: {after_error:?}" |
| 263 | ); |
| 264 | } |