2022-04-30 13:27:50 +03:00
|
|
|
// vtest flaky: true
|
2021-10-03 20:26:38 +03:00
|
|
|
// vtest retry: 3
|
2020-11-16 19:32:50 +03:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
2021-04-04 17:05:06 +03:00
|
|
|
const (
|
|
|
|
vexe = os.getenv('VEXE')
|
|
|
|
vroot = os.dir(vexe)
|
2022-11-03 10:24:52 +03:00
|
|
|
tfolder = os.join_path(os.vtmp_dir(), 'v', 'tests', 'os_process')
|
2022-09-16 04:56:19 +03:00
|
|
|
test_os_process = os.join_path(tfolder, 'test_os_process.exe')
|
2021-04-04 17:05:06 +03:00
|
|
|
test_os_process_source = os.join_path(vroot, 'cmd/tools/test_os_process.v')
|
|
|
|
)
|
2021-02-22 10:42:00 +03:00
|
|
|
|
2022-09-25 10:29:25 +03:00
|
|
|
fn testsuite_begin() {
|
2022-09-16 04:56:19 +03:00
|
|
|
os.rmdir_all(tfolder) or {}
|
2022-10-16 09:28:57 +03:00
|
|
|
os.mkdir_all(tfolder)!
|
2021-04-04 17:05:06 +03:00
|
|
|
if os.getenv('WINE_TEST_OS_PROCESS_EXE') != '' {
|
2021-04-07 16:25:11 +03:00
|
|
|
// Make it easier to run the test under wine emulation, by just
|
2021-04-04 17:05:06 +03:00
|
|
|
// prebuilding the executable with:
|
|
|
|
// v -os windows -o x.exe cmd/tools/test_os_process.v
|
|
|
|
// WINE_TEST_OS_PROCESS_EXE=x.exe ./v -os windows vlib/os/process_test.v
|
2022-10-16 09:28:57 +03:00
|
|
|
os.cp(os.getenv('WINE_TEST_OS_PROCESS_EXE'), test_os_process)!
|
2021-04-04 17:05:06 +03:00
|
|
|
} else {
|
2022-01-22 22:13:16 +03:00
|
|
|
os.system('${os.quoted_path(vexe)} -o ${os.quoted_path(test_os_process)} ${os.quoted_path(test_os_process_source)}')
|
2021-04-04 17:05:06 +03:00
|
|
|
}
|
|
|
|
assert os.exists(test_os_process)
|
2021-02-22 10:42:00 +03:00
|
|
|
}
|
|
|
|
|
2022-09-25 10:29:25 +03:00
|
|
|
fn testsuite_end() {
|
2022-09-16 04:56:19 +03:00
|
|
|
os.rmdir_all(tfolder) or {}
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:32:50 +03:00
|
|
|
fn test_getpid() {
|
|
|
|
pid := os.getpid()
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('current pid: ${pid}')
|
2020-11-16 19:32:50 +03:00
|
|
|
assert pid != 0
|
|
|
|
}
|
|
|
|
|
2023-04-13 14:48:32 +03:00
|
|
|
fn test_set_work_folder() {
|
|
|
|
new_work_folder := os.real_path(os.temp_dir())
|
|
|
|
parent_working_folder := os.getwd()
|
|
|
|
dump(new_work_folder)
|
|
|
|
dump(parent_working_folder)
|
|
|
|
if new_work_folder == parent_working_folder {
|
|
|
|
eprintln('... skipping ${@METHOD} because the working folder is the temporary one')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut p := os.new_process(test_os_process)
|
|
|
|
p.set_args(['-show_wd', '-target', 'stdout'])
|
|
|
|
p.set_work_folder(new_work_folder)
|
|
|
|
p.set_redirect_stdio()
|
|
|
|
p.wait()
|
|
|
|
assert p.code == 0
|
|
|
|
output := p.stdout_slurp().trim_space()
|
|
|
|
p.close()
|
|
|
|
$if trace_process_output ? {
|
|
|
|
eprintln('p output: "${output}"')
|
|
|
|
}
|
|
|
|
child_work_folder := output.find_between('stdout, WORK_DIR=', '\n').trim_space()
|
|
|
|
dump(child_work_folder)
|
|
|
|
assert child_work_folder == new_work_folder
|
|
|
|
new_parent_work_folder := os.getwd()
|
|
|
|
dump(new_parent_work_folder)
|
|
|
|
assert new_parent_work_folder == parent_working_folder
|
|
|
|
assert new_parent_work_folder != child_work_folder
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_done() {
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:32:50 +03:00
|
|
|
fn test_run() {
|
2021-02-22 10:42:00 +03:00
|
|
|
mut p := os.new_process(test_os_process)
|
|
|
|
p.set_args(['-timeout_ms', '150', '-period_ms', '50'])
|
2020-11-16 19:32:50 +03: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 10:42:00 +03:00
|
|
|
$if trace_process_output ? {
|
2022-11-15 16:53:13 +03:00
|
|
|
os.system('ps -opid= -oppid= -ouser= -onice= -of= -ovsz= -orss= -otime= -oargs= -p ${p.pid}')
|
2021-02-22 10:42:00 +03:00
|
|
|
}
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(50 * time.millisecond)
|
2020-11-16 19:32:50 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
p.wait()
|
|
|
|
assert p.code == 0
|
|
|
|
assert p.status == .exited
|
|
|
|
//
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('polling iterations: ${i}')
|
2021-04-05 22:38:55 +03:00
|
|
|
assert i < 50
|
2021-05-09 21:31:04 +03:00
|
|
|
p.close()
|
2020-11-16 19:32:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_wait() {
|
2021-02-22 10:42:00 +03:00
|
|
|
mut p := os.new_process(test_os_process)
|
|
|
|
assert p.status != .exited
|
2020-11-16 19:32:50 +03:00
|
|
|
p.wait()
|
|
|
|
assert p.status == .exited
|
2021-02-22 10:42:00 +03:00
|
|
|
assert p.code == 0
|
|
|
|
assert p.pid != os.getpid()
|
2021-05-09 21:31:04 +03:00
|
|
|
p.close()
|
2020-11-16 19:32:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_slurping_output() {
|
2021-02-22 10:42:00 +03:00
|
|
|
mut p := os.new_process(test_os_process)
|
2021-02-22 18:24:52 +03:00
|
|
|
p.set_args(['-timeout_ms', '500', '-period_ms', '50'])
|
2020-11-16 19:32:50 +03:00
|
|
|
p.set_redirect_stdio()
|
2021-02-22 10:42:00 +03:00
|
|
|
assert p.status != .exited
|
2020-11-16 19:32:50 +03:00
|
|
|
p.wait()
|
|
|
|
assert p.status == .exited
|
2021-02-22 10:42:00 +03:00
|
|
|
assert p.code == 0
|
2020-11-16 19:32:50 +03:00
|
|
|
output := p.stdout_slurp().trim_space()
|
|
|
|
errors := p.stderr_slurp().trim_space()
|
2021-05-09 21:31:04 +03:00
|
|
|
p.close()
|
2021-02-22 10:42:00 +03:00
|
|
|
$if trace_process_output ? {
|
|
|
|
eprintln('---------------------------')
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('p output: "${output}"')
|
|
|
|
eprintln('p errors: "${errors}"')
|
2021-02-22 10:42:00 +03:00
|
|
|
eprintln('---------------------------')
|
|
|
|
}
|
2021-04-04 17:05:06 +03:00
|
|
|
// dump(output)
|
2021-02-22 10:42:00 +03:00
|
|
|
assert output.contains('stdout, 1')
|
|
|
|
assert output.contains('stdout, 2')
|
|
|
|
assert output.contains('stdout, 3')
|
|
|
|
assert output.contains('stdout, 4')
|
2021-04-04 17:05:06 +03:00
|
|
|
//
|
|
|
|
// dump(errors)
|
2021-02-22 10:42:00 +03:00
|
|
|
assert errors.contains('stderr, 1')
|
|
|
|
assert errors.contains('stderr, 2')
|
|
|
|
assert errors.contains('stderr, 3')
|
|
|
|
assert errors.contains('stderr, 4')
|
2020-11-16 19:32:50 +03:00
|
|
|
}
|