v2 / vlib / v / live / live_test_template.vv
62 lines · 55 sloc · 1.31 KB · d29ee738a8efbc1f01a6ae12c432fd869a520b1a
Raw
1module mymodule
2
3import time
4import os
5import v.live
6import math
7
8const some_constant = f64(5.5)
9
10fn append_to_file(fname string, s string) {
11 mut f := os.open_append(fname) or {
12 println('>>>> could not open file ${fname} for appending, err: ${err} ')
13 return
14 }
15 f.write_string(time.now().format_rfc3339_micro()) or {}
16 f.write_string(' || ') or {}
17 f.writeln(s) or {
18 println('>>>> could not write to ${fname}, err: ${err} ')
19 return
20 }
21 // info := live.info()
22 // f.writeln('>>> reloads: ${info.reloads} | ok reloads: ${info.reloads_ok}')
23 f.close()
24}
25
26fn myprintln(s string) {
27 append_to_file('#OUTPUT_FILE#', s)
28 println(s)
29 os.flush()
30}
31
32@[live]
33fn pmessage() string {
34 _ := some_constant * math.sin(2.0 * math.pi * f64(time.ticks() % 6000) / 6000)
35 return 'ORIGINAL'
36}
37
38const delay = 20
39
40pub fn mymain() {
41 mut info := live.info()
42 info.recheck_period_ms = 5
43 myprintln('START')
44 myprintln('DATE: ' + time.now().str())
45 pmessage()
46 pmessage()
47 max_cycles := os.getenv_opt('LIVE_CYCLES') or { '1' }.int()
48 // NB: 1000 * 20 = maximum of ~20s runtime
49 for i := 0; i < max_cycles; i++ {
50 s := pmessage()
51 myprintln(s)
52 append_to_file(os.resource_abs_path(s + '.txt'), s)
53 if s == 'STOP' {
54 break
55 }
56 time.sleep(delay * time.millisecond)
57 }
58 pmessage()
59 pmessage()
60 myprintln('DATE: ' + time.now().str())
61 myprintln('END')
62}
63