| 1 | use super::Bounded; |
| 2 | use crate::{Checkpoint, Failure, Gate, Stop}; |
| 3 | use std::{ |
| 4 | cell::{Cell, RefCell}, |
| 5 | io::{Seek, SeekFrom, Write}, |
| 6 | time::{Duration, Instant}, |
| 7 | }; |
| 8 | |
| 9 | fn gate<C: FnMut(Checkpoint) -> Result<(), Stop>>(control: C) -> Gate<C> { |
| 10 | Gate { |
| 11 | control: RefCell::new(control), |
| 12 | deadline: Instant::now() + Duration::from_secs(300), |
| 13 | first: Cell::new(None), |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | fn cause(error: std::io::Error) -> Failure { |
| 18 | *error.get_ref().unwrap().downcast_ref::<Failure>().unwrap() |
| 19 | } |
| 20 | |
| 21 | #[test] |
| 22 | fn memory_storage_patch_does_not_double_charge_extent() { |
| 23 | let gate = gate(|_| Ok(())); |
| 24 | let mut output = Bounded::new(&gate, 4, 7, 3); |
| 25 | output.write_all(b"abcd").unwrap(); |
| 26 | output.seek(SeekFrom::Start(1)).unwrap(); |
| 27 | output.write_all(b"XY").unwrap(); |
| 28 | output.seek(SeekFrom::End(0)).unwrap(); |
| 29 | output.flush().unwrap(); |
| 30 | assert_eq!(output.into_bytes(), b"aXYd"); |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn memory_storage_budget_failure_precedes_allocation_and_is_sticky() { |
| 35 | let calls = Cell::new(0); |
| 36 | let gate = gate(|_| { |
| 37 | calls.set(calls.get() + 1); |
| 38 | Ok(()) |
| 39 | }); |
| 40 | let mut output = Bounded::new(&gate, 4, 7, 3); |
| 41 | output.write_all(b"abcd").unwrap(); |
| 42 | let before = ( |
| 43 | output.bytes.clone(), |
| 44 | output.position, |
| 45 | output.bytes.capacity(), |
| 46 | ); |
| 47 | assert_eq!(cause(output.write(b"e").unwrap_err()), Failure::Budget); |
| 48 | let count = calls.get(); |
| 49 | assert_eq!( |
| 50 | cause(output.seek(SeekFrom::Start(0)).unwrap_err()), |
| 51 | Failure::Budget |
| 52 | ); |
| 53 | assert_eq!(cause(output.write(b"Z").unwrap_err()), Failure::Budget); |
| 54 | assert_eq!(cause(output.flush().unwrap_err()), Failure::Budget); |
| 55 | assert_eq!( |
| 56 | cause(output.seek(SeekFrom::Current(i64::MIN)).unwrap_err()), |
| 57 | Failure::Budget |
| 58 | ); |
| 59 | assert_eq!( |
| 60 | ( |
| 61 | output.bytes.clone(), |
| 62 | output.position, |
| 63 | output.bytes.capacity() |
| 64 | ), |
| 65 | before |
| 66 | ); |
| 67 | assert_eq!(calls.get(), count); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn memory_storage_processed_failure_precedes_extension() { |
| 72 | let gate = gate(|_| Ok(())); |
| 73 | let mut output = Bounded::new(&gate, 100, 5, 3); |
| 74 | output.write_all(b"ab").unwrap(); |
| 75 | let before = (output.bytes.clone(), output.bytes.capacity()); |
| 76 | assert_eq!(cause(output.write(b"c").unwrap_err()), Failure::Budget); |
| 77 | assert_eq!((output.bytes.clone(), output.bytes.capacity()), before); |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn memory_storage_checked_processed_sum_rejects_overflow() { |
| 82 | let gate = gate(|_| Ok(())); |
| 83 | let mut output = Bounded::new(&gate, u64::MAX, u64::MAX, u64::MAX); |
| 84 | assert_eq!(cause(output.write(b"x").unwrap_err()), Failure::Budget); |
| 85 | assert!(output.bytes.is_empty()); |
| 86 | assert_eq!(output.bytes.capacity(), 0); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn memory_storage_seek_modes_preserve_bytes_and_reject_holes() { |
| 91 | for invalid in [ |
| 92 | SeekFrom::Start(5), |
| 93 | SeekFrom::Current(i64::MIN), |
| 94 | SeekFrom::End(i64::MAX), |
| 95 | ] { |
| 96 | let gate = gate(|_| Ok(())); |
| 97 | let mut output = Bounded::new(&gate, 4, 4, 0); |
| 98 | output.write_all(b"abcd").unwrap(); |
| 99 | assert_eq!(output.seek(SeekFrom::Start(0)).unwrap(), 0); |
| 100 | assert_eq!(output.seek(SeekFrom::Current(2)).unwrap(), 2); |
| 101 | assert_eq!(output.seek(SeekFrom::End(-1)).unwrap(), 3); |
| 102 | assert_eq!(cause(output.seek(invalid).unwrap_err()), Failure::Engine); |
| 103 | assert_eq!(output.position, 3); |
| 104 | assert_eq!(output.bytes, b"abcd"); |
| 105 | assert_eq!(cause(output.write(b"X").unwrap_err()), Failure::Engine); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn memory_storage_cancelled_patch_is_atomic_and_latched() { |
| 111 | let armed = Cell::new(false); |
| 112 | let gate = gate(|_| { |
| 113 | if armed.get() { |
| 114 | Err(Stop::Cancelled) |
| 115 | } else { |
| 116 | Ok(()) |
| 117 | } |
| 118 | }); |
| 119 | let mut output = Bounded::new(&gate, 4, 4, 0); |
| 120 | output.write_all(b"abcd").unwrap(); |
| 121 | output.seek(SeekFrom::Start(1)).unwrap(); |
| 122 | let before = (output.bytes.clone(), output.position); |
| 123 | armed.set(true); |
| 124 | assert_eq!(cause(output.write(b"XY").unwrap_err()), Failure::Cancelled); |
| 125 | armed.set(false); |
| 126 | assert_eq!(cause(output.flush().unwrap_err()), Failure::Cancelled); |
| 127 | assert_eq!( |
| 128 | cause(output.seek(SeekFrom::Start(0)).unwrap_err()), |
| 129 | Failure::Cancelled |
| 130 | ); |
| 131 | assert_eq!((output.bytes.clone(), output.position), before); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn memory_storage_expired_real_clock_rejects_patch_without_callback() { |
| 136 | let calls = Cell::new(0); |
| 137 | let mut gate = gate(|_| { |
| 138 | calls.set(calls.get() + 1); |
| 139 | Ok(()) |
| 140 | }); |
| 141 | gate.deadline = Instant::now().checked_sub(Duration::from_secs(1)).unwrap(); |
| 142 | let mut output = Bounded::new(&gate, 4, 4, 0); |
| 143 | // 만료 시점의 저장기 상태를 직접 구성함. 실제 대기 경과 시험은 아님. |
| 144 | output.bytes = b"abcd".to_vec(); |
| 145 | output.position = 1; |
| 146 | assert_eq!(cause(output.write(b"XY").unwrap_err()), Failure::Deadline); |
| 147 | assert_eq!(cause(output.flush().unwrap_err()), Failure::Deadline); |
| 148 | assert_eq!( |
| 149 | cause(output.seek(SeekFrom::Start(0)).unwrap_err()), |
| 150 | Failure::Deadline |
| 151 | ); |
| 152 | assert_eq!(output.bytes, b"abcd"); |
| 153 | assert_eq!(output.position, 1); |
| 154 | assert_eq!(calls.get(), 0); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn memory_storage_injected_deadline_remains_after_callback_recovers() { |
| 159 | let armed = Cell::new(false); |
| 160 | let gate = gate(|_| { |
| 161 | if armed.get() { |
| 162 | Err(Stop::Deadline) |
| 163 | } else { |
| 164 | Ok(()) |
| 165 | } |
| 166 | }); |
| 167 | let mut output = Bounded::new(&gate, 4, 4, 0); |
| 168 | output.write_all(b"abcd").unwrap(); |
| 169 | armed.set(true); |
| 170 | assert_eq!( |
| 171 | cause(output.seek(SeekFrom::Start(0)).unwrap_err()), |
| 172 | Failure::Deadline |
| 173 | ); |
| 174 | armed.set(false); |
| 175 | assert_eq!(cause(output.write(b"X").unwrap_err()), Failure::Deadline); |
| 176 | assert_eq!(output.bytes, b"abcd"); |
| 177 | assert_eq!(output.position, 4); |
| 178 | } |
| 179 | |
| 180 | #[test] |
| 181 | fn memory_storage_first_failure_dominates_later_clock_and_callback() { |
| 182 | let calls = Cell::new(0); |
| 183 | let mut gate = gate(|_| { |
| 184 | calls.set(calls.get() + 1); |
| 185 | Err(Stop::Cancelled) |
| 186 | }); |
| 187 | assert_eq!(gate.fail(Failure::Engine), Failure::Engine); |
| 188 | assert_eq!(gate.fail(Failure::Allocation), Failure::Engine); |
| 189 | gate.deadline = Instant::now().checked_sub(Duration::from_secs(1)).unwrap(); |
| 190 | let mut output = Bounded::new(&gate, 0, 0, 0); |
| 191 | assert_eq!(cause(output.write(b"x").unwrap_err()), Failure::Engine); |
| 192 | assert_eq!(cause(output.flush().unwrap_err()), Failure::Engine); |
| 193 | assert_eq!(calls.get(), 0); |
| 194 | assert!(output.bytes.is_empty()); |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn memory_storage_typed_failure_survives_io_and_zip_wrapping() { |
| 199 | for failure in [ |
| 200 | Failure::Budget, |
| 201 | Failure::Cancelled, |
| 202 | Failure::Deadline, |
| 203 | Failure::Allocation, |
| 204 | Failure::Engine, |
| 205 | ] { |
| 206 | assert_eq!(crate::io_failure(std::io::Error::other(failure)), failure); |
| 207 | assert_eq!( |
| 208 | crate::zip_failure(zip::result::ZipError::Io(std::io::Error::other(failure))), |
| 209 | failure |
| 210 | ); |
| 211 | } |
| 212 | assert_eq!( |
| 213 | crate::io_failure(std::io::Error::other("synthetic")), |
| 214 | Failure::Engine |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn memory_storage_drop_cannot_clear_preexisting_failure() { |
| 220 | for method in [ |
| 221 | zip::CompressionMethod::Stored, |
| 222 | zip::CompressionMethod::Deflated, |
| 223 | ] { |
| 224 | let gate = gate(|_| Ok(())); |
| 225 | let mut output = Bounded::new(&gate, 4096, 8192, 0); |
| 226 | let mut writer = zip::ZipWriter::new(&mut output); |
| 227 | let options = zip::write::SimpleFileOptions::default().compression_method(method); |
| 228 | writer.start_file("a", options).unwrap(); |
| 229 | writer.write_all(b"123456789").unwrap(); |
| 230 | gate.fail(Failure::Cancelled); |
| 231 | drop(writer); |
| 232 | assert_eq!(gate.first.get(), Some(Failure::Cancelled)); |
| 233 | assert_eq!(cause(output.write(b"x").unwrap_err()), Failure::Cancelled); |
| 234 | assert_eq!(cause(output.flush().unwrap_err()), Failure::Cancelled); |
| 235 | } |
| 236 | } |