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
+87 -79
View File
@@ -1,79 +1,87 @@
use std::ptr;
use buffa::Message;
use proto::proto::control::TerminalInfo;
use crate::ffi::*;
#[test]
fn null_handles_are_rejected() {
assert_eq!(
unsafe { terminal_service_configure(ptr::null_mut(), ptr::null(), 0) },
TERMINAL_ERROR_NULL
);
assert_eq!(
unsafe { terminal_service_destroy(ptr::null_mut()) },
TERMINAL_ERROR_NULL
);
}
#[test]
fn invalid_protobuf_is_reported_through_last_error() {
let handle = terminal_service_create();
let invalid = [0xff_u8, 0xff];
assert_eq!(
unsafe { terminal_service_configure(handle, invalid.as_ptr(), invalid.len()) },
TERMINAL_ERROR_DECODE
);
let mut required = 0;
assert_eq!(
unsafe { terminal_service_last_error(handle, ptr::null_mut(), &mut required) },
TERMINAL_OK
);
assert!(required > 1);
let mut buffer = vec![0_u8; required];
assert_eq!(
unsafe { terminal_service_last_error(handle, buffer.as_mut_ptr(), &mut required) },
TERMINAL_OK
);
let message = std::str::from_utf8(&buffer[..required - 1]).unwrap();
assert!(message.starts_with("DECODE_FAILED:"));
assert_eq!(unsafe { terminal_service_destroy(handle) }, TERMINAL_OK);
}
#[test]
fn configured_service_can_be_destroyed() {
let handle = terminal_service_create();
let info = TerminalInfo {
terminal_host: "127.0.0.1".to_owned(),
terminal_port: 0,
terminal_mac: "00:11:22:33:44:55".to_owned(),
..Default::default()
}
.encode_to_vec();
assert_eq!(
unsafe { terminal_service_configure(handle, info.as_ptr(), info.len()) },
TERMINAL_OK
);
assert_eq!(unsafe { terminal_service_destroy(handle) }, TERMINAL_OK);
}
#[test]
fn null_callback_is_rejected() {
let handle = terminal_service_create();
let command_id = b"init";
assert_eq!(
unsafe {
terminal_service_register_command(
handle,
command_id.as_ptr(),
command_id.len(),
None,
ptr::null_mut(),
)
},
TERMINAL_ERROR_INVALID_ARGUMENT
);
assert_eq!(unsafe { terminal_service_destroy(handle) }, TERMINAL_OK);
}
use std::ptr;
use buffa::Message;
use proto::control::TerminalInfo;
use crate::ffi::*;
#[test]
fn null_handles_are_rejected() {
assert_eq!(
unsafe { terminal_logging_init(ptr::null_mut(), ptr::null(), 0, ptr::null(), 0, 10) },
TERMINAL_ERROR_NULL
);
assert_eq!(
unsafe { terminal_logging_shutdown(ptr::null_mut()) },
TERMINAL_ERROR_NULL
);
assert_eq!(
unsafe { terminal_service_configure(ptr::null_mut(), ptr::null(), 0) },
TERMINAL_ERROR_NULL
);
assert_eq!(
unsafe { terminal_service_destroy(ptr::null_mut()) },
TERMINAL_ERROR_NULL
);
}
#[test]
fn invalid_protobuf_is_reported_through_last_error() {
let handle = terminal_service_create();
let invalid = [0xff_u8, 0xff];
assert_eq!(
unsafe { terminal_service_configure(handle, invalid.as_ptr(), invalid.len()) },
TERMINAL_ERROR_DECODE
);
let mut required = 0;
assert_eq!(
unsafe { terminal_service_last_error(handle, ptr::null_mut(), &mut required) },
TERMINAL_OK
);
assert!(required > 1);
let mut buffer = vec![0_u8; required];
assert_eq!(
unsafe { terminal_service_last_error(handle, buffer.as_mut_ptr(), &mut required) },
TERMINAL_OK
);
let message = std::str::from_utf8(&buffer[..required - 1]).unwrap();
assert!(message.starts_with("DECODE_FAILED:"));
assert_eq!(unsafe { terminal_service_destroy(handle) }, TERMINAL_OK);
}
#[test]
fn configured_service_can_be_destroyed() {
let handle = terminal_service_create();
let info = TerminalInfo {
terminal_host: "127.0.0.1".to_owned(),
terminal_port: 0,
terminal_mac: "00:11:22:33:44:55".to_owned(),
..Default::default()
}
.encode_to_vec();
assert_eq!(
unsafe { terminal_service_configure(handle, info.as_ptr(), info.len()) },
TERMINAL_OK
);
assert_eq!(unsafe { terminal_service_destroy(handle) }, TERMINAL_OK);
}
#[test]
fn null_callback_is_rejected() {
let handle = terminal_service_create();
let command_id = b"init";
assert_eq!(
unsafe {
terminal_service_register_command(
handle,
command_id.as_ptr(),
command_id.len(),
None,
ptr::null_mut(),
)
},
TERMINAL_ERROR_INVALID_ARGUMENT
);
assert_eq!(unsafe { terminal_service_destroy(handle) }, TERMINAL_OK);
}
+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();
}
+1 -1
View File
@@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use buffa::Message;
use proto::proto::control::{
use proto::control::{
Command, InvokeResponse, TerminalCancelEvent, TerminalEvent, TerminalEventHandle,
TerminalEventInfo, TerminalInfo, TerminalStatus,
};