team-ljyzipelo

공개
271줄7.7 KB
1use super::*;
2
3fn 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]
15fn 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]
59fn 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]
99fn 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]
118fn 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]
136fn 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]
148fn 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]
161fn 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.charge("").unwrap();
178 }
179 assert_eq!(metadata.charge(""), Err(ErrorCode::Limit));
180}
181
182#[test]
183fn successful_item_completion_refreshes_only_live_work() {
184 struct Clock {
185 now: Duration,
186 watchdog: Watchdog,
187 }
188 impl Monitor for Clock {
189 fn check(&mut self) -> Result<()> {
190 self.watchdog.check(self.now)
191 }
192 fn activity(&mut self) -> Result<()> {
193 self.watchdog.activity(self.now)
194 }
195 }
196 let mut clock = Clock {
197 now: Duration::ZERO,
198 watchdog: Watchdog::new(),
199 };
200 clock.watchdog.resume(Duration::ZERO, true);
201 for item in 1..=100 {
202 clock.now += Duration::from_secs(10);
203 assert_eq!(completed(Ok(item), &mut clock), Ok(item));
204 }
205 clock.now += Duration::from_secs(30);
206 assert_eq!(
207 completed::<()>(Err(ErrorCode::Io), &mut clock),
208 Err(ErrorCode::Io)
209 );
210 for _ in 0..29 {
211 clock.now += Duration::from_secs(1);
212 clock.check().unwrap();
213 }
214 clock.now += Duration::from_secs(1);
215 assert_eq!(clock.check(), Err(ErrorCode::Limit));
216 assert_eq!(completed(Ok(()), &mut clock), Err(ErrorCode::Limit));
217 assert_eq!(
218 completed::<()>(Err(ErrorCode::Io), &mut clock),
219 Err(ErrorCode::Io)
220 );
221}
222
223#[test]
224fn empty_gzip_members_count_consumed_input_as_decode_progress() {
225 use std::io::Write;
226 struct DecodeClock {
227 now: Duration,
228 steps: usize,
229 watchdog: Watchdog,
230 }
231 impl Monitor for DecodeClock {
232 fn check(&mut self) -> Result<()> {
233 self.watchdog.check(self.now)
234 }
235 fn activity(&mut self) -> Result<()> {
236 self.watchdog.activity(self.now)
237 }
238 fn observe(&mut self, _: ResourceUsage, _: Option<&std::fs::File>) -> Result<()> {
239 self.steps += 1;
240 self.now += Duration::from_secs(10);
241 self.check()
242 }
243 }
244 let mut clock = DecodeClock {
245 now: Duration::ZERO,
246 steps: 0,
247 watchdog: Watchdog::new(),
248 };
249 clock.watchdog.resume(Duration::ZERO, true);
250 let member = [
251 31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
252 ];
253 let mut input = crate::spool::temporary().unwrap();
254 for _ in 0..8 {
255 input.write_all(&member).unwrap();
256 }
257 let catalog = crate::streaming::Catalog::open(
258 &mut input,
259 8 * member.len() as u64,
260 Limits::default(),
261 true,
262 None,
263 &mut clock,
264 )
265 .unwrap();
266 assert_eq!(catalog.format, Format::Gzip);
267 assert_eq!(catalog.members.len(), 1);
268 assert_eq!(catalog.members[0].entry.size, 0);
269 assert!(clock.steps >= 8);
270 clock.check().unwrap();
271}