1+ // vtest retry: 4
12import os
3+ import log
24import time
35
4- // vtest retry: 4
5-
66/*
77The goal of this test, is to simulate a developer, that has run a program, compiled with -live flag.
88
@@ -62,12 +62,8 @@ fn testsuite_begin() {
6262 os.mkdir_all (vtmp_folder) or {}
6363 os.mkdir_all (os.join_path (vtmp_folder, 'mymodule' ))!
6464 os.write_file (os.join_path (vtmp_folder, 'v.mod' ), '' )!
65- os.write_file (os.join_path (vtmp_folder, 'main.v' ), '
66- import mymodule
67- fn main() {
68- mymodule.mymain()
69- }
70- ' )!
65+ os.cp (os.join_path (os.dir (@FILE), 'live_test_template_main.vv' ), os.join_path (vtmp_folder,
66+ 'main.v' ))!
7167 if os.user_os () ! in ['linux' , 'solaris' ] && os.getenv ('FORCE_LIVE_TEST' ).len == 0 {
7268 eprintln ('Testing the runtime behaviour of -live mode,' )
7369 eprintln ('is reliable only on Linux/macOS for now.' )
@@ -93,15 +89,18 @@ fn watchdog() {
9389 sw := time.new_stopwatch ()
9490 for {
9591 elapsed_time_in_seconds := sw.elapsed ().seconds ()
96- dump (elapsed_time_in_seconds)
92+ $if print_watchdog_time ? {
93+ log.warn ('> dt: ${elapsed_time_in_seconds :6.3 f }s' )
94+ }
9795 if elapsed_time_in_seconds > 5 * 60 {
96+ log.warn ('> watchdog triggered, elapsed time: ${elapsed_time_in_seconds :6.3 f }s' )
9897 exit (3 )
9998 }
10099 time.sleep (1 * time.second)
101100 }
102101}
103102
104- @[debuglivetest]
103+ @[if debuglivetest ? ]
105104fn vprintln (s string ) {
106105 eprintln (s)
107106}
@@ -115,8 +114,9 @@ fn testsuite_end() {
115114 panic ('could not read ${output_file }, error: ${err }' )
116115 }
117116 mut histogram := map [string ]int {}
118- for line in output_lines {
119- histogram[line] = histogram[line] + 1
117+ for oline in output_lines {
118+ line := oline.all_after ('|| ' )
119+ histogram[line]++
120120 }
121121 for k, v in histogram {
122122 eprintln ('> found ${v :5 d } times: ${k }' )
@@ -126,50 +126,49 @@ fn testsuite_end() {
126126 assert histogram['ORIGINAL' ] > 0
127127 assert histogram['CHANGED' ] + histogram['ANOTHER' ] > 0
128128 // assert histogram['END'] > 0
129- os.rmdir_all (vtmp_folder) or {}
129+ $if ! keep_results ? {
130+ os.rmdir_all (vtmp_folder) or {}
131+ log.info ('Removed ${vtmp_folder } . Use `-d keep_results` to override.' )
132+ }
130133}
131134
132135fn change_source (new string ) {
133- time.sleep (100 * time.millisecond)
134- vprintln ('> change ORIGINAL to: ${new }' )
136+ log.info ('> change ORIGINAL to: ${new } ...' )
135137 atomic_write_source (live_program_source.replace ('ORIGINAL' , new))
136138 wait_for_file (new)
137139}
138140
139141fn wait_for_file (new string ) {
140- time.sleep (100 * time.millisecond)
141142 expected_file := os.join_path (vtmp_folder, new + '.txt' )
142- eprintln ('waiting for ${expected_file } ...' )
143- // os.system('tree $vtmp_folder')
144143 max_wait_cycles := os.getenv_opt ('WAIT_CYCLES' ) or { '1' }.int ()
144+ log.info ('waiting max_wait_cycles: ${max_wait_cycles } for file: ${expected_file } ...' )
145+ mut sw := time.new_stopwatch ()
145146 for i := 0 ; i < = max_wait_cycles; i++ {
146- if i % 25 == 0 {
147- vprintln (' checking ${i :- 10 d } for ${expected_file } ...' )
147+ if i > 0 && i % 500 == 0 {
148+ log. info (' checking ${i :3 d }/${ max_wait_cycles : - 3 d }, waited for: ${ sw . elapsed (). seconds (): 6.3 f }s, for ${expected_file } ...' )
148149 }
149150 if os.exists (expected_file) {
150151 assert true
151- vprintln ('> done. ' )
152- time.sleep (100 * time.millisecond)
152+ log. info ('> done waiting for ${ expected_file }, iteration: ${ i : 3 d }, waited for: ${ sw . elapsed (). seconds (): 6.3 f }s ' )
153+ time.sleep (80 * time.millisecond)
153154 break
154155 }
155- time.sleep (5 * time.millisecond)
156+ time.sleep (1 * time.millisecond)
156157 }
157158}
158159
159160fn setup_cycles_environment () {
160- mut max_live_cycles := 1000
161- mut max_wait_cycles := 400
162- if os.user_os () == 'macos' {
163- // max_live_cycles *= 5
164- // max_wait_cycles *= 5
165- }
161+ mut max_live_cycles := 1000 // read by live_test_template.vv
162+ mut max_wait_cycles := 5000
166163 os.setenv ('LIVE_CYCLES' , '${max_live_cycles }' , true )
167164 os.setenv ('WAIT_CYCLES' , '${max_wait_cycles }' , true )
168165}
169166
170167fn run_in_background (cmd string ) {
168+ log.warn ('running in background: ${cmd } ...' )
171169 spawn fn (cmd string ) {
172170 res := os.execute (cmd)
171+ log.warn ('Background cmd ended. res.exit_code: ${res .exit_code } | res.output.len: ${res .output .len }' )
173172 if res.exit_code != 0 {
174173 eprintln ('----------------------- background command failed: --------------------------' )
175174 eprintln ('----- exit_code: ${res .exit_code }, cmd: ${cmd }, output:' )
@@ -178,34 +177,37 @@ fn run_in_background(cmd string) {
178177 }
179178 assert res.exit_code == 0
180179 }(cmd)
181- time.sleep (1000 * time.millisecond)
182- eprintln ('... run_in_background, cmd: ${cmd }' )
180+ log.warn ('the live program should be running in the background now' )
183181}
184182
185183fn test_live_program_can_be_compiled () {
186184 setup_cycles_environment ()
187- eprintln ('Compiling...' )
188185 compile_cmd := '${os .quoted_path (vexe )} -cg -keepc -nocolor -live -o ${os .quoted_path (genexe_file )} ${os .quoted_path (main_source_file )}'
189- eprintln ( '> compile_cmd: ${ compile_cmd } ' )
190- time. sleep ( 1000 * time.millisecond) // improve chances of working on windows
186+ log. info ( 'Compiling with compile_cmd: ' )
187+ eprintln ( '> ${ compile_cmd }' )
191188 compile_res := os.system (compile_cmd)
189+ log.info ('> DONE' )
192190 assert compile_res == 0
193191 run_in_background ('${os .quoted_path (genexe_file )}' )
194192 wait_for_file ('ORIGINAL' )
195193}
196194
197195fn test_live_program_can_be_changed_1 () {
198196 change_source ('CHANGED' )
197+ time.sleep (250 * time.millisecond)
199198 assert true
200199}
201200
202201fn test_live_program_can_be_changed_2 () {
203202 change_source ('ANOTHER' )
203+ time.sleep (250 * time.millisecond)
204204 assert true
205205}
206206
207207fn test_live_program_can_be_changed_3 () {
208+ time.sleep (500 * time.millisecond)
208209 change_source ('STOP' )
210+ time.sleep (250 * time.millisecond)
209211 change_source ('STOP' )
210212 change_source ('STOP' )
211213 assert true
0 commit comments