blob: 4a438ea7292892f4396a5afa0bbc95729e3ff071 [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 Moolenaarad48e6c2020-04-21 22:19:45 +0200170 call assert_equal(0, ch_setoptions(handle, test_null_dict()))
171 call assert_equal(0, ch_setoptions(test_null_channel(), {'drop' : 'never'}))
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100172
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100173 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100174 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100175 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100176 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100177
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100178 " Send an eval request with special characters.
179 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
180 sleep 10m
181 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
182
183 " Send an eval request to get a line with special characters.
184 call setline(3, "a\nb\<CR>c\x01d\x7fe")
185 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
186 sleep 10m
187 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
188
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100189 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100190 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100191 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100192 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100193
Bram Moolenaar55fab432016-02-07 16:53:13 +0100194 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100195 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100196 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100197 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100198
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100199 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100200 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100201 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100202 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100203
Bram Moolenaarf4160862016-02-05 23:09:12 +0100204 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100205 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200206 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100207 call assert_equal('one', getline(line('$') - 2))
208 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100209
210 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100211 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
212 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100213
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100214 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100215
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100216 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100217 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100218 if exists('*reltimefloat')
219 let start = reltime()
220 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
221 let elapsed = reltime(start)
222 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
223 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100224
225 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100226 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100227 let resp = ch_read(handle)
228 call assert_equal(type([]), type(resp))
229 call assert_equal(type(11), type(resp[0]))
230 call assert_equal('waited', resp[1])
231
Bram Moolenaard7ece102016-02-02 23:23:02 +0100232 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100233 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100234endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100235
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100236func Test_communicate()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200237 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100238endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100239
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200240func Test_communicate_ipv6()
241 CheckIPv6
242 call Test_communicate()
243endfunc
244
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100245" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200246func Ch_two_channels(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200247 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200248 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar570497a2019-08-22 22:55:13 +0200249 if handle->ch_status() == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100250 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100251 return
252 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100253
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100254 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100255
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200256 let newhandle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100257 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100258 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100259 return
260 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100261 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
262 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100263
264 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100265 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100266
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100267 call ch_close(newhandle)
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200268 call assert_fails("call ch_close(newhandle)", 'E906:')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100269endfunc
270
271func Test_two_channels()
Bram Moolenaar570497a2019-08-22 22:55:13 +0200272 eval 'Test_two_channels()'->ch_log()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200273 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100274endfunc
275
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200276func Test_two_channels_ipv6()
277 CheckIPv6
278 call Test_two_channels()
279endfunc
280
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100281" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200282func Ch_server_crash(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200283 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100284 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100285 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100286 return
287 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100288
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100289 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100290
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100291 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100292endfunc
293
294func Test_server_crash()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200295 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100296endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100297
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200298func Test_server_crash_ipv6()
299 CheckIPv6
300 call Test_server_crash()
301endfunc
302
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100303"""""""""
304
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200305func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100306 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200307 unlet g:Ch_reply
308 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100309endfunc
310
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200311func Ch_channel_handler(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200312 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100313 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100314 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100315 return
316 endif
317
318 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100319 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200320 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100321
322 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100323 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200324 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100325endfunc
326
327func Test_channel_handler()
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200328 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200329 let s:chopt.callback = 'Ch_handler'
330 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200331 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200332 let s:chopt.callback = function('Ch_handler')
333 call s:run_server('Ch_channel_handler')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200334endfunc
335
336func Test_channel_handler_ipv6()
337 CheckIPv6
338 call Test_channel_handler()
Bram Moolenaarf6157282016-02-10 21:07:14 +0100339endfunc
340
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100341"""""""""
342
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200343let g:Ch_reply = ''
344func Ch_zeroHandler(chan, msg)
345 unlet g:Ch_reply
346 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100347endfunc
348
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200349let g:Ch_zero_reply = ''
350func Ch_oneHandler(chan, msg)
351 unlet g:Ch_zero_reply
352 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100353endfunc
354
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200355func Ch_channel_zero(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200356 let handle = (s:localhost .. a:port)->ch_open(s:chopt)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100357 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100358 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100359 return
360 endif
361
362 " Check that eval works.
363 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
364
365 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200366 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100367 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100368 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200369 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100370 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100371 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200372 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100373 endif
374
375 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200376 let g:Ch_reply = ''
377 let g:Ch_zero_reply = ''
378 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200379 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100380 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200381 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100382 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200383 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100384 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100385endfunc
386
387func Test_zero_reply()
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100388 " Run with channel handler
389 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200390 let s:chopt.callback = 'Ch_zeroHandler'
391 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100392 unlet s:chopt.callback
393
394 " Run without channel handler
395 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200396 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100397endfunc
398
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200399func Test_zero_reply_ipv6()
400 CheckIPv6
401 call Test_zero_reply()
402endfunc
403
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100404"""""""""
405
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200406let g:Ch_reply1 = ""
407func Ch_handleRaw1(chan, msg)
408 unlet g:Ch_reply1
409 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100410endfunc
411
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200412let g:Ch_reply2 = ""
413func Ch_handleRaw2(chan, msg)
414 unlet g:Ch_reply2
415 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100416endfunc
417
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200418let g:Ch_reply3 = ""
419func Ch_handleRaw3(chan, msg)
420 unlet g:Ch_reply3
421 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100422endfunc
423
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200424func Ch_raw_one_time_callback(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200425 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100426 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100427 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100428 return
429 endif
430 call ch_setoptions(handle, {'mode': 'raw'})
431
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100432 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200433 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200434 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200435 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
436 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200437 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100438 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200439 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100440endfunc
441
442func Test_raw_one_time_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200443 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100444endfunc
445
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200446func Test_raw_one_time_callback_ipv6()
447 CheckIPv6
448 call Test_raw_one_time_callback()
449endfunc
450
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100451"""""""""
452
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100453" Test that trying to connect to a non-existing port fails quickly.
454func Test_connect_waittime()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100455 CheckFunction reltimefloat
Bram Moolenaar373a8762020-03-19 19:44:32 +0100456 " this is timing sensitive
457 let g:test_is_flaky = 1
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100458
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100459 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100460 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100461 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100462 " Oops, port does exists.
463 call ch_close(handle)
464 else
465 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100466 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100467 endif
468
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100469 " We intend to use a socket that doesn't exist and wait for half a second
470 " before giving up. If the socket does exist it can fail in various ways.
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100471 " Check for "Connection reset by peer" to avoid flakiness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100472 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100473 try
474 let handle = ch_open('localhost:9867', {'waittime': 500})
475 if ch_status(handle) != "fail"
476 " Oops, port does exists.
477 call ch_close(handle)
478 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100479 " Failed connection should wait about 500 msec. Can be longer if the
480 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100481 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100482 endif
483 catch
484 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100485 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100486 endif
487 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100488endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100489
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100490"""""""""
491
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100492func Test_raw_pipe()
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100493 " Add a dummy close callback to avoid that messages are dropped when calling
494 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200495 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100496 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200497 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200498 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100499 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200500
501 call assert_equal("open", ch_status(job))
502 call assert_equal("open", ch_status(job), {"part": "out"})
503 call assert_equal("open", ch_status(job), {"part": "err"})
504 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
505 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
506
507 let dict = ch_info(job)
508 call assert_true(dict.id != 0)
509 call assert_equal('open', dict.status)
510 call assert_equal('open', dict.out_status)
511 call assert_equal('RAW', dict.out_mode)
512 call assert_equal('pipe', dict.out_io)
513 call assert_equal('open', dict.err_status)
514 call assert_equal('RAW', dict.err_mode)
515 call assert_equal('pipe', dict.err_io)
516
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100517 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100518 " For a change use the job where a channel is expected.
519 call ch_sendraw(job, "echo something\n")
520 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100521 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
522
Bram Moolenaar151f6562016-03-07 21:19:38 +0100523 call ch_sendraw(job, "double this\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200524 let g:handle = job->job_getchannel()
525 call WaitFor('g:handle->ch_canread()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100526 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100527 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100528 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
529
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200530 let g:Ch_reply = ""
531 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200532 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200533
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200534 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'callback' : 'abc'})", 'E917:')
535 call assert_fails("let i = ch_evalexpr(job, '2 + 2')", 'E912:')
536 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'drop' : ''})", 'E475:')
537 call assert_fails("let i = ch_evalraw(test_null_job(), '2 + 2')", 'E906:')
538
Bram Moolenaar570497a2019-08-22 22:55:13 +0200539 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100540 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
541 finally
542 call job_stop(job)
543 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100544
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200545 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200546 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar570497a2019-08-22 22:55:13 +0200547 let info = job->job_info()
Bram Moolenaar8950a562016-03-12 15:22:55 +0100548 call assert_equal("dead", info.status)
549 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200550 call assert_equal(2, len(info.cmd))
551 call assert_equal("test_channel_pipe.py", info.cmd[1])
552
553 let found = 0
554 for j in job_info()
555 if j == job
556 let found += 1
557 endif
558 endfor
559 call assert_equal(1, found)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100560
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200561 call assert_fails("call job_stop('abc')", 'E475:')
562 call assert_fails("call job_stop(job, [])", 'E474:')
563 call assert_fails("call job_stop(test_null_job())", 'E916:')
564
Bram Moolenaar8b633132020-03-20 18:20:51 +0100565 " Try to use the job and channel where a number is expected. This is not
566 " related to testing the raw pipe. This test is here just to reuse the
567 " already created job/channel.
568 let ch = job_getchannel(job)
569 call assert_fails('let i = job + 1', 'E910:')
570 call assert_fails('let j = ch + 1', 'E913:')
571 call assert_fails('echo 2.0 == job', 'E911:')
572 call assert_fails('echo 2.0 == ch', 'E914:')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100573endfunc
574
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100575func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100576 " Add a dummy close callback to avoid that messages are dropped when calling
577 " ch_canread().
578 " Also test the non-blocking option.
579 let job = job_start(s:python . " test_channel_pipe.py",
580 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
581 call assert_equal(v:t_job, type(job))
582 call assert_equal("run", job_status(job))
583
584 call assert_equal("open", ch_status(job))
585 call assert_equal("open", ch_status(job), {"part": "out"})
586
587 try
588 " Create a blob with the echo command and write it.
589 let blob = 0z00
590 let cmd = "echo something\n"
591 for i in range(0, len(cmd) - 1)
592 let blob[i] = char2nr(cmd[i])
593 endfor
594 call assert_equal(len(cmd), len(blob))
595 call ch_sendraw(job, blob)
596
597 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200598 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100599 let expected = 'something'
600 for i in range(0, len(expected) - 1)
601 call assert_equal(char2nr(expected[i]), msg[i])
602 endfor
603
604 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
605 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
606 finally
607 call job_stop(job)
608 endtry
609
610 let g:Ch_job = job
611 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
612 let info = job_info(job)
613 call assert_equal("dead", info.status)
614endfunc
615
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100616func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100617 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100618 call assert_equal("run", job_status(job))
619 try
620 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100621 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200622 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100623
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100624 call ch_sendraw(handle, "echoerr wrong\n")
625 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
626
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100627 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100628 call assert_equal("this", ch_readraw(handle))
629 call assert_equal("AND this", ch_readraw(handle))
630
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200631 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200632 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200633
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100634 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100635 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100636 finally
637 call job_stop(job)
638 endtry
639endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100640
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200641func Stop_g_job()
642 call job_stop(g:job)
643 if has('win32')
644 " On MS-Windows the server must close the file handle before we are able
645 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200646 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200647 sleep 10m
648 endif
649endfunc
650
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100651func Test_nl_read_file()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100652 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200653 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100654 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200655 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100656 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200657 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100658 call assert_equal("something", ch_readraw(handle))
659 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
660 call assert_equal("this", ch_readraw(handle))
661 call assert_equal("AND this", ch_readraw(handle))
662 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200663 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100664 call delete('Xinput')
665 endtry
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200666 call assert_fails("echo ch_read(test_null_channel(), {'callback' : 'abc'})", 'E475:')
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100667endfunc
668
Bram Moolenaare98d1212016-03-08 15:37:41 +0100669func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200670 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100671 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200672 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100673 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200674 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100675 call ch_sendraw(handle, "echo line one\n")
676 call ch_sendraw(handle, "echo line two\n")
677 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200678 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100679 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200680 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100681 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100682 call delete('Xoutput')
683 endtry
684endfunc
685
686func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200687 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100688 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200689 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100690 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200691 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100692 call ch_sendraw(handle, "echoerr line one\n")
693 call ch_sendraw(handle, "echoerr line two\n")
694 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200695 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100696 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200697 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100698 call delete('Xoutput')
699 endtry
700endfunc
701
702func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200703 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100704 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200705 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100706 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200707 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100708 call ch_sendraw(handle, "echoerr line one\n")
709 call ch_sendraw(handle, "echo line two\n")
710 call ch_sendraw(handle, "double this\n")
711 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200712 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100713 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200714 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100715 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100716 call delete('Xoutput')
717 endtry
718endfunc
719
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200720func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200721 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200722endfunc
723
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200724func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200725 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200726 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200727 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100728 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100729 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200730 if a:do_msg
731 let expected[0] = 'Reading from channel output...'
732 else
733 let options['out_msg'] = 0
734 call remove(expected, 0)
735 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100736 else
737 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100738 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100739 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200740 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100741 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200742 if a:nomod
743 let options['out_modifiable'] = 0
744 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100745 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100746 call assert_equal("run", job_status(job))
747 try
748 let handle = job_getchannel(job)
749 call ch_sendraw(handle, "echo line one\n")
750 call ch_sendraw(handle, "echo line two\n")
751 call ch_sendraw(handle, "double this\n")
752 call ch_sendraw(handle, "quit\n")
753 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100754 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200755 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200756 if a:nomod
757 call assert_equal(0, &modifiable)
758 else
759 call assert_equal(1, &modifiable)
760 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200761 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100762 bwipe!
763 finally
764 call job_stop(job)
765 endtry
766endfunc
767
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100768func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200769 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100770endfunc
771
772func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200773 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100774endfunc
775
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200776func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200777 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200778endfunc
779
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200780func Test_pipe_to_buffer_name_nomsg()
781 call Run_test_pipe_to_buffer(1, 0, 1)
782endfunc
783
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200784func Test_close_output_buffer()
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200785 enew!
786 let test_lines = ['one', 'two']
787 call setline(1, test_lines)
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200788 let options = {'out_io': 'buffer'}
789 let options['out_name'] = 'buffer-output'
790 let options['out_msg'] = 0
791 split buffer-output
792 let job = job_start(s:python . " test_channel_write.py", options)
793 call assert_equal("run", job_status(job))
794 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200795 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200796 quit!
797 sleep 100m
798 " Make sure the write didn't happen to the wrong buffer.
799 call assert_equal(test_lines, getline(1, line('$')))
800 call assert_equal(-1, bufwinnr('buffer-output'))
801 sbuf buffer-output
802 call assert_notequal(-1, bufwinnr('buffer-output'))
803 sleep 100m
804 close " no more writes
805 bwipe!
806 finally
807 call job_stop(job)
808 endtry
809endfunc
810
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200811func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100812 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200813 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100814 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100815 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200816 if a:do_msg
817 let expected[0] = 'Reading from channel error...'
818 else
819 let options['err_msg'] = 0
820 call remove(expected, 0)
821 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100822 else
823 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100824 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100825 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200826 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100827 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200828 if a:nomod
829 let options['err_modifiable'] = 0
830 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100831 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100832 call assert_equal("run", job_status(job))
833 try
834 let handle = job_getchannel(job)
835 call ch_sendraw(handle, "echoerr line one\n")
836 call ch_sendraw(handle, "echoerr line two\n")
837 call ch_sendraw(handle, "doubleerr this\n")
838 call ch_sendraw(handle, "quit\n")
839 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200840 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200841 if a:nomod
842 call assert_equal(0, &modifiable)
843 else
844 call assert_equal(1, &modifiable)
845 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100846 bwipe!
847 finally
848 call job_stop(job)
849 endtry
850endfunc
851
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100852func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200853 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100854endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100855
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100856func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200857 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200858endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100859
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200860func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200861 call Run_test_pipe_err_to_buffer(1, 1, 1)
862endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100863
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200864func Test_pipe_err_to_buffer_name_nomsg()
865 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100866endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100867
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100868func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100869 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100870 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100871 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200872 let handle = job_getchannel(job)
873 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'out'))
874 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'err'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100875 try
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100876 call ch_sendraw(handle, "echo line one\n")
877 call ch_sendraw(handle, "echoerr line two\n")
878 call ch_sendraw(handle, "double this\n")
879 call ch_sendraw(handle, "doubleerr that\n")
880 call ch_sendraw(handle, "quit\n")
881 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200882 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 +0100883 bwipe!
884 finally
885 call job_stop(job)
886 endtry
887endfunc
888
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100889func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100890 sp pipe-input
891 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200892 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100893 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100894 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100895 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100896 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100897 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100898
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100899 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100900 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200901 if has('unix') && !a:use_name
902 call assert_equal(bufnr('%'), ch_getbufnr(job, 'in'))
903 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100904 try
905 let handle = job_getchannel(job)
906 call assert_equal('one', ch_read(handle))
907 call assert_equal('two', ch_read(handle))
908 call assert_equal('three', ch_read(handle))
909 bwipe!
910 finally
911 call job_stop(job)
912 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100913endfunc
914
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100915func Test_pipe_from_buffer_name()
916 call Run_test_pipe_from_buffer(1)
917endfunc
918
919func Test_pipe_from_buffer_nr()
920 call Run_test_pipe_from_buffer(0)
921endfunc
922
Bram Moolenaar0874a832016-09-01 15:11:51 +0200923func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200924 CheckExecutable sort
925
Bram Moolenaar0874a832016-09-01 15:11:51 +0200926 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
927 if a:use_buffer
928 split sortin
929 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
930 let options.in_io = 'buffer'
931 let options.in_name = 'sortin'
932 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200933 if !a:all
934 let options.in_top = 2
935 let options.in_bot = 4
936 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100937 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200938
939 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100940 call assert_equal("run", job_status(job))
941 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200942 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200943 endif
944
Bram Moolenaare2956092019-01-25 21:01:17 +0100945 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200946
Bram Moolenaard8b55492016-09-01 14:35:22 +0200947 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200948 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200949 call assert_equal('Reading from channel output...', getline(1))
950 if a:all
951 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
952 else
953 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
954 endif
955
Bram Moolenaare2956092019-01-25 21:01:17 +0100956 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200957 if a:use_buffer
958 bwipe! sortin
959 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200960 bwipe! sortout
961endfunc
962
963func Test_pipe_through_sort_all()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200964 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200965endfunc
966
967func Test_pipe_through_sort_some()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200968 call Run_pipe_through_sort(0, 1)
969endfunc
970
971func Test_pipe_through_sort_feed()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200972 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200973endfunc
974
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100975func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100976 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100977 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100978 call assert_equal("run", job_status(job))
979 try
980 let handle = job_getchannel(job)
981 call ch_sendraw(handle, "echo line one\n")
982 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200983 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200984 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100985 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
986 bwipe!
987 finally
988 call job_stop(job)
989 endtry
990endfunc
991
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100992func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100993 CheckFunction reltimefloat
994
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100995 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100996 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100997 call assert_equal("run", job_status(job))
998 try
999 let handle = job_getchannel(job)
1000 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
1001 call ch_sendraw(handle, "echo [-2, 12.34]\n")
1002 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001003 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001004 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1005 bwipe!
1006 finally
1007 call job_stop(job)
1008 endtry
1009endfunc
1010
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001011" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001012func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001013 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001014 if getline(line('$') - a:offset) == a:line
1015 break
1016 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001017 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001018 endfor
1019endfunc
1020
1021func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001022 " Create two buffers, one to read from and one to write to.
1023 split pipe-output
1024 set buftype=nofile
1025 split pipe-input
1026 set buftype=nofile
1027
1028 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001029 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001030 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1031 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001032 call assert_equal("run", job_status(job))
1033 try
1034 exe "normal Gaecho hello\<CR>"
1035 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001036 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001037 call assert_equal('hello', getline('$'))
1038
1039 exe bufwinnr('pipe-input') . "wincmd w"
1040 exe "normal Gadouble this\<CR>"
1041 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001042 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001043 call assert_equal('this', getline(line('$') - 1))
1044 call assert_equal('AND this', getline('$'))
1045
1046 bwipe!
1047 exe bufwinnr('pipe-input') . "wincmd w"
1048 bwipe!
1049 finally
1050 call job_stop(job)
1051 endtry
1052endfunc
1053
1054func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001055 " Create one buffer to read from and to write to.
1056 split pipe-io
1057 set buftype=nofile
1058
1059 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001060 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001061 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1062 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001063 call assert_equal("run", job_status(job))
1064 try
1065 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001066 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001067 call assert_equal('hello', getline(line('$') - 1))
1068
1069 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001070 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001071 call assert_equal('this', getline(line('$') - 2))
1072 call assert_equal('AND this', getline(line('$') - 1))
1073
1074 bwipe!
1075 finally
1076 call job_stop(job)
1077 endtry
1078endfunc
1079
Bram Moolenaar4641a122019-07-29 22:10:23 +02001080func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001081 CheckScreendump
1082
Bram Moolenaar4641a122019-07-29 22:10:23 +02001083 let lines =<< trim END
1084 new Xscrollbuffer
1085 call setline(1, range(1, 200))
1086 $
1087 redraw
1088 wincmd w
1089 call deletebufline('Xscrollbuffer', 1, '$')
1090 if has('win32')
1091 let cmd = ['cmd', '/c', 'echo sometext']
1092 else
1093 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1094 endif
1095 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1096 END
1097 call writefile(lines, 'XtestBufferScroll')
1098 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001099 call TermWait(buf, 50)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001100 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1101
1102 " clean up
1103 call StopVimInTerminal(buf)
1104 call delete('XtestBufferScroll')
1105endfunc
1106
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001107func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001108 " We cannot check that no I/O works, we only check that the job starts
1109 " properly.
1110 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001111 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001112 call assert_equal("run", job_status(job))
1113 try
1114 call assert_equal('something', ch_read(job))
1115 finally
1116 call job_stop(job)
1117 endtry
1118
1119 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001120 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001121 call assert_equal("run", job_status(job))
1122 try
1123 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1124 finally
1125 call job_stop(job)
1126 endtry
1127
1128 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001129 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001130 call assert_equal("run", job_status(job))
1131 try
1132 call assert_equal('something', ch_read(job))
1133 finally
1134 call job_stop(job)
1135 endtry
1136
1137 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001138 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001139 call assert_equal("run", job_status(job))
1140 call job_stop(job)
1141
1142 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001143 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001144 call assert_equal("run", job_status(job))
1145 call assert_equal('channel fail', string(job_getchannel(job)))
1146 call assert_equal('fail', ch_status(job))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001147 call assert_equal('no process', string(test_null_job()))
1148 call assert_equal('channel fail', string(test_null_channel()))
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001149 call job_stop(job)
1150endfunc
1151
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001152func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001153 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1154 split testout
1155 let job = job_start([s:python, '-c',
1156 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001157 " the job may be done quickly, also accept "dead"
1158 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001159 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001160 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001161 let totlen = 0
1162 for line in getline(1, '$')
1163 call assert_equal('', substitute(line, '^\.*', '', ''))
1164 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001165 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001166 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001167 finally
1168 call job_stop(job)
1169 bwipe!
1170 endtry
1171endfunc
1172
Bram Moolenaarde279892016-03-11 22:19:44 +01001173func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001174 let job = job_start(s:python . " test_channel_pipe.py")
1175 call assert_equal("run", job_status(job))
1176 let handle = job_getchannel(job)
1177 try
1178 call ch_sendraw(handle, "echo something\n")
1179 call assert_equal("something", ch_readraw(handle))
1180 finally
1181 call job_stop(job)
1182 endtry
1183
1184 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1185 call assert_equal("run", job_status(job))
1186 let handle = job_getchannel(job)
1187 try
1188 call ch_sendraw(handle, "echo again\n")
1189 call assert_equal("again", ch_readraw(handle))
1190 finally
1191 call job_stop(job)
1192 endtry
1193endfunc
1194
Bram Moolenaar75f72652016-03-20 22:16:56 +01001195func Test_out_cb()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001196 let dict = {'thisis': 'dict: '}
1197 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001198 if type(a:msg) == v:t_string
1199 let g:Ch_outmsg = self.thisis . a:msg
1200 else
1201 let g:Ch_outobj = a:msg
1202 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001203 endfunc
1204 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001205 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001206 endfunc
1207 let job = job_start(s:python . " test_channel_pipe.py",
1208 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001209 \ 'out_mode': 'json',
1210 \ 'err_cb': dict.errHandler,
1211 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001212 call assert_equal("run", job_status(job))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001213 call test_garbagecollect_now()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001214 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001215 let g:Ch_outmsg = ''
1216 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001217 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1218 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001219 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1220 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001221
1222 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001223 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001224 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001225 " For unknown reasons this can be very slow on Mac.
1226 if has('mac')
1227 let timeout = 20000
1228 else
1229 let timeout = 5000
1230 endif
1231 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001232 finally
1233 call job_stop(job)
1234 endtry
1235endfunc
1236
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001237func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001238 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001239 let g:Ch_msg1 = ''
1240 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001241 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001242 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001243 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001244 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001245 let s:counter += 1
1246 endfunc
1247 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001248 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001249 let s:counter += 1
1250 endfunc
1251 let job = job_start(s:python . " test_channel_pipe.py quit now",
1252 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001253 \ 'close_cb': 'CloseHandler'})
1254 " the job may be done quickly, also accept "dead"
1255 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001256 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001257 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1258 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001259 finally
1260 call job_stop(job)
1261 delfunc OutHandler
1262 delfunc CloseHandler
1263 endtry
1264endfunc
1265
Bram Moolenaar437905c2016-04-26 19:01:05 +02001266func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001267 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001268 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001269 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001270 endfunc
1271 let job = job_start(s:python . " test_channel_pipe.py quit now",
1272 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001273 " the job may be done quickly, also accept "dead"
1274 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001275 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001276 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001277 finally
1278 call job_stop(job)
1279 delfunc CloseHandler
1280 endtry
1281endfunc
1282
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001283" Use channel in NL mode but received text does not end in NL.
1284func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001285 let g:Ch_received = ''
1286 func! CloseHandler(chan)
1287 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1288 let g:Ch_received .= ch_read(a:chan)
1289 endwhile
1290 endfunc
1291 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1292 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001293 " the job may be done quickly, also accept "dead"
1294 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001295 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001296 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001297 finally
1298 call job_stop(job)
1299 delfunc CloseHandler
1300 endtry
1301endfunc
1302
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001303func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001304 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001305 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1306 \ 'out_mode': 'json',
1307 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1308 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001309 call assert_equal("run", job_status(job))
1310 try
1311 let g:Ch_outmsg = ''
1312 let g:Ch_errmsg = ''
1313 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1314 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001315 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1316 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001317 finally
1318 call job_stop(job)
1319 endtry
1320endfunc
1321
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001322func Test_close_and_exit_cb()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001323 let g:retdict = {'ret': {}}
1324 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001325 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001326 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001327 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001328 let self.ret['exit_cb'] = job_status(a:job)
1329 endfunc
1330
Bram Moolenaare2956092019-01-25 21:01:17 +01001331 let job = job_start([&shell, &shellcmdflag, 'echo'],
1332 \ {'close_cb': g:retdict.close_cb,
1333 \ 'exit_cb': g:retdict.exit_cb})
1334 " the job may be done quickly, also accept "dead"
1335 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001336 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001337 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001338 call assert_equal('dead', g:retdict.ret['exit_cb'])
1339 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001340endfunc
1341
Bram Moolenaard46ae142016-02-16 13:33:52 +01001342""""""""""
1343
Bram Moolenaar0b146882018-09-06 16:27:24 +02001344function ExitCbWipe(job, status)
1345 exe g:wipe_buf 'bw!'
1346endfunction
1347
1348" This caused a crash, because messages were handled while peeking for a
1349" character.
1350func Test_exit_cb_wipes_buf()
1351 if !has('timers')
1352 return
1353 endif
1354 set cursorline lazyredraw
1355 call test_override('redraw_flag', 1)
1356 new
1357 let g:wipe_buf = bufnr('')
1358
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001359 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001360 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001361 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1362 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1363 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1364 call timer_stop(timer)
1365
1366 set nocursorline nolazyredraw
1367 unlet g:wipe_buf
1368 call test_override('ALL', 0)
1369endfunc
1370
1371""""""""""
1372
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001373let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001374func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001375 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001376 unlet s:channelfd
1377endfunc
1378
1379" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001380func Ch_unlet_handle(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001381 let s:channelfd = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001382 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001383 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001384endfunc
1385
1386func Test_unlet_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001387 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001388endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001389
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001390func Test_unlet_handle_ipv6()
1391 CheckIPv6
1392 call Test_unlet_handle()
1393endfunc
1394
Bram Moolenaard46ae142016-02-16 13:33:52 +01001395""""""""""
1396
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001397let g:Ch_unletResponse = ''
1398func Ch_CloseHandler(handle, msg)
1399 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001400 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001401endfunc
1402
1403" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001404func Ch_close_handle(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001405 let s:channelfd = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001406 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001407 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001408endfunc
1409
1410func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001411 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001412endfunc
1413
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001414func Test_close_handle_ipv6()
1415 CheckIPv6
1416 call Test_close_handle()
1417endfunc
1418
1419""""""""""
1420
1421func Ch_open_ipv6(port)
1422 let handle = ch_open('[::1]:' .. a:port, s:chopt)
1423 call assert_notequal('fail', ch_status(handle))
1424endfunc
1425
1426func Test_open_ipv6()
1427 CheckIPv6
1428 call s:run_server('Ch_open_ipv6')
1429endfunc
1430
Bram Moolenaard46ae142016-02-16 13:33:52 +01001431""""""""""
1432
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001433func Test_open_fail()
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001434 call assert_fails("let ch = ch_open('noserver')", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001435 echo ch
1436 let d = ch
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001437 call assert_fails("let ch = ch_open('noserver', 10)", 'E474:')
1438 call assert_fails("let ch = ch_open('localhost:-1')", 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001439 call assert_fails("let ch = ch_open('localhost:65537')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001440 call assert_fails("let ch = ch_open('localhost:8765', {'timeout' : -1})",
1441 \ 'E474:')
1442 call assert_fails("let ch = ch_open('localhost:8765', {'axby' : 1})",
1443 \ 'E475:')
1444 call assert_fails("let ch = ch_open('localhost:8765', {'mode' : 'abc'})",
1445 \ 'E475:')
1446 call assert_fails("let ch = ch_open('localhost:8765', {'part' : 'out'})",
1447 \ 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001448 call assert_fails("let ch = ch_open('[::]')", 'E475:')
1449 call assert_fails("let ch = ch_open('[::.80')", 'E475:')
1450 call assert_fails("let ch = ch_open('[::]8080')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001451endfunc
1452
1453func Test_ch_info_fail()
1454 call assert_fails("let x = ch_info(10)", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001455endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001456
1457""""""""""
1458
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001459func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001460 " Wait up to a second for the port to open.
1461 let s:chopt.waittime = 1000
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001462 let channel = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001463 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001464 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001465 return
1466 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001467 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001468 call ch_close(channel)
1469endfunc
1470
1471func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001472 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001473 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001474endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001475
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001476func Test_open_delay_ipv6()
1477 CheckIPv6
1478 call Test_open_delay()
1479endfunc
1480
Bram Moolenaarece61b02016-02-20 21:39:05 +01001481"""""""""
1482
1483function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001484 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001485endfunc
1486
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001487function Ch_test_call(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001488 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001489 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001490 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001491 return
1492 endif
1493
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001494 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001495 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001496 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001497
1498 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'callback' : 'abc'})", 'E917:')
1499 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'drop' : ''})", 'E475:')
1500 call assert_fails("let i = ch_evalexpr(test_null_job(), '2 + 2')", 'E906:')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001501endfunc
1502
1503func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001504 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001505endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001506
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001507func Test_call_ipv6()
1508 CheckIPv6
1509 call Test_call()
1510endfunc
1511
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001512"""""""""
1513
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001514let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001515function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001516 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001517endfunc
1518
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001519function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001520 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001521 let g:Ch_exit_job = g:currentJob
1522 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001523endfunc
1524
1525func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001526 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001527
Bram Moolenaarf386f082019-07-29 23:03:03 +02001528 " wait up to a second for the job to exit
1529 for i in range(100)
1530 if g:Ch_job_exit_ret == 'done'
1531 break
1532 endif
1533 sleep 10m
1534 " calling job_status() triggers the callback
1535 call job_status(g:Ch_exit_job)
1536 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001537
Bram Moolenaarf386f082019-07-29 23:03:03 +02001538 call assert_equal('done', g:Ch_job_exit_ret)
1539 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1540 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001541endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001542
Bram Moolenaar97792de2016-10-15 18:36:49 +02001543function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001544 if job_info(a:job).process == g:exit_cb_val.process
1545 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1546 endif
1547 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001548endfunction
1549
1550func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001551 CheckFunction reltimefloat
1552
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001553 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001554 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001555 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001556 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001557 let elapsed = reltimefloat(g:exit_cb_val.end)
1558 call assert_true(elapsed > 0.5)
1559 call assert_true(elapsed < 1.0)
1560
1561 " case: unreferenced job, using timer
1562 if !has('timers')
1563 return
1564 endif
1565
1566 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1567 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1568 let g:exit_cb_val.process = job_info(g:job).process
1569 unlet g:job
1570 call Standby(1000)
1571 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1572 let elapsed = reltimefloat(g:exit_cb_val.end)
1573 else
1574 let elapsed = 1.0
1575 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001576 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001577endfunc
1578
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001579"""""""""
1580
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001581let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001582function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001583 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001584endfunc
1585
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001586function Ch_test_close_callback(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001587 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001588 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001589 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001590 return
1591 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001592 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001593
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001594 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001595 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001596endfunc
1597
1598func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001599 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001600endfunc
1601
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001602func Test_close_callback_ipv6()
1603 CheckIPv6
1604 call Test_close_callback()
1605endfunc
1606
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001607function Ch_test_close_partial(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001608 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001609 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001610 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001611 return
1612 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001613 let g:Ch_d = {}
1614 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001615 let self.close_ret = 'closed'
1616 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001617 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001618
1619 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001620 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001621 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001622endfunc
1623
1624func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001625 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001626endfunc
1627
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001628func Test_close_partial_ipv6()
1629 CheckIPv6
1630 call Test_close_partial()
1631endfunc
1632
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001633func Test_job_start_fails()
1634 " this was leaking memory
1635 call assert_fails("call job_start([''])", "E474:")
Bram Moolenaar80385682016-03-27 19:13:35 +02001636 call assert_fails('call job_start($x)', 'E474:')
1637 call assert_fails('call job_start("")', 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001638 call assert_fails('call job_start("ls", {"out_io" : "abc"})', 'E475:')
1639 call assert_fails('call job_start("ls", {"err_io" : "abc"})', 'E475:')
1640 call assert_fails('call job_start("ls", [])', 'E715:')
1641 call assert_fails("call job_start('ls', {'in_top' : -1})", 'E475:')
1642 call assert_fails("call job_start('ls', {'in_bot' : -1})", 'E475:')
1643 call assert_fails("call job_start('ls', {'channel' : -1})", 'E475:')
1644 call assert_fails("call job_start('ls', {'callback' : -1})", 'E475:')
1645 call assert_fails("call job_start('ls', {'out_cb' : -1})", 'E475:')
1646 call assert_fails("call job_start('ls', {'err_cb' : -1})", 'E475:')
1647 call assert_fails("call job_start('ls', {'close_cb' : -1})", 'E475:')
1648 call assert_fails("call job_start('ls', {'exit_cb' : -1})", 'E475:')
1649 call assert_fails("call job_start('ls', {'term_name' : []})", 'E475:')
1650 call assert_fails("call job_start('ls', {'term_finish' : 'run'})", 'E475:')
1651 call assert_fails("call job_start('ls', {'term_api' : []})", 'E475:')
1652 call assert_fails("call job_start('ls', {'stoponexit' : []})", 'E475:')
1653 call assert_fails("call job_start('ls', {'in_io' : 'file'})", 'E920:')
1654 call assert_fails("call job_start('ls', {'out_io' : 'file'})", 'E920:')
1655 call assert_fails("call job_start('ls', {'err_io' : 'file'})", 'E920:')
1656 call assert_fails("call job_start('ls', {'in_mode' : 'abc'})", 'E475:')
1657 call assert_fails("call job_start('ls', {'out_mode' : 'abc'})", 'E475:')
1658 call assert_fails("call job_start('ls', {'err_mode' : 'abc'})", 'E475:')
1659 call assert_fails("call job_start('ls',
1660 \ {'in_io' : 'buffer', 'in_buf' : 99999})", 'E86:')
1661 call assert_fails("call job_start('ls',
1662 \ {'out_io' : 'buffer', 'out_buf' : 99999})", 'E86:')
1663 call assert_fails("call job_start('ls',
1664 \ {'err_io' : 'buffer', 'err_buf' : 99999})", 'E86:')
1665
1666 call assert_fails("call job_start('ls',
1667 \ {'in_io' : 'buffer', 'in_buf' : -1})", 'E475:')
1668 call assert_fails("call job_start('ls',
1669 \ {'out_io' : 'buffer', 'out_buf' : -1})", 'E475:')
1670 call assert_fails("call job_start('ls',
1671 \ {'err_io' : 'buffer', 'err_buf' : -1})", 'E475:')
1672
1673 set nomodifiable
1674 call assert_fails("call job_start('cmd /c dir',
1675 \ {'out_io' : 'buffer', 'out_buf' :" .. bufnr() .. "})", 'E21:')
1676 call assert_fails("call job_start('cmd /c dir',
1677 \ {'err_io' : 'buffer', 'err_buf' :" .. bufnr() .. "})", 'E21:')
1678 set modifiable
1679
1680 call assert_fails("call job_start('ls', {'in_io' : 'buffer'})", 'E915:')
1681
1682 edit! XXX
1683 let bnum = bufnr()
1684 enew
1685 call assert_fails("call job_start('ls',
1686 \ {'in_io' : 'buffer', 'in_buf' : bnum})", 'E918:')
1687
1688 " Empty job tests
1689 " This was crashing on MS-Windows.
1690 call assert_fails('let job = job_start([""])', 'E474:')
1691 call assert_fails('let job = job_start([" "])', 'E474:')
1692 call assert_fails('let job = job_start("")', 'E474:')
1693 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaar00157952020-04-13 17:44:47 +02001694 call assert_fails('let job = job_start(["ls", []])', 'E730:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001695 call assert_fails('call job_setoptions(test_null_job(), {})', 'E916:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001696 %bw!
Bram Moolenaar80385682016-03-27 19:13:35 +02001697endfunc
1698
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001699func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001700 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001701 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001702 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001703 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001704 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001705 call job_stop(g:job, 'kill')
1706 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001707 endtry
1708endfunc
1709
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001710" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001711func Test_partial_in_channel_cycle()
1712 let d = {}
1713 let d.a = function('string', [d])
1714 try
1715 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001716 call test_garbagecollect_now()
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001717 catch
1718 call assert_exception('E901:')
1719 endtry
1720 unlet d
1721endfunc
1722
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001723func Test_using_freed_memory()
1724 let g:a = job_start(['ls'])
1725 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001726 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001727endfunc
1728
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001729func Test_collapse_buffers()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001730 CheckExecutable cat
1731
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001732 sp test_channel.vim
1733 let g:linecount = line('$')
1734 close
1735 split testout
1736 1,$delete
1737 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001738 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001739 bwipe!
1740endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001741
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001742func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001743 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001744 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001745
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001746 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001747 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001748 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001749 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1750 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001751
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001752 bdel test_buffer
1753 call assert_equal([], getbufline(bufnr, 1, '$'))
1754
1755 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001756 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001757 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1758 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1759
1760 bwipe! test_buffer
1761endfunc
1762
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001763func Test_cmd_parsing()
1764 if !has('unix')
1765 return
1766 endif
1767 call assert_false(filereadable("file with space"))
1768 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001769 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001770 call delete("file with space")
1771
1772 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001773 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001774 call delete("file with space")
1775endfunc
1776
Bram Moolenaar82117982016-08-27 19:21:48 +02001777func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001778 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001779
1780 " Test lines from the job containing NUL are stored correctly in a buffer.
1781 new
1782 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1783 w! Xtestread
1784 bwipe!
1785 split testout
1786 1,$delete
1787 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1788 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001789 call assert_equal("asdf\nasdf", getline(1))
1790 call assert_equal("xxx\n", getline(2))
1791 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001792
1793 call delete('Xtestread')
1794 bwipe!
1795
1796 " Test lines from a buffer with NUL bytes are written correctly to the job.
1797 new mybuffer
1798 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1799 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 +02001800 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001801 bwipe!
1802 split Xtestwrite
1803 call assert_equal("asdf\nasdf", getline(1))
1804 call assert_equal("xxx\n", getline(2))
1805 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001806 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001807
1808 call delete('Xtestwrite')
1809 bwipe!
1810endfunc
1811
Bram Moolenaarec68a992016-10-03 21:37:41 +02001812func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001813 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001814 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001815 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001816 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001817 unlet g:linecount
1818endfunc
1819
1820func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001821 func s:close_cb(ch)
1822 while ch_status(a:ch) == 'buffered'
1823 let g:out .= ch_read(a:ch)
1824 endwhile
1825 endfunc
1826
1827 let g:out = ''
1828 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1829 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001830 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001831 call WaitForAssert({-> assert_equal('123', g:out)})
1832 unlet g:out
1833 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001834endfunc
1835
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001836func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001837 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001838 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001839 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001840 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001841 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001842 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001843endfunc
1844
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001845func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001846 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001847
1848 " Check that backslash in $COMSPEC is handled properly.
1849 let g:echostr = ''
1850 let cmd = $COMSPEC . ' /c echo 123'
1851 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1852 let info = job_info(job)
1853 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1854
1855 call WaitForAssert({-> assert_equal("123", g:echostr)})
1856 unlet g:echostr
1857endfunc
1858
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001859func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001860 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001861 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001862 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001863 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001864 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001865 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001866 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1867 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001868 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001869 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001870endfunc
1871
1872func Test_cwd()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001873 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001874 if has('win32')
1875 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001876 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001877 else
1878 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001879 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001880 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001881 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001882 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001883 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001884 let expect = substitute(expect, '[/\\]$', '', '')
1885 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1886 if $CI != '' && stridx(g:envstr, '/private/') == 0
1887 let g:envstr = g:envstr[8:]
1888 endif
1889 call assert_equal(expect, g:envstr)
1890 finally
1891 call job_stop(job)
1892 unlet g:envstr
1893 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001894endfunc
1895
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001896function Ch_test_close_lambda(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001897 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001898 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001899 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001900 return
1901 endif
1902 let g:Ch_close_ret = ''
1903 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001904 call test_garbagecollect_now()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001905
1906 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001907 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001908endfunc
1909
1910func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001911 call s:run_server('Ch_test_close_lambda')
1912endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001913
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001914func Test_close_lambda_ipv6()
1915 CheckIPv6
1916 call Test_close_lambda()
1917endfunc
1918
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001919func s:test_list_args(cmd, out, remove_lf)
1920 try
1921 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001922 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 +02001923 call WaitFor('"" != g:out')
1924 if has('win32')
1925 let g:out = substitute(g:out, '\r', '', 'g')
1926 endif
1927 if a:remove_lf
1928 let g:out = substitute(g:out, '\n$', '', 'g')
1929 endif
1930 call assert_equal(a:out, g:out)
1931 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001932 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001933 unlet g:out
1934 endtry
1935endfunc
1936
1937func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001938 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1939 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1940 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1941 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1942 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1943 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1944 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1945 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1946 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1947 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1948 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1949 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1950 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1951
1952 " tests which not contain spaces in the argument
1953 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1954 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1955 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1956 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1957 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1958 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1959 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1960 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1961 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1962endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001963
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001964func Test_keep_pty_open()
1965 if !has('unix')
1966 return
1967 endif
1968
Bram Moolenaare2956092019-01-25 21:01:17 +01001969 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1970 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001971 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1972 call assert_inrange(200, 1000, elapsed)
1973 call job_stop(job)
1974endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001975
1976func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001977 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01001978 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01001979
1980 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001981 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001982 endfunc
1983
1984 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001985 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001986 call Resume()
1987 endfunc
1988
1989 func TimerCb(timer)
1990 if has('win32')
1991 let cmd = ['cmd', '/c', 'echo.']
1992 else
1993 let cmd = ['echo']
1994 endif
1995 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1996 call substitute(repeat('a', 100000), '.', '', 'g')
1997 endfunc
1998
1999 " We should be interrupted before 'updatetime' elapsed.
2000 let g:val = 0
2001 call timer_start(1, 'TimerCb')
2002 let elapsed = Standby(&ut)
2003 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002004
2005 " Wait for both OutCb() and ExitCb() to have been called before deleting
2006 " them.
2007 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01002008 call job_stop(g:job)
2009
2010 delfunc OutCb
2011 delfunc ExitCb
2012 delfunc TimerCb
2013 unlet! g:val
2014 unlet! g:job
2015endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01002016
2017func Test_raw_large_data()
2018 try
2019 let g:out = ''
2020 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01002021 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
2022 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01002023
Bram Moolenaare2956092019-01-25 21:01:17 +01002024 let outlen = 79999
2025 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02002026 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01002027 call WaitFor({-> len(g:out) >= outlen}, 10000)
2028 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01002029 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2030 finally
2031 call job_stop(job)
2032 unlet g:out
2033 endtry
2034endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002035
Bram Moolenaar65240682019-02-10 22:23:26 +01002036func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002037 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01002038
2039 try
2040 let job = job_start(s:python . " test_channel_pipe.py busy",
2041 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
2042 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
2043 finally
2044 call job_stop(job)
2045 endtry
2046endfunc
2047
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002048func Test_job_exitval_and_termsig()
2049 if !has('unix')
2050 return
2051 endif
2052
2053 " Terminate job normally
2054 let cmd = ['echo']
2055 let job = job_start(cmd)
2056 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2057 let info = job_info(job)
2058 call assert_equal(0, info.exitval)
2059 call assert_equal("", info.termsig)
2060
2061 " Terminate job by signal
2062 let cmd = ['sleep', '10']
2063 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01002064 " 10m usually works but 50m is needed when running Valgrind
2065 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002066 call job_stop(job)
2067 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2068 let info = job_info(job)
2069 call assert_equal(-1, info.exitval)
2070 call assert_equal("term", info.termsig)
2071endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01002072
2073func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002074 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01002075
2076 call writefile(['test'], 'Xtestin')
2077 let in_opts = [{},
2078 \ {'in_io': 'null'},
2079 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
2080 let out_opts = [{},
2081 \ {'out_io': 'null'},
2082 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
2083 let err_opts = [{},
2084 \ {'err_io': 'null'},
2085 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
2086 \ {'err_io': 'out'}]
2087 let opts = []
2088
2089 for in_opt in in_opts
2090 let x = copy(in_opt)
2091 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002092 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002093 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002094 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002095 let opts += [extend({'pty': 1}, x)]
2096 endfor
2097 endfor
2098 endfor
2099
2100 for opt in opts
2101 let job = job_start('echo', opt)
2102 let info = job_info(job)
2103 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2104 \ get(opt, 'in_io', 'tty'),
2105 \ get(opt, 'out_io', 'tty'),
2106 \ get(opt, 'err_io', 'tty'))
2107
2108 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2109 call assert_notequal('', info.tty_in, msg)
2110 else
2111 call assert_equal('', info.tty_in, msg)
2112 endif
2113 call assert_equal(info.tty_in, info.tty_out, msg)
2114
2115 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2116 endfor
2117
2118 call delete('Xtestin')
2119 call delete('Xtestout')
2120 call delete('Xtesterr')
2121endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02002122
2123" Do this last, it stops any channel log.
2124func Test_zz_nl_err_to_out_pipe()
Bram Moolenaar570497a2019-08-22 22:55:13 +02002125 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02002126 call ch_log('Test_zz_nl_err_to_out_pipe()')
2127 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
2128 call assert_equal("run", job_status(job))
2129 try
2130 let handle = job_getchannel(job)
2131 call ch_sendraw(handle, "echo something\n")
2132 call assert_equal("something", ch_readraw(handle))
2133
2134 call ch_sendraw(handle, "echoerr wrong\n")
2135 call assert_equal("wrong", ch_readraw(handle))
2136 finally
2137 call job_stop(job)
2138 call ch_logfile('')
2139 let loglines = readfile('Xlog')
2140 call assert_true(len(loglines) > 10)
2141 let found_test = 0
2142 let found_send = 0
2143 let found_recv = 0
2144 let found_stop = 0
2145 for l in loglines
2146 if l =~ 'Test_zz_nl_err_to_out_pipe'
2147 let found_test = 1
2148 endif
2149 if l =~ 'SEND on.*echo something'
2150 let found_send = 1
2151 endif
2152 if l =~ 'RECV on.*something'
2153 let found_recv = 1
2154 endif
2155 if l =~ 'Stopping job with'
2156 let found_stop = 1
2157 endif
2158 endfor
2159 call assert_equal(1, found_test)
2160 call assert_equal(1, found_send)
2161 call assert_equal(1, found_recv)
2162 call assert_equal(1, found_stop)
2163 " On MS-Windows need to sleep for a moment to be able to delete the file.
2164 sleep 10m
2165 call delete('Xlog')
2166 endtry
2167endfunc
2168
2169" Do this last, it stops any channel log.
2170func Test_zz_ch_log()
2171 call ch_logfile('Xlog', 'w')
2172 call ch_log('hello there')
2173 call ch_log('%s%s')
2174 call ch_logfile('')
2175 let text = readfile('Xlog')
2176 call assert_match("hello there", text[1])
2177 call assert_match("%s%s", text[2])
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002178 call mkdir("Xdir1")
2179 call assert_fails("call ch_logfile('Xdir1')", 'E484:')
2180 cal delete("Xdir1", 'd')
Bram Moolenaarf386f082019-07-29 23:03:03 +02002181 call delete('Xlog')
2182endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002183
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002184func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002185 if has('win32')
2186 let cmd = 'cmd /c pause'
2187 else
2188 let cmd = 'grep foo'
2189 endif
2190 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002191 call job_stop(g:job)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002192 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002193 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002194 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002195 call job_stop(g:job, 'term')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002196 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002197 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002198 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002199 call job_stop(g:job, 'kill')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002200 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002201 call assert_equal(-1, job_info(g:job).exitval)
2202endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002203
2204func Test_issue_5485()
2205 let $VAR1 = 'global'
2206 let g:Ch_reply = ""
2207 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2208 let g:Ch_job = l:job
2209 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2210 unlet $VAR1
2211endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002212
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002213func Test_job_trailing_space_unix()
2214 CheckUnix
2215 CheckExecutable cat
2216 let job = job_start("cat ", #{in_io: 'null'})
2217 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2218 call assert_equal(0, job_info(job).exitval)
2219endfunc
2220
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002221func Test_ch_getbufnr()
2222 let ch = test_null_channel()
2223 call assert_equal(-1, ch_getbufnr(ch, 'in'))
2224 call assert_equal(-1, ch_getbufnr(ch, 'out'))
2225 call assert_equal(-1, ch_getbufnr(ch, 'err'))
2226 call assert_equal(-1, ch_getbufnr(ch, ''))
2227endfunc
2228
2229" Test for unsupported options passed to ch_status()
2230func Test_invalid_job_chan_options()
2231 let ch = test_null_channel()
2232 let invalid_opts = [
2233 \ {'in_io' : 'null'},
2234 \ {'out_io' : 'null'},
2235 \ {'err_io' : 'null'},
2236 \ {'mode' : 'json'},
2237 \ {'out_mode' : 'json'},
2238 \ {'err_mode' : 'json'},
2239 \ {'noblock' : 1},
2240 \ {'in_name' : '/a/b'},
2241 \ {'pty' : 1},
2242 \ {'in_buf' : 1},
2243 \ {'out_buf' : 1},
2244 \ {'err_buf' : 1},
2245 \ {'out_modifiable' : 1},
2246 \ {'err_modifiable' : 1},
2247 \ {'out_msg' : 1},
2248 \ {'err_msg' : 1},
2249 \ {'in_top' : 1},
2250 \ {'in_bot' : 1},
2251 \ {'channel' : ch},
2252 \ {'callback' : ''},
2253 \ {'out_cb' : ''},
2254 \ {'err_cb' : ''},
2255 \ {'close_cb' : ''},
2256 \ {'exit_cb' : ''},
2257 \ {'term_opencmd' : ''},
2258 \ {'eof_chars' : ''},
2259 \ {'term_rows' : 10},
2260 \ {'term_cols' : 10},
2261 \ {'vertical' : 0},
2262 \ {'curwin' : 1},
2263 \ {'bufnr' : 1},
2264 \ {'hidden' : 0},
2265 \ {'norestore' : 0},
2266 \ {'term_kill' : 'kill'},
2267 \ {'tty_type' : ''},
2268 \ {'term_highlight' : ''},
2269 \ {'env' : {}},
2270 \ {'cwd' : ''},
2271 \ {'timeout' : 0},
2272 \ {'out_timeout' : 0},
2273 \ {'err_timeout' : 0},
2274 \ {'id' : 0},
2275 \ {'stoponexit' : ''},
2276 \ {'block_write' : 1}
2277 \ ]
2278 if has('gui')
2279 call add(invalid_opts, {'ansi_colors' : []})
2280 endif
2281
2282 for opt in invalid_opts
2283 call assert_fails("let x = ch_status(ch, opt)", 'E475:')
2284 endfor
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002285 call assert_equal('fail', ch_status(ch, test_null_dict()))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002286endfunc
2287
2288" Test for passing the command and the arguments as List on MS-Windows
2289func Test_job_with_list_args()
2290 CheckMSWindows
2291
2292 enew!
2293 let bnum = bufnr()
2294 let job = job_start(['cmd', '/c', 'echo', 'Hello', 'World'], {'out_io' : 'buffer', 'out_buf' : bnum})
2295 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2296 call assert_equal('Hello World', getline(1))
2297 %bw!
2298endfunc
2299
Bram Moolenaar8b633132020-03-20 18:20:51 +01002300" vim: shiftwidth=2 sts=2 expandtab