test: MockWriteClient tracks ACK response count

Changes the MockWriteClient to track how many success responses it has
returned in response to a write request.
pull/24376/head
Dom Dwyer 2023-05-08 12:09:45 +02:00
parent ac656ab1f9
commit cf622c1b91
No known key found for this signature in database
GPG Key ID: E4C40DBD9157879A
1 changed files with 16 additions and 1 deletions

View File

@ -44,6 +44,7 @@ pub mod mock {
struct State {
calls: Vec<WriteRequest>,
ret: Box<dyn Iterator<Item = Result<(), RpcWriteClientError>> + Send + Sync>,
returned_oks: usize,
}
/// A mock implementation of the [`WriteClient`] for testing purposes.
@ -66,6 +67,7 @@ pub mod mock {
state: Mutex::new(State {
calls: Default::default(),
ret: Box::new(iter::repeat_with(|| Ok(()))),
returned_oks: 0,
}),
}
}
@ -77,6 +79,12 @@ pub mod mock {
self.state.lock().calls.clone()
}
/// Retrieve the number of times this mock returned [`Ok`] to a write
/// request.
pub fn success_count(&self) -> usize {
self.state.lock().returned_oks
}
/// Read values off of the provided iterator and return them for calls
/// to [`Self::write()`].
#[cfg(test)]
@ -95,7 +103,14 @@ pub mod mock {
async fn write(&self, op: WriteRequest) -> Result<(), RpcWriteClientError> {
let mut guard = self.state.lock();
guard.calls.push(op);
guard.ret.next().expect("no mock response")
let ret = guard.ret.next().expect("no mock response");
if ret.is_ok() {
guard.returned_oks += 1;
}
ret
}
}
}