| 1 | use super::*; |
| 2 | |
| 3 | fn budget() -> Budget { |
| 4 | Budget { |
| 5 | soft: Limits::default(), |
| 6 | ceiling: Limits { |
| 7 | input: u64::MAX, |
| 8 | output: 64 * 1024 * 1024 * 1024, |
| 9 | entries: 200_000, |
| 10 | }, |
| 11 | granted: ResourceScope::default(), |
| 12 | } |
| 13 | } |
| 14 | #[test] |
| 15 | fn output_boundaries_and_absolute_ceiling() { |
| 16 | let mut b = budget(); |
| 17 | let soft = b.soft.output; |
| 18 | for n in [soft - 1, soft] { |
| 19 | assert!( |
| 20 | !b.required(ResourceUsage { |
| 21 | actual_bytes: Some(n), |
| 22 | ..ResourceUsage::default() |
| 23 | }) |
| 24 | .unwrap() |
| 25 | .any() |
| 26 | ); |
| 27 | } |
| 28 | let usage = ResourceUsage { |
| 29 | actual_bytes: Some(soft + 1), |
| 30 | ..ResourceUsage::default() |
| 31 | }; |
| 32 | let scope = b.required(usage).unwrap(); |
| 33 | assert_eq!( |
| 34 | scope, |
| 35 | ResourceScope { |
| 36 | output: true, |
| 37 | ..ResourceScope::default() |
| 38 | } |
| 39 | ); |
| 40 | b.grant(scope); |
| 41 | assert!(!b.required(usage).unwrap().any()); |
| 42 | assert!( |
| 43 | !b.required(ResourceUsage { |
| 44 | actual_bytes: Some(b.ceiling.output), |
| 45 | ..ResourceUsage::default() |
| 46 | }) |
| 47 | .unwrap() |
| 48 | .any() |
| 49 | ); |
| 50 | assert_eq!( |
| 51 | b.required(ResourceUsage { |
| 52 | actual_bytes: Some(b.ceiling.output + 1), |
| 53 | ..ResourceUsage::default() |
| 54 | }), |
| 55 | Err(ErrorCode::Limit) |
| 56 | ); |
| 57 | } |
| 58 | #[test] |
| 59 | fn entries_need_their_own_grant_and_keep_absolute_ceiling() { |
| 60 | let mut b = budget(); |
| 61 | b.grant(ResourceScope { |
| 62 | output: true, |
| 63 | ..ResourceScope::default() |
| 64 | }); |
| 65 | assert!( |
| 66 | !b.required(ResourceUsage { |
| 67 | estimated_entries: Some(10_000), |
| 68 | ..ResourceUsage::default() |
| 69 | }) |
| 70 | .unwrap() |
| 71 | .any() |
| 72 | ); |
| 73 | let usage = ResourceUsage { |
| 74 | estimated_entries: Some(10_001), |
| 75 | ..ResourceUsage::default() |
| 76 | }; |
| 77 | assert!(b.required(usage).unwrap().entries); |
| 78 | b.grant(ResourceScope { |
| 79 | entries: true, |
| 80 | ..ResourceScope::default() |
| 81 | }); |
| 82 | assert!( |
| 83 | !b.required(ResourceUsage { |
| 84 | estimated_entries: Some(200_000), |
| 85 | ..ResourceUsage::default() |
| 86 | }) |
| 87 | .unwrap() |
| 88 | .any() |
| 89 | ); |
| 90 | assert_eq!( |
| 91 | b.required(ResourceUsage { |
| 92 | actual_entries: Some(200_001), |
| 93 | ..ResourceUsage::default() |
| 94 | }), |
| 95 | Err(ErrorCode::Limit) |
| 96 | ); |
| 97 | } |
| 98 | #[test] |
| 99 | fn ratio_floor_boundary_unknown_and_zero_input() { |
| 100 | let b = budget(); |
| 101 | let usage = |output, input| ResourceUsage { |
| 102 | ratio_output_bytes: Some(output), |
| 103 | compressed_bytes: input, |
| 104 | ..ResourceUsage::default() |
| 105 | }; |
| 106 | assert!( |
| 107 | !b.required(usage(256 * 1024 * 1024 - 1, Some(1))) |
| 108 | .unwrap() |
| 109 | .ratio |
| 110 | ); |
| 111 | assert!(!b.required(usage(300_000_000, Some(300_000))).unwrap().ratio); |
| 112 | assert!(b.required(usage(300_000_001, Some(300_000))).unwrap().ratio); |
| 113 | assert!(b.required(usage(256 * 1024 * 1024, Some(0))).unwrap().ratio); |
| 114 | assert!(!b.required(usage(256 * 1024 * 1024, None)).unwrap().ratio); |
| 115 | assert!(!b.required(ResourceUsage::default()).unwrap().any()); |
| 116 | } |
| 117 | #[test] |
| 118 | fn estimated_and_actual_totals_are_both_enforced() { |
| 119 | let b = budget(); |
| 120 | for usage in [ |
| 121 | ResourceUsage { |
| 122 | estimated_bytes: Some(b.soft.output + 1), |
| 123 | actual_bytes: Some(0), |
| 124 | ..ResourceUsage::default() |
| 125 | }, |
| 126 | ResourceUsage { |
| 127 | estimated_bytes: Some(0), |
| 128 | actual_bytes: Some(b.soft.output + 1), |
| 129 | ..ResourceUsage::default() |
| 130 | }, |
| 131 | ] { |
| 132 | assert!(b.required(usage).unwrap().output); |
| 133 | } |
| 134 | } |
| 135 | #[test] |
| 136 | fn idle_timeout_and_activity_use_virtual_time() { |
| 137 | let mut w = Watchdog::new(); |
| 138 | assert!(w.check(Duration::from_secs(300)).is_ok()); |
| 139 | assert_eq!(w.check(Duration::from_secs(301)), Err(ErrorCode::Limit)); |
| 140 | w.resume(Duration::from_secs(299), true); |
| 141 | assert!(w.check(Duration::from_secs(358)).is_ok()); |
| 142 | assert_eq!(w.check(Duration::from_secs(359)), Err(ErrorCode::Limit)); |
| 143 | w.activity(Duration::from_secs(350)).unwrap(); |
| 144 | assert!(w.check(Duration::from_secs(409)).is_ok()); |
| 145 | assert_eq!(w.check(Duration::from_secs(410)), Err(ErrorCode::Limit)); |
| 146 | } |
| 147 | #[test] |
| 148 | fn approved_progress_can_run_longer_than_five_minutes() { |
| 149 | let mut w = Watchdog::new(); |
| 150 | w.resume(Duration::ZERO, true); |
| 151 | for t in (1..=3600).step_by(30) { |
| 152 | w.activity(Duration::from_secs(t)).unwrap(); |
| 153 | } |
| 154 | assert!(w.check(Duration::from_secs(3600)).is_ok()); |
| 155 | w.resume(Duration::from_secs(3900), false); |
| 156 | assert!(w.check(Duration::from_secs(3959)).is_ok()); |
| 157 | assert_eq!(w.check(Duration::from_secs(3960)), Err(ErrorCode::Limit)); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn granted_entry_policy_keeps_independent_metadata_budget() { |
| 162 | let mut b = budget(); |
| 163 | b.grant(ResourceScope { |
| 164 | entries: true, |
| 165 | ..ResourceScope::default() |
| 166 | }); |
| 167 | assert!( |
| 168 | !b.required(ResourceUsage { |
| 169 | estimated_entries: Some(200_000), |
| 170 | ..ResourceUsage::default() |
| 171 | }) |
| 172 | .unwrap() |
| 173 | .any() |
| 174 | ); |
| 175 | let mut metadata = crate::archive::MetadataBudget::default(); |
| 176 | for _ in 0..32768 { |
| 177 | metadata |
| 178 | .charge_with("", crate::archive::MetadataLimits::default()) |
| 179 | .unwrap(); |
| 180 | } |
| 181 | assert_eq!( |
| 182 | metadata.charge_with("", crate::archive::MetadataLimits::default()), |
| 183 | Err(ErrorCode::Limit) |
| 184 | ); |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn successful_item_completion_refreshes_only_live_work() { |
| 189 | struct Clock { |
| 190 | now: Duration, |
| 191 | watchdog: Watchdog, |
| 192 | } |
| 193 | impl Monitor for Clock { |
| 194 | fn check(&mut self) -> Result<()> { |
| 195 | self.watchdog.check(self.now) |
| 196 | } |
| 197 | fn activity(&mut self) -> Result<()> { |
| 198 | self.watchdog.activity(self.now) |
| 199 | } |
| 200 | } |
| 201 | let mut clock = Clock { |
| 202 | now: Duration::ZERO, |
| 203 | watchdog: Watchdog::new(), |
| 204 | }; |
| 205 | clock.watchdog.resume(Duration::ZERO, true); |
| 206 | for item in 1..=100 { |
| 207 | clock.now += Duration::from_secs(10); |
| 208 | assert_eq!(completed(Ok(item), &mut clock), Ok(item)); |
| 209 | } |
| 210 | clock.now += Duration::from_secs(30); |
| 211 | assert_eq!( |
| 212 | completed::<()>(Err(ErrorCode::Io), &mut clock), |
| 213 | Err(ErrorCode::Io) |
| 214 | ); |
| 215 | for _ in 0..29 { |
| 216 | clock.now += Duration::from_secs(1); |
| 217 | clock.check().unwrap(); |
| 218 | } |
| 219 | clock.now += Duration::from_secs(1); |
| 220 | assert_eq!(clock.check(), Err(ErrorCode::Limit)); |
| 221 | assert_eq!(completed(Ok(()), &mut clock), Err(ErrorCode::Limit)); |
| 222 | assert_eq!( |
| 223 | completed::<()>(Err(ErrorCode::Io), &mut clock), |
| 224 | Err(ErrorCode::Io) |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn empty_gzip_members_count_consumed_input_as_decode_progress() { |
| 230 | use std::io::Write; |
| 231 | struct DecodeClock { |
| 232 | now: Duration, |
| 233 | steps: usize, |
| 234 | watchdog: Watchdog, |
| 235 | } |
| 236 | impl Monitor for DecodeClock { |
| 237 | fn check(&mut self) -> Result<()> { |
| 238 | self.watchdog.check(self.now) |
| 239 | } |
| 240 | fn activity(&mut self) -> Result<()> { |
| 241 | self.watchdog.activity(self.now) |
| 242 | } |
| 243 | fn observe(&mut self, _: ResourceUsage, _: Option<&std::fs::File>) -> Result<()> { |
| 244 | self.steps += 1; |
| 245 | self.now += Duration::from_secs(10); |
| 246 | self.check() |
| 247 | } |
| 248 | } |
| 249 | let mut clock = DecodeClock { |
| 250 | now: Duration::ZERO, |
| 251 | steps: 0, |
| 252 | watchdog: Watchdog::new(), |
| 253 | }; |
| 254 | clock.watchdog.resume(Duration::ZERO, true); |
| 255 | let member = [ |
| 256 | 31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 257 | ]; |
| 258 | let mut input = crate::spool::temporary().unwrap(); |
| 259 | for _ in 0..8 { |
| 260 | input.write_all(&member).unwrap(); |
| 261 | } |
| 262 | let catalog = crate::streaming::Catalog::open( |
| 263 | &mut input, |
| 264 | 8 * member.len() as u64, |
| 265 | Limits::default(), |
| 266 | true, |
| 267 | None, |
| 268 | &mut clock, |
| 269 | ) |
| 270 | .unwrap(); |
| 271 | assert_eq!(catalog.format, Format::Gzip); |
| 272 | assert_eq!(catalog.members.len(), 1); |
| 273 | assert_eq!(catalog.members[0].entry.size, 0); |
| 274 | assert!(clock.steps >= 8); |
| 275 | clock.check().unwrap(); |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn metadata_monitor_keeps_its_budget_and_parent_cancellation() { |
| 280 | struct Parent { |
| 281 | observed: usize, |
| 282 | active: usize, |
| 283 | cancelled: bool, |
| 284 | } |
| 285 | impl Monitor for Parent { |
| 286 | fn metadata_limits(&self) -> crate::archive::MetadataLimits { |
| 287 | crate::archive::MetadataLimits { |
| 288 | bytes: 16, |
| 289 | nodes: 4, |
| 290 | enlarged: false, |
| 291 | } |
| 292 | } |
| 293 | fn check(&mut self) -> Result<()> { |
| 294 | if self.cancelled { |
| 295 | Err(ErrorCode::None) |
| 296 | } else { |
| 297 | Ok(()) |
| 298 | } |
| 299 | } |
| 300 | fn activity(&mut self) -> Result<()> { |
| 301 | self.check()?; |
| 302 | self.active += 1; |
| 303 | Ok(()) |
| 304 | } |
| 305 | fn observe(&mut self, _: ResourceUsage, _: Option<&std::fs::File>) -> Result<()> { |
| 306 | self.observed += 1; |
| 307 | Err(ErrorCode::Limit) |
| 308 | } |
| 309 | } |
| 310 | let mut parent = Parent { |
| 311 | observed: 0, |
| 312 | active: 0, |
| 313 | cancelled: false, |
| 314 | }; |
| 315 | { |
| 316 | let mut metadata = MetadataMonitor(&mut parent); |
| 317 | assert_eq!(metadata.metadata_limits().bytes, 16); |
| 318 | metadata |
| 319 | .observe( |
| 320 | ResourceUsage { |
| 321 | estimated_bytes: Some(16), |
| 322 | actual_bytes: Some(16), |
| 323 | compressed_bytes: Some(1), |
| 324 | ratio_output_bytes: Some(16), |
| 325 | ..ResourceUsage::default() |
| 326 | }, |
| 327 | None, |
| 328 | ) |
| 329 | .unwrap(); |
| 330 | completed(Ok(()), &mut metadata).unwrap(); |
| 331 | for estimated in [false, true] { |
| 332 | assert_eq!( |
| 333 | metadata.observe( |
| 334 | ResourceUsage { |
| 335 | estimated_bytes: estimated.then_some(17), |
| 336 | actual_bytes: (!estimated).then_some(17), |
| 337 | ..ResourceUsage::default() |
| 338 | }, |
| 339 | None |
| 340 | ), |
| 341 | Err(ErrorCode::Limit) |
| 342 | ); |
| 343 | } |
| 344 | } |
| 345 | assert_eq!(parent.observed, 0); |
| 346 | assert_eq!(parent.active, 1); |
| 347 | parent.cancelled = true; |
| 348 | let mut metadata = MetadataMonitor(&mut parent); |
| 349 | assert_eq!(metadata.check(), Err(ErrorCode::None)); |
| 350 | assert_eq!( |
| 351 | metadata.observe(ResourceUsage::default(), None), |
| 352 | Err(ErrorCode::None) |
| 353 | ); |
| 354 | assert_eq!(completed(Ok(()), &mut metadata), Err(ErrorCode::None)); |
| 355 | } |