- 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.
126 lines
4.0 KiB
Rust
126 lines
4.0 KiB
Rust
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();
|
|
}
|