Add terminal service implementation and integration tests
- Introduced terminal.h header file defining the terminal service API. - Implemented integration tests for terminal service registration and health checks. - Added unit tests for FFI layer, health checks, and terminal service functionality. - Created a mock center for testing terminal registration and deregistration. - Ensured proper error handling and response validation in tests.
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use connectrpc::{
|
||||
ConnectError, RequestContext, Response, ServiceRequest, ServiceResult, ServiceStream,
|
||||
};
|
||||
use futures_util::stream;
|
||||
use proto::proto::control::TerminalInfo;
|
||||
use proto::proto::grpc::health::v1::{
|
||||
Health, HealthCheckRequest, HealthCheckResponse, health_check_response::ServingStatus,
|
||||
};
|
||||
use tokio::sync::{oneshot, watch};
|
||||
|
||||
use crate::RegistryService;
|
||||
use crate::health::{check, endpoint, monitor};
|
||||
|
||||
struct FakeHealth(watch::Receiver<ServingStatus>);
|
||||
|
||||
#[allow(refining_impl_trait)]
|
||||
impl Health for FakeHealth {
|
||||
async fn check(
|
||||
&self,
|
||||
_: RequestContext,
|
||||
request: ServiceRequest<'_, HealthCheckRequest>,
|
||||
) -> ServiceResult<HealthCheckResponse> {
|
||||
assert!(request.to_owned_message().service.is_empty());
|
||||
Response::ok(HealthCheckResponse {
|
||||
status: (*self.0.borrow()).into(),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
async fn watch(
|
||||
&self,
|
||||
_: RequestContext,
|
||||
_: ServiceRequest<'_, HealthCheckRequest>,
|
||||
) -> ServiceResult<ServiceStream<HealthCheckResponse>> {
|
||||
Response::stream_ok(stream::empty::<Result<HealthCheckResponse, ConnectError>>())
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_status(
|
||||
receiver: &mut watch::Receiver<proto::proto::control::TerminalInfos>,
|
||||
id: &str,
|
||||
status: ServingStatus,
|
||||
) {
|
||||
tokio::time::timeout(
|
||||
Duration::from_secs(3),
|
||||
receiver.wait_for(|snapshot| {
|
||||
snapshot
|
||||
.infos
|
||||
.get(id)
|
||||
.is_some_and(|runtime| runtime.status == status)
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_monitor_tracks_failure_and_recovery_without_removal() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let service = RegistryService::open(directory.path().join("terminals.bin"))
|
||||
.await
|
||||
.unwrap();
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let (status, receiver) = watch::channel(ServingStatus::SERVING);
|
||||
let rpc = connectrpc::Router::new().add_service(Arc::new(FakeHealth(receiver)));
|
||||
let (stop, stopped) = oneshot::channel();
|
||||
let server = tokio::spawn(async move {
|
||||
axum::serve(
|
||||
listener,
|
||||
axum::Router::new().fallback_service(rpc.into_axum_service()),
|
||||
)
|
||||
.with_graceful_shutdown(async {
|
||||
let _ = stopped.await;
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
});
|
||||
let id = service
|
||||
.register(TerminalInfo {
|
||||
terminal_host: "127.0.0.1".into(),
|
||||
terminal_port: address.port().into(),
|
||||
terminal_mac: "probe".into(),
|
||||
..Default::default()
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let mut snapshots = service.subscribe();
|
||||
let worker = tokio::spawn(monitor(
|
||||
service.clone(),
|
||||
Duration::from_millis(20),
|
||||
Duration::from_millis(100),
|
||||
));
|
||||
wait_status(&mut snapshots, &id, ServingStatus::SERVING).await;
|
||||
status.send_replace(ServingStatus::NOT_SERVING);
|
||||
wait_status(&mut snapshots, &id, ServingStatus::NOT_SERVING).await;
|
||||
status.send_replace(ServingStatus::SERVING);
|
||||
wait_status(&mut snapshots, &id, ServingStatus::SERVING).await;
|
||||
stop.send(()).unwrap();
|
||||
server.await.unwrap();
|
||||
wait_status(&mut snapshots, &id, ServingStatus::NOT_SERVING).await;
|
||||
assert_eq!(snapshots.borrow().infos.len(), 1);
|
||||
service.shutdown().await;
|
||||
tokio::time::timeout(Duration::from_secs(1), worker)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn timeout_covers_http2_connection_setup() {
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let info = TerminalInfo {
|
||||
terminal_host: "127.0.0.1".into(),
|
||||
terminal_port: listener.local_addr().unwrap().port().into(),
|
||||
..Default::default()
|
||||
};
|
||||
let status = tokio::time::timeout(
|
||||
Duration::from_secs(1),
|
||||
check(&info, Duration::from_millis(20)),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(status, ServingStatus::NOT_SERVING);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn endpoint_supports_ipv6_and_hostnames() {
|
||||
for host in ["::1", "[::1]", "localhost"] {
|
||||
let uri = endpoint(&TerminalInfo {
|
||||
terminal_host: host.into(),
|
||||
terminal_port: 1234,
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(uri.port_u16(), Some(1234));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user