use tokio::sync::OnceCell;
use svc_storage_client_grpc::prelude::Clients;
pub(crate) static CLIENTS: OnceCell<GrpcClients> = OnceCell::const_new();
pub async fn get_clients() -> &'static GrpcClients {
CLIENTS
.get_or_init(|| async move {
let config = crate::Config::try_from_env().unwrap_or_default();
GrpcClients::default(config)
})
.await
}
#[derive(Clone, Debug)]
pub struct GrpcClients {
pub storage: Clients,
}
impl GrpcClients {
pub fn default(config: crate::config::Config) -> Self {
let storage_clients = Clients::new(config.storage_host_grpc, config.storage_port_grpc);
GrpcClients {
storage: storage_clients,
}
}
}
#[cfg(test)]
mod tests {
use svc_storage_client_grpc::prelude::Client;
use super::*;
#[tokio::test]
async fn test_grpc_clients_default() {
crate::get_log_handle().await;
ut_info!("(test_grpc_clients_default) Start.");
let config = crate::Config::default();
let clients = GrpcClients::default(config);
let vehicle = &clients.storage.vehicle;
ut_debug!("(test_grpc_clients_default) vehicle: {:?}", vehicle);
assert_eq!(vehicle.get_name(), "vehicle");
let vertipad = &clients.storage.vertipad;
ut_debug!("(test_grpc_clients_default) vertipad: {:?}", vertipad);
assert_eq!(vertipad.get_name(), "vertipad");
let vertiport = &clients.storage.vertiport;
ut_debug!("(test_grpc_clients_default) vertiport: {:?}", vertiport);
assert_eq!(vertiport.get_name(), "vertiport");
ut_info!("(test_grpc_clients_default) Success.");
}
}