2020-11-16 17:32:50 +01:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
2021-02-22 08:42:00 +01:00
|
|
|
const vexe = os.getenv('VEXE')
|
|
|
|
|
|
|
|
const vroot = os.dir(vexe)
|
|
|
|
|
|
|
|
const test_os_process = os.join_path(os.temp_dir(), 'v', 'test_os_process.exe')
|
|
|
|
|
|
|
|
const test_os_process_source = os.join_path(vroot, 'cmd/tools/test_os_process.v')
|
|
|
|
|
|
|
|
fn testsuite_begin() {
|
2021-03-06 20:04:51 +01:00
|
|
|
os.rm(test_os_process) or {}
|
2021-02-22 08:42:00 +01:00
|
|
|
assert os.system('$vexe -o $test_os_process $test_os_process_source') == 0
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:32:50 +01:00
|
|
|
fn test_getpid() {
|
|
|
|
pid := os.getpid()
|
|
|
|
eprintln('current pid: $pid')
|
|
|
|
assert pid != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_run() {
|
|
|
|
if os.user_os() == 'windows' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//
|
2021-02-22 08:42:00 +01:00
|
|
|
mut p := os.new_process(test_os_process)
|
|
|
|
p.set_args(['-timeout_ms', '150', '-period_ms', '50'])
|
2020-11-16 17:32:50 +01:00
|
|
|
p.run()
|
|
|
|
assert p.status == .running
|
|
|
|
assert p.pid > 0
|
|
|
|
assert p.pid != os.getpid()
|
|
|
|
mut i := 0
|
|
|
|
for {
|
|
|
|
if !p.is_alive() {
|
|
|
|
break
|
|
|
|
}
|
2021-02-22 08:42:00 +01:00
|
|
|
$if trace_process_output ? {
|
|
|
|
os.system('ps -opid= -oppid= -ouser= -onice= -of= -ovsz= -orss= -otime= -oargs= -p $p.pid')
|
|
|
|
}
|
2021-02-27 18:41:06 +01:00
|
|
|
time.sleep(50 * time.millisecond)
|
2020-11-16 17:32:50 +01:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
p.wait()
|
|
|
|
assert p.code == 0
|
|
|
|
assert p.status == .exited
|
|
|
|
//
|
|
|
|
eprintln('polling iterations: $i')
|
|
|
|
assert i < 20
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_wait() {
|
|
|
|
if os.user_os() == 'windows' {
|
|
|
|
return
|
|
|
|
}
|
2021-02-22 08:42:00 +01:00
|
|
|
mut p := os.new_process(test_os_process)
|
|
|
|
assert p.status != .exited
|
2020-11-16 17:32:50 +01:00
|
|
|
p.wait()
|
|
|
|
assert p.status == .exited
|
2021-02-22 08:42:00 +01:00
|
|
|
assert p.code == 0
|
|
|
|
assert p.pid != os.getpid()
|
2020-11-16 17:32:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_slurping_output() {
|
|
|
|
if os.user_os() == 'windows' {
|
|
|
|
return
|
|
|
|
}
|
2021-02-22 08:42:00 +01:00
|
|
|
mut p := os.new_process(test_os_process)
|
2021-02-22 16:24:52 +01:00
|
|
|
p.set_args(['-timeout_ms', '500', '-period_ms', '50'])
|
2020-11-16 17:32:50 +01:00
|
|
|
p.set_redirect_stdio()
|
2021-02-22 08:42:00 +01:00
|
|
|
assert p.status != .exited
|
2020-11-16 17:32:50 +01:00
|
|
|
p.wait()
|
|
|
|
assert p.status == .exited
|
2021-02-22 08:42:00 +01:00
|
|
|
assert p.code == 0
|
2020-11-16 17:32:50 +01:00
|
|
|
output := p.stdout_slurp().trim_space()
|
|
|
|
errors := p.stderr_slurp().trim_space()
|
2021-02-22 08:42:00 +01:00
|
|
|
$if trace_process_output ? {
|
|
|
|
eprintln('---------------------------')
|
|
|
|
eprintln('p output: "$output"')
|
|
|
|
eprintln('p errors: "$errors"')
|
|
|
|
eprintln('---------------------------')
|
|
|
|
}
|
|
|
|
assert output.contains('stdout, 1')
|
|
|
|
assert output.contains('stdout, 2')
|
|
|
|
assert output.contains('stdout, 3')
|
|
|
|
assert output.contains('stdout, 4')
|
|
|
|
assert errors.contains('stderr, 1')
|
|
|
|
assert errors.contains('stderr, 2')
|
|
|
|
assert errors.contains('stderr, 3')
|
|
|
|
assert errors.contains('stderr, 4')
|
2020-11-16 17:32:50 +01:00
|
|
|
}
|