diff --git a/src/resolver/lookup.rs b/src/resolver/lookup.rs index c208b67f..6f41bfb6 100644 --- a/src/resolver/lookup.rs +++ b/src/resolver/lookup.rs @@ -32,20 +32,17 @@ impl Future for LookupIpFutureStub{ type Output = Result; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - println!("[POLL FUTURE]"); let query = self.query_answer.lock().unwrap().as_mut().poll(cx) ; match query { Poll::Pending => { - println!(" [return pending]"); return Poll::Pending; }, Poll::Ready(Err(_)) => { - println!(" [ready empty]"); self.waker = Some(cx.waker().clone()); - let referenced_query = Arc::clone(&self.query_answer); //same as self.query.clone() + let referenced_query = Arc::clone(&self.query_answer); tokio::spawn( lookup_stub( self.name.clone(), @@ -54,11 +51,10 @@ impl Future for LookupIpFutureStub{ self.waker.clone(), referenced_query, self.config.clone())); - println!(" [return pending]"); + return Poll::Pending; }, Poll::Ready(Ok(ip_addr)) => { - println!(" [return ready]"); return Poll::Ready(Ok(ip_addr)); } } @@ -72,7 +68,6 @@ impl LookupIpFutureStub { config: ResolverConfig, cache: DnsCache ) -> Self { - println!("[LOOKUP CREATE FUTURE]"); Self { name: name, @@ -93,7 +88,6 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y referenced_query:Arc> + Send>>>>, config: ResolverConfig, ) { - println!("[LOOKUP STUB]"); // Create random generator let mut rng = thread_rng(); @@ -112,15 +106,14 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y ); if let Some(cache_lookup) = cache.get(name.clone(), Rtype::A) { - println!("[LOOKUP STUB] cached data {:?}",cache_lookup); - //Add Answer + // Add Answer let answer: Vec = cache_lookup .iter() .map(|rr_cache_value| rr_cache_value.get_resource_record()) .collect::>(); new_query.set_answer(answer); - // return Ok(new_query); + } // Create Server failure query @@ -130,10 +123,8 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y let mut retry_count = 0; for (conn_udp,conn_tcp) in name_servers.iter() { - println!("[LOOKUP STUB] retry {}",retry_count); if retry_count > config.get_retry() { - println!("[LOOKUP STUB] max tries"); break; } @@ -163,26 +154,23 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y } } _ => continue, - } + } retry_count = retry_count + 1; } // Wake up task if let Some(waker) = waker { - println!(" [wake up task]"); waker.wake(); } let mut future_query = referenced_query.lock().unwrap(); *future_query = future::ready(Ok(response.clone())).boxed(); - println!("[save answer]") } #[cfg(test)] mod async_resolver_test { - use crate::dns_cache::cache_data; // use tokio::runtime::Runtime; use crate::message::rdata::a_rdata::ARdata; use crate::message::rdata::Rdata; @@ -221,40 +209,28 @@ mod async_resolver_test { // TODO: test poll (not shure) - // TODO: lookup_stub test - - // TODO: lookup_stub test save query in lookup struct - - // TODO: lookup_stub numer of retries - #[ignore] #[tokio::test] async fn lookup_stub_max_tries(){ let domain_name = DomainName::new_from_string("example.com".to_string()); + let timeout = Duration::from_secs(2); let mut config: ResolverConfig = ResolverConfig::default(); + let non_existent_server:IpAddr = IpAddr::V4(Ipv4Addr::new(44, 44, 1, 81)); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(non_existent_server, timeout); + let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(non_existent_server, timeout); + config.set_name_servers(vec![(conn_udp,conn_tcp)]); config.set_retry(1); - let cache = DnsCache::new(); - let waker = None; - - let query = Arc::new(Mutex::new(future::err(ResolverError::Message("Empty")).boxed())); - - // Create vector of name servers - let non_existente_server:IpAddr = IpAddr::V4(Ipv4Addr::new(234,1 ,4, 44)); - let timeout: Duration = Duration::from_secs(20); - - let conn_udp:ClientUDPConnection = ClientUDPConnection::new(non_existente_server, timeout); - let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(non_existente_server, timeout); - - let name_servers = vec![(conn_udp,conn_tcp)]; - - lookup_stub(domain_name, cache, name_servers, waker,query,config).await; - - //FIXME: is not working + let response_future = LookupIpFutureStub::lookup(domain_name, config, cache).await; + println!("response_future {:?}",response_future); + assert_eq!(response_future.is_ok(), true); + assert_eq!(response_future.unwrap().get_header().get_ancount(), 0); + // assert_eq!(response_future.unwrap().get_header().get_rcode() , 2); //FIXME: }