Add terminal service implementation and integration tests
- Introduced terminal.h header file defining the terminal service API. - Implemented integration tests for terminal service registration and health checks. - Added unit tests for FFI layer, health checks, and terminal service functionality. - Created a mock center for testing terminal registration and deregistration. - Ensured proper error handling and response validation in tests.
This commit is contained in:
@@ -0,0 +1,281 @@
|
||||
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::proto::control::{CenterClient, TerminalInfo};
|
||||
use proto::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 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();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user