From: Ian Jackson Date: Sat, 27 Feb 2021 00:26:04 +0000 (+0000) Subject: apitest: break out await_update X-Git-Tag: otter-0.4.0~341 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=8a070a649f840638c238d802148fa9cf0ea174d5;p=otter.git apitest: break out await_update Signed-off-by: Ian Jackson --- diff --git a/apitest/at-otter.rs b/apitest/at-otter.rs index 724a637c..98454cb0 100644 --- a/apitest/at-otter.rs +++ b/apitest/at-otter.rs @@ -223,22 +223,37 @@ impl Session { } #[throws(AE)] - fn resynch_pieces(&mut self) { + fn await_update< + R, + G: FnMut(&mut Session, Generation) -> Option, + F: FnMut(&mut Session, Generation, &str, &serde_json::Value) -> Option, + > (&mut self, mut f: F, mut g: G) -> R { 'overall: loop { let update = self.updates.recv()?; let update = update.as_array().unwrap(); - let new_gen = &update[0]; + let new_gen = Generation( + update[0] + .as_i64().unwrap() + .try_into().unwrap() + ); + if let Some(y) = g(self, new_gen) { break 'overall y } for ue in update[1].as_array().unwrap() { let (k,v) = ue.as_object().unwrap().iter().next().unwrap(); - let got_cseq: RawClientSequence = match k.as_str() { - "Recorded" => v["cseq"].as_i64().unwrap().try_into().unwrap(), - "Log" => continue, - _ => throw!(anyhow!("unknown update: {}", ue)), - }; - if got_cseq == self.cseq { break 'overall } + if let Some(y) = f(self, new_gen, k, v) { break 'overall y } } } } + + #[throws(AE)] + fn resynch_pieces(&mut self) { + self.await_update(|session, _gen, k, v| { + let got_cseq: RawClientSequence = match k { + "Recorded" => v["cseq"].as_i64().unwrap().try_into().unwrap(), + _ => return None, + }; + if got_cseq == session.cseq { Some(()) } else { None } + }, |_,_| None)?; + } } impl Ctx {