- Added `RpcError` enum for comprehensive error handling in RPC operations. - Introduced `HealthReporter` and `HealthService` for managing service health statuses. - Implemented health check and watch functionality in the health service. - Created integration tests for health check service, covering various scenarios. - Added unit tests for client options validation and stream functionality. - Established logging infrastructure for terminal and registry components. - Implemented logging lifecycle management and conflict resolution in tests.
149 lines
4.6 KiB
Rust
149 lines
4.6 KiB
Rust
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use connectrpc::{
|
|
ConnectError, RequestContext, Response, ServiceRequest, ServiceResult, ServiceStream,
|
|
};
|
|
use futures_util::stream;
|
|
use proto::control::TerminalInfo;
|
|
use proto::grpc::health::v1::{
|
|
Health, HealthCheckRequest, HealthCheckResponse, health_check_response::ServingStatus,
|
|
};
|
|
use tokio::sync::{oneshot, watch};
|
|
|
|
use crate::RegistryService;
|
|
use crate::health::{check, 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::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);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn readiness_cannot_override_shutdown() {
|
|
let directory = tempfile::tempdir().unwrap();
|
|
let service = RegistryService::open(directory.path().join("terminals.bin"))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
service.health_reporter().status(""),
|
|
Some(ServingStatus::NOT_SERVING)
|
|
);
|
|
let _ = tokio::join!(service.shutdown(), service.set_ready());
|
|
assert!(service.set_ready().await.is_err());
|
|
for name in ["", "control.Center"] {
|
|
assert_eq!(
|
|
service.health_reporter().status(name),
|
|
Some(ServingStatus::NOT_SERVING)
|
|
);
|
|
}
|
|
}
|