| 1 | use crate::{fixtures::*, support::*}; |
| 2 | use job::{Limits, Outcome, gzip, publication, snapshot}; |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | fn snapshot_limit(kind: snapshot::LimitKind) { |
| 6 | let sandbox = Sandbox::new(); |
| 7 | let archive = stored(b"abc"); |
| 8 | let length = archive.len() as u64; |
| 9 | let input = sandbox.file("input.gz", &archive); |
| 10 | let parent = sandbox.parent(); |
| 11 | let mut limits = Limits::default(); |
| 12 | match kind { |
| 13 | snapshot::LimitKind::Bytes => limits.snapshot.max_bytes = length - 1, |
| 14 | snapshot::LimitKind::IoBytes => limits.snapshot.max_io_bytes = 2 * length + 1, |
| 15 | snapshot::LimitKind::ReadCalls => limits.snapshot.max_read_calls = 3, |
| 16 | _ => unreachable!(), |
| 17 | } |
| 18 | let (report, _) = run(&input, &parent, limits); |
| 19 | match &report.outcome { |
| 20 | Outcome::SnapshotFailed(snapshot::Failure { |
| 21 | cause: snapshot::Error::Limit(actual), |
| 22 | usage, |
| 23 | }) => { |
| 24 | assert_eq!(*actual, kind); |
| 25 | assert_eq!(*usage, report.usage.snapshot); |
| 26 | } |
| 27 | _ => panic!("snapshot 한도 오류가 아님"), |
| 28 | } |
| 29 | let expected = if kind == snapshot::LimitKind::Bytes { |
| 30 | snapshot::Usage { |
| 31 | open_calls: 1, |
| 32 | metadata_calls: 1, |
| 33 | ..snapshot::Usage::default() |
| 34 | } |
| 35 | } else { |
| 36 | snapshot::Usage { |
| 37 | open_calls: 4, |
| 38 | metadata_calls: 4, |
| 39 | read_calls: 3, |
| 40 | reserved_read_bytes: 2 * length + 1, |
| 41 | actual_read_bytes: 2 * length, |
| 42 | compared_bytes: length, |
| 43 | } |
| 44 | }; |
| 45 | assert_eq!(report.usage.snapshot, expected); |
| 46 | assert!(names(&parent).is_empty()); |
| 47 | sandbox.finish(); |
| 48 | } |
| 49 | #[test] |
| 50 | fn snapshot_byte_limit_preserves_partial_usage() { |
| 51 | snapshot_limit(snapshot::LimitKind::Bytes); |
| 52 | } |
| 53 | #[test] |
| 54 | fn snapshot_io_limit_retains_last_eof_reservation_boundary() { |
| 55 | snapshot_limit(snapshot::LimitKind::IoBytes); |
| 56 | } |
| 57 | #[test] |
| 58 | fn snapshot_call_limit_retains_last_eof_reservation_boundary() { |
| 59 | snapshot_limit(snapshot::LimitKind::ReadCalls); |
| 60 | } |
| 61 | |
| 62 | fn gzip_limit(field: &str) { |
| 63 | let sandbox = Sandbox::new(); |
| 64 | let archive = stored(b"abc"); |
| 65 | let length = archive.len() as u64; |
| 66 | let input = sandbox.file("input.gz", &archive); |
| 67 | let parent = sandbox.parent(); |
| 68 | let mut limits = Limits::default(); |
| 69 | let expected = match field { |
| 70 | "input" => { |
| 71 | limits.gzip.input = length - 1; |
| 72 | gzip::Usage::default() |
| 73 | } |
| 74 | "processed" => { |
| 75 | limits.gzip.processed = length + 2; |
| 76 | gzip::Usage { |
| 77 | processed: length + 3, |
| 78 | output: 0, |
| 79 | metadata: 10, |
| 80 | } |
| 81 | } |
| 82 | "output" => { |
| 83 | limits.gzip.output = 2; |
| 84 | gzip::Usage { |
| 85 | processed: length + 3, |
| 86 | output: 0, |
| 87 | metadata: 10, |
| 88 | } |
| 89 | } |
| 90 | "metadata" => { |
| 91 | limits.gzip.metadata = 9; |
| 92 | gzip::Usage { |
| 93 | processed: 9, |
| 94 | output: 0, |
| 95 | metadata: 9, |
| 96 | } |
| 97 | } |
| 98 | _ => unreachable!(), |
| 99 | }; |
| 100 | let (report, trace) = run(&input, &parent, limits); |
| 101 | assert!(matches!( |
| 102 | report.outcome, |
| 103 | Outcome::DecodeFailed(gzip::Error::Reader(gzip::Failure::Budget)) |
| 104 | )); |
| 105 | assert_eq!(report.usage.gzip, expected); |
| 106 | snapshot_usage(report.usage.snapshot, archive.len()); |
| 107 | if field == "processed" { |
| 108 | assert!(report.usage.gzip.processed > trace.points.last().unwrap().usage.gzip.processed); |
| 109 | } |
| 110 | assert!(names(&parent).is_empty()); |
| 111 | sandbox.finish(); |
| 112 | } |
| 113 | #[test] |
| 114 | fn gzip_input_limit_stops_after_snapshot() { |
| 115 | gzip_limit("input"); |
| 116 | } |
| 117 | #[test] |
| 118 | fn gzip_processed_failure_includes_post_checkpoint_charge() { |
| 119 | gzip_limit("processed"); |
| 120 | } |
| 121 | #[test] |
| 122 | fn gzip_selected_output_limit_preserves_consumed_cost() { |
| 123 | gzip_limit("output"); |
| 124 | } |
| 125 | #[test] |
| 126 | fn gzip_metadata_limit_preserves_header_cost() { |
| 127 | gzip_limit("metadata"); |
| 128 | } |
| 129 | |
| 130 | fn publication_limit(field: &str) { |
| 131 | let sandbox = Sandbox::new(); |
| 132 | let archive = stored(b"abc"); |
| 133 | let input = sandbox.file("input.gz", &archive); |
| 134 | let parent = sandbox.parent(); |
| 135 | let mut limits = Limits::default(); |
| 136 | match field { |
| 137 | "bytes" => limits.publication.max_bytes = 2, |
| 138 | "io" => limits.publication.max_io_bytes = 6, |
| 139 | "calls" => limits.publication.max_io_calls = 2, |
| 140 | _ => unreachable!(), |
| 141 | } |
| 142 | let (report, _) = run(&input, &parent, limits); |
| 143 | let native = match &report.outcome { |
| 144 | Outcome::Publication(native) => native, |
| 145 | _ => panic!("게시 단계 미도달"), |
| 146 | }; |
| 147 | assert!(matches!( |
| 148 | native.outcome, |
| 149 | publication::Outcome::NotCommitted(publication::Error::LimitExceeded) |
| 150 | )); |
| 151 | let expected = if field == "bytes" { |
| 152 | cleanup(&native.cleanup, 0); |
| 153 | publication::Usage::default() |
| 154 | } else { |
| 155 | cleanup(&native.cleanup, 2); |
| 156 | publication::Usage { |
| 157 | open_calls: 2, |
| 158 | metadata_calls: 3, |
| 159 | sync_calls: 1, |
| 160 | write_calls: 1, |
| 161 | read_calls: 1, |
| 162 | reserved_io_bytes: 6, |
| 163 | actual_written_bytes: 3, |
| 164 | actual_read_bytes: 3, |
| 165 | ..publication::Usage::default() |
| 166 | } |
| 167 | }; |
| 168 | assert_eq!(report.usage.publication, expected); |
| 169 | snapshot_usage(report.usage.snapshot, archive.len()); |
| 170 | gzip_usage(report.usage.gzip, &archive, b"abc", 10); |
| 171 | assert!(names(&parent).is_empty()); |
| 172 | sandbox.finish(); |
| 173 | } |
| 174 | #[test] |
| 175 | fn publication_byte_limit_preserves_completed_prior_stages() { |
| 176 | publication_limit("bytes"); |
| 177 | } |
| 178 | #[test] |
| 179 | fn publication_io_limit_retains_pre_eof_usage() { |
| 180 | publication_limit("io"); |
| 181 | } |
| 182 | #[test] |
| 183 | fn publication_call_limit_retains_pre_eof_usage() { |
| 184 | publication_limit("calls"); |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn exact_limits_in_all_stages_allow_commit() { |
| 189 | let sandbox = Sandbox::new(); |
| 190 | let archive = stored(b"abc"); |
| 191 | let length = archive.len() as u64; |
| 192 | let input = sandbox.file("input.gz", &archive); |
| 193 | let before = identity(&input); |
| 194 | let parent = sandbox.parent(); |
| 195 | let limits = Limits { |
| 196 | snapshot: snapshot::Limits { |
| 197 | max_bytes: length, |
| 198 | max_io_bytes: 2 * length + 2, |
| 199 | max_read_calls: 4, |
| 200 | ..snapshot::Limits::default() |
| 201 | }, |
| 202 | gzip: gzip::Limits { |
| 203 | input: length, |
| 204 | processed: length + 3, |
| 205 | output: 3, |
| 206 | metadata: 10, |
| 207 | ..gzip::Limits::default() |
| 208 | }, |
| 209 | publication: publication::Limits { |
| 210 | max_bytes: 3, |
| 211 | max_io_bytes: 7, |
| 212 | max_io_calls: 3, |
| 213 | ..publication::Limits::default() |
| 214 | }, |
| 215 | }; |
| 216 | let (report, _) = run(&input, &parent, limits); |
| 217 | observations(&report, (before.0, before.1), &archive, b"abc", 10); |
| 218 | committed(&report, &parent, b"abc"); |
| 219 | sandbox.finish(); |
| 220 | } |
| 221 | |
| 222 | fn invalid_duration(stage: u8) { |
| 223 | let sandbox = Sandbox::new(); |
| 224 | let archive = stored(b"abc"); |
| 225 | let input = sandbox.file("input.gz", &archive); |
| 226 | let parent = sandbox.parent(); |
| 227 | let mut limits = Limits::default(); |
| 228 | match stage { |
| 229 | 0 => limits.snapshot.max_duration = Duration::ZERO, |
| 230 | 1 => limits.gzip.duration = Duration::ZERO, |
| 231 | 2 => limits.publication.max_duration = Duration::ZERO, |
| 232 | _ => unreachable!(), |
| 233 | } |
| 234 | let (report, trace) = run(&input, &parent, limits); |
| 235 | match (&report.outcome, stage) { |
| 236 | (Outcome::SnapshotFailed(failure), 0) => { |
| 237 | assert!(matches!(failure.cause, snapshot::Error::InvalidLimits)); |
| 238 | assert_eq!(report.usage, job::Usage::default()); |
| 239 | assert!(trace.points.is_empty()); |
| 240 | } |
| 241 | (Outcome::DecodeFailed(gzip::Error::Reader(gzip::Failure::InvalidRequest)), 1) => { |
| 242 | snapshot_usage(report.usage.snapshot, archive.len()); |
| 243 | assert_eq!(report.usage.gzip, gzip::Usage::default()); |
| 244 | assert!(trace.points.iter().all(|p| phase(p.event) == 0)); |
| 245 | } |
| 246 | (Outcome::Publication(native), 2) => { |
| 247 | snapshot_usage(report.usage.snapshot, archive.len()); |
| 248 | gzip_usage(report.usage.gzip, &archive, b"abc", 10); |
| 249 | assert!(matches!( |
| 250 | native.outcome, |
| 251 | publication::Outcome::NotCommitted(publication::Error::InvalidLimits) |
| 252 | )); |
| 253 | assert_eq!(native.usage, publication::Usage::default()); |
| 254 | cleanup(&native.cleanup, 0); |
| 255 | assert!(trace.points.iter().all(|p| phase(p.event) <= 1)); |
| 256 | } |
| 257 | _ => panic!("단계별 잘못된 한도 오류가 아님"), |
| 258 | } |
| 259 | assert!(names(&parent).is_empty()); |
| 260 | sandbox.finish(); |
| 261 | } |
| 262 | #[test] |
| 263 | fn invalid_snapshot_duration_has_zero_cost() { |
| 264 | invalid_duration(0); |
| 265 | } |
| 266 | #[test] |
| 267 | fn invalid_decode_duration_retains_snapshot_cost() { |
| 268 | invalid_duration(1); |
| 269 | } |
| 270 | #[test] |
| 271 | fn invalid_publication_duration_retains_both_prior_costs() { |
| 272 | invalid_duration(2); |
| 273 | } |