#[macro_export]
macro_rules! fake_backend_shell {
(
$(#[$attr:meta])*
$vis:vis struct $name:ident {
state: $state_ty:ty,
event: $event_ty:ty,
error: $error_ty:ty $(,)?
}
) => {
$(#[$attr])*
#[derive(Default, Clone)]
$vis struct $name {
inner: ::std::sync::Arc<::tokio::sync::Mutex<$state_ty>>,
}
impl $name {
#[must_use]
$vis fn new() -> Self {
Self::default()
}
$vis async fn record_event(&self, event: $event_ty) {
self.inner.lock().await.events.push(event);
}
$vis async fn take_fail_next(&self) -> Option<$error_ty> {
self.inner.lock().await.fail_next.take()
}
$vis async fn events(&self) -> Vec<$event_ty>
where
$event_ty: Clone,
{
self.inner.lock().await.events.clone()
}
$vis async fn call_count(&self) -> usize {
self.inner.lock().await.events.len()
}
$vis async fn fail_next(&self, err: $error_ty) {
self.inner.lock().await.fail_next = Some(err);
}
$vis async fn reset(&self) {
let mut state = self.inner.lock().await;
state.events.clear();
state.fail_next = None;
}
}
};
}
#[cfg(test)]
mod tests {
use thiserror::Error;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FakeFooEvent {
Insert(String),
Remove(String),
}
#[derive(Debug, Clone, Error)]
pub enum FakeFooError {
#[error("backend: {0}")]
Backend(String),
}
#[derive(Default)]
pub struct FakeFooState {
events: Vec<FakeFooEvent>,
fail_next: Option<FakeFooError>,
}
crate::fake_backend_shell! {
pub struct FakeFoo {
state: FakeFooState,
event: FakeFooEvent,
error: FakeFooError,
}
}
#[tokio::test]
async fn fresh_fake_has_empty_event_log() {
let f = FakeFoo::new();
assert_eq!(f.call_count().await, 0);
assert!(f.events().await.is_empty());
}
#[tokio::test]
async fn record_event_appends_to_log() {
let f = FakeFoo::new();
f.record_event(FakeFooEvent::Insert("a".into())).await;
f.record_event(FakeFooEvent::Remove("a".into())).await;
assert_eq!(f.call_count().await, 2);
let events = f.events().await;
assert_eq!(events[0], FakeFooEvent::Insert("a".into()));
assert_eq!(events[1], FakeFooEvent::Remove("a".into()));
}
#[tokio::test]
async fn fail_next_pins_next_call_single_shot() {
let f = FakeFoo::new();
f.fail_next(FakeFooError::Backend("boom".into())).await;
let err = f.take_fail_next().await.unwrap();
assert!(matches!(err, FakeFooError::Backend(_)));
assert!(f.take_fail_next().await.is_none());
}
#[tokio::test]
async fn reset_clears_events_and_fail_next() {
let f = FakeFoo::new();
f.record_event(FakeFooEvent::Insert("x".into())).await;
f.fail_next(FakeFooError::Backend("y".into())).await;
f.reset().await;
assert_eq!(f.call_count().await, 0);
assert!(f.take_fail_next().await.is_none());
}
#[tokio::test]
async fn clone_shares_inner_state() {
let f1 = FakeFoo::new();
let f2 = f1.clone();
f1.record_event(FakeFooEvent::Insert("a".into())).await;
assert_eq!(f2.call_count().await, 1);
}
#[tokio::test]
async fn cross_task_assertion_via_clone() {
let f = FakeFoo::new();
let f2 = f.clone();
let h = tokio::spawn(async move {
f2.record_event(FakeFooEvent::Insert("from-task".into()))
.await;
});
h.await.unwrap();
assert_eq!(f.call_count().await, 1);
}
#[async_trait::async_trait]
trait FooBackend {
async fn insert(&self, key: String) -> Result<(), FakeFooError>;
}
#[async_trait::async_trait]
impl FooBackend for FakeFoo {
async fn insert(&self, key: String) -> Result<(), FakeFooError> {
if let Some(err) = self.take_fail_next().await {
return Err(err);
}
self.record_event(FakeFooEvent::Insert(key)).await;
Ok(())
}
}
#[tokio::test]
async fn operator_trait_impl_uses_helpers() {
let f = FakeFoo::new();
f.insert("x".into()).await.unwrap();
assert_eq!(f.call_count().await, 1);
}
#[tokio::test]
async fn operator_trait_impl_returns_fail_next_error() {
let f = FakeFoo::new();
f.fail_next(FakeFooError::Backend("rejected".into())).await;
let err = f.insert("x".into()).await.unwrap_err();
assert!(matches!(err, FakeFooError::Backend(_)));
assert_eq!(f.call_count().await, 0);
}
}