| 103 | 103 | | } |
| 104 | 104 | | } |
| 105 | 105 | | |
| 106 | + | /// An API with no address, which sends nothing: what the tests use, so |
| 107 | + | /// a step's run is not held up by reports that go nowhere (each one |
| 108 | + | /// tried three times, with waits between, over a second and a half). |
| 109 | + | #[cfg(test)] |
| 110 | + | pub(crate) fn offline() -> Api { |
| 111 | + | Api { base: String::new(), job: "job_1".into(), token: "t".into() } |
| 112 | + | } |
| 113 | + | |
| 106 | 114 | | pub(crate) fn report(&self, report: Value) { |
| 115 | + | // No address: nowhere to send it (Api::offline). |
| 116 | + | if self.base.is_empty() { |
| 117 | + | return; |
| 118 | + | } |
| 107 | 119 | | // A report that cannot be sent is tried a few times, then dropped: |
| 108 | 120 | | // the job goes on, and g1t notices a silent job by itself. |
| 109 | 121 | | for attempt in 0..3 { |
| ⋯ |
| 135 | 147 | | last: Instant, |
| 136 | 148 | | /// When anything was last sent, for the ping. |
| 137 | 149 | | sent: Instant, |
| 150 | + | /// Everything sent so far, so a test reads the whole log however its |
| 151 | + | /// batches fell (`buffered`). |
| 152 | + | #[cfg(test)] |
| 153 | + | flushed: String, |
| 138 | 154 | | } |
| 139 | 155 | | |
| 140 | 156 | | impl Log { |
| ⋯ |
| 148 | 164 | | buffer: String::new(), |
| 149 | 165 | | last: Instant::now(), |
| 150 | 166 | | sent: Instant::now(), |
| 167 | + | #[cfg(test)] |
| 168 | + | flushed: String::new(), |
| 151 | 169 | | } |
| 152 | 170 | | } |
| 153 | 171 | | |
| 154 | | − | /// What waits to be sent. |
| 172 | + | /// The log so far: what was sent and what waits to be. |
| 155 | 173 | | #[cfg(all(test, unix))] |
| 156 | 174 | | pub(crate) fn buffered(&self) -> String { |
| 157 | | − | self.buffer.clone() |
| 175 | + | format!("{}{}", self.flushed, self.buffer) |
| 158 | 176 | | } |
| 159 | 177 | | |
| 160 | 178 | | /// Starts writing to step `number` (0 for the job's setup). |
| ⋯ |
| 210 | 228 | | } |
| 211 | 229 | | self.sent = Instant::now(); |
| 212 | 230 | | let text = std::mem::take(&mut self.buffer); |
| 231 | + | #[cfg(test)] |
| 232 | + | self.flushed.push_str(&text); |
| 213 | 233 | | self.api.report(json!({ "kind": "log", "step": self.step, "text": text })); |
| 214 | 234 | | } |
| 215 | 235 | | |
| ⋯ |
| 286 | 306 | | use super::*; |
| 287 | 307 | | |
| 288 | 308 | | fn log(secrets: &[&str]) -> Log { |
| 289 | | − | let api = Api { base: "http://127.0.0.1:9".into(), job: "job_1".into(), token: "t".into() }; |
| 290 | | − | Log::new(api, mask::all_variants(secrets.iter().copied())) |
| 309 | + | Log::new(Api::offline(), mask::all_variants(secrets.iter().copied())) |
| 291 | 310 | | } |
| 292 | 311 | | |
| 293 | 312 | | #[test] |