| 1 | use super::*; |
| 2 | use std::collections::VecDeque; |
| 3 | struct FirstLimit { |
| 4 | first: bool, |
| 5 | process: ProcessFactory, |
| 6 | } |
| 7 | struct Failed { |
| 8 | job: u64, |
| 9 | events: VecDeque<HostEvent>, |
| 10 | } |
| 11 | impl Connection for Failed { |
| 12 | fn send(&mut self, request: Request) -> Result<(), String> { |
| 13 | assert!(matches!(request.command, Command::Start(_))); |
| 14 | self.events.extend([ |
| 15 | HostEvent::Frame { |
| 16 | job_id: self.job, |
| 17 | sequence: 0, |
| 18 | event: Event::Accepted { encryption: true }, |
| 19 | }, |
| 20 | HostEvent::Frame { |
| 21 | job_id: self.job, |
| 22 | sequence: 1, |
| 23 | event: Event::Terminal(Terminal { |
| 24 | outcome: Outcome::Failed, |
| 25 | error: Some(ErrorCode::Limit), |
| 26 | publication: Publication::None, |
| 27 | created_count: 0, |
| 28 | retained_partial_count: 0, |
| 29 | durability: Durability::NotApplicable, |
| 30 | cleanup: Cleanup::Complete, |
| 31 | }), |
| 32 | }, |
| 33 | HostEvent::StdoutEof, |
| 34 | HostEvent::StderrEof, |
| 35 | HostEvent::Exited { success: true }, |
| 36 | ]); |
| 37 | Ok(()) |
| 38 | } |
| 39 | fn poll(&mut self) -> Option<HostEvent> { |
| 40 | self.events.pop_front() |
| 41 | } |
| 42 | } |
| 43 | impl Factory for FirstLimit { |
| 44 | fn connect(&mut self, job: u64) -> Result<Box<dyn Connection>, String> { |
| 45 | if self.first { |
| 46 | self.first = false; |
| 47 | Ok(Box::new(Failed { |
| 48 | job, |
| 49 | events: VecDeque::new(), |
| 50 | })) |
| 51 | } else { |
| 52 | self.process.connect(job) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | #[test] |
| 57 | fn model_retry_uses_real_guarded_codec_and_requires_a_new_job_approval() { |
| 58 | for mode in 0..3 { |
| 59 | let scratch = Scratch::new(); |
| 60 | let archive = scratch.path("sample.zip"); |
| 61 | fs::write(&archive, include_bytes!("fixtures/sample.zip")).unwrap(); |
| 62 | let mut factory = FirstLimit { |
| 63 | first: true, |
| 64 | process: ProcessFactory::new(binary("zipelo-archive-worker")), |
| 65 | }; |
| 66 | let mut model = Model::default(); |
| 67 | assert!(model.begin(Start::List { archive }, &mut factory)); |
| 68 | let first_job = model.job_id().unwrap(); |
| 69 | wait(&mut model); |
| 70 | assert!(model.can_retry_expanded()); |
| 71 | assert!(model.retry_expanded(&mut factory)); |
| 72 | let job = model.job_id().unwrap(); |
| 73 | assert_ne!(first_job, job); |
| 74 | let deadline = Instant::now() + Duration::from_secs(10); |
| 75 | while model.resource.is_none() { |
| 76 | model.poll(); |
| 77 | assert!(model.error.is_none(), "{:?}", model.error); |
| 78 | assert!(Instant::now() < deadline); |
| 79 | std::thread::sleep(Duration::from_millis(1)); |
| 80 | } |
| 81 | let q = model.resource.clone().unwrap(); |
| 82 | assert_eq!( |
| 83 | q.scope, |
| 84 | ResourceScope { |
| 85 | output: true, |
| 86 | entries: true, |
| 87 | ratio: false |
| 88 | } |
| 89 | ); |
| 90 | assert_eq!(q.usage.actual_bytes, None); |
| 91 | assert!(q.usage.available_bytes.is_some()); |
| 92 | assert!(model.entries.is_empty()); |
| 93 | assert!(!model.resource_reply(first_job, q.id, true)); |
| 94 | if mode == 2 { |
| 95 | model.cancel(); |
| 96 | } else { |
| 97 | assert!(model.resource_reply(job, q.id, mode == 0)); |
| 98 | } |
| 99 | wait(&mut model); |
| 100 | assert!(model.confirmed, "{:?}", model.error); |
| 101 | assert_eq!( |
| 102 | model.terminal.as_ref().unwrap().outcome, |
| 103 | if mode == 0 { |
| 104 | Outcome::Completed |
| 105 | } else { |
| 106 | Outcome::Cancelled |
| 107 | } |
| 108 | ); |
| 109 | assert_eq!(model.list_id.is_some(), mode == 0); |
| 110 | assert!(model.resource.is_none()); |
| 111 | } |
| 112 | } |