use std::collections::VecDeque;
use parking_lot::Mutex;
use rskit_errors::{AppError, AppResult};
pub struct MockProvider<I, O> {
responses: Mutex<VecDeque<AppResult<O>>>,
calls: Mutex<Vec<I>>,
}
impl<I: Clone + Send + Sync + 'static, O: Clone + Send + Sync + 'static> MockProvider<I, O> {
pub fn new() -> Self {
Self {
responses: Mutex::new(VecDeque::new()),
calls: Mutex::new(Vec::new()),
}
}
pub fn will_return(&self, response: O) -> &Self {
self.responses.lock().push_back(Ok(response));
self
}
pub fn will_fail(&self, err: AppError) -> &Self {
self.responses.lock().push_back(Err(err));
self
}
pub fn calls(&self) -> Vec<I> {
self.calls.lock().clone()
}
pub fn call_count(&self) -> usize {
self.calls.lock().len()
}
pub fn execute(&self, input: I) -> AppResult<O> {
self.calls.lock().push(input);
self.responses
.lock()
.pop_front()
.expect("MockProvider: no more responses enqueued")
}
}
impl<I: Clone + Send + Sync + 'static, O: Clone + Send + Sync + 'static> Default
for MockProvider<I, O>
{
fn default() -> Self {
Self::new()
}
}