cachekit-rs 0.8.0

Caching for Rust (beta). Supports cachekit.io SaaS, Redis, Memcached, local File, and Cloudflare Workers.
Documentation
//! Shared test utilities for the cachekit integration tests.

#![allow(dead_code)]

use std::collections::HashMap;
use std::time::Duration;

use async_trait::async_trait;
use tokio::sync::Mutex;
use zeroize::Zeroizing;

use cachekit::backend::{Backend, HealthStatus};
use cachekit::client::SharedBackend;
use cachekit::error::BackendError;

/// In-memory mock backend backed by a `Mutex<HashMap>` for use in tests.
///
/// The internal store uses `Arc<Mutex>` so cloning the backend shares state,
/// regardless of whether the outer wrapper is `Arc` or `Rc`.
#[derive(Debug, Default, Clone)]
pub struct MockBackend {
    pub store: std::sync::Arc<Mutex<HashMap<String, Vec<u8>>>>,
}

impl MockBackend {
    /// Create a new mock and immediately wrap it as a [`SharedBackend`].
    ///
    /// Use [`MockBackend::new_with_handle`] when tests need to inspect the
    /// store directly (e.g., to verify ciphertext).
    pub fn shared() -> SharedBackend {
        let mock = Self::default();
        Self::into_shared(mock)
    }

    /// Create a new mock, returning both a [`SharedBackend`] and a clone
    /// for direct store inspection.
    pub fn new_with_handle() -> (SharedBackend, Self) {
        let mock = Self::default();
        let handle = mock.clone();
        (Self::into_shared(mock), handle)
    }

    fn into_shared(mock: Self) -> SharedBackend {
        #[cfg(not(any(target_arch = "wasm32", feature = "unsync")))]
        {
            std::sync::Arc::new(mock)
        }
        #[cfg(any(target_arch = "wasm32", feature = "unsync"))]
        {
            std::rc::Rc::new(mock)
        }
    }
}

#[cfg_attr(not(any(target_arch = "wasm32", feature = "unsync")), async_trait)]
#[cfg_attr(any(target_arch = "wasm32", feature = "unsync"), async_trait(?Send))]
impl Backend for MockBackend {
    async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, BackendError> {
        Ok(self.store.lock().await.get(key).cloned())
    }

    async fn set(
        &self,
        key: &str,
        value: Vec<u8>,
        _ttl: Option<Duration>,
    ) -> Result<(), BackendError> {
        self.store.lock().await.insert(key.to_owned(), value);
        Ok(())
    }

    async fn delete(&self, key: &str) -> Result<bool, BackendError> {
        Ok(self.store.lock().await.remove(key).is_some())
    }

    async fn exists(&self, key: &str) -> Result<bool, BackendError> {
        Ok(self.store.lock().await.contains_key(key))
    }

    async fn health(&self) -> Result<HealthStatus, BackendError> {
        Ok(HealthStatus {
            is_healthy: true,
            latency_ms: 0.0,
            backend_type: "mock".to_owned(),
            details: HashMap::new(),
        })
    }
}

/// RAII guard for `#[serial]` env tests: records each variable's pre-test
/// value and restores it on drop — including on assertion failure — so a
/// test can never destroy state the surrounding shell exported.
pub struct EnvGuard {
    /// `Zeroizing` because the saved set includes `CACHEKIT_MASTER_KEY` and
    /// `CACHEKIT_PREVIOUS_MASTER_KEYS` — a pre-test shell value is real key
    /// material, so the copy this guard holds is wiped on drop.
    saved: Vec<(&'static str, Option<Zeroizing<String>>)>,
}

impl EnvGuard {
    /// Apply `(name, value)` pairs: `Some` sets the variable, `None` removes
    /// it. The prior value of every named variable is restored on drop.
    ///
    /// Panics — before touching any variable — if one holds a non-UTF-8
    /// value: it could not be restored as a `String`, and treating it as
    /// absent would make drop delete it.
    pub fn set(vars: &[(&'static str, Option<&str>)]) -> Self {
        let saved = vars
            .iter()
            .map(|(name, _)| match std::env::var(name) {
                Ok(v) => (*name, Some(Zeroizing::new(v))),
                Err(std::env::VarError::NotPresent) => (*name, None),
                Err(std::env::VarError::NotUnicode(_)) => {
                    panic!("EnvGuard: {name} holds a non-UTF-8 value; refusing to clobber it")
                }
            })
            .collect();
        for (name, value) in vars {
            match value {
                Some(v) => std::env::set_var(name, v),
                None => std::env::remove_var(name),
            }
        }
        Self { saved }
    }
}

impl Drop for EnvGuard {
    fn drop(&mut self) {
        for (name, value) in &self.saved {
            match value {
                Some(v) => std::env::set_var(name, v.as_str()),
                None => std::env::remove_var(name),
            }
        }
    }
}

/// Backend whose every data operation fails with a transient error — the
/// "backend is down" fixture for breaker and not-counted-read tests.
#[derive(Debug, Default, Clone)]
pub struct FailingBackend;

impl FailingBackend {
    /// Wrap as a [`SharedBackend`].
    pub fn shared() -> SharedBackend {
        #[cfg(not(any(target_arch = "wasm32", feature = "unsync")))]
        {
            std::sync::Arc::new(Self)
        }
        #[cfg(any(target_arch = "wasm32", feature = "unsync"))]
        {
            std::rc::Rc::new(Self)
        }
    }
}

#[cfg_attr(not(any(target_arch = "wasm32", feature = "unsync")), async_trait)]
#[cfg_attr(any(target_arch = "wasm32", feature = "unsync"), async_trait(?Send))]
impl Backend for FailingBackend {
    async fn get(&self, _key: &str) -> Result<Option<Vec<u8>>, BackendError> {
        Err(BackendError::transient("down"))
    }

    async fn set(&self, _: &str, _: Vec<u8>, _: Option<Duration>) -> Result<(), BackendError> {
        Err(BackendError::transient("down"))
    }

    async fn delete(&self, _key: &str) -> Result<bool, BackendError> {
        Err(BackendError::transient("down"))
    }

    async fn exists(&self, _key: &str) -> Result<bool, BackendError> {
        Err(BackendError::transient("down"))
    }

    async fn health(&self) -> Result<HealthStatus, BackendError> {
        Ok(HealthStatus {
            is_healthy: false,
            latency_ms: 0.0,
            backend_type: "failing".to_owned(),
            details: HashMap::new(),
        })
    }
}