]> gitweb.factorcode.org Git - factor.git/blob - basis/io/launcher/unix/unix-tests.factor
use ?delete-file instead of [ delete-file ] ignore-errors.
[factor.git] / basis / io / launcher / unix / unix-tests.factor
1 USING: accessors calendar concurrency.promises continuations
2 debugger.unix destructors io io.backend.unix io.directories
3 io.encodings.ascii io.encodings.binary io.encodings.utf8
4 io.files io.launcher io.launcher.unix io.streams.duplex
5 io.timeouts kernel libc locals math namespaces sequences threads
6 tools.test unix.process ;
7 IN: io.launcher.unix.tests
8
9
10 [
11     { } [ { "touch" "launcher-test-1" } try-process ] unit-test
12
13     { t } [ "launcher-test-1" exists? ] unit-test
14
15     { } [
16         "launcher-test-1" ?delete-file
17     ] unit-test
18
19     { } [
20         <process>
21             "echo Hello" >>command
22             "launcher-test-1" >>stdout
23         try-process
24     ] unit-test
25
26     { "Hello\n" } [
27         { "cat" "launcher-test-1" }
28         ascii <process-reader> stream-contents
29     ] unit-test
30
31     { } [
32         "launcher-test-1" ?delete-file
33     ] unit-test
34
35     { } [
36         <process>
37             "cat" >>command
38             +closed+ >>stdin
39             "launcher-test-1" >>stdout
40         try-process
41     ] unit-test
42
43     { "" } [
44         { "cat" "launcher-test-1" }
45         ascii <process-reader> stream-contents
46     ] unit-test
47
48     { } [
49         2 [
50             "launcher-test-1" binary <file-appender> [
51                 <process>
52                     swap >>stdout
53                     "echo Hello" >>command
54                 try-process
55             ] with-disposal
56         ] times
57     ] unit-test
58
59     { "Hello\nHello\n" } [
60         { "cat" "launcher-test-1" }
61         ascii <process-reader> stream-contents
62     ] unit-test
63
64     { "hi\n" } [
65         <process>
66             { "echo" "hi" } >>command
67             "launcher-test-2" >>stdout
68         try-process
69         "launcher-test-2" utf8 file-contents
70     ] unit-test
71
72     { "hi\nhi\n" } [
73         2 [
74             <process>
75                 "echo hi" >>command
76                 "launcher-test-3" <appender> >>stdout
77             try-process
78         ] times
79         "launcher-test-3" utf8 file-contents
80     ] unit-test
81
82 ] with-test-directory
83
84 { t } [
85     <process>
86         "env" >>command
87         { { "A" "B" } } >>environment
88     ascii <process-reader> stream-lines
89     "A=B" swap member?
90 ] unit-test
91
92 { { "A=B" } } [
93     <process>
94         "env" >>command
95         { { "A" "B" } } >>environment
96         +replace-environment+ >>environment-mode
97     ascii <process-reader> stream-lines
98 ] unit-test
99
100 { t } [
101     "ls" utf8 <process-stream> stream-contents >boolean
102 ] unit-test
103
104 { "Hello world.\n" } [
105     "cat" utf8 <process-stream> [
106         "Hello world.\n" write
107         output-stream get dispose
108         input-stream get stream-contents
109     ] with-stream
110 ] unit-test
111
112 ! Test process timeouts
113 [
114     <process>
115         { "sleep" "10" } >>command
116         1 seconds >>timeout
117     run-process
118 ] [ process-was-killed? ] must-fail-with
119
120 [
121     <process>
122         { "sleep" "10" } >>command
123         1 seconds >>timeout
124     try-process
125 ] [ process-was-killed? ] must-fail-with
126
127 [
128     <process>
129         { "sleep" "10" } >>command
130         1 seconds >>timeout
131     try-output-process
132 ] [ io-timeout? ] must-fail-with
133
134 ! Killed processes were exiting with code 0 on FreeBSD
135 { f } [
136     [let
137         <promise> :> p
138         <promise> :> s
139
140         [
141             "sleep 1000" run-detached
142             [ p fulfill ] [ wait-for-process s fulfill ] bi
143         ] in-thread
144
145         p 1 seconds ?promise-timeout (kill-process)
146         s 3 seconds ?promise-timeout 0 =
147     ]
148 ] unit-test
149
150 ! Make sure that subprocesses don't inherit our signal mask
151
152 ! First, ensure that the Factor VM ignores SIGPIPE
153 : send-sigpipe ( pid -- )
154     "SIGPIPE" signal-names index 1 +
155     kill io-error ;
156
157 { } [ (current-process) send-sigpipe ] unit-test
158
159 ! Spawn a process
160 { T{ signal f 13 } } [
161     "sleep 1000" run-detached
162     1 seconds sleep
163     [ handle>> send-sigpipe ]
164     [ 2 seconds swap set-timeout ]
165     [ wait-for-process ]
166     tri
167 ] unit-test
168
169 ! Test priority
170 { 0 } [
171     <process>
172         { "bash" "-c" "sleep 2&" } >>command
173         +low-priority+ >>priority
174     run-process status>>
175 ] unit-test
176
177 ! Check that processes launched with the group option kill their children (or not)
178 ! This test should leave two sleeps running for 30 seconds.
179 [
180     <process> { "bash" "-c" "sleep 30& sleep 30" } >>command
181         +same-group+ >>group
182         500 milliseconds >>timeout
183     run-process
184 ] [ process-was-killed? ] must-fail-with
185
186 ! This test should kill the sleep after 500ms.
187 [
188     <process> { "bash" "-c" "sleep 30& sleep 30" } >>command
189         +new-group+ >>group
190         500 milliseconds >>timeout
191     run-process
192 ] [ process-was-killed? ] must-fail-with
193
194 ! This test should kill the sleep after 500ms.
195 [
196     <process> { "bash" "-c" "sleep 30& sleep 30" } >>command
197         +new-session+ >>group
198         500 milliseconds >>timeout
199     run-process
200 ] [ process-was-killed? ] must-fail-with