v2 / vlib / x / templating / dtm / dynamic_template_manager_cache_system_test.v
176 lines · 152 sloc · 4.35 KB · 3eff1b83cf719199d0ff6f63524da63c9294ffd5
Raw
1// vtest retry: 3
2module dtm
3
4import os
5import time
6
7const temp_dtm_dir = 'dynamic_template_manager_cache_system_test'
8const temp_cache_dir = 'vcache_dtm'
9const temp_templates_dir = 'templates'
10const temp_html_fp = 'temp.html'
11const temp_html_n = 'temp'
12const vtmp_dir = os.vtmp_dir()
13
14fn testsuite_begin() {
15 temp_folder := os.join_path(vtmp_dir, temp_dtm_dir)
16 os.mkdir_all(temp_folder)!
17
18 vcache_path := os.join_path(temp_folder, temp_cache_dir)
19 templates_path := os.join_path(temp_folder, temp_templates_dir)
20
21 os.mkdir_all(vcache_path)!
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
42fn test_check_and_clear_cache_files() {
43 dtmi := init_dtm(false, 0)
44 check_and_clear_cache_files(dtmi.template_cache_folder)!
45
46 count_cache_files := os.ls(dtmi.template_cache_folder)!
47 assert count_cache_files.len == 0
48}
49
50fn test_create_template_cache_and_display() {
51 mut dtmi := init_dtm(true, max_size_data_in_memory)
52 defer {
53 dtmi.stop_cache_handler()
54 }
55 html := dtmi.create_cache()
56 assert html.len > 10
57}
58
59fn test_get_cache() {
60 mut dtmi := init_dtm(true, max_size_data_in_memory)
61 dtmi.create_cache()
62 defer {
63 dtmi.stop_cache_handler()
64 }
65 dtm_placeholders := map[string]DtmMultiTypeMap{}
66 temp_html_file := os.join_path(dtmi.template_folder, temp_html_fp)
67 html_mem := dtmi.get_cache(temp_html_n, temp_html_file, &dtm_placeholders)
68 assert html_mem.len > 10
69}
70
71fn test_chandler_clear_specific_cache() {
72 mut dtmi := init_dtm(true, 0)
73 defer {
74 dtmi.stop_cache_handler()
75 }
76 dtmi.create_cache()
77 lock dtmi.template_caches {
78 cache_file := os.join_path(dtmi.template_cache_folder,
79 '${dtmi.template_caches[0].name}_${dtmi.template_caches[0].checksum}.cache')
80 index, is_success := dtmi.chandler_clear_specific_cache(dtmi.template_caches[0].id)
81 assert is_success == true
82 assert index == 0
83 cache_exist := os.exists(cache_file)
84 assert cache_exist == false
85 }
86}
87
88fn test_cache_timestamp_is_initialized() {
89 mut dtmi := init_dtm(true, max_size_data_in_memory)
90 defer {
91 dtmi.stop_cache_handler()
92 }
93 date_to_str := dtmi.c_time.str()
94 assert date_to_str.len > 10
95}
96
97fn test_process_rendered_cache_delete_request() {
98 mut dtmi := init_dtm(true, max_size_data_in_memory)
99 defer {
100 dtmi.stop_cache_handler()
101 }
102 dtmi.create_cache()
103 path_f := os.join_path(dtmi.template_folder, temp_html_fp)
104 lock dtmi.template_caches {
105 assert dtmi.template_caches[0].id == 1
106 assert dtmi.template_caches[0].name == temp_html_n
107 assert dtmi.template_caches[0].path == path_f
108 }
109 dtmi.id_to_handlered = 1
110 dtmi.ch_cache_handler <- TemplateCache{
111 id: 1
112 cache_request: .delete
113 }
114 dtmi.sync_cache()
115 rlock dtmi.template_caches {
116 assert dtmi.template_caches.len == 0
117 }
118}
119
120fn testsuite_end() {
121 temp_folder := os.join_path(vtmp_dir, temp_dtm_dir)
122 os.rmdir_all(temp_folder) or {}
123}
124
125// Utilities function :
126
127fn init_dtm(b bool, m int) &DynamicTemplateManager {
128 temp_folder := os.join_path(vtmp_dir, temp_dtm_dir)
129 vcache_path := os.join_path(temp_folder, temp_cache_dir)
130 templates_path := os.join_path(temp_folder, temp_templates_dir)
131
132 init_params := DynamicTemplateManagerInitialisationParams{
133 active_cache_server: b
134 max_size_data_in_mem: m
135 test_cache_dir: vcache_path
136 test_template_dir: templates_path
137 }
138
139 dtm := initialize(init_params)
140
141 return dtm
142}
143
144fn (mut tm DynamicTemplateManager) create_cache() string {
145 temp_html_file := os.join_path(tm.template_folder, temp_html_fp)
146 html_last_mod := os.file_last_mod_unix(temp_html_file)
147 c_time := get_current_unix_micro_timestamp()
148 cache_delay_exp := i64(500) * i64(1000000)
149 placeholder := map[string]DtmMultiTypeMap{}
150 content_checksum := ''
151 html := tm.create_template_cache_and_display(.new, html_last_mod, c_time, temp_html_file,
152 temp_html_n, cache_delay_exp, &placeholder, content_checksum, TemplateType.html)
153 tm.sync_cache()
154
155 return html
156}
157
158fn (mut tm DynamicTemplateManager) sync_cache() {
159 mut count := 0
160 for {
161 select {
162 _ := <-tm.is_ready {
163 break
164 }
165 else {
166 time.sleep(50 * time.millisecond)
167 // Wait a total of 2 seconds to check if the data cache is ready
168 if count >= 40 {
169 tm.abort_test = true
170 break
171 }
172 count++
173 }
174 }
175 }
176}
177