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:
@@ -0,0 +1,68 @@
|
||||
use crate::rpc::client::{RpcChannel, RpcClientOptions, RpcEndpoint};
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn validates_endpoints_and_preserves_compatibility_exports() {
|
||||
let defaults = RpcClientOptions::default();
|
||||
assert_eq!(defaults.connect_timeout, Duration::from_secs(3));
|
||||
assert!(defaults.request_timeout.is_none());
|
||||
for host in ["localhost", "127.0.0.1", "::1", "[::1]"] {
|
||||
let endpoint = RpcEndpoint::from_host_port(host, 1234).unwrap();
|
||||
assert_eq!(endpoint.uri().port_u16(), Some(1234));
|
||||
assert_eq!(
|
||||
RpcEndpoint::from_uri(endpoint.uri().clone()).unwrap(),
|
||||
endpoint
|
||||
);
|
||||
}
|
||||
for host in [
|
||||
"",
|
||||
" foo ",
|
||||
"user@host",
|
||||
"host/path",
|
||||
"host?query",
|
||||
"host:99",
|
||||
"[foo]",
|
||||
] {
|
||||
assert!(RpcEndpoint::from_host_port(host, 1234).is_err());
|
||||
}
|
||||
assert!(RpcEndpoint::from_host_port("localhost", 0).is_err());
|
||||
for uri in [
|
||||
"https://localhost:99/",
|
||||
"http://localhost/",
|
||||
"http://localhost:0/",
|
||||
"http://localhost:65536/",
|
||||
"http://localhost:99/path",
|
||||
"http://localhost:99/?q=1",
|
||||
"http://user@localhost:99/",
|
||||
] {
|
||||
assert!(
|
||||
RpcEndpoint::from_uri(uri.parse().unwrap()).is_err(),
|
||||
"{uri}"
|
||||
);
|
||||
}
|
||||
let _: crate::control::TerminalInfo = crate::proto::control::TerminalInfo::default();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_options_fail_before_connecting() {
|
||||
let endpoint = RpcEndpoint::from_host_port("localhost", 1234).unwrap();
|
||||
for options in [
|
||||
RpcClientOptions {
|
||||
connect_timeout: Duration::ZERO,
|
||||
..Default::default()
|
||||
},
|
||||
RpcClientOptions {
|
||||
request_timeout: Some(Duration::ZERO),
|
||||
..Default::default()
|
||||
},
|
||||
RpcClientOptions {
|
||||
buffer_capacity: 0,
|
||||
..Default::default()
|
||||
},
|
||||
] {
|
||||
assert!(matches!(
|
||||
RpcChannel::connect(&endpoint, options).await,
|
||||
Err(crate::rpc::RpcError::InvalidOptions(_))
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
mod client;
|
||||
mod stream;
|
||||
@@ -0,0 +1,42 @@
|
||||
use crate::rpc::stream::watch_stream;
|
||||
use futures_util::StreamExt;
|
||||
use tokio::sync::watch;
|
||||
|
||||
#[tokio::test]
|
||||
async fn initial_latest_multiple_subscribers_and_shutdown() {
|
||||
let (sender, receiver) = watch::channel(0);
|
||||
let (stop, shutdown) = watch::channel(false);
|
||||
let mut first = Box::pin(watch_stream(receiver.clone(), shutdown.clone()));
|
||||
let mut second = Box::pin(watch_stream(receiver, shutdown.clone()));
|
||||
sender.send_replace(1);
|
||||
assert_eq!(first.next().await.unwrap().unwrap(), 1);
|
||||
assert_eq!(second.next().await.unwrap().unwrap(), 1);
|
||||
sender.send_replace(2);
|
||||
sender.send_replace(3);
|
||||
assert_eq!(first.next().await.unwrap().unwrap(), 3);
|
||||
assert_eq!(second.next().await.unwrap().unwrap(), 3);
|
||||
stop.send_replace(true);
|
||||
assert!(first.next().await.is_none());
|
||||
assert!(second.next().await.is_none());
|
||||
assert!(
|
||||
Box::pin(watch_stream(sender.subscribe(), shutdown))
|
||||
.next()
|
||||
.await
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn channel_closure_ends_stream() {
|
||||
let (sender, receiver) = watch::channel(0);
|
||||
let (stop, shutdown) = watch::channel(false);
|
||||
let mut stream = Box::pin(watch_stream(receiver, shutdown));
|
||||
stream.next().await.unwrap().unwrap();
|
||||
drop(sender);
|
||||
assert!(stream.next().await.is_none());
|
||||
let (sender, receiver) = watch::channel(0);
|
||||
let mut stream = Box::pin(watch_stream(receiver, stop.subscribe()));
|
||||
drop(stop);
|
||||
assert!(stream.next().await.is_none());
|
||||
drop(sender);
|
||||
}
|
||||
Reference in New Issue
Block a user