1
1
module test_subprocess
2
2
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
3
- use stdlib_system, only: process_type, run, is_running, wait, update
3
+ use stdlib_system, only: process_type, run, is_running, wait, update, elapsed, has_win32
4
4
5
5
implicit none
6
6
@@ -27,27 +27,36 @@ subroutine test_run_synchronous(error)
27
27
process = run(command, wait= .true. , want_stdout= .true. )
28
28
call check(error, process% completed)
29
29
if (allocated (error)) return
30
-
31
- call check(error, trim (process% stdout) == " Hello" )
30
+
31
+ call check(error, trim (process% stdout) == " Hello" , " stdout=< " // trim (process % stdout) // " >, expected <Hello> " )
32
32
end subroutine test_run_synchronous
33
33
34
34
! > Test running an asynchronous process
35
35
subroutine test_run_asynchronous (error )
36
36
type (error_type), allocatable , intent (out ) :: error
37
37
type (process_type) :: process
38
38
logical :: running
39
- character (len=* ), parameter :: command = " sleep 1"
40
39
41
- process = run(command, wait= .false. )
42
- call check(error, .not. process% completed)
40
+ ! The closest possible to a cross-platform command that waits
41
+ if (has_win32()) then
42
+ process = run(" ping -n 2 127.0.0.1" , wait= .false. )
43
+ else
44
+ process = run(" ping -c 2 127.0.0.1" , wait= .false. )
45
+ endif
46
+ ! Should not be immediately completed
47
+ call check(error, .not. process% completed, " ping process should not complete immediately" )
43
48
if (allocated (error)) return
44
49
45
50
running = is_running(process)
46
- call check(error, running)
51
+ call check(error, running, " ping process should still be running immediately after started " )
47
52
if (allocated (error)) return
48
53
49
54
call wait(process)
50
- call check(error, process% completed)
55
+ call check(error, process% completed, " process should be complete after `call wait`" )
56
+ if (allocated (error)) return
57
+
58
+ call check(error, elapsed(process)>1.0e-4 , " There should be a non-zero elapsed time" )
59
+
51
60
end subroutine test_run_asynchronous
52
61
53
62
! > Test updating and checking process state
@@ -62,10 +71,13 @@ subroutine test_process_state(error)
62
71
call check(error, process% completed)
63
72
if (allocated (error)) return
64
73
65
- call check(error, process% exit_code == 0 )
74
+ call check(error, process% exit_code == 0 , " Check zero exit code" )
75
+ if (allocated (error)) return
76
+
77
+ call check(error, len_trim (process% stderr) == 0 , " Check no stderr output" )
66
78
if (allocated (error)) return
67
79
68
- call check(error, trim (process% stdout) == " Testing" )
80
+ call check(error, trim (process% stdout) == " Testing" , " stdout=< " // trim (process % stdout) // " >, expected <Testing> " )
69
81
if (allocated (error)) return
70
82
end subroutine test_process_state
71
83
0 commit comments