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,177 @@
|
||||
use proto::proto::control::TerminalInfo;
|
||||
use proto::proto::grpc::health::v1::health_check_response::ServingStatus;
|
||||
|
||||
use crate::RegistryService;
|
||||
use crate::service::normalize_info;
|
||||
|
||||
fn info() -> TerminalInfo {
|
||||
TerminalInfo {
|
||||
terminal_host: "127.0.0.1".into(),
|
||||
terminal_port: 1234,
|
||||
terminal_mac: " AA:BB ".into(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn identity_idempotency_restart_and_deregistration() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let path = directory.path().join("terminals.bin");
|
||||
let service = RegistryService::open(&path).await.unwrap();
|
||||
let mut receiver = service.subscribe();
|
||||
let id = service.register(info()).await.unwrap();
|
||||
receiver.borrow_and_update();
|
||||
let mut normalized = info();
|
||||
normalized.terminal_mac = "aa:bb".into();
|
||||
assert_eq!(service.register(normalized).await.unwrap(), id);
|
||||
assert!(!receiver.has_changed().unwrap());
|
||||
let target = service.probe_targets().await.pop().unwrap();
|
||||
service
|
||||
.update_health(&target, ServingStatus::SERVING.into())
|
||||
.await;
|
||||
let mut healthy = service.subscribe();
|
||||
let persisted = std::fs::read(&path).unwrap();
|
||||
service
|
||||
.update_health(&target, ServingStatus::SERVING.into())
|
||||
.await;
|
||||
assert!(!healthy.has_changed().unwrap());
|
||||
assert_eq!(std::fs::read(&path).unwrap(), persisted);
|
||||
healthy.borrow_and_update();
|
||||
let restored = RegistryService::open(&path).await.unwrap();
|
||||
assert_eq!(
|
||||
restored.subscribe().borrow().infos[&id].status,
|
||||
ServingStatus::UNKNOWN
|
||||
);
|
||||
assert_eq!(restored.register(info()).await.unwrap(), id);
|
||||
restored.deregister(id.clone()).await.unwrap();
|
||||
let receiver = restored.subscribe();
|
||||
restored.deregister(id.clone()).await.unwrap();
|
||||
assert!(!receiver.has_changed().unwrap());
|
||||
assert_eq!(restored.register(info()).await.unwrap(), id);
|
||||
assert_eq!(
|
||||
RegistryService::open(path)
|
||||
.await
|
||||
.unwrap()
|
||||
.register(info())
|
||||
.await
|
||||
.unwrap(),
|
||||
id
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn concurrent_registration_and_stale_probe_results() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let service = RegistryService::open(directory.path().join("terminals.bin"))
|
||||
.await
|
||||
.unwrap();
|
||||
let (first, second) = tokio::join!(service.register(info()), service.register(info()));
|
||||
let id = first.unwrap();
|
||||
assert_eq!(id, second.unwrap());
|
||||
assert_eq!(service.subscribe().borrow().infos.len(), 1);
|
||||
let old = service.probe_targets().await.pop().unwrap();
|
||||
let mut updated = info();
|
||||
updated.terminal_port += 1;
|
||||
assert_eq!(service.register(updated).await.unwrap(), id);
|
||||
service
|
||||
.update_health(&old, ServingStatus::SERVING.into())
|
||||
.await;
|
||||
assert_eq!(
|
||||
service.subscribe().borrow().infos[&id].status,
|
||||
ServingStatus::UNKNOWN
|
||||
);
|
||||
service.deregister(id.clone()).await.unwrap();
|
||||
service.register(info()).await.unwrap();
|
||||
service
|
||||
.update_health(&old, ServingStatus::SERVING.into())
|
||||
.await;
|
||||
assert_eq!(
|
||||
service.subscribe().borrow().infos[&id].status,
|
||||
ServingStatus::UNKNOWN
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn failed_persistence_does_not_commit_memory() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let path = directory.path().join("terminals.bin");
|
||||
let service = RegistryService::open(&path).await.unwrap();
|
||||
let receiver = service.subscribe();
|
||||
std::fs::create_dir(&path).unwrap();
|
||||
assert!(service.register(info()).await.is_err());
|
||||
assert!(receiver.borrow().infos.is_empty());
|
||||
assert!(!receiver.has_changed().unwrap());
|
||||
std::fs::remove_dir(&path).unwrap();
|
||||
let id = service.register(info()).await.unwrap();
|
||||
std::fs::remove_file(&path).unwrap();
|
||||
std::fs::create_dir(&path).unwrap();
|
||||
let mut updated = info();
|
||||
updated.terminal_port = 4321;
|
||||
assert!(service.register(updated).await.is_err());
|
||||
assert_eq!(receiver.borrow().infos[&id].info.terminal_port, 1234);
|
||||
assert!(service.deregister(id.clone()).await.is_err());
|
||||
assert!(receiver.borrow().infos.contains_key(&id));
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn cancelled_caller_does_not_interrupt_an_accepted_transaction() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let path = directory.path().join("terminals.bin");
|
||||
let service = RegistryService::open(&path).await.unwrap();
|
||||
let mut receiver = service.subscribe();
|
||||
let mut registration = Box::pin(service.register(info()));
|
||||
assert!(futures_util::poll!(registration.as_mut()).is_pending());
|
||||
drop(registration);
|
||||
tokio::time::timeout(std::time::Duration::from_secs(2), receiver.changed())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let committed = receiver.borrow().clone();
|
||||
let restored = RegistryService::open(&path).await.unwrap();
|
||||
assert_eq!(committed, *restored.subscribe().borrow());
|
||||
let id = committed.infos.keys().next().unwrap().clone();
|
||||
let mut deregistration = Box::pin(service.deregister(id));
|
||||
assert!(futures_util::poll!(deregistration.as_mut()).is_pending());
|
||||
drop(deregistration);
|
||||
tokio::time::timeout(std::time::Duration::from_secs(2), receiver.changed())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(receiver.borrow().infos.is_empty());
|
||||
assert!(
|
||||
RegistryService::open(path)
|
||||
.await
|
||||
.unwrap()
|
||||
.subscribe()
|
||||
.borrow()
|
||||
.infos
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_registration_is_rejected() {
|
||||
for port in [0, 65536] {
|
||||
assert!(
|
||||
normalize_info(TerminalInfo {
|
||||
terminal_port: port,
|
||||
..info()
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
normalize_info(TerminalInfo {
|
||||
terminal_mac: " ".into(),
|
||||
..info()
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
normalize_info(TerminalInfo {
|
||||
terminal_host: " ".into(),
|
||||
..info()
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user