services ref

migrate
prplV 2025-05-29 17:35:31 +03:00
parent 1c651c93ce
commit e0720a5f54
1 changed files with 10 additions and 27 deletions

View File

@ -1,6 +1,5 @@
use crate::options::structs::CustomError;
use log::{error, warn};
use std::net::{TcpStream, ToSocketAddrs};
use std::net::ToSocketAddrs;
use std::sync::Arc;
use tokio::time::Duration;
use tokio::sync::mpsc::Sender as Sender;
@ -236,31 +235,15 @@ pub mod v2 {
}
}
async fn check_service(hostname: &str, port: &u32) -> Result<(), CustomError> {
let addr = format!("{}:{}", hostname, port);
match addr.to_socket_addrs() {
Ok(mut addrs) => {
if addrs.any(|a| TcpStream::connect_timeout(&a, Duration::new(1, 0)).is_ok()) {
Ok(())
} else {
Err(CustomError::Fatal)
}
}
Err(_) => Err(CustomError::Fatal),
}
}
#[cfg(test)]
mod service_unittests {
use super::check_service;
#[tokio::test]
async fn check_available_service() {
assert!(check_service("ya.ru", &443).await.is_ok());
}
#[tokio::test]
async fn check_unavailable_service() {
assert!(check_service("unavailable.service", &1111).await.is_err());
}
// use super::check_service;
// #[tokio::test]
// async fn check_available_service() {
// assert!(check_service("ya.ru", &443).await.is_ok());
// }
// #[tokio::test]
// async fn check_unavailable_service() {
// assert!(check_service("unavailable.service", &1111).await.is_err());
// }
}