- 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.
384 lines
12 KiB
Rust
384 lines
12 KiB
Rust
use std::ffi::c_void;
|
|
use std::time::Duration;
|
|
|
|
use buffa::Message;
|
|
use buffa_types::google::protobuf::{Empty, StringValue};
|
|
use connectrpc::Protocol;
|
|
use connectrpc::client::{ClientConfig, Http2Connection};
|
|
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 {
|
|
let directory = tempfile::tempdir().unwrap();
|
|
let service = RegistryService::open(directory.path().join("terminals.bin"))
|
|
.await
|
|
.unwrap();
|
|
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let uri: axum::http::Uri = format!("http://{}", listener.local_addr().unwrap())
|
|
.parse()
|
|
.unwrap();
|
|
let (stop, stopped) = oneshot::channel();
|
|
let server = tokio::spawn(serve(
|
|
listener,
|
|
service.clone(),
|
|
Duration::from_secs(10),
|
|
Duration::from_millis(100),
|
|
async {
|
|
let _ = stopped.await;
|
|
},
|
|
));
|
|
let connection = Http2Connection::connect_plaintext(uri.clone())
|
|
.await
|
|
.unwrap();
|
|
let client = CenterClient::new(
|
|
connection.shared(32),
|
|
ClientConfig::new(uri)
|
|
.with_protocol(Protocol::Grpc)
|
|
.with_default_timeout(Duration::from_secs(5)),
|
|
);
|
|
let mut first = client.subscribe_terminal(Empty::default()).await.unwrap();
|
|
let mut second = client.subscribe_terminal(Empty::default()).await.unwrap();
|
|
assert!(
|
|
first
|
|
.message()
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.to_owned_message()
|
|
.infos
|
|
.is_empty()
|
|
);
|
|
assert!(
|
|
second
|
|
.message()
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.to_owned_message()
|
|
.infos
|
|
.is_empty()
|
|
);
|
|
let invalid = client
|
|
.register_terminal(TerminalInfo::default())
|
|
.await
|
|
.unwrap()
|
|
.into_owned();
|
|
assert!(!invalid.success);
|
|
assert!(invalid.message.starts_with("INVALID_ARGUMENT:"));
|
|
let mut info = TerminalInfo {
|
|
terminal_host: "127.0.0.1".into(),
|
|
terminal_port: 1234,
|
|
terminal_mac: " AA:BB ".into(),
|
|
..Default::default()
|
|
};
|
|
let response = client
|
|
.register_terminal(info.clone())
|
|
.await
|
|
.unwrap()
|
|
.into_owned();
|
|
assert!(response.success);
|
|
let id = response.message;
|
|
assert_eq!(
|
|
first
|
|
.message()
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.to_owned_message()
|
|
.infos[&id]
|
|
.info
|
|
.terminal_mac,
|
|
"aa:bb"
|
|
);
|
|
assert!(
|
|
second
|
|
.message()
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.to_owned_message()
|
|
.infos
|
|
.contains_key(&id)
|
|
);
|
|
info.terminal_port = 4321;
|
|
let updated = client
|
|
.register_terminal(info.clone())
|
|
.await
|
|
.unwrap()
|
|
.into_owned();
|
|
assert!(updated.success);
|
|
assert_eq!(updated.message, id);
|
|
loop {
|
|
let snapshot = first.message().await.unwrap().unwrap().to_owned_message();
|
|
if snapshot.infos[&id].info.terminal_port == 4321 {
|
|
break;
|
|
}
|
|
}
|
|
for _attempt in 0..2 {
|
|
assert!(
|
|
client
|
|
.deregister_terminal(StringValue {
|
|
value: id.clone(),
|
|
..Default::default()
|
|
})
|
|
.await
|
|
.unwrap()
|
|
.into_owned()
|
|
.success
|
|
);
|
|
}
|
|
loop {
|
|
if first
|
|
.message()
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.to_owned_message()
|
|
.infos
|
|
.is_empty()
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
stop.send(()).unwrap();
|
|
assert!(first.message().await.unwrap().is_none());
|
|
drop(second);
|
|
drop(first);
|
|
drop(client);
|
|
server.await.unwrap().unwrap();
|
|
})
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
struct TerminalHandle(*mut c_void);
|
|
|
|
impl Drop for TerminalHandle {
|
|
fn drop(&mut self) {
|
|
unsafe {
|
|
terminal_service_destroy(self.0);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn real_terminal_survives_registry_restart_and_deregisters() {
|
|
let directory = tempfile::tempdir().unwrap();
|
|
let path = directory.path().join("terminals.bin");
|
|
let runtime = tokio::runtime::Runtime::new().unwrap();
|
|
let service = runtime.block_on(RegistryService::open(&path)).unwrap();
|
|
let listener = runtime.block_on(TcpListener::bind("127.0.0.1:0")).unwrap();
|
|
let address = listener.local_addr().unwrap();
|
|
let (stop, stopped) = oneshot::channel();
|
|
let server = runtime.spawn(serve(
|
|
listener,
|
|
service.clone(),
|
|
Duration::from_millis(20),
|
|
Duration::from_secs(1),
|
|
async {
|
|
let _ = stopped.await;
|
|
},
|
|
));
|
|
let handle = TerminalHandle(terminal_service_create());
|
|
let info = TerminalInfo {
|
|
terminal_host: "127.0.0.1".into(),
|
|
terminal_port: 0,
|
|
terminal_mac: "real-terminal".into(),
|
|
..Default::default()
|
|
}
|
|
.encode_to_vec();
|
|
assert_eq!(
|
|
unsafe { terminal_service_configure(handle.0, info.as_ptr(), info.len()) },
|
|
TERMINAL_OK
|
|
);
|
|
let host = b"127.0.0.1";
|
|
let mut bound_port = 0;
|
|
assert_eq!(
|
|
unsafe {
|
|
terminal_service_start(
|
|
handle.0,
|
|
host.as_ptr(),
|
|
host.len(),
|
|
address.port(),
|
|
&mut bound_port,
|
|
)
|
|
},
|
|
TERMINAL_OK
|
|
);
|
|
assert_ne!(bound_port, 0);
|
|
let id = runtime.block_on(async {
|
|
let mut receiver = service.subscribe();
|
|
let snapshot = tokio::time::timeout(
|
|
Duration::from_secs(3),
|
|
receiver.wait_for(|snapshot| {
|
|
snapshot
|
|
.infos
|
|
.values()
|
|
.any(|runtime| runtime.status == ServingStatus::SERVING)
|
|
}),
|
|
)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
snapshot.infos.keys().next().unwrap().clone()
|
|
});
|
|
stop.send(()).unwrap();
|
|
runtime.block_on(async {
|
|
tokio::time::timeout(Duration::from_secs(6), server)
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.unwrap();
|
|
});
|
|
let restored = runtime.block_on(RegistryService::open(path)).unwrap();
|
|
assert_eq!(
|
|
restored.subscribe().borrow().infos[&id].status,
|
|
ServingStatus::UNKNOWN
|
|
);
|
|
assert_eq!(
|
|
restored.subscribe().borrow().infos[&id].info.terminal_port,
|
|
u32::from(bound_port)
|
|
);
|
|
let listener = runtime.block_on(TcpListener::bind(address)).unwrap();
|
|
let (stop, stopped) = oneshot::channel();
|
|
let server = runtime.spawn(serve(
|
|
listener,
|
|
restored.clone(),
|
|
Duration::from_millis(20),
|
|
Duration::from_secs(1),
|
|
async {
|
|
let _ = stopped.await;
|
|
},
|
|
));
|
|
runtime.block_on(async {
|
|
let mut receiver = restored.subscribe();
|
|
tokio::time::timeout(
|
|
Duration::from_secs(3),
|
|
receiver.wait_for(|snapshot| snapshot.infos[&id].status == ServingStatus::SERVING),
|
|
)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
});
|
|
assert_eq!(unsafe { terminal_service_stop(handle.0) }, TERMINAL_OK);
|
|
assert!(restored.subscribe().borrow().infos.is_empty());
|
|
drop(handle);
|
|
stop.send(()).unwrap();
|
|
runtime.block_on(async {
|
|
tokio::time::timeout(Duration::from_secs(6), server)
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.unwrap();
|
|
});
|
|
}
|