@@ -945,4 +945,86 @@ mod cache_data_test{
945
945
}
946
946
}
947
947
948
+ #[ test]
949
+ //this test is going to prove if the cleaning after the timeout is acting correctly one layer down (CacheByDomain)
950
+ // ------BEFORE THE 5 SECONDS-----
951
+ // RTYPE:A -> {uchile (invalid) -> [..], example.com (valid) -> [..]}
952
+ // RTYPE:NS -> {uchile (valid) -> [..], example.com (invalid) -> [...]}
953
+ //-------AFTER THE 5 SECONDS-----
954
+ // RTYPE:A -> {example.com -> [...]}
955
+ // RTYPE:NS -> {uchile.com -> [...]}
956
+ fn filter_timout_cache_data_cleaning_layer_down ( ) {
957
+ use std:: { thread, time} ;
958
+ let mut cache_record_type = CacheByRecordType :: new ( ) ;
959
+ //Defaults Rdatas to use
960
+ let a_rdata = Rdata :: A ( ARdata :: new ( ) ) ;
961
+ let ns_rdata = Rdata :: NS ( NsRdata :: new ( ) ) ;
962
+
963
+
964
+ let mut domain_name_1 = DomainName :: new ( ) ;
965
+ domain_name_1. set_name ( String :: from ( "example.com" ) ) ;
966
+
967
+ let mut domain_name_2 = DomainName :: new ( ) ;
968
+ domain_name_2. set_name ( String :: from ( "uchile.cl" ) ) ;
969
+
970
+ //adding in A rtypes
971
+ let mut resource_record_valid_a = ResourceRecord :: new ( a_rdata. clone ( ) ) ;
972
+ resource_record_valid_a. set_ttl ( 1000 ) ;
973
+ let rr_cache_valid_a = RRStoredData :: new ( resource_record_valid_a. clone ( ) ) ;
974
+ cache_record_type. add_to_cache_data ( Rtype :: A , domain_name_1. clone ( ) , rr_cache_valid_a) ;
975
+
976
+ let mut resource_record_invalid_a = ResourceRecord :: new ( a_rdata. clone ( ) ) ;
977
+ resource_record_invalid_a. set_ttl ( 4 ) ;
978
+ let rr_cache_invalid_a = RRStoredData :: new ( resource_record_invalid_a. clone ( ) ) ;
979
+ cache_record_type. add_to_cache_data ( Rtype :: A , domain_name_2. clone ( ) , rr_cache_invalid_a) ;
980
+
981
+ //adding in NS rtypes
982
+ let mut resource_record_valid_ns = ResourceRecord :: new ( ns_rdata. clone ( ) ) ;
983
+ resource_record_valid_ns. set_ttl ( 1000 ) ;
984
+ let rr_cache_valid_ns = RRStoredData :: new ( resource_record_valid_ns. clone ( ) ) ;
985
+ cache_record_type. add_to_cache_data ( Rtype :: NS , domain_name_2. clone ( ) , rr_cache_valid_ns) ;
986
+
987
+ let mut resource_record_invalid_ns = ResourceRecord :: new ( ns_rdata. clone ( ) ) ;
988
+ resource_record_invalid_ns. set_ttl ( 4 ) ;
989
+ let rr_cache_invalid_ns = RRStoredData :: new ( resource_record_invalid_ns. clone ( ) ) ;
990
+ cache_record_type. add_to_cache_data ( Rtype :: NS , domain_name_1. clone ( ) , rr_cache_invalid_ns) ;
991
+
992
+
993
+ //check if every record_types_data (HashMap for A and for NS) has 2 element
994
+ let record_types_data = cache_record_type. get_cache_data ( ) ;
995
+ //CacheByDomainName for A type
996
+ if let Some ( record_types_data_a) = record_types_data. get ( & Rtype :: A ) {
997
+ // println!("the cache by domain for A type is : \n {:?}",record_types_data_a.get_domain_names_data());
998
+ assert_eq ! ( record_types_data_a. get_domain_names_data( ) . len( ) , 2 ) ;
999
+ }
1000
+ //CacheByDomainName for NS type
1001
+ if let Some ( record_types_data_ns) = record_types_data. get ( & Rtype :: NS ) {
1002
+ // println!("the cache by domain for NS type is : \n {:?}",record_types_data_ns.get_domain_names_data());
1003
+ assert_eq ! ( record_types_data_ns. get_domain_names_data( ) . len( ) , 2 ) ;
1004
+ }
1005
+
1006
+ println ! ( "Before timeout: {:?}" , Utc :: now( ) ) ;
1007
+ thread:: sleep ( time:: Duration :: from_secs ( 5 ) ) ;
1008
+ println ! ( "After timeout: {:?}" , Utc :: now( ) ) ;
1009
+ cache_record_type. filter_timeout_cache_data ( ) ;
1010
+
1011
+ let record_types_data_after_cleaning = cache_record_type. get_cache_data ( ) ;
1012
+
1013
+ //after the cleaning, each cache shoud have 1 element
1014
+ if let Some ( record_types_data_a) = record_types_data_after_cleaning. get ( & Rtype :: A ) {
1015
+ println ! ( "the cache by domain for A type after the cleaning is : \n {:?}" , record_types_data_a. get_domain_names_data( ) ) ;
1016
+ //FIXME: Does not delete the invadil rrstore, instead points to a empty array (same error as in cache by domain)
1017
+ assert_eq ! ( record_types_data_a. get_domain_names_data( ) . len( ) , 1 ) ;
1018
+ }
1019
+ //CacheByDomainName for NS type
1020
+ if let Some ( record_types_data_ns) = record_types_data_after_cleaning. get ( & Rtype :: NS ) {
1021
+ println ! ( "the cache by domain for NS type after the cleaning is : \n {:?}" , record_types_data_ns. get_domain_names_data( ) ) ;
1022
+ //FIXME: Does not delete the invadil rrstore, instead points to a empty array (same error as in cache by domain)
1023
+ assert_eq ! ( record_types_data_ns. get_domain_names_data( ) . len( ) , 1 ) ;
1024
+ }
1025
+
1026
+
1027
+ }
1028
+
1029
+
948
1030
}
0 commit comments