blob: 019b959251d748bb8c791087a0a22b37ac736408 [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 Moolenaardfc33a62020-04-29 22:30:13 +02009source view_util.vim
Bram Moolenaar321efdd2016-07-15 17:09:11 +020010
11let s:python = PythonProg()
12if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010013 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020014 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010015endif
16
Bram Moolenaar37175402017-03-18 20:18:45 +010017" Uncomment the next line to see what happens. Output is in
18" src/testdir/channellog.
Bram Moolenaarf386f082019-07-29 23:03:03 +020019" Add ch_log() calls where you want to see what happens.
Bram Moolenaar37175402017-03-18 20:18:45 +010020" call ch_logfile('channellog', 'w')
21
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020022func SetUp()
23 if g:testfunc =~ '_ipv6()$'
24 let s:localhost = '[::1]:'
25 let s:testscript = 'test_channel_6.py'
26 else
27 let s:localhost = 'localhost:'
28 let s:testscript = 'test_channel.py'
29 endif
30 let s:chopt = {}
31 call ch_log(g:testfunc)
32endfunc
Bram Moolenaar3b05b132016-02-03 23:25:07 +010033
Bram Moolenaar4b96df52020-01-26 22:00:26 +010034" Run "testfunc" after starting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010035func s:run_server(testfunc, ...)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020036 call RunServer(s:testscript, a:testfunc, a:000)
Bram Moolenaar373a8762020-03-19 19:44:32 +010037
38 " communicating with a server can be flaky
39 let g:test_is_flaky = 1
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010040endfunc
41
Bram Moolenaar819524702018-02-27 19:10:00 +010042" Return a list of open files.
43" Can be used to make sure no resources leaked.
44" Returns an empty list on systems where this is not supported.
45func s:get_resources()
46 let pid = getpid()
47
Bram Moolenaar39536dd2019-01-29 22:58:21 +010048 if executable('lsof')
Bram Moolenaar819524702018-02-27 19:10:00 +010049 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
50 elseif isdirectory('/proc/' . pid . '/fd/')
51 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
52 else
53 return []
54 endif
55endfunc
56
Bram Moolenaar321efdd2016-07-15 17:09:11 +020057let g:Ch_responseMsg = ''
58func Ch_requestHandler(handle, msg)
59 let g:Ch_responseHandle = a:handle
60 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010061endfunc
62
Bram Moolenaar321efdd2016-07-15 17:09:11 +020063func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010064 " Avoid dropping messages, since we don't use a callback here.
65 let s:chopt.drop = 'never'
Bram Moolenaar0b146882018-09-06 16:27:24 +020066 " Also add the noblock flag to try it out.
67 let s:chopt.noblock = 1
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020068 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +010069 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010070 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010071 return
72 endif
Bram Moolenaarf386f082019-07-29 23:03:03 +020073
74 " check that getjob without a job is handled correctly
75 call assert_equal('no process', string(ch_getjob(handle)))
76
Bram Moolenaar570497a2019-08-22 22:55:13 +020077 let dict = handle->ch_info()
Bram Moolenaar03602ec2016-03-20 20:57:45 +010078 call assert_true(dict.id != 0)
79 call assert_equal('open', dict.status)
80 call assert_equal(a:port, string(dict.port))
81 call assert_equal('open', dict.sock_status)
82 call assert_equal('socket', dict.sock_io)
83
Bram Moolenaard7ece102016-02-02 23:23:02 +010084 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010085 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010086
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010087 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010088 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
89 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
90 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
91
92 " split command should work
93 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020094 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010095 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010096
Bram Moolenaarf1f07922016-08-26 17:58:53 +020097 " string with ][ should work
98 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
99
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100100 " nothing to read now
101 call assert_equal(0, ch_canread(handle))
102
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200103 " sending three messages quickly then reading should work
104 for i in range(3)
105 call ch_sendexpr(handle, 'echo hello ' . i)
106 endfor
107 call assert_equal('hello 0', ch_read(handle)[1])
108 call assert_equal('hello 1', ch_read(handle)[1])
109 call assert_equal('hello 2', ch_read(handle)[1])
110
Bram Moolenaard7ece102016-02-02 23:23:02 +0100111 " Request that triggers sending two ex commands. These will usually be
112 " handled before getting the response, but it's not guaranteed, thus wait a
113 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100114 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200115 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +0100116 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100117
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100118 " Request command "foo bar", which fails silently.
119 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200120 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100121
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100122 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200123 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100124
Bram Moolenaara07fec92016-02-05 21:04:08 +0100125 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200126 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
127 call WaitFor('exists("g:Ch_responseHandle")')
128 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100129 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100130 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200131 call assert_equal(handle, g:Ch_responseHandle)
132 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100133 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200134 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100135
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200136 let g:Ch_responseMsg = ''
137 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
138 call WaitFor('exists("g:Ch_responseHandle")')
139 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100140 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100141 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200142 call assert_equal(handle, g:Ch_responseHandle)
143 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100144 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200145 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100146
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200147 " Using lambda.
148 let g:Ch_responseMsg = ''
149 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
150 call WaitFor('exists("g:Ch_responseHandle")')
151 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100152 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200153 else
154 call assert_equal(handle, g:Ch_responseHandle)
155 unlet g:Ch_responseHandle
156 endif
157 call assert_equal('got it', g:Ch_responseMsg)
158
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100159 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200160 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100161
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100162 " check setting options (without testing the effect)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200163 eval handle->ch_setoptions({'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100164 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100165 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100166 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100167 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100168 call ch_setoptions(handle, {'drop': 'never'})
169 call ch_setoptions(handle, {'drop': 'auto'})
170 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200171 call assert_equal(0, ch_setoptions(handle, test_null_dict()))
172 call assert_equal(0, ch_setoptions(test_null_channel(), {'drop' : 'never'}))
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100173
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100174 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100175 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100176 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100177 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100178
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100179 " Send an eval request with special characters.
180 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
181 sleep 10m
182 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
183
184 " Send an eval request to get a line with special characters.
185 call setline(3, "a\nb\<CR>c\x01d\x7fe")
186 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
187 sleep 10m
188 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
189
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100190 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100191 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100192 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100193 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100194
Bram Moolenaar55fab432016-02-07 16:53:13 +0100195 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100196 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100197 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100198 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100199
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100200 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100201 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100202 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100203 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100204
Bram Moolenaarf4160862016-02-05 23:09:12 +0100205 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100206 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200207 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100208 call assert_equal('one', getline(line('$') - 2))
209 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100210
211 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100212 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
213 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100214
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100215 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100216
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100217 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100218 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100219 if exists('*reltimefloat')
220 let start = reltime()
221 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
222 let elapsed = reltime(start)
223 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
224 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100225
226 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100227 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100228 let resp = ch_read(handle)
229 call assert_equal(type([]), type(resp))
230 call assert_equal(type(11), type(resp[0]))
231 call assert_equal('waited', resp[1])
232
Bram Moolenaard7ece102016-02-02 23:23:02 +0100233 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100234 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100235endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100236
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100237func Test_communicate()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200238 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100239endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100240
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200241func Test_communicate_ipv6()
242 CheckIPv6
243 call Test_communicate()
244endfunc
245
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100246" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200247func Ch_two_channels(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200248 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200249 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar570497a2019-08-22 22:55:13 +0200250 if handle->ch_status() == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100251 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100252 return
253 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100254
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100255 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100256
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200257 let newhandle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100258 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100259 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100260 return
261 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100262 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
263 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100264
265 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100266 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100267
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100268 call ch_close(newhandle)
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200269 call assert_fails("call ch_close(newhandle)", 'E906:')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100270endfunc
271
272func Test_two_channels()
Bram Moolenaar570497a2019-08-22 22:55:13 +0200273 eval 'Test_two_channels()'->ch_log()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200274 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100275endfunc
276
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200277func Test_two_channels_ipv6()
278 CheckIPv6
279 call Test_two_channels()
280endfunc
281
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100282" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200283func Ch_server_crash(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200284 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100285 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100286 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100287 return
288 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100289
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100290 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100291
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100292 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100293endfunc
294
295func Test_server_crash()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200296 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100297endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100298
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200299func Test_server_crash_ipv6()
300 CheckIPv6
301 call Test_server_crash()
302endfunc
303
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100304"""""""""
305
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200306func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100307 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200308 unlet g:Ch_reply
309 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100310endfunc
311
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200312func Ch_channel_handler(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200313 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100314 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100315 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100316 return
317 endif
318
319 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100320 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200321 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100322
323 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100324 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200325 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100326endfunc
327
328func Test_channel_handler()
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200329 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200330 let s:chopt.callback = 'Ch_handler'
331 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200332 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200333 let s:chopt.callback = function('Ch_handler')
334 call s:run_server('Ch_channel_handler')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200335endfunc
336
337func Test_channel_handler_ipv6()
338 CheckIPv6
339 call Test_channel_handler()
Bram Moolenaarf6157282016-02-10 21:07:14 +0100340endfunc
341
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100342"""""""""
343
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200344let g:Ch_reply = ''
345func Ch_zeroHandler(chan, msg)
346 unlet g:Ch_reply
347 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100348endfunc
349
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200350let g:Ch_zero_reply = ''
351func Ch_oneHandler(chan, msg)
352 unlet g:Ch_zero_reply
353 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100354endfunc
355
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200356func Ch_channel_zero(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200357 let handle = (s:localhost .. a:port)->ch_open(s:chopt)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100358 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100359 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100360 return
361 endif
362
363 " Check that eval works.
364 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
365
366 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200367 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100368 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100369 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200370 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100371 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100372 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200373 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100374 endif
375
376 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200377 let g:Ch_reply = ''
378 let g:Ch_zero_reply = ''
379 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200380 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100381 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200382 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100383 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200384 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100385 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100386endfunc
387
388func Test_zero_reply()
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100389 " Run with channel handler
390 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200391 let s:chopt.callback = 'Ch_zeroHandler'
392 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100393 unlet s:chopt.callback
394
395 " Run without channel handler
396 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200397 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100398endfunc
399
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200400func Test_zero_reply_ipv6()
401 CheckIPv6
402 call Test_zero_reply()
403endfunc
404
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100405"""""""""
406
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200407let g:Ch_reply1 = ""
408func Ch_handleRaw1(chan, msg)
409 unlet g:Ch_reply1
410 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100411endfunc
412
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200413let g:Ch_reply2 = ""
414func Ch_handleRaw2(chan, msg)
415 unlet g:Ch_reply2
416 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100417endfunc
418
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200419let g:Ch_reply3 = ""
420func Ch_handleRaw3(chan, msg)
421 unlet g:Ch_reply3
422 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100423endfunc
424
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200425func Ch_raw_one_time_callback(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200426 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100427 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100428 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100429 return
430 endif
431 call ch_setoptions(handle, {'mode': 'raw'})
432
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100433 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200434 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200435 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200436 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
437 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200438 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100439 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200440 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100441endfunc
442
443func Test_raw_one_time_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200444 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100445endfunc
446
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200447func Test_raw_one_time_callback_ipv6()
448 CheckIPv6
449 call Test_raw_one_time_callback()
450endfunc
451
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100452"""""""""
453
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100454" Test that trying to connect to a non-existing port fails quickly.
455func Test_connect_waittime()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100456 CheckFunction reltimefloat
Bram Moolenaar373a8762020-03-19 19:44:32 +0100457 " this is timing sensitive
458 let g:test_is_flaky = 1
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100459
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100460 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100461 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100462 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100463 " Oops, port does exists.
464 call ch_close(handle)
465 else
466 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100467 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100468 endif
469
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100470 " We intend to use a socket that doesn't exist and wait for half a second
471 " before giving up. If the socket does exist it can fail in various ways.
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100472 " Check for "Connection reset by peer" to avoid flakiness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100473 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100474 try
475 let handle = ch_open('localhost:9867', {'waittime': 500})
476 if ch_status(handle) != "fail"
477 " Oops, port does exists.
478 call ch_close(handle)
479 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100480 " Failed connection should wait about 500 msec. Can be longer if the
481 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100482 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100483 endif
484 catch
485 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100486 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100487 endif
488 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100489endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100490
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100491"""""""""
492
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100493func Test_raw_pipe()
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100494 " Add a dummy close callback to avoid that messages are dropped when calling
495 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200496 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100497 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200498 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200499 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100500 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200501
502 call assert_equal("open", ch_status(job))
503 call assert_equal("open", ch_status(job), {"part": "out"})
504 call assert_equal("open", ch_status(job), {"part": "err"})
505 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
506 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
507
508 let dict = ch_info(job)
509 call assert_true(dict.id != 0)
510 call assert_equal('open', dict.status)
511 call assert_equal('open', dict.out_status)
512 call assert_equal('RAW', dict.out_mode)
513 call assert_equal('pipe', dict.out_io)
514 call assert_equal('open', dict.err_status)
515 call assert_equal('RAW', dict.err_mode)
516 call assert_equal('pipe', dict.err_io)
517
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100518 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100519 " For a change use the job where a channel is expected.
520 call ch_sendraw(job, "echo something\n")
521 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100522 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
523
Bram Moolenaar151f6562016-03-07 21:19:38 +0100524 call ch_sendraw(job, "double this\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200525 let g:handle = job->job_getchannel()
526 call WaitFor('g:handle->ch_canread()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100527 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100528 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100529 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
530
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200531 let g:Ch_reply = ""
532 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200533 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200534
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200535 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'callback' : 'abc'})", 'E917:')
536 call assert_fails("let i = ch_evalexpr(job, '2 + 2')", 'E912:')
537 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'drop' : ''})", 'E475:')
538 call assert_fails("let i = ch_evalraw(test_null_job(), '2 + 2')", 'E906:')
539
Bram Moolenaar570497a2019-08-22 22:55:13 +0200540 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100541 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
542 finally
543 call job_stop(job)
544 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100545
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200546 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200547 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar570497a2019-08-22 22:55:13 +0200548 let info = job->job_info()
Bram Moolenaar8950a562016-03-12 15:22:55 +0100549 call assert_equal("dead", info.status)
550 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200551 call assert_equal(2, len(info.cmd))
552 call assert_equal("test_channel_pipe.py", info.cmd[1])
553
554 let found = 0
555 for j in job_info()
556 if j == job
557 let found += 1
558 endif
559 endfor
560 call assert_equal(1, found)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100561
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200562 call assert_fails("call job_stop('abc')", 'E475:')
563 call assert_fails("call job_stop(job, [])", 'E474:')
564 call assert_fails("call job_stop(test_null_job())", 'E916:')
565
Bram Moolenaar8b633132020-03-20 18:20:51 +0100566 " Try to use the job and channel where a number is expected. This is not
567 " related to testing the raw pipe. This test is here just to reuse the
568 " already created job/channel.
569 let ch = job_getchannel(job)
570 call assert_fails('let i = job + 1', 'E910:')
571 call assert_fails('let j = ch + 1', 'E913:')
572 call assert_fails('echo 2.0 == job', 'E911:')
573 call assert_fails('echo 2.0 == ch', 'E914:')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100574endfunc
575
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100576func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100577 " Add a dummy close callback to avoid that messages are dropped when calling
578 " ch_canread().
579 " Also test the non-blocking option.
580 let job = job_start(s:python . " test_channel_pipe.py",
581 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
582 call assert_equal(v:t_job, type(job))
583 call assert_equal("run", job_status(job))
584
585 call assert_equal("open", ch_status(job))
586 call assert_equal("open", ch_status(job), {"part": "out"})
587
588 try
589 " Create a blob with the echo command and write it.
590 let blob = 0z00
591 let cmd = "echo something\n"
592 for i in range(0, len(cmd) - 1)
593 let blob[i] = char2nr(cmd[i])
594 endfor
595 call assert_equal(len(cmd), len(blob))
596 call ch_sendraw(job, blob)
597
598 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200599 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100600 let expected = 'something'
601 for i in range(0, len(expected) - 1)
602 call assert_equal(char2nr(expected[i]), msg[i])
603 endfor
604
605 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
606 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
607 finally
608 call job_stop(job)
609 endtry
610
611 let g:Ch_job = job
612 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
613 let info = job_info(job)
614 call assert_equal("dead", info.status)
615endfunc
616
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100617func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100618 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100619 call assert_equal("run", job_status(job))
620 try
621 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100622 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200623 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100624
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100625 call ch_sendraw(handle, "echoerr wrong\n")
626 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
627
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100628 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100629 call assert_equal("this", ch_readraw(handle))
630 call assert_equal("AND this", ch_readraw(handle))
631
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200632 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200633 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200634
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100635 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100636 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100637 finally
638 call job_stop(job)
639 endtry
640endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100641
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200642func Stop_g_job()
643 call job_stop(g:job)
644 if has('win32')
645 " On MS-Windows the server must close the file handle before we are able
646 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200647 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200648 sleep 10m
649 endif
650endfunc
651
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100652func Test_nl_read_file()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100653 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200654 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100655 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200656 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100657 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200658 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100659 call assert_equal("something", ch_readraw(handle))
660 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
661 call assert_equal("this", ch_readraw(handle))
662 call assert_equal("AND this", ch_readraw(handle))
663 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200664 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100665 call delete('Xinput')
666 endtry
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200667 call assert_fails("echo ch_read(test_null_channel(), {'callback' : 'abc'})", 'E475:')
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100668endfunc
669
Bram Moolenaare98d1212016-03-08 15:37:41 +0100670func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200671 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100672 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200673 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100674 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200675 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100676 call ch_sendraw(handle, "echo line one\n")
677 call ch_sendraw(handle, "echo line two\n")
678 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200679 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100680 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200681 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100682 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100683 call delete('Xoutput')
684 endtry
685endfunc
686
687func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200688 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100689 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200690 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100691 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200692 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100693 call ch_sendraw(handle, "echoerr line one\n")
694 call ch_sendraw(handle, "echoerr line two\n")
695 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200696 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100697 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200698 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100699 call delete('Xoutput')
700 endtry
701endfunc
702
703func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200704 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100705 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200706 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100707 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200708 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100709 call ch_sendraw(handle, "echoerr line one\n")
710 call ch_sendraw(handle, "echo line two\n")
711 call ch_sendraw(handle, "double this\n")
712 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200713 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100714 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200715 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100716 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100717 call delete('Xoutput')
718 endtry
719endfunc
720
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200721func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200722 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200723endfunc
724
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200725func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200726 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200727 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200728 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100729 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100730 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200731 if a:do_msg
732 let expected[0] = 'Reading from channel output...'
733 else
734 let options['out_msg'] = 0
735 call remove(expected, 0)
736 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100737 else
738 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100739 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100740 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200741 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100742 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200743 if a:nomod
744 let options['out_modifiable'] = 0
745 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100746 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100747 call assert_equal("run", job_status(job))
748 try
749 let handle = job_getchannel(job)
750 call ch_sendraw(handle, "echo line one\n")
751 call ch_sendraw(handle, "echo line two\n")
752 call ch_sendraw(handle, "double this\n")
753 call ch_sendraw(handle, "quit\n")
754 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100755 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200756 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200757 if a:nomod
758 call assert_equal(0, &modifiable)
759 else
760 call assert_equal(1, &modifiable)
761 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200762 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100763 bwipe!
764 finally
765 call job_stop(job)
766 endtry
767endfunc
768
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100769func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200770 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100771endfunc
772
773func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200774 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100775endfunc
776
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200777func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200778 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200779endfunc
780
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200781func Test_pipe_to_buffer_name_nomsg()
782 call Run_test_pipe_to_buffer(1, 0, 1)
783endfunc
784
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200785func Test_close_output_buffer()
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200786 enew!
787 let test_lines = ['one', 'two']
788 call setline(1, test_lines)
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200789 let options = {'out_io': 'buffer'}
790 let options['out_name'] = 'buffer-output'
791 let options['out_msg'] = 0
792 split buffer-output
793 let job = job_start(s:python . " test_channel_write.py", options)
794 call assert_equal("run", job_status(job))
795 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200796 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200797 quit!
798 sleep 100m
799 " Make sure the write didn't happen to the wrong buffer.
800 call assert_equal(test_lines, getline(1, line('$')))
801 call assert_equal(-1, bufwinnr('buffer-output'))
802 sbuf buffer-output
803 call assert_notequal(-1, bufwinnr('buffer-output'))
804 sleep 100m
805 close " no more writes
806 bwipe!
807 finally
808 call job_stop(job)
809 endtry
810endfunc
811
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200812func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100813 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200814 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100815 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100816 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200817 if a:do_msg
818 let expected[0] = 'Reading from channel error...'
819 else
820 let options['err_msg'] = 0
821 call remove(expected, 0)
822 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100823 else
824 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100825 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100826 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200827 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100828 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200829 if a:nomod
830 let options['err_modifiable'] = 0
831 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100832 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100833 call assert_equal("run", job_status(job))
834 try
835 let handle = job_getchannel(job)
836 call ch_sendraw(handle, "echoerr line one\n")
837 call ch_sendraw(handle, "echoerr line two\n")
838 call ch_sendraw(handle, "doubleerr this\n")
839 call ch_sendraw(handle, "quit\n")
840 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200841 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200842 if a:nomod
843 call assert_equal(0, &modifiable)
844 else
845 call assert_equal(1, &modifiable)
846 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100847 bwipe!
848 finally
849 call job_stop(job)
850 endtry
851endfunc
852
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100853func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200854 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100855endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100856
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100857func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200858 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200859endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100860
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200861func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200862 call Run_test_pipe_err_to_buffer(1, 1, 1)
863endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100864
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200865func Test_pipe_err_to_buffer_name_nomsg()
866 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100867endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100868
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100869func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100870 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100871 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100872 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200873 let handle = job_getchannel(job)
874 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'out'))
875 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'err'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100876 try
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100877 call ch_sendraw(handle, "echo line one\n")
878 call ch_sendraw(handle, "echoerr line two\n")
879 call ch_sendraw(handle, "double this\n")
880 call ch_sendraw(handle, "doubleerr that\n")
881 call ch_sendraw(handle, "quit\n")
882 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200883 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 +0100884 bwipe!
885 finally
886 call job_stop(job)
887 endtry
888endfunc
889
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100890func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100891 sp pipe-input
892 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200893 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100894 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100895 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100896 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100897 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100898 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100899
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100900 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100901 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200902 if has('unix') && !a:use_name
903 call assert_equal(bufnr('%'), ch_getbufnr(job, 'in'))
904 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100905 try
906 let handle = job_getchannel(job)
907 call assert_equal('one', ch_read(handle))
908 call assert_equal('two', ch_read(handle))
909 call assert_equal('three', ch_read(handle))
910 bwipe!
911 finally
912 call job_stop(job)
913 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100914endfunc
915
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100916func Test_pipe_from_buffer_name()
917 call Run_test_pipe_from_buffer(1)
918endfunc
919
920func Test_pipe_from_buffer_nr()
921 call Run_test_pipe_from_buffer(0)
922endfunc
923
Bram Moolenaar0874a832016-09-01 15:11:51 +0200924func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200925 CheckExecutable sort
926
Bram Moolenaar0874a832016-09-01 15:11:51 +0200927 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
928 if a:use_buffer
929 split sortin
930 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
931 let options.in_io = 'buffer'
932 let options.in_name = 'sortin'
933 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200934 if !a:all
935 let options.in_top = 2
936 let options.in_bot = 4
937 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100938 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200939
940 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100941 call assert_equal("run", job_status(job))
942 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200943 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200944 endif
945
Bram Moolenaare2956092019-01-25 21:01:17 +0100946 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200947
Bram Moolenaard8b55492016-09-01 14:35:22 +0200948 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200949 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200950 call assert_equal('Reading from channel output...', getline(1))
951 if a:all
952 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
953 else
954 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
955 endif
956
Bram Moolenaare2956092019-01-25 21:01:17 +0100957 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200958 if a:use_buffer
959 bwipe! sortin
960 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200961 bwipe! sortout
962endfunc
963
964func Test_pipe_through_sort_all()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200965 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200966endfunc
967
968func Test_pipe_through_sort_some()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200969 call Run_pipe_through_sort(0, 1)
970endfunc
971
972func Test_pipe_through_sort_feed()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200973 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200974endfunc
975
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100976func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100977 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100978 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100979 call assert_equal("run", job_status(job))
980 try
981 let handle = job_getchannel(job)
982 call ch_sendraw(handle, "echo line one\n")
983 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200984 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200985 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100986 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
987 bwipe!
988 finally
989 call job_stop(job)
990 endtry
991endfunc
992
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100993func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100994 CheckFunction reltimefloat
995
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100996 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100997 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100998 call assert_equal("run", job_status(job))
999 try
1000 let handle = job_getchannel(job)
1001 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
1002 call ch_sendraw(handle, "echo [-2, 12.34]\n")
1003 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001004 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001005 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1006 bwipe!
1007 finally
1008 call job_stop(job)
1009 endtry
1010endfunc
1011
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001012" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001013func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001014 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001015 if getline(line('$') - a:offset) == a:line
1016 break
1017 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001018 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001019 endfor
1020endfunc
1021
1022func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001023 " Create two buffers, one to read from and one to write to.
1024 split pipe-output
1025 set buftype=nofile
1026 split pipe-input
1027 set buftype=nofile
1028
1029 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001030 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001031 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1032 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001033 call assert_equal("run", job_status(job))
1034 try
1035 exe "normal Gaecho hello\<CR>"
1036 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001037 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001038 call assert_equal('hello', getline('$'))
1039
1040 exe bufwinnr('pipe-input') . "wincmd w"
1041 exe "normal Gadouble this\<CR>"
1042 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001043 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001044 call assert_equal('this', getline(line('$') - 1))
1045 call assert_equal('AND this', getline('$'))
1046
1047 bwipe!
1048 exe bufwinnr('pipe-input') . "wincmd w"
1049 bwipe!
1050 finally
1051 call job_stop(job)
1052 endtry
1053endfunc
1054
1055func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001056 " Create one buffer to read from and to write to.
1057 split pipe-io
1058 set buftype=nofile
1059
1060 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001061 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001062 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1063 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001064 call assert_equal("run", job_status(job))
1065 try
1066 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001067 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001068 call assert_equal('hello', getline(line('$') - 1))
1069
1070 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001071 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001072 call assert_equal('this', getline(line('$') - 2))
1073 call assert_equal('AND this', getline(line('$') - 1))
1074
1075 bwipe!
1076 finally
1077 call job_stop(job)
1078 endtry
1079endfunc
1080
Bram Moolenaar4641a122019-07-29 22:10:23 +02001081func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001082 CheckScreendump
1083
Bram Moolenaar4641a122019-07-29 22:10:23 +02001084 let lines =<< trim END
1085 new Xscrollbuffer
1086 call setline(1, range(1, 200))
1087 $
1088 redraw
1089 wincmd w
1090 call deletebufline('Xscrollbuffer', 1, '$')
1091 if has('win32')
1092 let cmd = ['cmd', '/c', 'echo sometext']
1093 else
1094 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1095 endif
1096 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1097 END
1098 call writefile(lines, 'XtestBufferScroll')
1099 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001100 call TermWait(buf, 50)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001101 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1102
1103 " clean up
1104 call StopVimInTerminal(buf)
1105 call delete('XtestBufferScroll')
1106endfunc
1107
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001108func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001109 " We cannot check that no I/O works, we only check that the job starts
1110 " properly.
1111 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001112 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001113 call assert_equal("run", job_status(job))
1114 try
1115 call assert_equal('something', ch_read(job))
1116 finally
1117 call job_stop(job)
1118 endtry
1119
1120 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001121 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001122 call assert_equal("run", job_status(job))
1123 try
1124 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1125 finally
1126 call job_stop(job)
1127 endtry
1128
1129 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001130 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001131 call assert_equal("run", job_status(job))
1132 try
1133 call assert_equal('something', ch_read(job))
1134 finally
1135 call job_stop(job)
1136 endtry
1137
1138 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001139 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001140 call assert_equal("run", job_status(job))
1141 call job_stop(job)
1142
1143 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001144 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001145 call assert_equal("run", job_status(job))
1146 call assert_equal('channel fail', string(job_getchannel(job)))
1147 call assert_equal('fail', ch_status(job))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001148 call assert_equal('no process', string(test_null_job()))
1149 call assert_equal('channel fail', string(test_null_channel()))
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001150 call job_stop(job)
1151endfunc
1152
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001153func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001154 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1155 split testout
1156 let job = job_start([s:python, '-c',
1157 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001158 " the job may be done quickly, also accept "dead"
1159 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001160 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001161 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001162 let totlen = 0
1163 for line in getline(1, '$')
1164 call assert_equal('', substitute(line, '^\.*', '', ''))
1165 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001166 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001167 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001168 finally
1169 call job_stop(job)
1170 bwipe!
1171 endtry
1172endfunc
1173
Bram Moolenaarde279892016-03-11 22:19:44 +01001174func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001175 let job = job_start(s:python . " test_channel_pipe.py")
1176 call assert_equal("run", job_status(job))
1177 let handle = job_getchannel(job)
1178 try
1179 call ch_sendraw(handle, "echo something\n")
1180 call assert_equal("something", ch_readraw(handle))
1181 finally
1182 call job_stop(job)
1183 endtry
1184
1185 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1186 call assert_equal("run", job_status(job))
1187 let handle = job_getchannel(job)
1188 try
1189 call ch_sendraw(handle, "echo again\n")
1190 call assert_equal("again", ch_readraw(handle))
1191 finally
1192 call job_stop(job)
1193 endtry
1194endfunc
1195
Bram Moolenaar75f72652016-03-20 22:16:56 +01001196func Test_out_cb()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001197 let dict = {'thisis': 'dict: '}
1198 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001199 if type(a:msg) == v:t_string
1200 let g:Ch_outmsg = self.thisis . a:msg
1201 else
1202 let g:Ch_outobj = a:msg
1203 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001204 endfunc
1205 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001206 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001207 endfunc
1208 let job = job_start(s:python . " test_channel_pipe.py",
1209 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001210 \ 'out_mode': 'json',
1211 \ 'err_cb': dict.errHandler,
1212 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001213 call assert_equal("run", job_status(job))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001214 call test_garbagecollect_now()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001215 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001216 let g:Ch_outmsg = ''
1217 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001218 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1219 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001220 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1221 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001222
1223 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001224 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001225 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001226 " For unknown reasons this can be very slow on Mac.
1227 if has('mac')
1228 let timeout = 20000
1229 else
1230 let timeout = 5000
1231 endif
1232 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001233 finally
1234 call job_stop(job)
1235 endtry
1236endfunc
1237
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001238func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001239 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001240 let g:Ch_msg1 = ''
1241 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001242 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001243 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001244 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001245 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001246 let s:counter += 1
1247 endfunc
1248 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001249 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001250 let s:counter += 1
1251 endfunc
1252 let job = job_start(s:python . " test_channel_pipe.py quit now",
1253 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001254 \ 'close_cb': 'CloseHandler'})
1255 " the job may be done quickly, also accept "dead"
1256 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001257 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001258 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1259 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001260 finally
1261 call job_stop(job)
1262 delfunc OutHandler
1263 delfunc CloseHandler
1264 endtry
1265endfunc
1266
Bram Moolenaar437905c2016-04-26 19:01:05 +02001267func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001268 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001269 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001270 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001271 endfunc
1272 let job = job_start(s:python . " test_channel_pipe.py quit now",
1273 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001274 " the job may be done quickly, also accept "dead"
1275 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001276 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001277 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001278 finally
1279 call job_stop(job)
1280 delfunc CloseHandler
1281 endtry
1282endfunc
1283
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001284" Use channel in NL mode but received text does not end in NL.
1285func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001286 let g:Ch_received = ''
1287 func! CloseHandler(chan)
1288 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1289 let g:Ch_received .= ch_read(a:chan)
1290 endwhile
1291 endfunc
1292 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1293 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001294 " the job may be done quickly, also accept "dead"
1295 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001296 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001297 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001298 finally
1299 call job_stop(job)
1300 delfunc CloseHandler
1301 endtry
1302endfunc
1303
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001304func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001305 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001306 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1307 \ 'out_mode': 'json',
1308 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1309 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001310 call assert_equal("run", job_status(job))
1311 try
1312 let g:Ch_outmsg = ''
1313 let g:Ch_errmsg = ''
1314 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1315 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001316 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1317 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001318 finally
1319 call job_stop(job)
1320 endtry
1321endfunc
1322
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001323func Test_close_and_exit_cb()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001324 let g:retdict = {'ret': {}}
1325 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001326 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001327 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001328 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001329 let self.ret['exit_cb'] = job_status(a:job)
1330 endfunc
1331
Bram Moolenaare2956092019-01-25 21:01:17 +01001332 let job = job_start([&shell, &shellcmdflag, 'echo'],
1333 \ {'close_cb': g:retdict.close_cb,
1334 \ 'exit_cb': g:retdict.exit_cb})
1335 " the job may be done quickly, also accept "dead"
1336 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001337 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001338 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001339 call assert_equal('dead', g:retdict.ret['exit_cb'])
1340 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001341endfunc
1342
Bram Moolenaard46ae142016-02-16 13:33:52 +01001343""""""""""
1344
Bram Moolenaar0b146882018-09-06 16:27:24 +02001345function ExitCbWipe(job, status)
1346 exe g:wipe_buf 'bw!'
1347endfunction
1348
1349" This caused a crash, because messages were handled while peeking for a
1350" character.
1351func Test_exit_cb_wipes_buf()
1352 if !has('timers')
1353 return
1354 endif
1355 set cursorline lazyredraw
1356 call test_override('redraw_flag', 1)
1357 new
1358 let g:wipe_buf = bufnr('')
1359
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001360 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001361 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001362 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1363 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1364 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1365 call timer_stop(timer)
1366
1367 set nocursorline nolazyredraw
1368 unlet g:wipe_buf
1369 call test_override('ALL', 0)
1370endfunc
1371
1372""""""""""
1373
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001374let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001375func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001376 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001377 unlet s:channelfd
1378endfunc
1379
1380" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001381func Ch_unlet_handle(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001382 let s:channelfd = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001383 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001384 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001385endfunc
1386
1387func Test_unlet_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001388 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001389endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001390
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001391func Test_unlet_handle_ipv6()
1392 CheckIPv6
1393 call Test_unlet_handle()
1394endfunc
1395
Bram Moolenaard46ae142016-02-16 13:33:52 +01001396""""""""""
1397
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001398let g:Ch_unletResponse = ''
1399func Ch_CloseHandler(handle, msg)
1400 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001401 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001402endfunc
1403
1404" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001405func Ch_close_handle(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001406 let s:channelfd = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001407 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001408 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001409endfunc
1410
1411func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001412 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001413endfunc
1414
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001415func Test_close_handle_ipv6()
1416 CheckIPv6
1417 call Test_close_handle()
1418endfunc
1419
1420""""""""""
1421
1422func Ch_open_ipv6(port)
1423 let handle = ch_open('[::1]:' .. a:port, s:chopt)
1424 call assert_notequal('fail', ch_status(handle))
1425endfunc
1426
1427func Test_open_ipv6()
1428 CheckIPv6
1429 call s:run_server('Ch_open_ipv6')
1430endfunc
1431
Bram Moolenaard46ae142016-02-16 13:33:52 +01001432""""""""""
1433
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001434func Test_open_fail()
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001435 call assert_fails("let ch = ch_open('noserver')", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001436 echo ch
1437 let d = ch
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001438 call assert_fails("let ch = ch_open('noserver', 10)", 'E474:')
1439 call assert_fails("let ch = ch_open('localhost:-1')", 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001440 call assert_fails("let ch = ch_open('localhost:65537')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001441 call assert_fails("let ch = ch_open('localhost:8765', {'timeout' : -1})",
1442 \ 'E474:')
1443 call assert_fails("let ch = ch_open('localhost:8765', {'axby' : 1})",
1444 \ 'E475:')
1445 call assert_fails("let ch = ch_open('localhost:8765', {'mode' : 'abc'})",
1446 \ 'E475:')
1447 call assert_fails("let ch = ch_open('localhost:8765', {'part' : 'out'})",
1448 \ 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001449 call assert_fails("let ch = ch_open('[::]')", 'E475:')
1450 call assert_fails("let ch = ch_open('[::.80')", 'E475:')
1451 call assert_fails("let ch = ch_open('[::]8080')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001452endfunc
1453
1454func Test_ch_info_fail()
1455 call assert_fails("let x = ch_info(10)", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001456endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001457
1458""""""""""
1459
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001460func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001461 " Wait up to a second for the port to open.
1462 let s:chopt.waittime = 1000
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001463 let channel = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001464 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001465 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001466 return
1467 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001468 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001469 call ch_close(channel)
1470endfunc
1471
1472func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001473 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001474 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001475endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001476
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001477func Test_open_delay_ipv6()
1478 CheckIPv6
1479 call Test_open_delay()
1480endfunc
1481
Bram Moolenaarece61b02016-02-20 21:39:05 +01001482"""""""""
1483
1484function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001485 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001486endfunc
1487
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001488function Ch_test_call(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001489 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001490 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001491 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001492 return
1493 endif
1494
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001495 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001496 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001497 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001498
1499 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'callback' : 'abc'})", 'E917:')
1500 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'drop' : ''})", 'E475:')
1501 call assert_fails("let i = ch_evalexpr(test_null_job(), '2 + 2')", 'E906:')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001502endfunc
1503
1504func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001505 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001506endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001507
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001508func Test_call_ipv6()
1509 CheckIPv6
1510 call Test_call()
1511endfunc
1512
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001513"""""""""
1514
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001515let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001516function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001517 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001518endfunc
1519
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001520function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001521 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001522 let g:Ch_exit_job = g:currentJob
1523 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001524endfunc
1525
1526func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001527 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001528
Bram Moolenaarf386f082019-07-29 23:03:03 +02001529 " wait up to a second for the job to exit
1530 for i in range(100)
1531 if g:Ch_job_exit_ret == 'done'
1532 break
1533 endif
1534 sleep 10m
1535 " calling job_status() triggers the callback
1536 call job_status(g:Ch_exit_job)
1537 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001538
Bram Moolenaarf386f082019-07-29 23:03:03 +02001539 call assert_equal('done', g:Ch_job_exit_ret)
1540 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1541 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001542endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001543
Bram Moolenaar97792de2016-10-15 18:36:49 +02001544function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001545 if job_info(a:job).process == g:exit_cb_val.process
1546 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1547 endif
1548 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001549endfunction
1550
1551func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001552 CheckFunction reltimefloat
1553
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001554 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001555 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001556 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001557 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001558 let elapsed = reltimefloat(g:exit_cb_val.end)
1559 call assert_true(elapsed > 0.5)
1560 call assert_true(elapsed < 1.0)
1561
1562 " case: unreferenced job, using timer
1563 if !has('timers')
1564 return
1565 endif
1566
1567 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1568 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1569 let g:exit_cb_val.process = job_info(g:job).process
1570 unlet g:job
1571 call Standby(1000)
1572 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1573 let elapsed = reltimefloat(g:exit_cb_val.end)
1574 else
1575 let elapsed = 1.0
1576 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001577 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001578endfunc
1579
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001580"""""""""
1581
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001582let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001583function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001584 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001585endfunc
1586
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001587function Ch_test_close_callback(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001588 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001589 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001590 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001591 return
1592 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001593 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001594
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001595 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001596 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001597endfunc
1598
1599func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001600 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001601endfunc
1602
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001603func Test_close_callback_ipv6()
1604 CheckIPv6
1605 call Test_close_callback()
1606endfunc
1607
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001608function Ch_test_close_partial(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001609 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001610 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001611 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001612 return
1613 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001614 let g:Ch_d = {}
1615 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001616 let self.close_ret = 'closed'
1617 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001618 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001619
1620 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001621 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001622 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001623endfunc
1624
1625func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001626 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001627endfunc
1628
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001629func Test_close_partial_ipv6()
1630 CheckIPv6
1631 call Test_close_partial()
1632endfunc
1633
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001634func Test_job_start_fails()
1635 " this was leaking memory
1636 call assert_fails("call job_start([''])", "E474:")
Bram Moolenaar80385682016-03-27 19:13:35 +02001637 call assert_fails('call job_start($x)', 'E474:')
1638 call assert_fails('call job_start("")', 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001639 call assert_fails('call job_start("ls", {"out_io" : "abc"})', 'E475:')
1640 call assert_fails('call job_start("ls", {"err_io" : "abc"})', 'E475:')
1641 call assert_fails('call job_start("ls", [])', 'E715:')
1642 call assert_fails("call job_start('ls', {'in_top' : -1})", 'E475:')
1643 call assert_fails("call job_start('ls', {'in_bot' : -1})", 'E475:')
1644 call assert_fails("call job_start('ls', {'channel' : -1})", 'E475:')
1645 call assert_fails("call job_start('ls', {'callback' : -1})", 'E475:')
1646 call assert_fails("call job_start('ls', {'out_cb' : -1})", 'E475:')
1647 call assert_fails("call job_start('ls', {'err_cb' : -1})", 'E475:')
1648 call assert_fails("call job_start('ls', {'close_cb' : -1})", 'E475:')
1649 call assert_fails("call job_start('ls', {'exit_cb' : -1})", 'E475:')
1650 call assert_fails("call job_start('ls', {'term_name' : []})", 'E475:')
1651 call assert_fails("call job_start('ls', {'term_finish' : 'run'})", 'E475:')
1652 call assert_fails("call job_start('ls', {'term_api' : []})", 'E475:')
1653 call assert_fails("call job_start('ls', {'stoponexit' : []})", 'E475:')
1654 call assert_fails("call job_start('ls', {'in_io' : 'file'})", 'E920:')
1655 call assert_fails("call job_start('ls', {'out_io' : 'file'})", 'E920:')
1656 call assert_fails("call job_start('ls', {'err_io' : 'file'})", 'E920:')
1657 call assert_fails("call job_start('ls', {'in_mode' : 'abc'})", 'E475:')
1658 call assert_fails("call job_start('ls', {'out_mode' : 'abc'})", 'E475:')
1659 call assert_fails("call job_start('ls', {'err_mode' : 'abc'})", 'E475:')
1660 call assert_fails("call job_start('ls',
1661 \ {'in_io' : 'buffer', 'in_buf' : 99999})", 'E86:')
1662 call assert_fails("call job_start('ls',
1663 \ {'out_io' : 'buffer', 'out_buf' : 99999})", 'E86:')
1664 call assert_fails("call job_start('ls',
1665 \ {'err_io' : 'buffer', 'err_buf' : 99999})", 'E86:')
1666
1667 call assert_fails("call job_start('ls',
1668 \ {'in_io' : 'buffer', 'in_buf' : -1})", 'E475:')
1669 call assert_fails("call job_start('ls',
1670 \ {'out_io' : 'buffer', 'out_buf' : -1})", 'E475:')
1671 call assert_fails("call job_start('ls',
1672 \ {'err_io' : 'buffer', 'err_buf' : -1})", 'E475:')
1673
1674 set nomodifiable
1675 call assert_fails("call job_start('cmd /c dir',
1676 \ {'out_io' : 'buffer', 'out_buf' :" .. bufnr() .. "})", 'E21:')
1677 call assert_fails("call job_start('cmd /c dir',
1678 \ {'err_io' : 'buffer', 'err_buf' :" .. bufnr() .. "})", 'E21:')
1679 set modifiable
1680
1681 call assert_fails("call job_start('ls', {'in_io' : 'buffer'})", 'E915:')
1682
1683 edit! XXX
1684 let bnum = bufnr()
1685 enew
1686 call assert_fails("call job_start('ls',
1687 \ {'in_io' : 'buffer', 'in_buf' : bnum})", 'E918:')
1688
1689 " Empty job tests
1690 " This was crashing on MS-Windows.
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:')
1694 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaar00157952020-04-13 17:44:47 +02001695 call assert_fails('let job = job_start(["ls", []])', 'E730:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001696 call assert_fails('call job_setoptions(test_null_job(), {})', 'E916:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001697 %bw!
Bram Moolenaar80385682016-03-27 19:13:35 +02001698endfunc
1699
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001700func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001701 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001702 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001703 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001704 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001705 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001706 call job_stop(g:job, 'kill')
1707 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001708 endtry
1709endfunc
1710
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001711" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001712func Test_partial_in_channel_cycle()
1713 let d = {}
1714 let d.a = function('string', [d])
1715 try
1716 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001717 call test_garbagecollect_now()
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001718 catch
1719 call assert_exception('E901:')
1720 endtry
1721 unlet d
1722endfunc
1723
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001724func Test_using_freed_memory()
1725 let g:a = job_start(['ls'])
1726 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001727 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001728endfunc
1729
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001730func Test_collapse_buffers()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001731 CheckExecutable cat
1732
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001733 sp test_channel.vim
1734 let g:linecount = line('$')
1735 close
1736 split testout
1737 1,$delete
1738 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001739 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001740 bwipe!
1741endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001742
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001743func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001744 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001745 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001746
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001747 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001748 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001749 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001750 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1751 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001752
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001753 bdel test_buffer
1754 call assert_equal([], getbufline(bufnr, 1, '$'))
1755
1756 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001757 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001758 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1759 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1760
1761 bwipe! test_buffer
1762endfunc
1763
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001764func Test_cmd_parsing()
1765 if !has('unix')
1766 return
1767 endif
1768 call assert_false(filereadable("file with space"))
1769 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001770 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001771 call delete("file with space")
1772
1773 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001774 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001775 call delete("file with space")
1776endfunc
1777
Bram Moolenaar82117982016-08-27 19:21:48 +02001778func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001779 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001780
1781 " Test lines from the job containing NUL are stored correctly in a buffer.
1782 new
1783 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1784 w! Xtestread
1785 bwipe!
1786 split testout
1787 1,$delete
1788 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1789 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001790 call assert_equal("asdf\nasdf", getline(1))
1791 call assert_equal("xxx\n", getline(2))
1792 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001793
1794 call delete('Xtestread')
1795 bwipe!
1796
1797 " Test lines from a buffer with NUL bytes are written correctly to the job.
1798 new mybuffer
1799 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1800 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 +02001801 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001802 bwipe!
1803 split Xtestwrite
1804 call assert_equal("asdf\nasdf", getline(1))
1805 call assert_equal("xxx\n", getline(2))
1806 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001807 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001808
1809 call delete('Xtestwrite')
1810 bwipe!
1811endfunc
1812
Bram Moolenaarec68a992016-10-03 21:37:41 +02001813func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001814 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001815 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001816 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001817 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001818 unlet g:linecount
1819endfunc
1820
1821func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001822 func s:close_cb(ch)
1823 while ch_status(a:ch) == 'buffered'
1824 let g:out .= ch_read(a:ch)
1825 endwhile
1826 endfunc
1827
1828 let g:out = ''
1829 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1830 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001831 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001832 call WaitForAssert({-> assert_equal('123', g:out)})
1833 unlet g:out
1834 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001835endfunc
1836
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001837func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001838 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001839 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001840 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001841 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001842 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001843 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001844endfunc
1845
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001846func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001847 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001848
1849 " Check that backslash in $COMSPEC is handled properly.
1850 let g:echostr = ''
1851 let cmd = $COMSPEC . ' /c echo 123'
1852 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1853 let info = job_info(job)
1854 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1855
1856 call WaitForAssert({-> assert_equal("123", g:echostr)})
1857 unlet g:echostr
1858endfunc
1859
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001860func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001861 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001862 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001863 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001864 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001865 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001866 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001867 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1868 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001869 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001870 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001871endfunc
1872
1873func Test_cwd()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001874 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001875 if has('win32')
1876 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001877 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001878 else
1879 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001880 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001881 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001882 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001883 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001884 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001885 let expect = substitute(expect, '[/\\]$', '', '')
1886 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1887 if $CI != '' && stridx(g:envstr, '/private/') == 0
1888 let g:envstr = g:envstr[8:]
1889 endif
1890 call assert_equal(expect, g:envstr)
1891 finally
1892 call job_stop(job)
1893 unlet g:envstr
1894 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001895endfunc
1896
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001897function Ch_test_close_lambda(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001898 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001899 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001900 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001901 return
1902 endif
1903 let g:Ch_close_ret = ''
1904 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001905 call test_garbagecollect_now()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001906
1907 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001908 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001909endfunc
1910
1911func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001912 call s:run_server('Ch_test_close_lambda')
1913endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001914
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001915func Test_close_lambda_ipv6()
1916 CheckIPv6
1917 call Test_close_lambda()
1918endfunc
1919
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001920func s:test_list_args(cmd, out, remove_lf)
1921 try
1922 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001923 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 +02001924 call WaitFor('"" != g:out')
1925 if has('win32')
1926 let g:out = substitute(g:out, '\r', '', 'g')
1927 endif
1928 if a:remove_lf
1929 let g:out = substitute(g:out, '\n$', '', 'g')
1930 endif
1931 call assert_equal(a:out, g:out)
1932 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001933 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001934 unlet g:out
1935 endtry
1936endfunc
1937
1938func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001939 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 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\nworld'')', "hello\nworld", 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("hello\"world\"")', 'hello"world"', 0)
1948 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1949 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1950 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1951 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1952
1953 " tests which not contain spaces in the argument
1954 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1955 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 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\"world\"")', 'hello"world"', 1)
1962 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1963endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001964
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001965func Test_keep_pty_open()
1966 if !has('unix')
1967 return
1968 endif
1969
Bram Moolenaare2956092019-01-25 21:01:17 +01001970 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1971 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001972 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1973 call assert_inrange(200, 1000, elapsed)
1974 call job_stop(job)
1975endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001976
1977func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001978 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01001979 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01001980
1981 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001982 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001983 endfunc
1984
1985 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001986 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001987 call Resume()
1988 endfunc
1989
1990 func TimerCb(timer)
1991 if has('win32')
1992 let cmd = ['cmd', '/c', 'echo.']
1993 else
1994 let cmd = ['echo']
1995 endif
1996 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1997 call substitute(repeat('a', 100000), '.', '', 'g')
1998 endfunc
1999
2000 " We should be interrupted before 'updatetime' elapsed.
2001 let g:val = 0
2002 call timer_start(1, 'TimerCb')
2003 let elapsed = Standby(&ut)
2004 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002005
2006 " Wait for both OutCb() and ExitCb() to have been called before deleting
2007 " them.
2008 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01002009 call job_stop(g:job)
2010
2011 delfunc OutCb
2012 delfunc ExitCb
2013 delfunc TimerCb
2014 unlet! g:val
2015 unlet! g:job
2016endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01002017
2018func Test_raw_large_data()
2019 try
2020 let g:out = ''
2021 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01002022 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
2023 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01002024
Bram Moolenaare2956092019-01-25 21:01:17 +01002025 let outlen = 79999
2026 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02002027 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01002028 call WaitFor({-> len(g:out) >= outlen}, 10000)
2029 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01002030 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2031 finally
2032 call job_stop(job)
2033 unlet g:out
2034 endtry
2035endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002036
Bram Moolenaar65240682019-02-10 22:23:26 +01002037func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002038 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01002039
2040 try
2041 let job = job_start(s:python . " test_channel_pipe.py busy",
2042 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
2043 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
2044 finally
2045 call job_stop(job)
2046 endtry
2047endfunc
2048
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002049func Test_job_exitval_and_termsig()
2050 if !has('unix')
2051 return
2052 endif
2053
2054 " Terminate job normally
2055 let cmd = ['echo']
2056 let job = job_start(cmd)
2057 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2058 let info = job_info(job)
2059 call assert_equal(0, info.exitval)
2060 call assert_equal("", info.termsig)
2061
2062 " Terminate job by signal
2063 let cmd = ['sleep', '10']
2064 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01002065 " 10m usually works but 50m is needed when running Valgrind
2066 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002067 call job_stop(job)
2068 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2069 let info = job_info(job)
2070 call assert_equal(-1, info.exitval)
2071 call assert_equal("term", info.termsig)
2072endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01002073
2074func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002075 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01002076
2077 call writefile(['test'], 'Xtestin')
2078 let in_opts = [{},
2079 \ {'in_io': 'null'},
2080 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
2081 let out_opts = [{},
2082 \ {'out_io': 'null'},
2083 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
2084 let err_opts = [{},
2085 \ {'err_io': 'null'},
2086 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
2087 \ {'err_io': 'out'}]
2088 let opts = []
2089
2090 for in_opt in in_opts
2091 let x = copy(in_opt)
2092 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002093 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002094 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002095 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002096 let opts += [extend({'pty': 1}, x)]
2097 endfor
2098 endfor
2099 endfor
2100
2101 for opt in opts
2102 let job = job_start('echo', opt)
2103 let info = job_info(job)
2104 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2105 \ get(opt, 'in_io', 'tty'),
2106 \ get(opt, 'out_io', 'tty'),
2107 \ get(opt, 'err_io', 'tty'))
2108
2109 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2110 call assert_notequal('', info.tty_in, msg)
2111 else
2112 call assert_equal('', info.tty_in, msg)
2113 endif
2114 call assert_equal(info.tty_in, info.tty_out, msg)
2115
2116 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2117 endfor
2118
2119 call delete('Xtestin')
2120 call delete('Xtestout')
2121 call delete('Xtesterr')
2122endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02002123
2124" Do this last, it stops any channel log.
2125func Test_zz_nl_err_to_out_pipe()
Bram Moolenaar570497a2019-08-22 22:55:13 +02002126 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02002127 call ch_log('Test_zz_nl_err_to_out_pipe()')
2128 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
2129 call assert_equal("run", job_status(job))
2130 try
2131 let handle = job_getchannel(job)
2132 call ch_sendraw(handle, "echo something\n")
2133 call assert_equal("something", ch_readraw(handle))
2134
2135 call ch_sendraw(handle, "echoerr wrong\n")
2136 call assert_equal("wrong", ch_readraw(handle))
2137 finally
2138 call job_stop(job)
2139 call ch_logfile('')
2140 let loglines = readfile('Xlog')
2141 call assert_true(len(loglines) > 10)
2142 let found_test = 0
2143 let found_send = 0
2144 let found_recv = 0
2145 let found_stop = 0
2146 for l in loglines
2147 if l =~ 'Test_zz_nl_err_to_out_pipe'
2148 let found_test = 1
2149 endif
2150 if l =~ 'SEND on.*echo something'
2151 let found_send = 1
2152 endif
2153 if l =~ 'RECV on.*something'
2154 let found_recv = 1
2155 endif
2156 if l =~ 'Stopping job with'
2157 let found_stop = 1
2158 endif
2159 endfor
2160 call assert_equal(1, found_test)
2161 call assert_equal(1, found_send)
2162 call assert_equal(1, found_recv)
2163 call assert_equal(1, found_stop)
2164 " On MS-Windows need to sleep for a moment to be able to delete the file.
2165 sleep 10m
2166 call delete('Xlog')
2167 endtry
2168endfunc
2169
2170" Do this last, it stops any channel log.
2171func Test_zz_ch_log()
2172 call ch_logfile('Xlog', 'w')
2173 call ch_log('hello there')
2174 call ch_log('%s%s')
2175 call ch_logfile('')
2176 let text = readfile('Xlog')
2177 call assert_match("hello there", text[1])
2178 call assert_match("%s%s", text[2])
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002179 call mkdir("Xdir1")
2180 call assert_fails("call ch_logfile('Xdir1')", 'E484:')
2181 cal delete("Xdir1", 'd')
Bram Moolenaarf386f082019-07-29 23:03:03 +02002182 call delete('Xlog')
2183endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002184
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002185func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002186 if has('win32')
2187 let cmd = 'cmd /c pause'
2188 else
2189 let cmd = 'grep foo'
2190 endif
2191 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002192 call job_stop(g:job)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002193 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002194 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002195 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002196 call job_stop(g:job, 'term')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002197 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002198 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002199 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002200 call job_stop(g:job, 'kill')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002201 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002202 call assert_equal(-1, job_info(g:job).exitval)
2203endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002204
2205func Test_issue_5485()
2206 let $VAR1 = 'global'
2207 let g:Ch_reply = ""
2208 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2209 let g:Ch_job = l:job
2210 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2211 unlet $VAR1
2212endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002213
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002214func Test_job_trailing_space_unix()
2215 CheckUnix
2216 CheckExecutable cat
2217 let job = job_start("cat ", #{in_io: 'null'})
2218 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2219 call assert_equal(0, job_info(job).exitval)
2220endfunc
2221
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002222func Test_ch_getbufnr()
2223 let ch = test_null_channel()
2224 call assert_equal(-1, ch_getbufnr(ch, 'in'))
2225 call assert_equal(-1, ch_getbufnr(ch, 'out'))
2226 call assert_equal(-1, ch_getbufnr(ch, 'err'))
2227 call assert_equal(-1, ch_getbufnr(ch, ''))
2228endfunc
2229
2230" Test for unsupported options passed to ch_status()
2231func Test_invalid_job_chan_options()
2232 let ch = test_null_channel()
2233 let invalid_opts = [
2234 \ {'in_io' : 'null'},
2235 \ {'out_io' : 'null'},
2236 \ {'err_io' : 'null'},
2237 \ {'mode' : 'json'},
2238 \ {'out_mode' : 'json'},
2239 \ {'err_mode' : 'json'},
2240 \ {'noblock' : 1},
2241 \ {'in_name' : '/a/b'},
2242 \ {'pty' : 1},
2243 \ {'in_buf' : 1},
2244 \ {'out_buf' : 1},
2245 \ {'err_buf' : 1},
2246 \ {'out_modifiable' : 1},
2247 \ {'err_modifiable' : 1},
2248 \ {'out_msg' : 1},
2249 \ {'err_msg' : 1},
2250 \ {'in_top' : 1},
2251 \ {'in_bot' : 1},
2252 \ {'channel' : ch},
2253 \ {'callback' : ''},
2254 \ {'out_cb' : ''},
2255 \ {'err_cb' : ''},
2256 \ {'close_cb' : ''},
2257 \ {'exit_cb' : ''},
2258 \ {'term_opencmd' : ''},
2259 \ {'eof_chars' : ''},
2260 \ {'term_rows' : 10},
2261 \ {'term_cols' : 10},
2262 \ {'vertical' : 0},
2263 \ {'curwin' : 1},
2264 \ {'bufnr' : 1},
2265 \ {'hidden' : 0},
2266 \ {'norestore' : 0},
2267 \ {'term_kill' : 'kill'},
2268 \ {'tty_type' : ''},
2269 \ {'term_highlight' : ''},
2270 \ {'env' : {}},
2271 \ {'cwd' : ''},
2272 \ {'timeout' : 0},
2273 \ {'out_timeout' : 0},
2274 \ {'err_timeout' : 0},
2275 \ {'id' : 0},
2276 \ {'stoponexit' : ''},
2277 \ {'block_write' : 1}
2278 \ ]
2279 if has('gui')
2280 call add(invalid_opts, {'ansi_colors' : []})
2281 endif
2282
2283 for opt in invalid_opts
2284 call assert_fails("let x = ch_status(ch, opt)", 'E475:')
2285 endfor
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002286 call assert_equal('fail', ch_status(ch, test_null_dict()))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002287endfunc
2288
2289" Test for passing the command and the arguments as List on MS-Windows
2290func Test_job_with_list_args()
2291 CheckMSWindows
2292
2293 enew!
2294 let bnum = bufnr()
2295 let job = job_start(['cmd', '/c', 'echo', 'Hello', 'World'], {'out_io' : 'buffer', 'out_buf' : bnum})
2296 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2297 call assert_equal('Hello World', getline(1))
2298 %bw!
2299endfunc
2300
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002301func ExitCb_cb_with_input(job, status)
2302 call feedkeys(":\<C-u>echo input('', 'default')\<CR>\<CR>", 'nx')
2303 call assert_equal('default', Screenline(&lines))
2304 let g:wait_exit_cb = 0
2305endfunc
2306
2307func Test_cb_with_input()
2308 let g:wait_exit_cb = 1
2309
2310 call job_start('echo "Vim''s test"',
2311 \ {'out_cb': 'ExitCb_cb_with_input'})
2312 call WaitForAssert({-> assert_equal(0, g:wait_exit_cb)})
2313
2314 unlet g:wait_exit_cb
2315endfunc
2316
Bram Moolenaar8b633132020-03-20 18:20:51 +01002317" vim: shiftwidth=2 sts=2 expandtab