use auto_discovery::{
config::DiscoveryConfig,
service::ServiceInfo,
types::{ServiceType, ProtocolType},
ServiceDiscovery,
};
use std::time::Duration;
use std::net::{IpAddr, Ipv4Addr};
use tracing::info;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
info!("đ Starting non-network test example");
info!("đ Testing configuration creation...");
let config = DiscoveryConfig::new()
.with_service_type(ServiceType::new("_http._tcp")?)
.with_protocol(ProtocolType::Upnp) .with_timeout(Duration::from_secs(1)) .with_verify_services(false);
info!("â
Configuration created successfully");
info!("đ§ Testing service info creation...");
let service = ServiceInfo::new(
"Test Service",
"_http._tcp",
8080,
Some(vec![
("version", "1.0"),
("description", "Test service for validation"),
])
)?
.with_address(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
info!("â
Service info created: {} on port {}", service.name(), service.port());
info!("đ Testing service discovery creation...");
let discovery = ServiceDiscovery::new(config).await?;
info!("â
Service discovery instance created");
info!("đ Testing quick discovery (with short timeout)...");
let start_time = std::time::Instant::now();
let discovered = discovery
.discover_services(Some(ProtocolType::Upnp))
.await;
let elapsed = start_time.elapsed();
info!("âąī¸ Discovery completed in {:?}", elapsed);
match discovered {
Ok(services) => {
info!("â
Discovery succeeded, found {} services", services.len());
},
Err(e) => {
info!("âšī¸ Discovery failed as expected (no network): {}", e);
}
}
info!("đ§ Testing simple API...");
use auto_discovery::simple::SimpleDiscovery;
let start_time = std::time::Instant::now();
let simple_discovery = SimpleDiscovery::new().await?;
let simple_result = simple_discovery.register_http_service("test-api", 3000).await;
let elapsed = start_time.elapsed();
info!("âąī¸ Simple API call completed in {:?}", elapsed);
match simple_result {
Ok(_handle) => {
info!("â
Simple service registration created successfully");
},
Err(e) => {
info!("âšī¸ Simple registration failed as expected (no network): {}", e);
}
}
info!("â° Testing timeout behavior...");
if elapsed < Duration::from_secs(5) {
info!("â
All operations completed quickly - no infinite loops detected");
} else {
info!("â ī¸ Operations took longer than expected");
}
info!("đ Non-network test completed successfully!");
info!("đ All API functions are working correctly without infinite loops");
Ok(())
}