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
+104 -2
View File
@@ -5,14 +5,116 @@ use buffa::Message;
use buffa_types::google::protobuf::{Empty, StringValue};
use connectrpc::Protocol;
use connectrpc::client::{ClientConfig, Http2Connection};
use proto::proto::control::{CenterClient, TerminalInfo};
use proto::proto::grpc::health::v1::health_check_response::ServingStatus;
use proto::control::{CenterClient, TerminalInfo};
use proto::grpc::health::v1::health_check_response::ServingStatus;
use terminal::ffi::*;
use tokio::net::TcpListener;
use tokio::sync::oneshot;
use crate::{RegistryService, runtime::serve};
#[tokio::test]
async fn registry_health_is_independent_of_downstream_health() {
use proto::grpc::health::v1::HealthCheckRequest;
use proto::rpc::client::{RpcChannel, RpcClientOptions, RpcEndpoint};
tokio::time::timeout(Duration::from_secs(5), async {
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 listener = 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 task = tokio::spawn(serve(
listener,
service.clone(),
Duration::from_millis(20),
Duration::from_millis(30),
std::future::pending(),
));
let channel = RpcChannel::connect(&endpoint, RpcClientOptions::default())
.await
.unwrap();
let health = channel.health_client();
for name in ["", "control.Center"] {
assert_eq!(
health
.check(HealthCheckRequest {
service: name.into(),
..Default::default()
})
.await
.unwrap()
.into_owned()
.status,
ServingStatus::SERVING
);
}
let mut updates = health.watch(HealthCheckRequest::default()).await.unwrap();
assert_eq!(
updates
.message()
.await
.unwrap()
.unwrap()
.to_owned_message()
.status,
ServingStatus::SERVING
);
let unresponsive = TcpListener::bind("127.0.0.1:0").await.unwrap();
let response = channel
.center_client()
.register_terminal(TerminalInfo {
terminal_host: "127.0.0.1".into(),
terminal_port: unresponsive.local_addr().unwrap().port().into(),
terminal_mac: "unresponsive".into(),
..Default::default()
})
.await
.unwrap()
.into_owned();
assert!(response.success);
let mut directory_updates = service.subscribe();
directory_updates
.wait_for(|snapshot| {
snapshot.infos[&response.message].status == ServingStatus::NOT_SERVING
})
.await
.unwrap();
assert_eq!(
health
.check(HealthCheckRequest::default())
.await
.unwrap()
.into_owned()
.status,
ServingStatus::SERVING
);
service.shutdown().await;
for name in ["", "control.Center"] {
assert_eq!(
service.health_reporter().status(name),
Some(ServingStatus::NOT_SERVING)
);
}
while let Some(message) = updates.message().await.unwrap() {
assert_eq!(
message.to_owned_message().status,
ServingStatus::NOT_SERVING
);
}
task.await.unwrap().unwrap();
assert!(service.set_ready().await.is_err());
})
.await
.unwrap();
}
#[tokio::test]
async fn center_rpc_register_update_subscribe_deregister_and_shutdown() {
tokio::time::timeout(Duration::from_secs(10), async {