tests: simplify the synchronized printing of results, using chan string

pull/6651/head
Delyan Angelov 2020-10-18 18:16:33 +03:00
parent cc477c50ed
commit a9659beabe
1 changed files with 23 additions and 39 deletions

View File

@ -8,14 +8,6 @@ import sync
import v.pref import v.pref
import v.util.vtest import v.util.vtest
pub struct TestMessageHandler {
mut:
messages []string
pub mut:
message_idx int
mtx &sync.Mutex
}
pub struct TestSession { pub struct TestSession {
pub mut: pub mut:
files []string files []string
@ -26,14 +18,24 @@ pub mut:
failed bool failed bool
benchmark benchmark.Benchmark benchmark benchmark.Benchmark
show_ok_tests bool show_ok_tests bool
message_handler &TestMessageHandler
root_relative bool // used by CI runs, so that the output is stable everywhere root_relative bool // used by CI runs, so that the output is stable everywhere
nmessages chan string // many publishers, single consumer/printer
nmessage_idx int // currently printed message index
} }
pub fn (mut mh TestMessageHandler) append_message(msg string) { pub fn (mut ts TestSession) append_message(msg string) {
mh.mtx.m_lock() ts.nmessages <- msg
mh.messages << msg }
mh.mtx.unlock()
pub fn (mut ts TestSession) print_messages() {
for {
mut nm := <- ts.nmessages
ts.nmessage_idx++
msg := nm.replace("TMP1", "${ts.nmessage_idx:1d}").
replace("TMP2", "${ts.nmessage_idx:2d}").
replace("TMP3", "${ts.nmessage_idx:3d}")
eprintln(msg)
}
} }
pub fn new_test_session(_vargs string) TestSession { pub fn new_test_session(_vargs string) TestSession {
@ -53,7 +55,6 @@ pub fn new_test_session(_vargs string) TestSession {
skip_files: skip_files skip_files: skip_files
vargs: vargs vargs: vargs
show_ok_tests: !_vargs.contains('-silent') show_ok_tests: !_vargs.contains('-silent')
message_handler: &TestMessageHandler(0)
} }
} }
@ -105,9 +106,9 @@ pub fn (mut ts TestSession) test() {
callback: worker_trunner callback: worker_trunner
}) })
// for handling messages across threads // for handling messages across threads
ts.message_handler = &TestMessageHandler{ ts.nmessages = chan string{cap: 10000}
mtx: sync.new_mutex() ts.nmessage_idx = 0
} go ts.print_messages()
pool_of_test_runners.set_shared_context(ts) pool_of_test_runners.set_shared_context(ts)
pool_of_test_runners.work_on_pointers(remaining_files.pointers()) pool_of_test_runners.work_on_pointers(remaining_files.pointers())
ts.benchmark.stop() ts.benchmark.stop()
@ -118,25 +119,8 @@ pub fn (mut ts TestSession) test() {
} }
} }
pub fn (mut m TestMessageHandler) display_message() {
m.mtx.m_lock()
defer {
m.messages.clear()
m.mtx.unlock()
}
for msg in m.messages {
m.message_idx++
eprintln(msg.
replace("TMP1", "${m.message_idx:1d}").
replace("TMP2", "${m.message_idx:2d}").
replace("TMP3", "${m.message_idx:3d}")
)
}
}
fn worker_trunner(mut p sync.PoolProcessor, idx int, thread_id int) voidptr { fn worker_trunner(mut p sync.PoolProcessor, idx int, thread_id int) voidptr {
mut ts := &TestSession(p.get_shared_context()) mut ts := &TestSession(p.get_shared_context())
defer { ts.message_handler.display_message() }
tmpd := os.temp_dir() tmpd := os.temp_dir()
show_stats := '-stats' in ts.vargs.split(' ') show_stats := '-stats' in ts.vargs.split(' ')
// tls_bench is used to format the step messages/timings // tls_bench is used to format the step messages/timings
@ -172,11 +156,11 @@ fn worker_trunner(mut p sync.PoolProcessor, idx int, thread_id int) voidptr {
if relative_file.replace('\\', '/') in ts.skip_files { if relative_file.replace('\\', '/') in ts.skip_files {
ts.benchmark.skip() ts.benchmark.skip()
tls_bench.skip() tls_bench.skip()
ts.message_handler.append_message(tls_bench.step_message_skip(relative_file)) ts.append_message(tls_bench.step_message_skip(relative_file))
return sync.no_result return sync.no_result
} }
if show_stats { if show_stats {
ts.message_handler.append_message(term.h_divider('-')) ts.append_message(term.h_divider('-'))
status := os.system(cmd) status := os.system(cmd)
if status == 0 { if status == 0 {
ts.benchmark.ok() ts.benchmark.ok()
@ -194,20 +178,20 @@ fn worker_trunner(mut p sync.PoolProcessor, idx int, thread_id int) voidptr {
ts.failed = true ts.failed = true
ts.benchmark.fail() ts.benchmark.fail()
tls_bench.fail() tls_bench.fail()
ts.message_handler.append_message(tls_bench.step_message_fail(relative_file)) ts.append_message(tls_bench.step_message_fail(relative_file))
return sync.no_result return sync.no_result
} }
if r.exit_code != 0 { if r.exit_code != 0 {
ts.failed = true ts.failed = true
ts.benchmark.fail() ts.benchmark.fail()
tls_bench.fail() tls_bench.fail()
ts.message_handler.append_message(tls_bench.step_message_fail('${relative_file}\n$r.output\n')) ts.append_message(tls_bench.step_message_fail('${relative_file}\n$r.output\n'))
} }
else { else {
ts.benchmark.ok() ts.benchmark.ok()
tls_bench.ok() tls_bench.ok()
if ts.show_ok_tests { if ts.show_ok_tests {
ts.message_handler.append_message(tls_bench.step_message_ok(relative_file)) ts.append_message(tls_bench.step_message_ok(relative_file))
} }
} }
} }