use super::*;
use crate::{SystemHealth, protocols::EndpointId};
use anyhow::Result;
use async_trait::async_trait;
use parking_lot::Mutex;
use std::sync::Arc;
#[async_trait]
pub trait RequestPlaneServer: Send + Sync {
async fn register_endpoint(
&self,
endpoint_name: String,
service_handler: Arc<dyn PushWorkHandler>,
instance_id: u64,
namespace: String,
component_name: String,
system_health: Arc<Mutex<SystemHealth>>,
) -> Result<()>;
async fn unregister_endpoint(&self, endpoint_name: &str, instance_id: u64) -> Result<()>;
async fn unregister_endpoint_instance(
&self,
endpoint_id: &EndpointId,
instance_id: u64,
) -> Result<()> {
self.unregister_endpoint(&endpoint_id.name, instance_id)
.await
}
fn address(&self) -> String;
fn transport_name(&self) -> &'static str;
fn is_healthy(&self) -> bool;
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Default)]
struct LegacyServer {
removed: Mutex<Option<(String, u64)>>,
}
#[async_trait]
impl RequestPlaneServer for LegacyServer {
async fn register_endpoint(
&self,
_: String,
_: Arc<dyn PushWorkHandler>,
_: u64,
_: String,
_: String,
_: Arc<Mutex<SystemHealth>>,
) -> Result<()> {
unreachable!()
}
async fn unregister_endpoint(&self, endpoint_name: &str, instance_id: u64) -> Result<()> {
*self.removed.lock() = Some((endpoint_name.to_string(), instance_id));
Ok(())
}
fn address(&self) -> String {
unreachable!()
}
fn transport_name(&self) -> &'static str {
unreachable!()
}
fn is_healthy(&self) -> bool {
unreachable!()
}
}
#[tokio::test]
async fn full_identity_cleanup_delegates_for_legacy_implementations() {
let server = LegacyServer::default();
let endpoint_id = EndpointId {
namespace: "test_namespace".into(),
component: "test_component".into(),
name: "generate".into(),
};
let plane: &dyn RequestPlaneServer = &server;
plane
.unregister_endpoint_instance(&endpoint_id, 0xa)
.await
.unwrap();
assert_eq!(*server.removed.lock(), Some(("generate".into(), 0xa)));
}
}