Skip to main content

grow_better_stack_logs/testing/
core_ops.rs

1// Generated by sdk-kit 0.3.0 — do not edit; change sdk-kit and run "sdk-kit sync"
2//! Conformance handler for the `core` category: `health ping serverVersion close`.
3
4use std::sync::Arc;
5use std::time::Duration;
6
7use async_trait::async_trait;
8use serde_json::{json, Value};
9
10use super::runner::{CategoryHandler, Scenario, ScenarioSession, StepOutput, Vars};
11use crate::core::{Error, HealthStatus, Result};
12
13use crate::BetterStackClient as Client;
14
15/// The core operations a `core` scenario drives (SPEC §2.4). Implemented for the service
16/// client; tests can supply a fake through [`CoreHandler::with_target`].
17#[async_trait]
18pub trait CoreTarget: Send + Sync {
19    /// Unauthenticated liveness check.
20    async fn health(&self) -> Result<HealthStatus>;
21    /// Authenticated round trip.
22    async fn ping(&self) -> Result<Duration>;
23    /// Server version string.
24    async fn server_version(&self) -> Result<String>;
25    /// Releases the target; idempotent.
26    async fn close(&self);
27}
28
29#[async_trait]
30impl CoreTarget for Client {
31    async fn health(&self) -> Result<HealthStatus> {
32        Client::health(self).await
33    }
34    async fn ping(&self) -> Result<Duration> {
35        Client::ping(self).await
36    }
37    async fn server_version(&self) -> Result<String> {
38        Client::server_version(self).await
39    }
40    async fn close(&self) {
41        Client::close(self).await
42    }
43}
44
45type Factory = Arc<dyn Fn() -> Result<Box<dyn CoreTarget>> + Send + Sync>;
46
47/// Runs `core` scenarios against a fresh client per scenario.
48#[derive(Clone)]
49pub struct CoreHandler {
50    factory: Factory,
51}
52
53impl std::fmt::Debug for CoreHandler {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        f.write_str("CoreHandler")
56    }
57}
58
59impl CoreHandler {
60    /// `factory` builds the client under test (called once per scenario).
61    pub fn new(factory: impl Fn() -> Result<Client> + Send + Sync + 'static) -> Self {
62        Self::with_target(factory)
63    }
64
65    /// Like [`CoreHandler::new`] for any [`CoreTarget`] (a fake in tests, a wrapper, …).
66    pub fn with_target<T, F>(factory: F) -> Self
67    where
68        T: CoreTarget + 'static,
69        F: Fn() -> Result<T> + Send + Sync + 'static,
70    {
71        CoreHandler {
72            factory: Arc::new(move || Ok(Box::new(factory()?) as Box<dyn CoreTarget>)),
73        }
74    }
75}
76
77struct CoreSession {
78    client: Box<dyn CoreTarget>,
79}
80
81#[async_trait]
82impl CategoryHandler for CoreHandler {
83    fn category(&self) -> &str {
84        "core"
85    }
86
87    async fn open(&self, _scenario: &Scenario, _vars: &Vars) -> Result<Box<dyn ScenarioSession>> {
88        Ok(Box::new(CoreSession {
89            client: (self.factory)()?,
90        }))
91    }
92}
93
94#[async_trait]
95impl ScenarioSession for CoreSession {
96    async fn run(&mut self, op: &str, _args: &Value, _vars: &Vars) -> Result<StepOutput> {
97        match op {
98            "health" => {
99                let status = self.client.health().await?;
100                Ok(StepOutput::json(serde_json::to_value(&status)?))
101            }
102            "ping" => {
103                let latency = self.client.ping().await?;
104                Ok(StepOutput::json(json!(latency.as_secs_f64() * 1000.0)))
105            }
106            "serverVersion" => Ok(StepOutput::json(Value::String(
107                self.client.server_version().await?,
108            ))),
109            "close" => {
110                self.client.close().await;
111                Ok(StepOutput::none())
112            }
113            other => Err(Error::unsupported(format!("core: unknown op {other:?}"))),
114        }
115    }
116
117    async fn close(&mut self) {
118        self.client.close().await;
119    }
120}