From 2cc3b74e19e6e30d122fed7db518a7e973245178 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 18 May 2022 11:30:28 +0300 Subject: [PATCH] tests: cleanup compiler_errors_test.v using language features (chan), instead of raw `sync.new_channel` and `unsafe { ch.push }` calls --- vlib/v/compiler_errors_test.v | 40 ++++++++++----------- vlib/v/tests/inout/push_work_on_channel.out | 15 ++++++++ vlib/v/tests/inout/push_work_on_channel.vv | 29 +++++++++++++++ 3 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 vlib/v/tests/inout/push_work_on_channel.out create mode 100644 vlib/v/tests/inout/push_work_on_channel.vv diff --git a/vlib/v/compiler_errors_test.v b/vlib/v/compiler_errors_test.v index fd82bdc685..1c58898952 100644 --- a/vlib/v/compiler_errors_test.v +++ b/vlib/v/compiler_errors_test.v @@ -4,7 +4,6 @@ import term import v.util.diff import v.util.vtest import time -import sync import runtime import benchmark @@ -16,8 +15,12 @@ const skip_on_ubuntu_musl = [ 'vlib/v/checker/tests/vweb_tmpl_used_var.vv', ] +const vexe = os.getenv('VEXE') + const turn_off_vcolors = os.setenv('VCOLORS', 'never', true) +const show_cmd = os.getenv('VTEST_SHOW_CMD') != '' + // This is needed, because some of the .vv files are tests, and we do need stable // output from them, that can be compared against their .out files: const turn_on_normal_test_runner = os.setenv('VTEST_RUNNER', 'normal', true) @@ -26,6 +29,8 @@ const should_autofix = os.getenv('VAUTOFIX') != '' const github_job = os.getenv('GITHUB_JOB') +const v_ci_ubuntu_musl = os.getenv('V_CI_UBUNTU_MUSL').len > 0 + struct TaskDescription { vexe string evars string @@ -54,7 +59,6 @@ mut: } fn test_all() { - vexe := os.getenv('VEXE') vroot := os.dir(vexe) os.chdir(vroot) or {} checker_dir := 'vlib/v/checker/tests' @@ -162,15 +166,11 @@ fn (mut tasks Tasks) add(custom_vexe string, dir string, voptions string, result } fn (mut tasks Tasks) add_evars(evars string, custom_vexe string, dir string, voptions string, result_extension string, tests []string, is_module bool) { - mut vexe := tasks.vexe - if custom_vexe != '' { - vexe = custom_vexe - } paths := vtest.filter_vtest_only(tests, basepath: dir) for path in paths { tasks.all << TaskDescription{ evars: evars - vexe: vexe + vexe: if custom_vexe != '' { custom_vexe } else { tasks.vexe } dir: dir voptions: voptions result_extension: result_extension @@ -186,14 +186,17 @@ fn bstep_message(mut bench benchmark.Benchmark, label string, msg string, sdurat // process an array of tasks in parallel, using no more than vjobs worker threads fn (mut tasks Tasks) run() { - tasks.show_cmd = os.getenv('VTEST_SHOW_CMD') != '' + if tasks.all.len == 0 { + return + } + tasks.show_cmd = show_cmd vjobs := if tasks.parallel_jobs > 0 { tasks.parallel_jobs } else { runtime.nr_jobs() } mut bench := benchmark.new_benchmark() bench.set_total_expected_steps(tasks.all.len) - mut work := sync.new_channel(u32(tasks.all.len)) - mut results := sync.new_channel(u32(tasks.all.len)) + mut work := chan TaskDescription{cap: tasks.all.len} + mut results := chan TaskDescription{cap: tasks.all.len} mut m_skip_files := skip_files.clone() - if os.getenv('V_CI_UBUNTU_MUSL').len > 0 { + if v_ci_ubuntu_musl { m_skip_files << skip_on_ubuntu_musl } $if noskip ? { @@ -220,11 +223,11 @@ fn (mut tasks Tasks) run() { if tasks.all[i].path in m_skip_files { tasks.all[i].is_skipped = true } - unsafe { work.push(&tasks.all[i]) } + work <- tasks.all[i] } work.close() for _ in 0 .. vjobs { - go work_processor(mut work, mut results) + go work_processor(work, results) } if github_job == '' { println('') @@ -233,7 +236,7 @@ fn (mut tasks Tasks) run() { mut total_errors := 0 for _ in 0 .. tasks.all.len { mut task := TaskDescription{} - results.pop(&task) + task = <-results bench.step() if task.is_skipped { bench.skip() @@ -285,16 +288,13 @@ fn (mut tasks Tasks) run() { // a single worker thread spends its time getting work from the `work` channel, // processing the task, and then putting the task in the `results` channel -fn work_processor(mut work sync.Channel, mut results sync.Channel) { +fn work_processor(work chan TaskDescription, results chan TaskDescription) { for { - mut task := TaskDescription{} - if !work.pop(&task) { - break - } + mut task := <-work or { break } sw := time.new_stopwatch() task.execute() task.took = sw.elapsed() - results.push(&task) + results <- task } } diff --git a/vlib/v/tests/inout/push_work_on_channel.out b/vlib/v/tests/inout/push_work_on_channel.out new file mode 100644 index 0000000000..811762cb94 --- /dev/null +++ b/vlib/v/tests/inout/push_work_on_channel.out @@ -0,0 +1,15 @@ +> main start +> work started +> main ch.close called +> work x: 0 +> work x: 1 +> work x: 2 +> work x: 3 +> work x: 4 +> work x: 5 +> work x: 6 +> work x: 7 +> work x: 8 +> work x: 9 +> work ended +> main task was finished diff --git a/vlib/v/tests/inout/push_work_on_channel.vv b/vlib/v/tests/inout/push_work_on_channel.vv new file mode 100644 index 0000000000..9ef86fc75a --- /dev/null +++ b/vlib/v/tests/inout/push_work_on_channel.vv @@ -0,0 +1,29 @@ +import time + +fn work(input chan u32, started chan bool) { + println('> work started') + started <- true + for { + x := <-input or { break } + println('> work x: $x') + time.sleep(50 * time.millisecond) + } + println('> work ended') +} + +fn main() { + println('> main start') + ch := chan u32{cap: 100} + work_started := chan bool{} + for x in 0 .. 10 { + ch <- x + } + task := go work(ch, work_started) + _ := <-work_started + + ch.close() + println('> main ch.close called') + + task.wait() + println('> main task was finished') +}