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