tests: cleanup compiler_errors_test.v using language features (chan), instead of raw `sync.new_channel` and `unsafe { ch.push }` calls

Delyan Angelov 2022-05-18 11:30:28 +03:00 committed by Jef Roosens
parent 21308289ad
commit 888c85dedc
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 64 additions and 20 deletions

View File

@ -4,7 +4,6 @@ import term
import v.util.diff import v.util.diff
import v.util.vtest import v.util.vtest
import time import time
import sync
import runtime import runtime
import benchmark import benchmark
@ -16,8 +15,12 @@ const skip_on_ubuntu_musl = [
'vlib/v/checker/tests/vweb_tmpl_used_var.vv', 'vlib/v/checker/tests/vweb_tmpl_used_var.vv',
] ]
const vexe = os.getenv('VEXE')
const turn_off_vcolors = os.setenv('VCOLORS', 'never', true) 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 // 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: // output from them, that can be compared against their .out files:
const turn_on_normal_test_runner = os.setenv('VTEST_RUNNER', 'normal', true) 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 github_job = os.getenv('GITHUB_JOB')
const v_ci_ubuntu_musl = os.getenv('V_CI_UBUNTU_MUSL').len > 0
struct TaskDescription { struct TaskDescription {
vexe string vexe string
evars string evars string
@ -54,7 +59,6 @@ mut:
} }
fn test_all() { fn test_all() {
vexe := os.getenv('VEXE')
vroot := os.dir(vexe) vroot := os.dir(vexe)
os.chdir(vroot) or {} os.chdir(vroot) or {}
checker_dir := 'vlib/v/checker/tests' 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) { 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) paths := vtest.filter_vtest_only(tests, basepath: dir)
for path in paths { for path in paths {
tasks.all << TaskDescription{ tasks.all << TaskDescription{
evars: evars evars: evars
vexe: vexe vexe: if custom_vexe != '' { custom_vexe } else { tasks.vexe }
dir: dir dir: dir
voptions: voptions voptions: voptions
result_extension: result_extension 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 // process an array of tasks in parallel, using no more than vjobs worker threads
fn (mut tasks Tasks) run() { 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() } vjobs := if tasks.parallel_jobs > 0 { tasks.parallel_jobs } else { runtime.nr_jobs() }
mut bench := benchmark.new_benchmark() mut bench := benchmark.new_benchmark()
bench.set_total_expected_steps(tasks.all.len) bench.set_total_expected_steps(tasks.all.len)
mut work := sync.new_channel<TaskDescription>(u32(tasks.all.len)) mut work := chan TaskDescription{cap: tasks.all.len}
mut results := sync.new_channel<TaskDescription>(u32(tasks.all.len)) mut results := chan TaskDescription{cap: tasks.all.len}
mut m_skip_files := skip_files.clone() 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 m_skip_files << skip_on_ubuntu_musl
} }
$if noskip ? { $if noskip ? {
@ -220,11 +223,11 @@ fn (mut tasks Tasks) run() {
if tasks.all[i].path in m_skip_files { if tasks.all[i].path in m_skip_files {
tasks.all[i].is_skipped = true tasks.all[i].is_skipped = true
} }
unsafe { work.push(&tasks.all[i]) } work <- tasks.all[i]
} }
work.close() work.close()
for _ in 0 .. vjobs { for _ in 0 .. vjobs {
go work_processor(mut work, mut results) go work_processor(work, results)
} }
if github_job == '' { if github_job == '' {
println('') println('')
@ -233,7 +236,7 @@ fn (mut tasks Tasks) run() {
mut total_errors := 0 mut total_errors := 0
for _ in 0 .. tasks.all.len { for _ in 0 .. tasks.all.len {
mut task := TaskDescription{} mut task := TaskDescription{}
results.pop(&task) task = <-results
bench.step() bench.step()
if task.is_skipped { if task.is_skipped {
bench.skip() bench.skip()
@ -285,16 +288,13 @@ fn (mut tasks Tasks) run() {
// a single worker thread spends its time getting work from the `work` channel, // 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 // 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 { for {
mut task := TaskDescription{} mut task := <-work or { break }
if !work.pop(&task) {
break
}
sw := time.new_stopwatch() sw := time.new_stopwatch()
task.execute() task.execute()
task.took = sw.elapsed() task.took = sw.elapsed()
results.push(&task) results <- task
} }
} }

View File

@ -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

View File

@ -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')
}