Skip to content

Commit

Permalink
add test timeout cache 2 rtypes 2 domains in cache by rtype
Browse files Browse the repository at this point in the history
  • Loading branch information
konegoro committed Jan 8, 2024
1 parent 635bf39 commit 68ab57a
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions src/dns_cache/cache_by_record_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ mod cache_data_test{
}

#[test]
fn filter_timout_cache_data_2_differents_rtypes(){
fn filter_timout_cache_data_2_differents_rtypes_same_domain(){
use std::{thread, time};
let mut cache_record_type = CacheByRecordType::new();
let a_rdata = Rdata::A(ARdata::new());
Expand Down Expand Up @@ -868,18 +868,76 @@ mod cache_data_test{

if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) {
if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name.clone()){
//the valid one still having the value
assert_eq!(rrstore_data_vec_a.len(), 1);
}
}

if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) {
println!("{:?}", record_types_data_ns);
assert!(false, "Si habia algo dentro del Rtype NS");
println!(" el CacheByDOmain de NS es {:?}", record_types_data_ns);
assert!(false, "Si habia algo dentro del Rtype NS y NO debía ser así");
} else {
assert!(true);
}
}


#[test]
fn filter_timout_cache_data_2_differents_rtypes_different_domain(){
use std::{thread, time};
let mut cache_record_type = CacheByRecordType::new();
let a_rdata = Rdata::A(ARdata::new());
let ns_rdata = Rdata::NS(NsRdata::new());

let mut resource_record_valid = ResourceRecord::new(a_rdata.clone());
resource_record_valid.set_ttl(1000);
let rr_cache_valid = RRStoredData::new(resource_record_valid.clone());

let mut resource_record_invalid = ResourceRecord::new(ns_rdata);
resource_record_invalid.set_ttl(4);
let rr_cache_invalid = RRStoredData::new(resource_record_invalid);

let mut domain_name_1 = DomainName::new();
domain_name_1.set_name(String::from("example.com"));

let mut domain_name_2 = DomainName::new();
domain_name_2.set_name(String::from("uchile.cl"));


cache_record_type.add_to_cache_data(Rtype::A, domain_name_1.clone(), rr_cache_valid);
cache_record_type.add_to_cache_data(Rtype::NS, domain_name_2.clone(), rr_cache_invalid);

//check if every record_types_data (HashMap for A and for NS) has 1 element
let record_types_data = cache_record_type.get_cache_data();
//CacheByDomainName for A type
if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) {
if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name_1.clone()){
assert_eq!(rrstore_data_vec_a.len(), 1);
}
}
//CacheByDomainName for NS type
if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) {
if let Some(rrstore_data_vec_ns) = record_types_data_ns.clone().get_from_host_data(domain_name_2.clone()){
assert_eq!(rrstore_data_vec_ns.len(), 1);
}
}

println!("Before timeout: {:?}", Utc::now());
thread::sleep(time::Duration::from_secs(5));
println!("After timeout: {:?}", Utc::now());
cache_record_type.filter_timeout_cache_data();

if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) {
if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name_1.clone()){
//the valid one still having the value
assert_eq!(rrstore_data_vec_a.len(), 1);
}
}

if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) {
println!(" el CacheByDomain de NS es : \n {:?}", record_types_data_ns);
assert!(false, "Si habia algo dentro del Rtype NS y NO debía ser así");
} else {
assert!(true);
}
}
}

0 comments on commit 68ab57a

Please sign in to comment.