feat(rpc): implement health check service and error handling

- 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.
This commit is contained in:
2026-10-09 15:57:35 +08:00
parent 7f331565c7
commit bbb67a68ab
65 changed files with 3313 additions and 695 deletions
+125 -67
View File
@@ -1,67 +1,125 @@
use futures_util::StreamExt;
use proto::proto::control::TerminalInfo;
use proto::proto::grpc::health::v1::health_check_response::ServingStatus;
use crate::health::TerminalHealth;
use crate::terminal::Service;
#[tokio::test]
async fn lifecycle_health_stream_and_shutdown() {
let service = Service::new_value();
let health = TerminalHealth(service.clone());
let mut responses = health.responses("");
assert_eq!(
responses.next().await.unwrap().unwrap().status,
ServingStatus::NOT_SERVING
);
service
.configure(TerminalInfo {
terminal_host: "127.0.0.1".into(),
terminal_mac: "test".into(),
..Default::default()
})
.unwrap();
service.set_starting().unwrap();
assert_eq!(
service.health_receiver().borrow().status,
ServingStatus::NOT_SERVING
);
service.set_ready("id".into()).unwrap();
assert_eq!(
responses.next().await.unwrap().unwrap().status,
ServingStatus::SERVING
);
service.set_stopping().unwrap();
assert_eq!(
responses.next().await.unwrap().unwrap().status,
ServingStatus::NOT_SERVING
);
service.set_stopped();
service.set_starting().unwrap();
service.set_configured();
assert_eq!(
service.health_receiver().borrow().status,
ServingStatus::NOT_SERVING
);
service.signal_shutdown();
assert!(responses.next().await.is_none());
assert!(health.responses("").next().await.is_none());
}
#[tokio::test]
async fn unknown_watch_stays_open_until_shutdown() {
let service = Service::new_value();
let health = TerminalHealth(service.clone());
let mut responses = health.responses("missing");
assert_eq!(
responses.next().await.unwrap().unwrap().status,
ServingStatus::SERVICE_UNKNOWN
);
assert!(
tokio::time::timeout(std::time::Duration::from_millis(20), responses.next())
.await
.is_err()
);
service.signal_shutdown();
assert!(responses.next().await.is_none());
}
use crate::terminal::Service;
use proto::control::TerminalInfo;
use proto::grpc::health::v1::HealthCheckRequest;
use proto::rpc::{
Router,
client::{HealthRpcClient, RpcChannel, RpcClientOptions, RpcEndpoint},
health::{HealthService, ServingStatus},
server,
};
use std::{sync::Arc, time::Duration};
fn assert_status(service: &Service, status: ServingStatus) {
for name in ["", "control.Terminal"] {
assert_eq!(service.health_reporter().status(name), Some(status));
}
}
#[test]
fn lifecycle_health_mapping() {
let service = Service::new_value();
assert_status(&service, ServingStatus::NOT_SERVING);
service
.configure(TerminalInfo {
terminal_host: "127.0.0.1".into(),
terminal_mac: "test".into(),
..Default::default()
})
.unwrap();
service.set_starting().unwrap();
assert_status(&service, ServingStatus::NOT_SERVING);
service.set_ready("id".into()).unwrap();
assert_status(&service, ServingStatus::SERVING);
service.set_stopping().unwrap();
assert_status(&service, ServingStatus::NOT_SERVING);
service.set_stopped();
service.set_starting().unwrap();
service.set_configured();
assert_status(&service, ServingStatus::NOT_SERVING);
}
async fn serve_health(
service: &Service,
) -> (
HealthRpcClient,
tokio::task::JoinHandle<std::io::Result<()>>,
) {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let endpoint =
RpcEndpoint::from_host_port("127.0.0.1", listener.local_addr().unwrap().port()).unwrap();
let mut shutdown = service.shutdown_receiver();
let router = Router::new().add_service(Arc::new(HealthService::new(
service.health_reporter(),
shutdown.clone(),
)));
let task = tokio::spawn(server::serve(listener, router, async move {
let _ = shutdown.wait_for(|stopped| *stopped).await;
}));
let channel = RpcChannel::connect(&endpoint, RpcClientOptions::default())
.await
.unwrap();
(channel.health_client(), task)
}
#[tokio::test]
async fn restart_uses_new_shutdown_channel() {
tokio::time::timeout(Duration::from_secs(5), async {
let service = Service::new_value();
service
.configure(TerminalInfo {
terminal_host: "127.0.0.1".into(),
terminal_mac: "test".into(),
..Default::default()
})
.unwrap();
service.set_starting().unwrap();
service.set_ready("first".into()).unwrap();
let (client, task) = serve_health(&service).await;
let mut first = client.watch(HealthCheckRequest::default()).await.unwrap();
assert_eq!(
first
.message()
.await
.unwrap()
.unwrap()
.to_owned_message()
.status,
ServingStatus::SERVING
);
service.signal_shutdown();
assert!(first.message().await.unwrap().is_none());
task.await.unwrap().unwrap();
service.set_stopped();
service.reset_shutdown().unwrap();
service.set_starting().unwrap();
let (client, task) = serve_health(&service).await;
let mut second = client.watch(HealthCheckRequest::default()).await.unwrap();
assert_eq!(
second
.message()
.await
.unwrap()
.unwrap()
.to_owned_message()
.status,
ServingStatus::NOT_SERVING
);
service.set_ready("second".into()).unwrap();
assert_eq!(
second
.message()
.await
.unwrap()
.unwrap()
.to_owned_message()
.status,
ServingStatus::SERVING
);
assert!(first.message().await.unwrap().is_none());
service.signal_shutdown();
assert!(second.message().await.unwrap().is_none());
task.await.unwrap().unwrap();
})
.await
.unwrap();
}