Skip to main content

mockforge_graphql/
protocol_server.rs

1//! Unified protocol server implementation for the GraphQL mock server.
2
3use async_trait::async_trait;
4use mockforge_core::protocol_abstraction::Protocol;
5use mockforge_core::protocol_server::MockProtocolServer;
6
7use crate::LatencyProfile;
8
9/// A `MockProtocolServer` wrapper around the GraphQL server startup.
10///
11/// Wraps [`crate::start_with_latency`] with shutdown-signal integration.
12pub struct GraphqlMockServer {
13    port: u16,
14    latency_profile: Option<LatencyProfile>,
15}
16
17impl GraphqlMockServer {
18    /// Create a new `GraphqlMockServer` with the given configuration.
19    pub fn new(port: u16, latency_profile: Option<LatencyProfile>) -> Self {
20        Self {
21            port,
22            latency_profile,
23        }
24    }
25}
26
27#[async_trait]
28impl MockProtocolServer for GraphqlMockServer {
29    fn protocol(&self) -> Protocol {
30        Protocol::GraphQL
31    }
32
33    async fn start(
34        &self,
35        mut shutdown: tokio::sync::watch::Receiver<()>,
36    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
37        let port = self.port;
38        let latency = self.latency_profile.clone();
39
40        tokio::select! {
41            result = crate::start_with_latency(port, latency) => {
42                result
43            }
44            _ = shutdown.changed() => {
45                tracing::info!("Shutting down GraphQL server on port {}", port);
46                Ok(())
47            }
48        }
49    }
50
51    fn port(&self) -> u16 {
52        self.port
53    }
54
55    fn description(&self) -> String {
56        format!("GraphQL server on port {}", self.port)
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_graphql_mock_server_protocol() {
66        let server = GraphqlMockServer::new(4000, None);
67        assert_eq!(server.protocol(), Protocol::GraphQL);
68    }
69
70    #[test]
71    fn test_graphql_mock_server_port() {
72        let server = GraphqlMockServer::new(4000, None);
73        assert_eq!(server.port(), 4000);
74    }
75
76    #[test]
77    fn test_graphql_mock_server_description() {
78        let server = GraphqlMockServer::new(4000, None);
79        assert_eq!(server.description(), "GraphQL server on port 4000");
80    }
81}