mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: add Process (#6786)
This commit is contained in:
63
vlib/os/process_test.v
Normal file
63
vlib/os/process_test.v
Normal file
@ -0,0 +1,63 @@
|
||||
import os
|
||||
import time
|
||||
|
||||
fn test_getpid() {
|
||||
pid := os.getpid()
|
||||
eprintln('current pid: $pid')
|
||||
assert pid != 0
|
||||
}
|
||||
|
||||
fn test_run() {
|
||||
if os.user_os() == 'windows' {
|
||||
return
|
||||
}
|
||||
//
|
||||
mut p := os.new_process('/bin/sleep')
|
||||
p.set_args(['0.2'])
|
||||
p.run()
|
||||
assert p.status == .running
|
||||
assert p.pid > 0
|
||||
assert p.pid != os.getpid()
|
||||
mut i := 0
|
||||
for {
|
||||
if !p.is_alive() {
|
||||
break
|
||||
}
|
||||
os.system('ps -opid= -oppid= -ouser= -onice= -of= -ovsz= -orss= -otime= -oargs= -p $p.pid')
|
||||
time.sleep_ms(50)
|
||||
i++
|
||||
}
|
||||
p.wait()
|
||||
assert p.code == 0
|
||||
assert p.status == .exited
|
||||
//
|
||||
eprintln('polling iterations: $i')
|
||||
assert i > 1
|
||||
assert i < 20
|
||||
}
|
||||
|
||||
fn test_wait() {
|
||||
if os.user_os() == 'windows' {
|
||||
return
|
||||
}
|
||||
mut p := os.new_process('/bin/date')
|
||||
p.wait()
|
||||
assert p.pid != os.getpid()
|
||||
assert p.code == 0
|
||||
assert p.status == .exited
|
||||
}
|
||||
|
||||
fn test_slurping_output() {
|
||||
if os.user_os() == 'windows' {
|
||||
return
|
||||
}
|
||||
mut p := os.new_process('/bin/date')
|
||||
p.set_redirect_stdio()
|
||||
p.wait()
|
||||
assert p.code == 0
|
||||
assert p.status == .exited
|
||||
output := p.stdout_slurp().trim_space()
|
||||
errors := p.stderr_slurp().trim_space()
|
||||
eprintln('p output: "$output"')
|
||||
eprintln('p errors: "$errors"')
|
||||
}
|
Reference in New Issue
Block a user