blob: af809eceb4c5bc0ccfdc7759302ab44e3c5d473c [file] [log] [blame]
Bram Moolenaarf386f082019-07-29 23:03:03 +02001" Test for channel and job functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaarf386f082019-07-29 23:03:03 +02003" When +channel is supported then +job is too, so we don't check for that.
Bram Moolenaar4641a122019-07-29 22:10:23 +02004source check.vim
5CheckFeature channel
Bram Moolenaare2469252016-02-04 10:54:34 +01006
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
Bram Moolenaar4641a122019-07-29 22:10:23 +02008source screendump.vim
Bram Moolenaar321efdd2016-07-15 17:09:11 +02009
10let s:python = PythonProg()
11if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010012 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020013 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010014endif
15
Bram Moolenaar37175402017-03-18 20:18:45 +010016" Uncomment the next line to see what happens. Output is in
17" src/testdir/channellog.
Bram Moolenaarf386f082019-07-29 23:03:03 +020018" Add ch_log() calls where you want to see what happens.
Bram Moolenaar37175402017-03-18 20:18:45 +010019" call ch_logfile('channellog', 'w')
20
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020021func SetUp()
22 if g:testfunc =~ '_ipv6()$'
23 let s:localhost = '[::1]:'
24 let s:testscript = 'test_channel_6.py'
25 else
26 let s:localhost = 'localhost:'
27 let s:testscript = 'test_channel.py'
28 endif
29 let s:chopt = {}
30 call ch_log(g:testfunc)
31endfunc
Bram Moolenaar3b05b132016-02-03 23:25:07 +010032
Bram Moolenaar4b96df52020-01-26 22:00:26 +010033" Run "testfunc" after starting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010034func s:run_server(testfunc, ...)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020035 call RunServer(s:testscript, a:testfunc, a:000)
Bram Moolenaar373a8762020-03-19 19:44:32 +010036
37 " communicating with a server can be flaky
38 let g:test_is_flaky = 1
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010039endfunc
40
Bram Moolenaar819524702018-02-27 19:10:00 +010041" Return a list of open files.
42" Can be used to make sure no resources leaked.
43" Returns an empty list on systems where this is not supported.
44func s:get_resources()
45 let pid = getpid()
46
Bram Moolenaar39536dd2019-01-29 22:58:21 +010047 if executable('lsof')
Bram Moolenaar819524702018-02-27 19:10:00 +010048 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
49 elseif isdirectory('/proc/' . pid . '/fd/')
50 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
51 else
52 return []
53 endif
54endfunc
55
Bram Moolenaar321efdd2016-07-15 17:09:11 +020056let g:Ch_responseMsg = ''
57func Ch_requestHandler(handle, msg)
58 let g:Ch_responseHandle = a:handle
59 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010060endfunc
61
Bram Moolenaar321efdd2016-07-15 17:09:11 +020062func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010063 " Avoid dropping messages, since we don't use a callback here.
64 let s:chopt.drop = 'never'
Bram Moolenaar0b146882018-09-06 16:27:24 +020065 " Also add the noblock flag to try it out.
66 let s:chopt.noblock = 1
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020067 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +010068 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010069 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010070 return
71 endif
Bram Moolenaarf386f082019-07-29 23:03:03 +020072
73 " check that getjob without a job is handled correctly
74 call assert_equal('no process', string(ch_getjob(handle)))
75
Bram Moolenaar570497a2019-08-22 22:55:13 +020076 let dict = handle->ch_info()
Bram Moolenaar03602ec2016-03-20 20:57:45 +010077 call assert_true(dict.id != 0)
78 call assert_equal('open', dict.status)
79 call assert_equal(a:port, string(dict.port))
80 call assert_equal('open', dict.sock_status)
81 call assert_equal('socket', dict.sock_io)
82
Bram Moolenaard7ece102016-02-02 23:23:02 +010083 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010084 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010085
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010086 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010087 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
88 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
89 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
90
91 " split command should work
92 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020093 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010094 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010095
Bram Moolenaarf1f07922016-08-26 17:58:53 +020096 " string with ][ should work
97 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
98
Bram Moolenaar4b785f62016-11-29 21:54:44 +010099 " nothing to read now
100 call assert_equal(0, ch_canread(handle))
101
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200102 " sending three messages quickly then reading should work
103 for i in range(3)
104 call ch_sendexpr(handle, 'echo hello ' . i)
105 endfor
106 call assert_equal('hello 0', ch_read(handle)[1])
107 call assert_equal('hello 1', ch_read(handle)[1])
108 call assert_equal('hello 2', ch_read(handle)[1])
109
Bram Moolenaard7ece102016-02-02 23:23:02 +0100110 " Request that triggers sending two ex commands. These will usually be
111 " handled before getting the response, but it's not guaranteed, thus wait a
112 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100113 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200114 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +0100115 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100116
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100117 " Request command "foo bar", which fails silently.
118 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200119 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100120
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100121 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200122 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100123
Bram Moolenaara07fec92016-02-05 21:04:08 +0100124 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200125 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
126 call WaitFor('exists("g:Ch_responseHandle")')
127 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100128 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100129 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200130 call assert_equal(handle, g:Ch_responseHandle)
131 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100132 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200133 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100134
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200135 let g:Ch_responseMsg = ''
136 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
137 call WaitFor('exists("g:Ch_responseHandle")')
138 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100139 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100140 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200141 call assert_equal(handle, g:Ch_responseHandle)
142 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100143 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200144 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100145
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200146 " Using lambda.
147 let g:Ch_responseMsg = ''
148 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
149 call WaitFor('exists("g:Ch_responseHandle")')
150 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100151 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200152 else
153 call assert_equal(handle, g:Ch_responseHandle)
154 unlet g:Ch_responseHandle
155 endif
156 call assert_equal('got it', g:Ch_responseMsg)
157
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100158 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200159 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100160
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100161 " check setting options (without testing the effect)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200162 eval handle->ch_setoptions({'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100163 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100164 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100165 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100166 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100167 call ch_setoptions(handle, {'drop': 'never'})
168 call ch_setoptions(handle, {'drop': 'auto'})
169 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100170
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100171 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100172 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100173 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100174 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100175
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100176 " Send an eval request with special characters.
177 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
178 sleep 10m
179 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
180
181 " Send an eval request to get a line with special characters.
182 call setline(3, "a\nb\<CR>c\x01d\x7fe")
183 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
184 sleep 10m
185 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
186
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100187 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100188 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100189 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100190 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100191
Bram Moolenaar55fab432016-02-07 16:53:13 +0100192 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100193 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100194 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100195 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100196
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100197 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100198 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100199 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100200 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100201
Bram Moolenaarf4160862016-02-05 23:09:12 +0100202 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100203 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200204 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100205 call assert_equal('one', getline(line('$') - 2))
206 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100207
208 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100209 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
210 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100211
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100212 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100213
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100214 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100215 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100216 if exists('*reltimefloat')
217 let start = reltime()
218 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
219 let elapsed = reltime(start)
220 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
221 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100222
223 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100224 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100225 let resp = ch_read(handle)
226 call assert_equal(type([]), type(resp))
227 call assert_equal(type(11), type(resp[0]))
228 call assert_equal('waited', resp[1])
229
Bram Moolenaard7ece102016-02-02 23:23:02 +0100230 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100231 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100232endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100233
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100234func Test_communicate()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200235 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100236endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100237
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200238func Test_communicate_ipv6()
239 CheckIPv6
240 call Test_communicate()
241endfunc
242
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100243" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200244func Ch_two_channels(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200245 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200246 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar570497a2019-08-22 22:55:13 +0200247 if handle->ch_status() == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100248 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100249 return
250 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100251
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100252 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100253
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200254 let newhandle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100255 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100256 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100257 return
258 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100259 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
260 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100261
262 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100263 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100264
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100265 call ch_close(newhandle)
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200266 call assert_fails("call ch_close(newhandle)", 'E906:')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100267endfunc
268
269func Test_two_channels()
Bram Moolenaar570497a2019-08-22 22:55:13 +0200270 eval 'Test_two_channels()'->ch_log()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200271 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100272endfunc
273
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200274func Test_two_channels_ipv6()
275 CheckIPv6
276 call Test_two_channels()
277endfunc
278
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100279" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200280func Ch_server_crash(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200281 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100282 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100283 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100284 return
285 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100286
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100287 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100288
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100289 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100290endfunc
291
292func Test_server_crash()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200293 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100294endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100295
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200296func Test_server_crash_ipv6()
297 CheckIPv6
298 call Test_server_crash()
299endfunc
300
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100301"""""""""
302
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200303func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100304 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200305 unlet g:Ch_reply
306 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100307endfunc
308
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200309func Ch_channel_handler(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200310 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100311 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100312 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100313 return
314 endif
315
316 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100317 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200318 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100319
320 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100321 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200322 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100323endfunc
324
325func Test_channel_handler()
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200326 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200327 let s:chopt.callback = 'Ch_handler'
328 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200329 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200330 let s:chopt.callback = function('Ch_handler')
331 call s:run_server('Ch_channel_handler')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200332endfunc
333
334func Test_channel_handler_ipv6()
335 CheckIPv6
336 call Test_channel_handler()
Bram Moolenaarf6157282016-02-10 21:07:14 +0100337endfunc
338
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100339"""""""""
340
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200341let g:Ch_reply = ''
342func Ch_zeroHandler(chan, msg)
343 unlet g:Ch_reply
344 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100345endfunc
346
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200347let g:Ch_zero_reply = ''
348func Ch_oneHandler(chan, msg)
349 unlet g:Ch_zero_reply
350 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100351endfunc
352
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200353func Ch_channel_zero(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200354 let handle = (s:localhost .. a:port)->ch_open(s:chopt)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100355 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100356 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100357 return
358 endif
359
360 " Check that eval works.
361 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
362
363 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200364 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100365 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100366 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200367 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100368 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100369 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200370 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100371 endif
372
373 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200374 let g:Ch_reply = ''
375 let g:Ch_zero_reply = ''
376 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200377 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100378 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200379 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100380 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200381 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100382 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100383endfunc
384
385func Test_zero_reply()
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100386 " Run with channel handler
387 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200388 let s:chopt.callback = 'Ch_zeroHandler'
389 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100390 unlet s:chopt.callback
391
392 " Run without channel handler
393 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200394 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100395endfunc
396
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200397func Test_zero_reply_ipv6()
398 CheckIPv6
399 call Test_zero_reply()
400endfunc
401
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100402"""""""""
403
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200404let g:Ch_reply1 = ""
405func Ch_handleRaw1(chan, msg)
406 unlet g:Ch_reply1
407 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100408endfunc
409
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200410let g:Ch_reply2 = ""
411func Ch_handleRaw2(chan, msg)
412 unlet g:Ch_reply2
413 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100414endfunc
415
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200416let g:Ch_reply3 = ""
417func Ch_handleRaw3(chan, msg)
418 unlet g:Ch_reply3
419 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100420endfunc
421
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200422func Ch_raw_one_time_callback(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200423 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100424 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100425 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100426 return
427 endif
428 call ch_setoptions(handle, {'mode': 'raw'})
429
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100430 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200431 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200432 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200433 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
434 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200435 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100436 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200437 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100438endfunc
439
440func Test_raw_one_time_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200441 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100442endfunc
443
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200444func Test_raw_one_time_callback_ipv6()
445 CheckIPv6
446 call Test_raw_one_time_callback()
447endfunc
448
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100449"""""""""
450
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100451" Test that trying to connect to a non-existing port fails quickly.
452func Test_connect_waittime()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100453 CheckFunction reltimefloat
Bram Moolenaar373a8762020-03-19 19:44:32 +0100454 " this is timing sensitive
455 let g:test_is_flaky = 1
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100456
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100457 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100458 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100459 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100460 " Oops, port does exists.
461 call ch_close(handle)
462 else
463 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100464 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100465 endif
466
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100467 " We intend to use a socket that doesn't exist and wait for half a second
468 " before giving up. If the socket does exist it can fail in various ways.
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100469 " Check for "Connection reset by peer" to avoid flakiness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100470 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100471 try
472 let handle = ch_open('localhost:9867', {'waittime': 500})
473 if ch_status(handle) != "fail"
474 " Oops, port does exists.
475 call ch_close(handle)
476 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100477 " Failed connection should wait about 500 msec. Can be longer if the
478 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100479 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100480 endif
481 catch
482 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100483 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100484 endif
485 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100486endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100487
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100488"""""""""
489
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100490func Test_raw_pipe()
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100491 " Add a dummy close callback to avoid that messages are dropped when calling
492 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200493 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100494 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200495 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200496 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100497 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200498
499 call assert_equal("open", ch_status(job))
500 call assert_equal("open", ch_status(job), {"part": "out"})
501 call assert_equal("open", ch_status(job), {"part": "err"})
502 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
503 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
504
505 let dict = ch_info(job)
506 call assert_true(dict.id != 0)
507 call assert_equal('open', dict.status)
508 call assert_equal('open', dict.out_status)
509 call assert_equal('RAW', dict.out_mode)
510 call assert_equal('pipe', dict.out_io)
511 call assert_equal('open', dict.err_status)
512 call assert_equal('RAW', dict.err_mode)
513 call assert_equal('pipe', dict.err_io)
514
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100515 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100516 " For a change use the job where a channel is expected.
517 call ch_sendraw(job, "echo something\n")
518 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100519 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
520
Bram Moolenaar151f6562016-03-07 21:19:38 +0100521 call ch_sendraw(job, "double this\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200522 let g:handle = job->job_getchannel()
523 call WaitFor('g:handle->ch_canread()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100524 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100525 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100526 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
527
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200528 let g:Ch_reply = ""
529 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200530 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200531
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200532 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'callback' : 'abc'})", 'E917:')
533 call assert_fails("let i = ch_evalexpr(job, '2 + 2')", 'E912:')
534 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'drop' : ''})", 'E475:')
535 call assert_fails("let i = ch_evalraw(test_null_job(), '2 + 2')", 'E906:')
536
Bram Moolenaar570497a2019-08-22 22:55:13 +0200537 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100538 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
539 finally
540 call job_stop(job)
541 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100542
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200543 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200544 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar570497a2019-08-22 22:55:13 +0200545 let info = job->job_info()
Bram Moolenaar8950a562016-03-12 15:22:55 +0100546 call assert_equal("dead", info.status)
547 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200548 call assert_equal(2, len(info.cmd))
549 call assert_equal("test_channel_pipe.py", info.cmd[1])
550
551 let found = 0
552 for j in job_info()
553 if j == job
554 let found += 1
555 endif
556 endfor
557 call assert_equal(1, found)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100558
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200559 call assert_fails("call job_stop('abc')", 'E475:')
560 call assert_fails("call job_stop(job, [])", 'E474:')
561 call assert_fails("call job_stop(test_null_job())", 'E916:')
562
Bram Moolenaar8b633132020-03-20 18:20:51 +0100563 " Try to use the job and channel where a number is expected. This is not
564 " related to testing the raw pipe. This test is here just to reuse the
565 " already created job/channel.
566 let ch = job_getchannel(job)
567 call assert_fails('let i = job + 1', 'E910:')
568 call assert_fails('let j = ch + 1', 'E913:')
569 call assert_fails('echo 2.0 == job', 'E911:')
570 call assert_fails('echo 2.0 == ch', 'E914:')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100571endfunc
572
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100573func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100574 " Add a dummy close callback to avoid that messages are dropped when calling
575 " ch_canread().
576 " Also test the non-blocking option.
577 let job = job_start(s:python . " test_channel_pipe.py",
578 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
579 call assert_equal(v:t_job, type(job))
580 call assert_equal("run", job_status(job))
581
582 call assert_equal("open", ch_status(job))
583 call assert_equal("open", ch_status(job), {"part": "out"})
584
585 try
586 " Create a blob with the echo command and write it.
587 let blob = 0z00
588 let cmd = "echo something\n"
589 for i in range(0, len(cmd) - 1)
590 let blob[i] = char2nr(cmd[i])
591 endfor
592 call assert_equal(len(cmd), len(blob))
593 call ch_sendraw(job, blob)
594
595 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200596 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100597 let expected = 'something'
598 for i in range(0, len(expected) - 1)
599 call assert_equal(char2nr(expected[i]), msg[i])
600 endfor
601
602 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
603 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
604 finally
605 call job_stop(job)
606 endtry
607
608 let g:Ch_job = job
609 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
610 let info = job_info(job)
611 call assert_equal("dead", info.status)
612endfunc
613
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100614func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100615 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100616 call assert_equal("run", job_status(job))
617 try
618 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100619 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200620 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100621
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100622 call ch_sendraw(handle, "echoerr wrong\n")
623 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
624
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100625 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100626 call assert_equal("this", ch_readraw(handle))
627 call assert_equal("AND this", ch_readraw(handle))
628
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200629 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200630 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200631
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100632 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100633 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100634 finally
635 call job_stop(job)
636 endtry
637endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100638
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200639func Stop_g_job()
640 call job_stop(g:job)
641 if has('win32')
642 " On MS-Windows the server must close the file handle before we are able
643 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200644 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200645 sleep 10m
646 endif
647endfunc
648
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100649func Test_nl_read_file()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100650 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200651 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100652 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200653 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100654 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200655 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100656 call assert_equal("something", ch_readraw(handle))
657 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
658 call assert_equal("this", ch_readraw(handle))
659 call assert_equal("AND this", ch_readraw(handle))
660 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200661 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100662 call delete('Xinput')
663 endtry
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200664 call assert_fails("echo ch_read(test_null_channel(), {'callback' : 'abc'})", 'E475:')
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100665endfunc
666
Bram Moolenaare98d1212016-03-08 15:37:41 +0100667func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200668 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100669 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200670 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100671 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200672 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100673 call ch_sendraw(handle, "echo line one\n")
674 call ch_sendraw(handle, "echo line two\n")
675 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200676 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100677 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200678 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100679 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100680 call delete('Xoutput')
681 endtry
682endfunc
683
684func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200685 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100686 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200687 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100688 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200689 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100690 call ch_sendraw(handle, "echoerr line one\n")
691 call ch_sendraw(handle, "echoerr line two\n")
692 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200693 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100694 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200695 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100696 call delete('Xoutput')
697 endtry
698endfunc
699
700func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200701 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100702 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200703 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100704 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200705 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100706 call ch_sendraw(handle, "echoerr line one\n")
707 call ch_sendraw(handle, "echo line two\n")
708 call ch_sendraw(handle, "double this\n")
709 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200710 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100711 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200712 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100713 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100714 call delete('Xoutput')
715 endtry
716endfunc
717
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200718func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200719 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200720endfunc
721
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200722func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200723 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200724 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200725 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100726 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100727 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200728 if a:do_msg
729 let expected[0] = 'Reading from channel output...'
730 else
731 let options['out_msg'] = 0
732 call remove(expected, 0)
733 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100734 else
735 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100736 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100737 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200738 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100739 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200740 if a:nomod
741 let options['out_modifiable'] = 0
742 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100743 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100744 call assert_equal("run", job_status(job))
745 try
746 let handle = job_getchannel(job)
747 call ch_sendraw(handle, "echo line one\n")
748 call ch_sendraw(handle, "echo line two\n")
749 call ch_sendraw(handle, "double this\n")
750 call ch_sendraw(handle, "quit\n")
751 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100752 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200753 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200754 if a:nomod
755 call assert_equal(0, &modifiable)
756 else
757 call assert_equal(1, &modifiable)
758 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200759 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100760 bwipe!
761 finally
762 call job_stop(job)
763 endtry
764endfunc
765
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100766func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200767 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100768endfunc
769
770func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200771 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100772endfunc
773
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200774func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200775 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200776endfunc
777
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200778func Test_pipe_to_buffer_name_nomsg()
779 call Run_test_pipe_to_buffer(1, 0, 1)
780endfunc
781
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200782func Test_close_output_buffer()
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200783 enew!
784 let test_lines = ['one', 'two']
785 call setline(1, test_lines)
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200786 let options = {'out_io': 'buffer'}
787 let options['out_name'] = 'buffer-output'
788 let options['out_msg'] = 0
789 split buffer-output
790 let job = job_start(s:python . " test_channel_write.py", options)
791 call assert_equal("run", job_status(job))
792 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200793 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200794 quit!
795 sleep 100m
796 " Make sure the write didn't happen to the wrong buffer.
797 call assert_equal(test_lines, getline(1, line('$')))
798 call assert_equal(-1, bufwinnr('buffer-output'))
799 sbuf buffer-output
800 call assert_notequal(-1, bufwinnr('buffer-output'))
801 sleep 100m
802 close " no more writes
803 bwipe!
804 finally
805 call job_stop(job)
806 endtry
807endfunc
808
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200809func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100810 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200811 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100812 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100813 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200814 if a:do_msg
815 let expected[0] = 'Reading from channel error...'
816 else
817 let options['err_msg'] = 0
818 call remove(expected, 0)
819 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100820 else
821 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100822 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100823 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200824 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100825 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200826 if a:nomod
827 let options['err_modifiable'] = 0
828 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100829 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100830 call assert_equal("run", job_status(job))
831 try
832 let handle = job_getchannel(job)
833 call ch_sendraw(handle, "echoerr line one\n")
834 call ch_sendraw(handle, "echoerr line two\n")
835 call ch_sendraw(handle, "doubleerr this\n")
836 call ch_sendraw(handle, "quit\n")
837 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200838 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200839 if a:nomod
840 call assert_equal(0, &modifiable)
841 else
842 call assert_equal(1, &modifiable)
843 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100844 bwipe!
845 finally
846 call job_stop(job)
847 endtry
848endfunc
849
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100850func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200851 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100852endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100853
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100854func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200855 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200856endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100857
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200858func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200859 call Run_test_pipe_err_to_buffer(1, 1, 1)
860endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100861
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200862func Test_pipe_err_to_buffer_name_nomsg()
863 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100864endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100865
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100866func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100867 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100868 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100869 call assert_equal("run", job_status(job))
870 try
871 let handle = job_getchannel(job)
872 call ch_sendraw(handle, "echo line one\n")
873 call ch_sendraw(handle, "echoerr line two\n")
874 call ch_sendraw(handle, "double this\n")
875 call ch_sendraw(handle, "doubleerr that\n")
876 call ch_sendraw(handle, "quit\n")
877 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200878 call WaitForAssert({-> assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100879 bwipe!
880 finally
881 call job_stop(job)
882 endtry
883endfunc
884
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100885func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100886 sp pipe-input
887 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200888 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100889 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100890 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100891 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100892 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100893 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100894
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100895 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100896 call assert_equal("run", job_status(job))
897 try
898 let handle = job_getchannel(job)
899 call assert_equal('one', ch_read(handle))
900 call assert_equal('two', ch_read(handle))
901 call assert_equal('three', ch_read(handle))
902 bwipe!
903 finally
904 call job_stop(job)
905 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100906endfunc
907
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100908func Test_pipe_from_buffer_name()
909 call Run_test_pipe_from_buffer(1)
910endfunc
911
912func Test_pipe_from_buffer_nr()
913 call Run_test_pipe_from_buffer(0)
914endfunc
915
Bram Moolenaar0874a832016-09-01 15:11:51 +0200916func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200917 CheckExecutable sort
918
Bram Moolenaar0874a832016-09-01 15:11:51 +0200919 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
920 if a:use_buffer
921 split sortin
922 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
923 let options.in_io = 'buffer'
924 let options.in_name = 'sortin'
925 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200926 if !a:all
927 let options.in_top = 2
928 let options.in_bot = 4
929 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100930 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200931
932 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100933 call assert_equal("run", job_status(job))
934 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200935 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200936 endif
937
Bram Moolenaare2956092019-01-25 21:01:17 +0100938 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200939
Bram Moolenaard8b55492016-09-01 14:35:22 +0200940 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200941 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200942 call assert_equal('Reading from channel output...', getline(1))
943 if a:all
944 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
945 else
946 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
947 endif
948
Bram Moolenaare2956092019-01-25 21:01:17 +0100949 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200950 if a:use_buffer
951 bwipe! sortin
952 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200953 bwipe! sortout
954endfunc
955
956func Test_pipe_through_sort_all()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200957 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200958endfunc
959
960func Test_pipe_through_sort_some()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200961 call Run_pipe_through_sort(0, 1)
962endfunc
963
964func Test_pipe_through_sort_feed()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200965 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200966endfunc
967
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100968func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100969 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100970 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100971 call assert_equal("run", job_status(job))
972 try
973 let handle = job_getchannel(job)
974 call ch_sendraw(handle, "echo line one\n")
975 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200976 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200977 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100978 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
979 bwipe!
980 finally
981 call job_stop(job)
982 endtry
983endfunc
984
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100985func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100986 CheckFunction reltimefloat
987
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100988 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100989 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100990 call assert_equal("run", job_status(job))
991 try
992 let handle = job_getchannel(job)
993 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
994 call ch_sendraw(handle, "echo [-2, 12.34]\n")
995 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200996 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100997 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
998 bwipe!
999 finally
1000 call job_stop(job)
1001 endtry
1002endfunc
1003
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001004" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001005func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001006 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001007 if getline(line('$') - a:offset) == a:line
1008 break
1009 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001010 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001011 endfor
1012endfunc
1013
1014func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001015 " Create two buffers, one to read from and one to write to.
1016 split pipe-output
1017 set buftype=nofile
1018 split pipe-input
1019 set buftype=nofile
1020
1021 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001022 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001023 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1024 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001025 call assert_equal("run", job_status(job))
1026 try
1027 exe "normal Gaecho hello\<CR>"
1028 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001029 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001030 call assert_equal('hello', getline('$'))
1031
1032 exe bufwinnr('pipe-input') . "wincmd w"
1033 exe "normal Gadouble this\<CR>"
1034 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001035 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001036 call assert_equal('this', getline(line('$') - 1))
1037 call assert_equal('AND this', getline('$'))
1038
1039 bwipe!
1040 exe bufwinnr('pipe-input') . "wincmd w"
1041 bwipe!
1042 finally
1043 call job_stop(job)
1044 endtry
1045endfunc
1046
1047func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001048 " Create one buffer to read from and to write to.
1049 split pipe-io
1050 set buftype=nofile
1051
1052 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001053 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001054 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1055 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001056 call assert_equal("run", job_status(job))
1057 try
1058 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001059 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001060 call assert_equal('hello', getline(line('$') - 1))
1061
1062 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001063 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001064 call assert_equal('this', getline(line('$') - 2))
1065 call assert_equal('AND this', getline(line('$') - 1))
1066
1067 bwipe!
1068 finally
1069 call job_stop(job)
1070 endtry
1071endfunc
1072
Bram Moolenaar4641a122019-07-29 22:10:23 +02001073func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001074 CheckScreendump
1075
Bram Moolenaar4641a122019-07-29 22:10:23 +02001076 let lines =<< trim END
1077 new Xscrollbuffer
1078 call setline(1, range(1, 200))
1079 $
1080 redraw
1081 wincmd w
1082 call deletebufline('Xscrollbuffer', 1, '$')
1083 if has('win32')
1084 let cmd = ['cmd', '/c', 'echo sometext']
1085 else
1086 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1087 endif
1088 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1089 END
1090 call writefile(lines, 'XtestBufferScroll')
1091 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001092 call TermWait(buf, 50)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001093 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1094
1095 " clean up
1096 call StopVimInTerminal(buf)
1097 call delete('XtestBufferScroll')
1098endfunc
1099
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001100func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001101 " We cannot check that no I/O works, we only check that the job starts
1102 " properly.
1103 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001104 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001105 call assert_equal("run", job_status(job))
1106 try
1107 call assert_equal('something', ch_read(job))
1108 finally
1109 call job_stop(job)
1110 endtry
1111
1112 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001113 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001114 call assert_equal("run", job_status(job))
1115 try
1116 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1117 finally
1118 call job_stop(job)
1119 endtry
1120
1121 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001122 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001123 call assert_equal("run", job_status(job))
1124 try
1125 call assert_equal('something', ch_read(job))
1126 finally
1127 call job_stop(job)
1128 endtry
1129
1130 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001131 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001132 call assert_equal("run", job_status(job))
1133 call job_stop(job)
1134
1135 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001136 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001137 call assert_equal("run", job_status(job))
1138 call assert_equal('channel fail', string(job_getchannel(job)))
1139 call assert_equal('fail', ch_status(job))
1140 call job_stop(job)
1141endfunc
1142
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001143func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001144 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1145 split testout
1146 let job = job_start([s:python, '-c',
1147 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001148 " the job may be done quickly, also accept "dead"
1149 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001150 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001151 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001152 let totlen = 0
1153 for line in getline(1, '$')
1154 call assert_equal('', substitute(line, '^\.*', '', ''))
1155 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001156 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001157 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001158 finally
1159 call job_stop(job)
1160 bwipe!
1161 endtry
1162endfunc
1163
Bram Moolenaarde279892016-03-11 22:19:44 +01001164func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001165 let job = job_start(s:python . " test_channel_pipe.py")
1166 call assert_equal("run", job_status(job))
1167 let handle = job_getchannel(job)
1168 try
1169 call ch_sendraw(handle, "echo something\n")
1170 call assert_equal("something", ch_readraw(handle))
1171 finally
1172 call job_stop(job)
1173 endtry
1174
1175 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1176 call assert_equal("run", job_status(job))
1177 let handle = job_getchannel(job)
1178 try
1179 call ch_sendraw(handle, "echo again\n")
1180 call assert_equal("again", ch_readraw(handle))
1181 finally
1182 call job_stop(job)
1183 endtry
1184endfunc
1185
Bram Moolenaar75f72652016-03-20 22:16:56 +01001186func Test_out_cb()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001187 let dict = {'thisis': 'dict: '}
1188 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001189 if type(a:msg) == v:t_string
1190 let g:Ch_outmsg = self.thisis . a:msg
1191 else
1192 let g:Ch_outobj = a:msg
1193 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001194 endfunc
1195 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001196 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001197 endfunc
1198 let job = job_start(s:python . " test_channel_pipe.py",
1199 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001200 \ 'out_mode': 'json',
1201 \ 'err_cb': dict.errHandler,
1202 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001203 call assert_equal("run", job_status(job))
1204 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001205 let g:Ch_outmsg = ''
1206 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001207 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1208 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001209 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1210 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001211
1212 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001213 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001214 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001215 " For unknown reasons this can be very slow on Mac.
1216 if has('mac')
1217 let timeout = 20000
1218 else
1219 let timeout = 5000
1220 endif
1221 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001222 finally
1223 call job_stop(job)
1224 endtry
1225endfunc
1226
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001227func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001228 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001229 let g:Ch_msg1 = ''
1230 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001231 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001232 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001233 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001234 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001235 let s:counter += 1
1236 endfunc
1237 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001238 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001239 let s:counter += 1
1240 endfunc
1241 let job = job_start(s:python . " test_channel_pipe.py quit now",
1242 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001243 \ 'close_cb': 'CloseHandler'})
1244 " the job may be done quickly, also accept "dead"
1245 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001246 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001247 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1248 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001249 finally
1250 call job_stop(job)
1251 delfunc OutHandler
1252 delfunc CloseHandler
1253 endtry
1254endfunc
1255
Bram Moolenaar437905c2016-04-26 19:01:05 +02001256func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001257 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001258 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001259 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001260 endfunc
1261 let job = job_start(s:python . " test_channel_pipe.py quit now",
1262 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001263 " the job may be done quickly, also accept "dead"
1264 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001265 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001266 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001267 finally
1268 call job_stop(job)
1269 delfunc CloseHandler
1270 endtry
1271endfunc
1272
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001273" Use channel in NL mode but received text does not end in NL.
1274func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001275 let g:Ch_received = ''
1276 func! CloseHandler(chan)
1277 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1278 let g:Ch_received .= ch_read(a:chan)
1279 endwhile
1280 endfunc
1281 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1282 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001283 " the job may be done quickly, also accept "dead"
1284 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001285 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001286 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001287 finally
1288 call job_stop(job)
1289 delfunc CloseHandler
1290 endtry
1291endfunc
1292
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001293func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001294 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001295 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1296 \ 'out_mode': 'json',
1297 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1298 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001299 call assert_equal("run", job_status(job))
1300 try
1301 let g:Ch_outmsg = ''
1302 let g:Ch_errmsg = ''
1303 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1304 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001305 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1306 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001307 finally
1308 call job_stop(job)
1309 endtry
1310endfunc
1311
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001312func Test_close_and_exit_cb()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001313 let g:retdict = {'ret': {}}
1314 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001315 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001316 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001317 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001318 let self.ret['exit_cb'] = job_status(a:job)
1319 endfunc
1320
Bram Moolenaare2956092019-01-25 21:01:17 +01001321 let job = job_start([&shell, &shellcmdflag, 'echo'],
1322 \ {'close_cb': g:retdict.close_cb,
1323 \ 'exit_cb': g:retdict.exit_cb})
1324 " the job may be done quickly, also accept "dead"
1325 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001326 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001327 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001328 call assert_equal('dead', g:retdict.ret['exit_cb'])
1329 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001330endfunc
1331
Bram Moolenaard46ae142016-02-16 13:33:52 +01001332""""""""""
1333
Bram Moolenaar0b146882018-09-06 16:27:24 +02001334function ExitCbWipe(job, status)
1335 exe g:wipe_buf 'bw!'
1336endfunction
1337
1338" This caused a crash, because messages were handled while peeking for a
1339" character.
1340func Test_exit_cb_wipes_buf()
1341 if !has('timers')
1342 return
1343 endif
1344 set cursorline lazyredraw
1345 call test_override('redraw_flag', 1)
1346 new
1347 let g:wipe_buf = bufnr('')
1348
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001349 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001350 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001351 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1352 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1353 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1354 call timer_stop(timer)
1355
1356 set nocursorline nolazyredraw
1357 unlet g:wipe_buf
1358 call test_override('ALL', 0)
1359endfunc
1360
1361""""""""""
1362
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001363let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001364func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001365 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001366 unlet s:channelfd
1367endfunc
1368
1369" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001370func Ch_unlet_handle(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001371 let s:channelfd = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001372 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001373 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001374endfunc
1375
1376func Test_unlet_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001377 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001378endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001379
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001380func Test_unlet_handle_ipv6()
1381 CheckIPv6
1382 call Test_unlet_handle()
1383endfunc
1384
Bram Moolenaard46ae142016-02-16 13:33:52 +01001385""""""""""
1386
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001387let g:Ch_unletResponse = ''
1388func Ch_CloseHandler(handle, msg)
1389 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001390 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001391endfunc
1392
1393" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001394func Ch_close_handle(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001395 let s:channelfd = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001396 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001397 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001398endfunc
1399
1400func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001401 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001402endfunc
1403
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001404func Test_close_handle_ipv6()
1405 CheckIPv6
1406 call Test_close_handle()
1407endfunc
1408
1409""""""""""
1410
1411func Ch_open_ipv6(port)
1412 let handle = ch_open('[::1]:' .. a:port, s:chopt)
1413 call assert_notequal('fail', ch_status(handle))
1414endfunc
1415
1416func Test_open_ipv6()
1417 CheckIPv6
1418 call s:run_server('Ch_open_ipv6')
1419endfunc
1420
Bram Moolenaard46ae142016-02-16 13:33:52 +01001421""""""""""
1422
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001423func Test_open_fail()
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001424 call assert_fails("let ch = ch_open('noserver')", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001425 echo ch
1426 let d = ch
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001427 call assert_fails("let ch = ch_open('noserver', 10)", 'E474:')
1428 call assert_fails("let ch = ch_open('localhost:-1')", 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001429 call assert_fails("let ch = ch_open('localhost:65537')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001430 call assert_fails("let ch = ch_open('localhost:8765', {'timeout' : -1})",
1431 \ 'E474:')
1432 call assert_fails("let ch = ch_open('localhost:8765', {'axby' : 1})",
1433 \ 'E475:')
1434 call assert_fails("let ch = ch_open('localhost:8765', {'mode' : 'abc'})",
1435 \ 'E475:')
1436 call assert_fails("let ch = ch_open('localhost:8765', {'part' : 'out'})",
1437 \ 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001438 call assert_fails("let ch = ch_open('[::]')", 'E475:')
1439 call assert_fails("let ch = ch_open('[::.80')", 'E475:')
1440 call assert_fails("let ch = ch_open('[::]8080')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001441endfunc
1442
1443func Test_ch_info_fail()
1444 call assert_fails("let x = ch_info(10)", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001445endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001446
1447""""""""""
1448
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001449func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001450 " Wait up to a second for the port to open.
1451 let s:chopt.waittime = 1000
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001452 let channel = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001453 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001454 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001455 return
1456 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001457 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001458 call ch_close(channel)
1459endfunc
1460
1461func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001462 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001463 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001464endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001465
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001466func Test_open_delay_ipv6()
1467 CheckIPv6
1468 call Test_open_delay()
1469endfunc
1470
Bram Moolenaarece61b02016-02-20 21:39:05 +01001471"""""""""
1472
1473function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001474 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001475endfunc
1476
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001477function Ch_test_call(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001478 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001479 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001480 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001481 return
1482 endif
1483
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001484 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001485 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001486 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001487
1488 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'callback' : 'abc'})", 'E917:')
1489 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'drop' : ''})", 'E475:')
1490 call assert_fails("let i = ch_evalexpr(test_null_job(), '2 + 2')", 'E906:')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001491endfunc
1492
1493func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001494 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001495endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001496
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001497func Test_call_ipv6()
1498 CheckIPv6
1499 call Test_call()
1500endfunc
1501
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001502"""""""""
1503
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001504let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001505function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001506 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001507endfunc
1508
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001509function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001510 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001511 let g:Ch_exit_job = g:currentJob
1512 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001513endfunc
1514
1515func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001516 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001517
Bram Moolenaarf386f082019-07-29 23:03:03 +02001518 " wait up to a second for the job to exit
1519 for i in range(100)
1520 if g:Ch_job_exit_ret == 'done'
1521 break
1522 endif
1523 sleep 10m
1524 " calling job_status() triggers the callback
1525 call job_status(g:Ch_exit_job)
1526 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001527
Bram Moolenaarf386f082019-07-29 23:03:03 +02001528 call assert_equal('done', g:Ch_job_exit_ret)
1529 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1530 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001531endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001532
Bram Moolenaar97792de2016-10-15 18:36:49 +02001533function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001534 if job_info(a:job).process == g:exit_cb_val.process
1535 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1536 endif
1537 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001538endfunction
1539
1540func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001541 CheckFunction reltimefloat
1542
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001543 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001544 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001545 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001546 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001547 let elapsed = reltimefloat(g:exit_cb_val.end)
1548 call assert_true(elapsed > 0.5)
1549 call assert_true(elapsed < 1.0)
1550
1551 " case: unreferenced job, using timer
1552 if !has('timers')
1553 return
1554 endif
1555
1556 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1557 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1558 let g:exit_cb_val.process = job_info(g:job).process
1559 unlet g:job
1560 call Standby(1000)
1561 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1562 let elapsed = reltimefloat(g:exit_cb_val.end)
1563 else
1564 let elapsed = 1.0
1565 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001566 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001567endfunc
1568
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001569"""""""""
1570
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001571let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001572function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001573 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001574endfunc
1575
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001576function Ch_test_close_callback(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001577 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001578 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001579 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001580 return
1581 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001582 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001583
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001584 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001585 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001586endfunc
1587
1588func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001589 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001590endfunc
1591
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001592func Test_close_callback_ipv6()
1593 CheckIPv6
1594 call Test_close_callback()
1595endfunc
1596
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001597function Ch_test_close_partial(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001598 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001599 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001600 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001601 return
1602 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001603 let g:Ch_d = {}
1604 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001605 let self.close_ret = 'closed'
1606 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001607 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001608
1609 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001610 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001611 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001612endfunc
1613
1614func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001615 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001616endfunc
1617
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001618func Test_close_partial_ipv6()
1619 CheckIPv6
1620 call Test_close_partial()
1621endfunc
1622
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001623func Test_job_start_fails()
1624 " this was leaking memory
1625 call assert_fails("call job_start([''])", "E474:")
Bram Moolenaar80385682016-03-27 19:13:35 +02001626 call assert_fails('call job_start($x)', 'E474:')
1627 call assert_fails('call job_start("")', 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001628 call assert_fails('call job_start("ls", {"out_io" : "abc"})', 'E475:')
1629 call assert_fails('call job_start("ls", {"err_io" : "abc"})', 'E475:')
1630 call assert_fails('call job_start("ls", [])', 'E715:')
1631 call assert_fails("call job_start('ls', {'in_top' : -1})", 'E475:')
1632 call assert_fails("call job_start('ls', {'in_bot' : -1})", 'E475:')
1633 call assert_fails("call job_start('ls', {'channel' : -1})", 'E475:')
1634 call assert_fails("call job_start('ls', {'callback' : -1})", 'E475:')
1635 call assert_fails("call job_start('ls', {'out_cb' : -1})", 'E475:')
1636 call assert_fails("call job_start('ls', {'err_cb' : -1})", 'E475:')
1637 call assert_fails("call job_start('ls', {'close_cb' : -1})", 'E475:')
1638 call assert_fails("call job_start('ls', {'exit_cb' : -1})", 'E475:')
1639 call assert_fails("call job_start('ls', {'term_name' : []})", 'E475:')
1640 call assert_fails("call job_start('ls', {'term_finish' : 'run'})", 'E475:')
1641 call assert_fails("call job_start('ls', {'term_api' : []})", 'E475:')
1642 call assert_fails("call job_start('ls', {'stoponexit' : []})", 'E475:')
1643 call assert_fails("call job_start('ls', {'in_io' : 'file'})", 'E920:')
1644 call assert_fails("call job_start('ls', {'out_io' : 'file'})", 'E920:')
1645 call assert_fails("call job_start('ls', {'err_io' : 'file'})", 'E920:')
1646 call assert_fails("call job_start('ls', {'in_mode' : 'abc'})", 'E475:')
1647 call assert_fails("call job_start('ls', {'out_mode' : 'abc'})", 'E475:')
1648 call assert_fails("call job_start('ls', {'err_mode' : 'abc'})", 'E475:')
1649 call assert_fails("call job_start('ls',
1650 \ {'in_io' : 'buffer', 'in_buf' : 99999})", 'E86:')
1651 call assert_fails("call job_start('ls',
1652 \ {'out_io' : 'buffer', 'out_buf' : 99999})", 'E86:')
1653 call assert_fails("call job_start('ls',
1654 \ {'err_io' : 'buffer', 'err_buf' : 99999})", 'E86:')
1655
1656 call assert_fails("call job_start('ls',
1657 \ {'in_io' : 'buffer', 'in_buf' : -1})", 'E475:')
1658 call assert_fails("call job_start('ls',
1659 \ {'out_io' : 'buffer', 'out_buf' : -1})", 'E475:')
1660 call assert_fails("call job_start('ls',
1661 \ {'err_io' : 'buffer', 'err_buf' : -1})", 'E475:')
1662
1663 set nomodifiable
1664 call assert_fails("call job_start('cmd /c dir',
1665 \ {'out_io' : 'buffer', 'out_buf' :" .. bufnr() .. "})", 'E21:')
1666 call assert_fails("call job_start('cmd /c dir',
1667 \ {'err_io' : 'buffer', 'err_buf' :" .. bufnr() .. "})", 'E21:')
1668 set modifiable
1669
1670 call assert_fails("call job_start('ls', {'in_io' : 'buffer'})", 'E915:')
1671
1672 edit! XXX
1673 let bnum = bufnr()
1674 enew
1675 call assert_fails("call job_start('ls',
1676 \ {'in_io' : 'buffer', 'in_buf' : bnum})", 'E918:')
1677
1678 " Empty job tests
1679 " This was crashing on MS-Windows.
1680 call assert_fails('let job = job_start([""])', 'E474:')
1681 call assert_fails('let job = job_start([" "])', 'E474:')
1682 call assert_fails('let job = job_start("")', 'E474:')
1683 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaar00157952020-04-13 17:44:47 +02001684 call assert_fails('let job = job_start(["ls", []])', 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001685 %bw!
Bram Moolenaar80385682016-03-27 19:13:35 +02001686endfunc
1687
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001688func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001689 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001690 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001691 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001692 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001693 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001694 call job_stop(g:job, 'kill')
1695 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001696 endtry
1697endfunc
1698
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001699" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001700func Test_partial_in_channel_cycle()
1701 let d = {}
1702 let d.a = function('string', [d])
1703 try
1704 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1705 catch
1706 call assert_exception('E901:')
1707 endtry
1708 unlet d
1709endfunc
1710
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001711func Test_using_freed_memory()
1712 let g:a = job_start(['ls'])
1713 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001714 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001715endfunc
1716
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001717func Test_collapse_buffers()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001718 CheckExecutable cat
1719
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001720 sp test_channel.vim
1721 let g:linecount = line('$')
1722 close
1723 split testout
1724 1,$delete
1725 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001726 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001727 bwipe!
1728endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001729
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001730func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001731 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001732 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001733
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001734 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001735 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001736 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001737 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1738 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001739
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001740 bdel test_buffer
1741 call assert_equal([], getbufline(bufnr, 1, '$'))
1742
1743 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001744 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001745 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1746 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1747
1748 bwipe! test_buffer
1749endfunc
1750
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001751func Test_cmd_parsing()
1752 if !has('unix')
1753 return
1754 endif
1755 call assert_false(filereadable("file with space"))
1756 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001757 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001758 call delete("file with space")
1759
1760 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001761 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001762 call delete("file with space")
1763endfunc
1764
Bram Moolenaar82117982016-08-27 19:21:48 +02001765func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001766 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001767
1768 " Test lines from the job containing NUL are stored correctly in a buffer.
1769 new
1770 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1771 w! Xtestread
1772 bwipe!
1773 split testout
1774 1,$delete
1775 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1776 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001777 call assert_equal("asdf\nasdf", getline(1))
1778 call assert_equal("xxx\n", getline(2))
1779 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001780
1781 call delete('Xtestread')
1782 bwipe!
1783
1784 " Test lines from a buffer with NUL bytes are written correctly to the job.
1785 new mybuffer
1786 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1787 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001788 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001789 bwipe!
1790 split Xtestwrite
1791 call assert_equal("asdf\nasdf", getline(1))
1792 call assert_equal("xxx\n", getline(2))
1793 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001794 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001795
1796 call delete('Xtestwrite')
1797 bwipe!
1798endfunc
1799
Bram Moolenaarec68a992016-10-03 21:37:41 +02001800func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001801 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001802 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001803 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001804 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001805 unlet g:linecount
1806endfunc
1807
1808func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001809 func s:close_cb(ch)
1810 while ch_status(a:ch) == 'buffered'
1811 let g:out .= ch_read(a:ch)
1812 endwhile
1813 endfunc
1814
1815 let g:out = ''
1816 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1817 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
1818 call WaitForAssert({-> assert_equal('123', g:out)})
1819 unlet g:out
1820 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001821endfunc
1822
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001823func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001824 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001825 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001826 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001827 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001828 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001829endfunc
1830
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001831func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001832 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001833
1834 " Check that backslash in $COMSPEC is handled properly.
1835 let g:echostr = ''
1836 let cmd = $COMSPEC . ' /c echo 123'
1837 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1838 let info = job_info(job)
1839 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1840
1841 call WaitForAssert({-> assert_equal("123", g:echostr)})
1842 unlet g:echostr
1843endfunc
1844
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001845func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001846 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001847 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001848 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001849 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001850 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001851 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001852 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1853 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001854 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001855 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001856endfunc
1857
1858func Test_cwd()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001859 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001860 if has('win32')
1861 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001862 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001863 else
1864 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001865 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001866 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001867 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001868 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001869 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001870 let expect = substitute(expect, '[/\\]$', '', '')
1871 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1872 if $CI != '' && stridx(g:envstr, '/private/') == 0
1873 let g:envstr = g:envstr[8:]
1874 endif
1875 call assert_equal(expect, g:envstr)
1876 finally
1877 call job_stop(job)
1878 unlet g:envstr
1879 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001880endfunc
1881
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001882function Ch_test_close_lambda(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001883 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001884 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001885 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001886 return
1887 endif
1888 let g:Ch_close_ret = ''
1889 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1890
1891 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001892 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001893endfunc
1894
1895func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001896 call s:run_server('Ch_test_close_lambda')
1897endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001898
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001899func Test_close_lambda_ipv6()
1900 CheckIPv6
1901 call Test_close_lambda()
1902endfunc
1903
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001904func s:test_list_args(cmd, out, remove_lf)
1905 try
1906 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001907 let job = job_start([s:python, '-c', a:cmd], {'callback': {ch, msg -> execute('let g:out .= msg')}, 'out_mode': 'raw'})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001908 call WaitFor('"" != g:out')
1909 if has('win32')
1910 let g:out = substitute(g:out, '\r', '', 'g')
1911 endif
1912 if a:remove_lf
1913 let g:out = substitute(g:out, '\n$', '', 'g')
1914 endif
1915 call assert_equal(a:out, g:out)
1916 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001917 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001918 unlet g:out
1919 endtry
1920endfunc
1921
1922func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001923 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1924 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1925 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1926 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1927 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1928 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1929 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1930 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1931 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1932 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1933 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1934 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1935 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1936
1937 " tests which not contain spaces in the argument
1938 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1939 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1940 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1941 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1942 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1943 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1944 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1945 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1946 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1947endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001948
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001949func Test_keep_pty_open()
1950 if !has('unix')
1951 return
1952 endif
1953
Bram Moolenaare2956092019-01-25 21:01:17 +01001954 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1955 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001956 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1957 call assert_inrange(200, 1000, elapsed)
1958 call job_stop(job)
1959endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001960
1961func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001962 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01001963 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01001964
1965 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001966 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001967 endfunc
1968
1969 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001970 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001971 call Resume()
1972 endfunc
1973
1974 func TimerCb(timer)
1975 if has('win32')
1976 let cmd = ['cmd', '/c', 'echo.']
1977 else
1978 let cmd = ['echo']
1979 endif
1980 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1981 call substitute(repeat('a', 100000), '.', '', 'g')
1982 endfunc
1983
1984 " We should be interrupted before 'updatetime' elapsed.
1985 let g:val = 0
1986 call timer_start(1, 'TimerCb')
1987 let elapsed = Standby(&ut)
1988 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001989
1990 " Wait for both OutCb() and ExitCb() to have been called before deleting
1991 " them.
1992 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01001993 call job_stop(g:job)
1994
1995 delfunc OutCb
1996 delfunc ExitCb
1997 delfunc TimerCb
1998 unlet! g:val
1999 unlet! g:job
2000endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01002001
2002func Test_raw_large_data()
2003 try
2004 let g:out = ''
2005 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01002006 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
2007 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01002008
Bram Moolenaare2956092019-01-25 21:01:17 +01002009 let outlen = 79999
2010 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02002011 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01002012 call WaitFor({-> len(g:out) >= outlen}, 10000)
2013 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01002014 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2015 finally
2016 call job_stop(job)
2017 unlet g:out
2018 endtry
2019endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002020
Bram Moolenaar65240682019-02-10 22:23:26 +01002021func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002022 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01002023
2024 try
2025 let job = job_start(s:python . " test_channel_pipe.py busy",
2026 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
2027 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
2028 finally
2029 call job_stop(job)
2030 endtry
2031endfunc
2032
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002033func Test_job_exitval_and_termsig()
2034 if !has('unix')
2035 return
2036 endif
2037
2038 " Terminate job normally
2039 let cmd = ['echo']
2040 let job = job_start(cmd)
2041 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2042 let info = job_info(job)
2043 call assert_equal(0, info.exitval)
2044 call assert_equal("", info.termsig)
2045
2046 " Terminate job by signal
2047 let cmd = ['sleep', '10']
2048 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01002049 " 10m usually works but 50m is needed when running Valgrind
2050 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002051 call job_stop(job)
2052 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2053 let info = job_info(job)
2054 call assert_equal(-1, info.exitval)
2055 call assert_equal("term", info.termsig)
2056endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01002057
2058func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002059 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01002060
2061 call writefile(['test'], 'Xtestin')
2062 let in_opts = [{},
2063 \ {'in_io': 'null'},
2064 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
2065 let out_opts = [{},
2066 \ {'out_io': 'null'},
2067 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
2068 let err_opts = [{},
2069 \ {'err_io': 'null'},
2070 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
2071 \ {'err_io': 'out'}]
2072 let opts = []
2073
2074 for in_opt in in_opts
2075 let x = copy(in_opt)
2076 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002077 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002078 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002079 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002080 let opts += [extend({'pty': 1}, x)]
2081 endfor
2082 endfor
2083 endfor
2084
2085 for opt in opts
2086 let job = job_start('echo', opt)
2087 let info = job_info(job)
2088 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2089 \ get(opt, 'in_io', 'tty'),
2090 \ get(opt, 'out_io', 'tty'),
2091 \ get(opt, 'err_io', 'tty'))
2092
2093 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2094 call assert_notequal('', info.tty_in, msg)
2095 else
2096 call assert_equal('', info.tty_in, msg)
2097 endif
2098 call assert_equal(info.tty_in, info.tty_out, msg)
2099
2100 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2101 endfor
2102
2103 call delete('Xtestin')
2104 call delete('Xtestout')
2105 call delete('Xtesterr')
2106endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02002107
2108" Do this last, it stops any channel log.
2109func Test_zz_nl_err_to_out_pipe()
Bram Moolenaar570497a2019-08-22 22:55:13 +02002110 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02002111 call ch_log('Test_zz_nl_err_to_out_pipe()')
2112 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
2113 call assert_equal("run", job_status(job))
2114 try
2115 let handle = job_getchannel(job)
2116 call ch_sendraw(handle, "echo something\n")
2117 call assert_equal("something", ch_readraw(handle))
2118
2119 call ch_sendraw(handle, "echoerr wrong\n")
2120 call assert_equal("wrong", ch_readraw(handle))
2121 finally
2122 call job_stop(job)
2123 call ch_logfile('')
2124 let loglines = readfile('Xlog')
2125 call assert_true(len(loglines) > 10)
2126 let found_test = 0
2127 let found_send = 0
2128 let found_recv = 0
2129 let found_stop = 0
2130 for l in loglines
2131 if l =~ 'Test_zz_nl_err_to_out_pipe'
2132 let found_test = 1
2133 endif
2134 if l =~ 'SEND on.*echo something'
2135 let found_send = 1
2136 endif
2137 if l =~ 'RECV on.*something'
2138 let found_recv = 1
2139 endif
2140 if l =~ 'Stopping job with'
2141 let found_stop = 1
2142 endif
2143 endfor
2144 call assert_equal(1, found_test)
2145 call assert_equal(1, found_send)
2146 call assert_equal(1, found_recv)
2147 call assert_equal(1, found_stop)
2148 " On MS-Windows need to sleep for a moment to be able to delete the file.
2149 sleep 10m
2150 call delete('Xlog')
2151 endtry
2152endfunc
2153
2154" Do this last, it stops any channel log.
2155func Test_zz_ch_log()
2156 call ch_logfile('Xlog', 'w')
2157 call ch_log('hello there')
2158 call ch_log('%s%s')
2159 call ch_logfile('')
2160 let text = readfile('Xlog')
2161 call assert_match("hello there", text[1])
2162 call assert_match("%s%s", text[2])
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002163 call mkdir("Xdir1")
2164 call assert_fails("call ch_logfile('Xdir1')", 'E484:')
2165 cal delete("Xdir1", 'd')
Bram Moolenaarf386f082019-07-29 23:03:03 +02002166 call delete('Xlog')
2167endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002168
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002169func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002170 if has('win32')
2171 let cmd = 'cmd /c pause'
2172 else
2173 let cmd = 'grep foo'
2174 endif
2175 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002176 call job_stop(g:job)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002177 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002178 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002179 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002180 call job_stop(g:job, 'term')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002181 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002182 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002183 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002184 call job_stop(g:job, 'kill')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002185 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002186 call assert_equal(-1, job_info(g:job).exitval)
2187endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002188
2189func Test_issue_5485()
2190 let $VAR1 = 'global'
2191 let g:Ch_reply = ""
2192 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2193 let g:Ch_job = l:job
2194 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2195 unlet $VAR1
2196endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002197
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002198func Test_job_trailing_space_unix()
2199 CheckUnix
2200 CheckExecutable cat
2201 let job = job_start("cat ", #{in_io: 'null'})
2202 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2203 call assert_equal(0, job_info(job).exitval)
2204endfunc
2205
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002206func Test_ch_getbufnr()
2207 let ch = test_null_channel()
2208 call assert_equal(-1, ch_getbufnr(ch, 'in'))
2209 call assert_equal(-1, ch_getbufnr(ch, 'out'))
2210 call assert_equal(-1, ch_getbufnr(ch, 'err'))
2211 call assert_equal(-1, ch_getbufnr(ch, ''))
2212endfunc
2213
2214" Test for unsupported options passed to ch_status()
2215func Test_invalid_job_chan_options()
2216 let ch = test_null_channel()
2217 let invalid_opts = [
2218 \ {'in_io' : 'null'},
2219 \ {'out_io' : 'null'},
2220 \ {'err_io' : 'null'},
2221 \ {'mode' : 'json'},
2222 \ {'out_mode' : 'json'},
2223 \ {'err_mode' : 'json'},
2224 \ {'noblock' : 1},
2225 \ {'in_name' : '/a/b'},
2226 \ {'pty' : 1},
2227 \ {'in_buf' : 1},
2228 \ {'out_buf' : 1},
2229 \ {'err_buf' : 1},
2230 \ {'out_modifiable' : 1},
2231 \ {'err_modifiable' : 1},
2232 \ {'out_msg' : 1},
2233 \ {'err_msg' : 1},
2234 \ {'in_top' : 1},
2235 \ {'in_bot' : 1},
2236 \ {'channel' : ch},
2237 \ {'callback' : ''},
2238 \ {'out_cb' : ''},
2239 \ {'err_cb' : ''},
2240 \ {'close_cb' : ''},
2241 \ {'exit_cb' : ''},
2242 \ {'term_opencmd' : ''},
2243 \ {'eof_chars' : ''},
2244 \ {'term_rows' : 10},
2245 \ {'term_cols' : 10},
2246 \ {'vertical' : 0},
2247 \ {'curwin' : 1},
2248 \ {'bufnr' : 1},
2249 \ {'hidden' : 0},
2250 \ {'norestore' : 0},
2251 \ {'term_kill' : 'kill'},
2252 \ {'tty_type' : ''},
2253 \ {'term_highlight' : ''},
2254 \ {'env' : {}},
2255 \ {'cwd' : ''},
2256 \ {'timeout' : 0},
2257 \ {'out_timeout' : 0},
2258 \ {'err_timeout' : 0},
2259 \ {'id' : 0},
2260 \ {'stoponexit' : ''},
2261 \ {'block_write' : 1}
2262 \ ]
2263 if has('gui')
2264 call add(invalid_opts, {'ansi_colors' : []})
2265 endif
2266
2267 for opt in invalid_opts
2268 call assert_fails("let x = ch_status(ch, opt)", 'E475:')
2269 endfor
2270endfunc
2271
2272" Test for passing the command and the arguments as List on MS-Windows
2273func Test_job_with_list_args()
2274 CheckMSWindows
2275
2276 enew!
2277 let bnum = bufnr()
2278 let job = job_start(['cmd', '/c', 'echo', 'Hello', 'World'], {'out_io' : 'buffer', 'out_buf' : bnum})
2279 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2280 call assert_equal('Hello World', getline(1))
2281 %bw!
2282endfunc
2283
Bram Moolenaar8b633132020-03-20 18:20:51 +01002284" vim: shiftwidth=2 sts=2 expandtab