| 1 | module dtm |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | const temp_dtm_dir = 'dynamic_template_manager_test' |
| 6 | const temp_templates_dir = 'templates' |
| 7 | const temp_html_fp = 'temp.html' |
| 8 | const temp_html_n = 'temp' |
| 9 | const vtmp_dir = os.vtmp_dir() |
| 10 | |
| 11 | fn dtm_test_root_dir() string { |
| 12 | return os.join_path(vtmp_dir, '${temp_dtm_dir}_${os.getpid()}') |
| 13 | } |
| 14 | |
| 15 | fn testsuite_begin() { |
| 16 | temp_folder := dtm_test_root_dir() |
| 17 | os.rmdir_all(temp_folder) or {} |
| 18 | os.mkdir_all(temp_folder)! |
| 19 | |
| 20 | templates_path := os.join_path(temp_folder, temp_templates_dir) |
| 21 | |
| 22 | os.mkdir_all(templates_path)! |
| 23 | |
| 24 | temp_html_file := os.join_path(templates_path, temp_html_fp) |
| 25 | |
| 26 | html_content := ' |
| 27 | <!DOCTYPE html> |
| 28 | <html> |
| 29 | <head> |
| 30 | <title>TEST</title> |
| 31 | </head> |
| 32 | <body> |
| 33 | <div> |
| 34 | <H1>TEST</H1> |
| 35 | </div> |
| 36 | </body> |
| 37 | </html>' |
| 38 | |
| 39 | os.write_file(temp_html_file, html_content)! |
| 40 | } |
| 41 | |
| 42 | fn test_initialize_dtm() { |
| 43 | dtmi := init_dtm(false, 0) |
| 44 | assert dtmi.dtm_init_is_ok == true |
| 45 | } |
| 46 | |
| 47 | fn test_create_template_cache_and_display() { |
| 48 | mut dtmi := init_dtm(false, 0) |
| 49 | temp_html_file := os.join_path(dtmi.template_folder, temp_html_fp) |
| 50 | html_last_mod := os.file_last_mod_unix(temp_html_file) |
| 51 | c_time := get_current_unix_micro_timestamp() |
| 52 | cache_delay_exp := i64(500) * i64(1000000) |
| 53 | placeholder := map[string]DtmMultiTypeMap{} |
| 54 | content_checksum := '' |
| 55 | html := dtmi.create_template_cache_and_display(.new, html_last_mod, c_time, temp_html_file, |
| 56 | temp_html_n, cache_delay_exp, &placeholder, content_checksum, TemplateType.html) |
| 57 | |
| 58 | assert html.len > 10 |
| 59 | } |
| 60 | |
| 61 | fn test_return_cache_info_isexistent() { |
| 62 | mut dtmi := init_dtm(false, 0) |
| 63 | path_template := os.join_path(dtmi.template_folder, temp_html_fp) |
| 64 | lock dtmi.template_caches { |
| 65 | dtmi.template_caches << TemplateCache{ |
| 66 | id: 1 |
| 67 | path: path_template |
| 68 | } |
| 69 | } |
| 70 | lock dtmi.nbr_of_remaining_template_request { |
| 71 | dtmi.nbr_of_remaining_template_request << RemainingTemplateRequest{ |
| 72 | id: 1 |
| 73 | } |
| 74 | } |
| 75 | cache_exists, _, _, _, _, _, _ := dtmi.return_cache_info_isexistent(path_template) |
| 76 | assert cache_exists == true |
| 77 | lock dtmi.template_caches { |
| 78 | dtmi.template_caches[0].id_redirection = 2 |
| 79 | dtmi.template_caches << TemplateCache{ |
| 80 | id: 2 |
| 81 | path: path_template |
| 82 | id_redirection: 3 |
| 83 | } |
| 84 | dtmi.template_caches << TemplateCache{ |
| 85 | id: 3 |
| 86 | path: path_template |
| 87 | id_redirection: 4 |
| 88 | } |
| 89 | dtmi.template_caches << TemplateCache{ |
| 90 | id: 4 |
| 91 | path: path_template |
| 92 | id_redirection: 5 |
| 93 | } |
| 94 | dtmi.template_caches << TemplateCache{ |
| 95 | id: 5 |
| 96 | path: path_template |
| 97 | } |
| 98 | } |
| 99 | lock dtmi.nbr_of_remaining_template_request { |
| 100 | dtmi.nbr_of_remaining_template_request << RemainingTemplateRequest{ |
| 101 | id: 2 |
| 102 | } |
| 103 | dtmi.nbr_of_remaining_template_request << RemainingTemplateRequest{ |
| 104 | id: 3 |
| 105 | } |
| 106 | dtmi.nbr_of_remaining_template_request << RemainingTemplateRequest{ |
| 107 | id: 4 |
| 108 | } |
| 109 | dtmi.nbr_of_remaining_template_request << RemainingTemplateRequest{ |
| 110 | id: 5 |
| 111 | } |
| 112 | } |
| 113 | _, id, _, _, _, _, _ := dtmi.return_cache_info_isexistent(path_template) |
| 114 | assert id == 5 |
| 115 | } |
| 116 | |
| 117 | fn test_remaining_template_request() { |
| 118 | mut dtmi := init_dtm(false, 0) |
| 119 | |
| 120 | lock dtmi.nbr_of_remaining_template_request { |
| 121 | dtmi.nbr_of_remaining_template_request << RemainingTemplateRequest{ |
| 122 | id: 1 |
| 123 | } |
| 124 | } |
| 125 | dtmi.remaining_template_request(true, 1) |
| 126 | rlock dtmi.nbr_of_remaining_template_request { |
| 127 | assert dtmi.nbr_of_remaining_template_request[0].nbr_of_remaining_request == 1 |
| 128 | } |
| 129 | dtmi.remaining_template_request(true, 1) |
| 130 | rlock dtmi.nbr_of_remaining_template_request { |
| 131 | assert dtmi.nbr_of_remaining_template_request[0].nbr_of_remaining_request == 2 |
| 132 | } |
| 133 | dtmi.remaining_template_request(false, 1) |
| 134 | rlock dtmi.nbr_of_remaining_template_request { |
| 135 | assert dtmi.nbr_of_remaining_template_request[0].nbr_of_remaining_request == 1 |
| 136 | } |
| 137 | dtmi.remaining_template_request(false, 1) |
| 138 | rlock dtmi.nbr_of_remaining_template_request { |
| 139 | assert dtmi.nbr_of_remaining_template_request[0].nbr_of_remaining_request == 0 |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | fn test_check_tmpl_and_placeholders_size() { |
| 144 | mut dtmi := init_dtm(false, 0) |
| 145 | temp_html_file := os.join_path(dtmi.template_folder, temp_html_fp) |
| 146 | placeholders := map[string]DtmMultiTypeMap{} |
| 147 | |
| 148 | path, filename, content_checksum, tmpl_type := dtmi.check_tmpl_and_placeholders_size(temp_html_file, |
| 149 | &placeholders)! |
| 150 | |
| 151 | is_html := if tmpl_type == TemplateType.html { |
| 152 | true |
| 153 | } else { |
| 154 | false |
| 155 | } |
| 156 | assert path.len > 10 |
| 157 | assert filename.len > 3 |
| 158 | assert is_html == true |
| 159 | // assert content_checksum.len > 3 |
| 160 | } |
| 161 | |
| 162 | fn test_chandler_prevent_cache_duplicate_request() { |
| 163 | mut dtmi := init_dtm(false, 0) |
| 164 | temp_html_file := os.join_path(dtmi.template_folder, temp_html_fp) |
| 165 | |
| 166 | lock dtmi.template_caches { |
| 167 | dtmi.template_caches << TemplateCache{ |
| 168 | id: 1 |
| 169 | path: temp_html_file |
| 170 | cache_request: .new |
| 171 | } |
| 172 | dtmi.template_caches << TemplateCache{ |
| 173 | id: 2 |
| 174 | path: temp_html_file |
| 175 | cache_request: .update |
| 176 | last_template_mod: i64(1) |
| 177 | } |
| 178 | dtmi.template_caches << TemplateCache{ |
| 179 | id: 3 |
| 180 | path: temp_html_file |
| 181 | cache_request: .exp_update |
| 182 | last_template_mod: i64(1) |
| 183 | generate_at: i64(100) |
| 184 | } |
| 185 | dtmi.template_caches << TemplateCache{ |
| 186 | id: 4 |
| 187 | cache_request: .delete |
| 188 | } |
| 189 | } |
| 190 | new_cache := TemplateCache{ |
| 191 | id: 5 |
| 192 | path: temp_html_file |
| 193 | cache_request: .new |
| 194 | } |
| 195 | update_cache := TemplateCache{ |
| 196 | id: 6 |
| 197 | path: temp_html_file |
| 198 | cache_request: .update |
| 199 | last_template_mod: i64(1) |
| 200 | } |
| 201 | exp_update_cache := TemplateCache{ |
| 202 | id: 7 |
| 203 | path: temp_html_file |
| 204 | cache_request: .exp_update |
| 205 | last_template_mod: i64(1) |
| 206 | generate_at: i64(10) |
| 207 | cache_delay_expiration: i64(10) |
| 208 | } |
| 209 | delete_cache := TemplateCache{ |
| 210 | id: 4 |
| 211 | cache_request: .delete |
| 212 | } |
| 213 | mut is_duplicate := dtmi.chandler_prevent_cache_duplicate_request(&new_cache) |
| 214 | assert is_duplicate == true |
| 215 | is_duplicate = dtmi.chandler_prevent_cache_duplicate_request(&update_cache) |
| 216 | assert is_duplicate == true |
| 217 | is_duplicate = dtmi.chandler_prevent_cache_duplicate_request(&exp_update_cache) |
| 218 | assert is_duplicate == true |
| 219 | is_duplicate = dtmi.chandler_prevent_cache_duplicate_request(&delete_cache) |
| 220 | assert is_duplicate == false |
| 221 | |
| 222 | lock dtmi.template_caches { |
| 223 | dtmi.template_caches.delete(3) |
| 224 | } |
| 225 | |
| 226 | is_duplicate = dtmi.chandler_prevent_cache_duplicate_request(&delete_cache) |
| 227 | assert is_duplicate == true |
| 228 | } |
| 229 | |
| 230 | fn test_chandler_remaining_cache_template_used() { |
| 231 | mut dtmi := init_dtm(false, 0) |
| 232 | lock dtmi.nbr_of_remaining_template_request { |
| 233 | dtmi.nbr_of_remaining_template_request << RemainingTemplateRequest{ |
| 234 | id: 1 |
| 235 | nbr_of_remaining_request: 0 |
| 236 | } |
| 237 | dtmi.nbr_of_remaining_template_request << RemainingTemplateRequest{ |
| 238 | id: 2 |
| 239 | nbr_of_remaining_request: 1 |
| 240 | need_to_delete: true |
| 241 | } |
| 242 | dtmi.nbr_of_remaining_template_request << RemainingTemplateRequest{ |
| 243 | id: 3 |
| 244 | nbr_of_remaining_request: 0 |
| 245 | need_to_delete: true |
| 246 | } |
| 247 | } |
| 248 | mut can_delete := dtmi.chandler_remaining_cache_template_used(CacheRequest.update, 3, 3) |
| 249 | assert can_delete == true |
| 250 | can_delete = dtmi.chandler_remaining_cache_template_used(CacheRequest.update, 2, 2) |
| 251 | assert can_delete == false |
| 252 | can_delete = dtmi.chandler_remaining_cache_template_used(CacheRequest.delete, 1, 0) |
| 253 | assert can_delete == true |
| 254 | can_delete = dtmi.chandler_remaining_cache_template_used(CacheRequest.new, 4, 0) |
| 255 | assert can_delete == true |
| 256 | } |
| 257 | |
| 258 | fn test_parse_tmpl_file() { |
| 259 | mut dtmi := init_dtm(false, 0) |
| 260 | temp_folder := dtm_test_root_dir() |
| 261 | templates_path := os.join_path(temp_folder, temp_templates_dir) |
| 262 | temp_html_file := os.join_path(templates_path, temp_html_fp) |
| 263 | |
| 264 | mut placeholders := map[string]DtmMultiTypeMap{} |
| 265 | |
| 266 | is_compressed := true |
| 267 | html := dtmi.parse_tmpl_file(temp_html_file, temp_html_n, &placeholders, is_compressed, |
| 268 | TemplateType.html) |
| 269 | |
| 270 | assert html.len > 0 |
| 271 | } |
| 272 | |
| 273 | fn test_check_if_cache_delay_iscorrect() { |
| 274 | check_if_cache_delay_iscorrect(i64(300 * 1000000), temp_html_n) or { assert false } |
| 275 | |
| 276 | check_if_cache_delay_iscorrect(i64(-100), temp_html_n) or { return } |
| 277 | assert false |
| 278 | } |
| 279 | |
| 280 | fn test_cache_request_route() { |
| 281 | mut dtmi := init_dtm(false, 0) |
| 282 | mut is_cache_exist := true |
| 283 | mut cache_delay_expiration := i64(400) |
| 284 | mut last_template_mod := get_current_unix_micro_timestamp() |
| 285 | mut test_current_template_mod := last_template_mod |
| 286 | mut cache_del_exp := 300 |
| 287 | mut gen_at := last_template_mod |
| 288 | mut content_checksum := 'checksumtest1' |
| 289 | mut current_content_checksum := 'checksumtest2' |
| 290 | |
| 291 | mut request_type, _ := dtmi.cache_request_route(is_cache_exist, cache_delay_expiration, |
| 292 | last_template_mod, test_current_template_mod, cache_del_exp, gen_at, |
| 293 | get_current_unix_micro_timestamp(), content_checksum, current_content_checksum) |
| 294 | |
| 295 | assert request_type == CacheRequest.update |
| 296 | |
| 297 | current_content_checksum = 'checksumtest1' |
| 298 | |
| 299 | request_type, _ = dtmi.cache_request_route(is_cache_exist, cache_delay_expiration, |
| 300 | last_template_mod, test_current_template_mod, cache_del_exp, gen_at, |
| 301 | get_current_unix_micro_timestamp(), content_checksum, current_content_checksum) |
| 302 | |
| 303 | assert request_type == CacheRequest.cached |
| 304 | |
| 305 | gen_at = (last_template_mod - 500) |
| 306 | |
| 307 | request_type, _ = dtmi.cache_request_route(is_cache_exist, cache_delay_expiration, |
| 308 | last_template_mod, test_current_template_mod, cache_del_exp, gen_at, |
| 309 | get_current_unix_micro_timestamp(), content_checksum, current_content_checksum) |
| 310 | |
| 311 | assert request_type == CacheRequest.exp_update |
| 312 | |
| 313 | is_cache_exist = false |
| 314 | |
| 315 | request_type, _ = dtmi.cache_request_route(is_cache_exist, cache_delay_expiration, |
| 316 | last_template_mod, test_current_template_mod, cache_del_exp, gen_at, |
| 317 | get_current_unix_micro_timestamp(), content_checksum, current_content_checksum) |
| 318 | |
| 319 | assert request_type == CacheRequest.new |
| 320 | } |
| 321 | |
| 322 | fn testsuite_end() { |
| 323 | temp_folder := dtm_test_root_dir() |
| 324 | os.rmdir_all(temp_folder) or {} |
| 325 | } |
| 326 | |
| 327 | // Utilities function : |
| 328 | |
| 329 | fn init_dtm(b bool, m int) &DynamicTemplateManager { |
| 330 | temp_folder := dtm_test_root_dir() |
| 331 | templates_path := os.join_path(temp_folder, temp_templates_dir) |
| 332 | |
| 333 | init_params := DynamicTemplateManagerInitialisationParams{ |
| 334 | active_cache_server: b |
| 335 | max_size_data_in_mem: m |
| 336 | test_template_dir: templates_path |
| 337 | } |
| 338 | |
| 339 | dtm := initialize(init_params) |
| 340 | |
| 341 | return dtm |
| 342 | } |
| 343 | |