|
1 | 1 | pub mod cache_by_record_type; |
2 | 2 |
|
| 3 | + |
3 | 4 | use crate::dns_cache::cache_by_record_type::CacheByRecordType; |
4 | 5 | use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; |
5 | 6 | use crate::message::rdata::Rdata; |
@@ -218,6 +219,7 @@ impl DnsCache { |
218 | 219 |
|
219 | 220 | #[cfg(test)] |
220 | 221 | mod dns_cache_test { |
| 222 | + use chrono::Utc; |
221 | 223 | use crate::dns_cache::DnsCache; |
222 | 224 | use crate::dns_cache::cache_by_record_type::CacheByRecordType; |
223 | 225 | use crate::dns_cache::cache_by_record_type::cache_by_domain_name::CacheByDomainName; |
@@ -498,4 +500,58 @@ mod dns_cache_test { |
498 | 500 |
|
499 | 501 | assert_eq!(cache.is_cached(domain_name, Rtype::A), true); |
500 | 502 | } |
| 503 | + |
| 504 | + #[test] |
| 505 | + fn timeout_cache_1_domain_same_type(){ |
| 506 | + use std::{thread, time}; |
| 507 | + let mut dns_cache = DnsCache::new(); |
| 508 | + |
| 509 | + dns_cache.set_max_size(3); |
| 510 | + |
| 511 | + let mut domain_name = DomainName::new(); |
| 512 | + domain_name.set_name(String::from("uchile.cl")); |
| 513 | + |
| 514 | + let a_rdata = Rdata::A(ARdata::new()); |
| 515 | + |
| 516 | + let mut resource_record = ResourceRecord::new(a_rdata.clone()); |
| 517 | + resource_record.set_ttl(4); |
| 518 | + |
| 519 | + dns_cache.add(domain_name.clone(), resource_record.clone()); |
| 520 | + |
| 521 | + assert_eq!(dns_cache.get_cache().get_cache_data().len(), 1); |
| 522 | + |
| 523 | + let mut resource_record_2 = ResourceRecord::new(a_rdata.clone()); |
| 524 | + resource_record_2.set_ttl(4); |
| 525 | + |
| 526 | + dns_cache.add(domain_name.clone(), resource_record_2.clone()); |
| 527 | + |
| 528 | + //because both are of the same type, the cache_data (cache by record type) has 1 element |
| 529 | + // Rdata::A -> cache_by_domain_name |
| 530 | + assert_eq!(dns_cache.get_cache().get_cache_data().len(), 1); |
| 531 | + if let Some(cache_by_domain_name) = dns_cache.get_cache().get(Rtype::A) { |
| 532 | + if let Some(rrstore_data_vec) = cache_by_domain_name.get(&domain_name) { |
| 533 | + assert_eq!(rrstore_data_vec.len(), 2); |
| 534 | + } |
| 535 | + } |
| 536 | + assert_eq!(dns_cache.get_size(), 2); |
| 537 | + |
| 538 | + println!("Before timeout: {:?}", Utc::now()); |
| 539 | + thread::sleep(time::Duration::from_secs(5)); |
| 540 | + println!("After timeout: {:?}", Utc::now()); |
| 541 | + dns_cache.timeout_cache(); |
| 542 | + |
| 543 | + //FIXME: the size shoud be 1 because we have only 1 resocurce_record associated with 1 domain |
| 544 | + // assert_eq!(dns_cache.get_size(), 1); |
| 545 | + |
| 546 | + //check iof the resource_record_2 was deleted |
| 547 | + if let Some(cache_by_domain_name) = dns_cache.get_cache().get(Rtype::A) { |
| 548 | + if let Some(rrstore_data_vec) = cache_by_domain_name.get(&domain_name) { |
| 549 | + //FIXME: gives 0 instead of 1, meaning all the rrstored records were deleted, when only 1 shoud have been deleted |
| 550 | + assert_eq!(rrstore_data_vec.len(), 1); |
| 551 | + } |
| 552 | + } |
| 553 | + |
| 554 | + } |
| 555 | + |
| 556 | + |
501 | 557 | } |
0 commit comments