blob: e1f8a5e352e42f84ee5ec4a06c1cfd27fe083973 [file] [log] [blame]
Bram Moolenaarf386f082019-07-29 23:03:03 +02001" Test for channel and job functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaarf386f082019-07-29 23:03:03 +02003" When +channel is supported then +job is too, so we don't check for that.
Bram Moolenaar4641a122019-07-29 22:10:23 +02004source check.vim
5CheckFeature channel
Bram Moolenaare2469252016-02-04 10:54:34 +01006
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
Bram Moolenaar4641a122019-07-29 22:10:23 +02008source screendump.vim
Bram Moolenaar321efdd2016-07-15 17:09:11 +02009
10let s:python = PythonProg()
11if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010012 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020013 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010014endif
15
Bram Moolenaar37175402017-03-18 20:18:45 +010016" Uncomment the next line to see what happens. Output is in
17" src/testdir/channellog.
Bram Moolenaarf386f082019-07-29 23:03:03 +020018" Add ch_log() calls where you want to see what happens.
Bram Moolenaar37175402017-03-18 20:18:45 +010019" call ch_logfile('channellog', 'w')
20
Bram Moolenaare74e8e72016-02-16 22:01:30 +010021let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010022
Bram Moolenaar4b96df52020-01-26 22:00:26 +010023" Run "testfunc" after starting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010024func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020025 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010026endfunc
27
Bram Moolenaar819524702018-02-27 19:10:00 +010028" Return a list of open files.
29" Can be used to make sure no resources leaked.
30" Returns an empty list on systems where this is not supported.
31func s:get_resources()
32 let pid = getpid()
33
Bram Moolenaar39536dd2019-01-29 22:58:21 +010034 if executable('lsof')
Bram Moolenaar819524702018-02-27 19:10:00 +010035 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
36 elseif isdirectory('/proc/' . pid . '/fd/')
37 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
38 else
39 return []
40 endif
41endfunc
42
Bram Moolenaar321efdd2016-07-15 17:09:11 +020043let g:Ch_responseMsg = ''
44func Ch_requestHandler(handle, msg)
45 let g:Ch_responseHandle = a:handle
46 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010047endfunc
48
Bram Moolenaar321efdd2016-07-15 17:09:11 +020049func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010050 " Avoid dropping messages, since we don't use a callback here.
51 let s:chopt.drop = 'never'
Bram Moolenaar0b146882018-09-06 16:27:24 +020052 " Also add the noblock flag to try it out.
53 let s:chopt.noblock = 1
Bram Moolenaard6a8d482016-02-10 20:32:20 +010054 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar5643db82016-12-03 14:29:10 +010055 unlet s:chopt.drop
Bram Moolenaar0b146882018-09-06 16:27:24 +020056 unlet s:chopt.noblock
Bram Moolenaar77073442016-02-13 23:23:53 +010057 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010058 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010059 return
60 endif
Bram Moolenaarf386f082019-07-29 23:03:03 +020061
62 " check that getjob without a job is handled correctly
63 call assert_equal('no process', string(ch_getjob(handle)))
64
Bram Moolenaar570497a2019-08-22 22:55:13 +020065 let dict = handle->ch_info()
Bram Moolenaar03602ec2016-03-20 20:57:45 +010066 call assert_true(dict.id != 0)
67 call assert_equal('open', dict.status)
68 call assert_equal(a:port, string(dict.port))
69 call assert_equal('open', dict.sock_status)
70 call assert_equal('socket', dict.sock_io)
71
Bram Moolenaard7ece102016-02-02 23:23:02 +010072 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010073 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010074
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010075 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010076 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
77 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
78 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
79
80 " split command should work
81 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020082 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010083 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010084
Bram Moolenaarf1f07922016-08-26 17:58:53 +020085 " string with ][ should work
86 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
87
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088 " nothing to read now
89 call assert_equal(0, ch_canread(handle))
90
Bram Moolenaarf1f07922016-08-26 17:58:53 +020091 " sending three messages quickly then reading should work
92 for i in range(3)
93 call ch_sendexpr(handle, 'echo hello ' . i)
94 endfor
95 call assert_equal('hello 0', ch_read(handle)[1])
96 call assert_equal('hello 1', ch_read(handle)[1])
97 call assert_equal('hello 2', ch_read(handle)[1])
98
Bram Moolenaard7ece102016-02-02 23:23:02 +010099 " Request that triggers sending two ex commands. These will usually be
100 " handled before getting the response, but it's not guaranteed, thus wait a
101 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100102 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200103 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +0100104 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100105
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100106 " Request command "foo bar", which fails silently.
107 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200108 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100109
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100110 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200111 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100112
Bram Moolenaara07fec92016-02-05 21:04:08 +0100113 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200114 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
115 call WaitFor('exists("g:Ch_responseHandle")')
116 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100117 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100118 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200119 call assert_equal(handle, g:Ch_responseHandle)
120 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100121 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200122 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100123
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200124 let g:Ch_responseMsg = ''
125 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
126 call WaitFor('exists("g:Ch_responseHandle")')
127 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100128 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100129 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200130 call assert_equal(handle, g:Ch_responseHandle)
131 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100132 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200133 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100134
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200135 " Using lambda.
136 let g:Ch_responseMsg = ''
137 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
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 Moolenaar069c1e72016-07-15 21:25:08 +0200141 else
142 call assert_equal(handle, g:Ch_responseHandle)
143 unlet g:Ch_responseHandle
144 endif
145 call assert_equal('got it', g:Ch_responseMsg)
146
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100147 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200148 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100149
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100150 " check setting options (without testing the effect)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200151 eval handle->ch_setoptions({'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100152 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100153 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100154 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100155 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100156 call ch_setoptions(handle, {'drop': 'never'})
157 call ch_setoptions(handle, {'drop': 'auto'})
158 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100159
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100160 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100161 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100162 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100163 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100164
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100165 " Send an eval request with special characters.
166 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
167 sleep 10m
168 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
169
170 " Send an eval request to get a line with special characters.
171 call setline(3, "a\nb\<CR>c\x01d\x7fe")
172 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
173 sleep 10m
174 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
175
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100176 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100177 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100178 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100179 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100180
Bram Moolenaar55fab432016-02-07 16:53:13 +0100181 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100182 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100183 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100184 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100185
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100186 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100187 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100188 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100189 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100190
Bram Moolenaarf4160862016-02-05 23:09:12 +0100191 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100192 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200193 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100194 call assert_equal('one', getline(line('$') - 2))
195 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100196
197 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100198 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
199 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100200
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100201 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100202
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100203 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100204 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100205 if exists('*reltimefloat')
206 let start = reltime()
207 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
208 let elapsed = reltime(start)
209 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
210 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100211
212 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100213 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100214 let resp = ch_read(handle)
215 call assert_equal(type([]), type(resp))
216 call assert_equal(type(11), type(resp[0]))
217 call assert_equal('waited', resp[1])
218
Bram Moolenaard7ece102016-02-02 23:23:02 +0100219 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100220 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100221endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100222
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100223func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100224 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200225 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100226endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100227
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100228" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200229func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100230 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200231 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar570497a2019-08-22 22:55:13 +0200232 if handle->ch_status() == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100233 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100234 return
235 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100236
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100237 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100238
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100239 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100240 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100241 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100242 return
243 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100244 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
245 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100246
247 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100248 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100249
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100250 call ch_close(newhandle)
251endfunc
252
253func Test_two_channels()
Bram Moolenaar570497a2019-08-22 22:55:13 +0200254 eval 'Test_two_channels()'->ch_log()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200255 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100256endfunc
257
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100258" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200259func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100260 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100261 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100262 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100263 return
264 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100265
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100266 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100267
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100268 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100269endfunc
270
271func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100272 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200273 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100274endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100275
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100276"""""""""
277
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200278func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100279 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200280 unlet g:Ch_reply
281 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100282endfunc
283
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200284func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100285 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100286 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100287 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100288 return
289 endif
290
291 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100292 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200293 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100294
295 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100296 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200297 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100298endfunc
299
300func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100301 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200302 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200303 let s:chopt.callback = 'Ch_handler'
304 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200305 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200306 let s:chopt.callback = function('Ch_handler')
307 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100308 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100309endfunc
310
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100311"""""""""
312
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200313let g:Ch_reply = ''
314func Ch_zeroHandler(chan, msg)
315 unlet g:Ch_reply
316 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100317endfunc
318
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200319let g:Ch_zero_reply = ''
320func Ch_oneHandler(chan, msg)
321 unlet g:Ch_zero_reply
322 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100323endfunc
324
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200325func Ch_channel_zero(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200326 let handle = ('localhost:' .. a:port)->ch_open(s:chopt)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100327 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100328 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100329 return
330 endif
331
332 " Check that eval works.
333 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
334
335 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200336 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100337 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100338 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200339 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100340 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100341 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200342 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100343 endif
344
345 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200346 let g:Ch_reply = ''
347 let g:Ch_zero_reply = ''
348 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200349 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100350 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200351 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100352 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200353 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100354 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100355endfunc
356
357func Test_zero_reply()
358 call ch_log('Test_zero_reply()')
359 " Run with channel handler
360 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200361 let s:chopt.callback = 'Ch_zeroHandler'
362 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100363 unlet s:chopt.callback
364
365 " Run without channel handler
366 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200367 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100368endfunc
369
370"""""""""
371
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200372let g:Ch_reply1 = ""
373func Ch_handleRaw1(chan, msg)
374 unlet g:Ch_reply1
375 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100376endfunc
377
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200378let g:Ch_reply2 = ""
379func Ch_handleRaw2(chan, msg)
380 unlet g:Ch_reply2
381 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100382endfunc
383
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200384let g:Ch_reply3 = ""
385func Ch_handleRaw3(chan, msg)
386 unlet g:Ch_reply3
387 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100388endfunc
389
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200390func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100391 let handle = ch_open('localhost:' . a:port, s:chopt)
392 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100393 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100394 return
395 endif
396 call ch_setoptions(handle, {'mode': 'raw'})
397
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100398 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200399 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200400 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200401 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
402 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200403 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100404 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200405 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100406endfunc
407
408func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100409 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200410 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100411endfunc
412
413"""""""""
414
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100415" Test that trying to connect to a non-existing port fails quickly.
416func Test_connect_waittime()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100417 CheckFunction reltimefloat
418
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100419 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100420 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100421 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100422 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100423 " Oops, port does exists.
424 call ch_close(handle)
425 else
426 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100427 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100428 endif
429
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100430 " We intend to use a socket that doesn't exist and wait for half a second
431 " before giving up. If the socket does exist it can fail in various ways.
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100432 " Check for "Connection reset by peer" to avoid flakiness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100433 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100434 try
435 let handle = ch_open('localhost:9867', {'waittime': 500})
436 if ch_status(handle) != "fail"
437 " Oops, port does exists.
438 call ch_close(handle)
439 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100440 " Failed connection should wait about 500 msec. Can be longer if the
441 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100442 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100443 endif
444 catch
445 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100446 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100447 endif
448 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100449endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100450
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100451"""""""""
452
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100453func Test_raw_pipe()
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100454 " Add a dummy close callback to avoid that messages are dropped when calling
455 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200456 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100457 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200458 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200459 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100460 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200461
462 call assert_equal("open", ch_status(job))
463 call assert_equal("open", ch_status(job), {"part": "out"})
464 call assert_equal("open", ch_status(job), {"part": "err"})
465 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
466 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
467
468 let dict = ch_info(job)
469 call assert_true(dict.id != 0)
470 call assert_equal('open', dict.status)
471 call assert_equal('open', dict.out_status)
472 call assert_equal('RAW', dict.out_mode)
473 call assert_equal('pipe', dict.out_io)
474 call assert_equal('open', dict.err_status)
475 call assert_equal('RAW', dict.err_mode)
476 call assert_equal('pipe', dict.err_io)
477
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100478 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100479 " For a change use the job where a channel is expected.
480 call ch_sendraw(job, "echo something\n")
481 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100482 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
483
Bram Moolenaar151f6562016-03-07 21:19:38 +0100484 call ch_sendraw(job, "double this\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200485 let g:handle = job->job_getchannel()
486 call WaitFor('g:handle->ch_canread()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100487 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100488 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100489 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
490
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200491 let g:Ch_reply = ""
492 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200493 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200494
Bram Moolenaar570497a2019-08-22 22:55:13 +0200495 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100496 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
497 finally
498 call job_stop(job)
499 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100500
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200501 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200502 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar570497a2019-08-22 22:55:13 +0200503 let info = job->job_info()
Bram Moolenaar8950a562016-03-12 15:22:55 +0100504 call assert_equal("dead", info.status)
505 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200506 call assert_equal(2, len(info.cmd))
507 call assert_equal("test_channel_pipe.py", info.cmd[1])
508
509 let found = 0
510 for j in job_info()
511 if j == job
512 let found += 1
513 endif
514 endfor
515 call assert_equal(1, found)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100516endfunc
517
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100518func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100519 " Add a dummy close callback to avoid that messages are dropped when calling
520 " ch_canread().
521 " Also test the non-blocking option.
522 let job = job_start(s:python . " test_channel_pipe.py",
523 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
524 call assert_equal(v:t_job, type(job))
525 call assert_equal("run", job_status(job))
526
527 call assert_equal("open", ch_status(job))
528 call assert_equal("open", ch_status(job), {"part": "out"})
529
530 try
531 " Create a blob with the echo command and write it.
532 let blob = 0z00
533 let cmd = "echo something\n"
534 for i in range(0, len(cmd) - 1)
535 let blob[i] = char2nr(cmd[i])
536 endfor
537 call assert_equal(len(cmd), len(blob))
538 call ch_sendraw(job, blob)
539
540 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200541 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100542 let expected = 'something'
543 for i in range(0, len(expected) - 1)
544 call assert_equal(char2nr(expected[i]), msg[i])
545 endfor
546
547 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
548 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
549 finally
550 call job_stop(job)
551 endtry
552
553 let g:Ch_job = job
554 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
555 let info = job_info(job)
556 call assert_equal("dead", info.status)
557endfunc
558
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100559func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100560 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100561 call assert_equal("run", job_status(job))
562 try
563 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100564 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200565 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100566
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100567 call ch_sendraw(handle, "echoerr wrong\n")
568 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
569
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100570 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100571 call assert_equal("this", ch_readraw(handle))
572 call assert_equal("AND this", ch_readraw(handle))
573
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200574 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200575 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200576
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100577 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100578 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100579 finally
580 call job_stop(job)
581 endtry
582endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100583
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200584func Stop_g_job()
585 call job_stop(g:job)
586 if has('win32')
587 " On MS-Windows the server must close the file handle before we are able
588 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200589 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200590 sleep 10m
591 endif
592endfunc
593
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100594func Test_nl_read_file()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100595 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200596 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100597 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200598 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100599 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200600 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100601 call assert_equal("something", ch_readraw(handle))
602 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
603 call assert_equal("this", ch_readraw(handle))
604 call assert_equal("AND this", ch_readraw(handle))
605 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200606 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100607 call delete('Xinput')
608 endtry
609endfunc
610
Bram Moolenaare98d1212016-03-08 15:37:41 +0100611func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200612 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100613 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200614 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100615 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200616 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100617 call ch_sendraw(handle, "echo line one\n")
618 call ch_sendraw(handle, "echo line two\n")
619 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200620 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100621 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200622 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100623 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100624 call delete('Xoutput')
625 endtry
626endfunc
627
628func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200629 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100630 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200631 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100632 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200633 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100634 call ch_sendraw(handle, "echoerr line one\n")
635 call ch_sendraw(handle, "echoerr line two\n")
636 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200637 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100638 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200639 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100640 call delete('Xoutput')
641 endtry
642endfunc
643
644func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200645 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100646 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200647 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100648 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200649 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100650 call ch_sendraw(handle, "echoerr line one\n")
651 call ch_sendraw(handle, "echo line two\n")
652 call ch_sendraw(handle, "double this\n")
653 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200654 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100655 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200656 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100657 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100658 call delete('Xoutput')
659 endtry
660endfunc
661
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200662func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200663 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200664endfunc
665
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200666func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200667 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200668 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200669 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100670 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100671 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200672 if a:do_msg
673 let expected[0] = 'Reading from channel output...'
674 else
675 let options['out_msg'] = 0
676 call remove(expected, 0)
677 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100678 else
679 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100680 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100681 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200682 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100683 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200684 if a:nomod
685 let options['out_modifiable'] = 0
686 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100687 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100688 call assert_equal("run", job_status(job))
689 try
690 let handle = job_getchannel(job)
691 call ch_sendraw(handle, "echo line one\n")
692 call ch_sendraw(handle, "echo line two\n")
693 call ch_sendraw(handle, "double this\n")
694 call ch_sendraw(handle, "quit\n")
695 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100696 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200697 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200698 if a:nomod
699 call assert_equal(0, &modifiable)
700 else
701 call assert_equal(1, &modifiable)
702 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200703 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100704 bwipe!
705 finally
706 call job_stop(job)
707 endtry
708endfunc
709
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100710func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200711 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100712endfunc
713
714func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200715 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100716endfunc
717
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200718func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200719 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200720endfunc
721
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200722func Test_pipe_to_buffer_name_nomsg()
723 call Run_test_pipe_to_buffer(1, 0, 1)
724endfunc
725
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200726func Test_close_output_buffer()
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200727 enew!
728 let test_lines = ['one', 'two']
729 call setline(1, test_lines)
730 call ch_log('Test_close_output_buffer()')
731 let options = {'out_io': 'buffer'}
732 let options['out_name'] = 'buffer-output'
733 let options['out_msg'] = 0
734 split buffer-output
735 let job = job_start(s:python . " test_channel_write.py", options)
736 call assert_equal("run", job_status(job))
737 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200738 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200739 quit!
740 sleep 100m
741 " Make sure the write didn't happen to the wrong buffer.
742 call assert_equal(test_lines, getline(1, line('$')))
743 call assert_equal(-1, bufwinnr('buffer-output'))
744 sbuf buffer-output
745 call assert_notequal(-1, bufwinnr('buffer-output'))
746 sleep 100m
747 close " no more writes
748 bwipe!
749 finally
750 call job_stop(job)
751 endtry
752endfunc
753
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200754func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100755 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200756 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100757 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100758 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200759 if a:do_msg
760 let expected[0] = 'Reading from channel error...'
761 else
762 let options['err_msg'] = 0
763 call remove(expected, 0)
764 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100765 else
766 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100767 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100768 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200769 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100770 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200771 if a:nomod
772 let options['err_modifiable'] = 0
773 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100774 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100775 call assert_equal("run", job_status(job))
776 try
777 let handle = job_getchannel(job)
778 call ch_sendraw(handle, "echoerr line one\n")
779 call ch_sendraw(handle, "echoerr line two\n")
780 call ch_sendraw(handle, "doubleerr this\n")
781 call ch_sendraw(handle, "quit\n")
782 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200783 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200784 if a:nomod
785 call assert_equal(0, &modifiable)
786 else
787 call assert_equal(1, &modifiable)
788 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100789 bwipe!
790 finally
791 call job_stop(job)
792 endtry
793endfunc
794
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100795func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200796 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100797endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100798
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100799func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200800 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200801endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100802
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200803func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200804 call Run_test_pipe_err_to_buffer(1, 1, 1)
805endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100806
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200807func Test_pipe_err_to_buffer_name_nomsg()
808 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100809endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100810
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100811func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100812 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100813 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100814 call assert_equal("run", job_status(job))
815 try
816 let handle = job_getchannel(job)
817 call ch_sendraw(handle, "echo line one\n")
818 call ch_sendraw(handle, "echoerr line two\n")
819 call ch_sendraw(handle, "double this\n")
820 call ch_sendraw(handle, "doubleerr that\n")
821 call ch_sendraw(handle, "quit\n")
822 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200823 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 +0100824 bwipe!
825 finally
826 call job_stop(job)
827 endtry
828endfunc
829
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100830func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100831 sp pipe-input
832 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200833 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100834 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100835 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100836 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100837 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100838 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100839
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100840 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100841 call assert_equal("run", job_status(job))
842 try
843 let handle = job_getchannel(job)
844 call assert_equal('one', ch_read(handle))
845 call assert_equal('two', ch_read(handle))
846 call assert_equal('three', ch_read(handle))
847 bwipe!
848 finally
849 call job_stop(job)
850 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100851endfunc
852
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100853func Test_pipe_from_buffer_name()
854 call Run_test_pipe_from_buffer(1)
855endfunc
856
857func Test_pipe_from_buffer_nr()
858 call Run_test_pipe_from_buffer(0)
859endfunc
860
Bram Moolenaar0874a832016-09-01 15:11:51 +0200861func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200862 CheckExecutable sort
863
Bram Moolenaar0874a832016-09-01 15:11:51 +0200864 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
865 if a:use_buffer
866 split sortin
867 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
868 let options.in_io = 'buffer'
869 let options.in_name = 'sortin'
870 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200871 if !a:all
872 let options.in_top = 2
873 let options.in_bot = 4
874 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100875 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200876
877 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100878 call assert_equal("run", job_status(job))
879 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200880 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200881 endif
882
Bram Moolenaare2956092019-01-25 21:01:17 +0100883 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200884
Bram Moolenaard8b55492016-09-01 14:35:22 +0200885 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200886 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200887 call assert_equal('Reading from channel output...', getline(1))
888 if a:all
889 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
890 else
891 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
892 endif
893
Bram Moolenaare2956092019-01-25 21:01:17 +0100894 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200895 if a:use_buffer
896 bwipe! sortin
897 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200898 bwipe! sortout
899endfunc
900
901func Test_pipe_through_sort_all()
902 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200903 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200904endfunc
905
906func Test_pipe_through_sort_some()
907 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200908 call Run_pipe_through_sort(0, 1)
909endfunc
910
911func Test_pipe_through_sort_feed()
912 call ch_log('Test_pipe_through_sort_feed()')
913 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200914endfunc
915
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100916func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100917 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100918 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100919 call assert_equal("run", job_status(job))
920 try
921 let handle = job_getchannel(job)
922 call ch_sendraw(handle, "echo line one\n")
923 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200924 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200925 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100926 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
927 bwipe!
928 finally
929 call job_stop(job)
930 endtry
931endfunc
932
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100933func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100934 CheckFunction reltimefloat
935
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100936 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100937 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100938 call assert_equal("run", job_status(job))
939 try
940 let handle = job_getchannel(job)
941 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
942 call ch_sendraw(handle, "echo [-2, 12.34]\n")
943 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200944 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100945 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
946 bwipe!
947 finally
948 call job_stop(job)
949 endtry
950endfunc
951
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100952" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100953func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100954 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100955 if getline(line('$') - a:offset) == a:line
956 break
957 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100958 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100959 endfor
960endfunc
961
962func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100963 " Create two buffers, one to read from and one to write to.
964 split pipe-output
965 set buftype=nofile
966 split pipe-input
967 set buftype=nofile
968
969 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100970 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200971 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
972 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100973 call assert_equal("run", job_status(job))
974 try
975 exe "normal Gaecho hello\<CR>"
976 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100977 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100978 call assert_equal('hello', getline('$'))
979
980 exe bufwinnr('pipe-input') . "wincmd w"
981 exe "normal Gadouble this\<CR>"
982 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100983 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100984 call assert_equal('this', getline(line('$') - 1))
985 call assert_equal('AND this', getline('$'))
986
987 bwipe!
988 exe bufwinnr('pipe-input') . "wincmd w"
989 bwipe!
990 finally
991 call job_stop(job)
992 endtry
993endfunc
994
995func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100996 " Create one buffer to read from and to write to.
997 split pipe-io
998 set buftype=nofile
999
1000 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001001 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001002 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1003 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001004 call assert_equal("run", job_status(job))
1005 try
1006 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001007 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001008 call assert_equal('hello', getline(line('$') - 1))
1009
1010 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001011 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001012 call assert_equal('this', getline(line('$') - 2))
1013 call assert_equal('AND this', getline(line('$') - 1))
1014
1015 bwipe!
1016 finally
1017 call job_stop(job)
1018 endtry
1019endfunc
1020
Bram Moolenaar4641a122019-07-29 22:10:23 +02001021func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001022 CheckScreendump
1023
Bram Moolenaar4641a122019-07-29 22:10:23 +02001024 let lines =<< trim END
1025 new Xscrollbuffer
1026 call setline(1, range(1, 200))
1027 $
1028 redraw
1029 wincmd w
1030 call deletebufline('Xscrollbuffer', 1, '$')
1031 if has('win32')
1032 let cmd = ['cmd', '/c', 'echo sometext']
1033 else
1034 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1035 endif
1036 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1037 END
1038 call writefile(lines, 'XtestBufferScroll')
1039 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaarf386f082019-07-29 23:03:03 +02001040 call term_wait(buf, 100)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001041 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1042
1043 " clean up
1044 call StopVimInTerminal(buf)
1045 call delete('XtestBufferScroll')
1046endfunc
1047
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001048func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001049 " We cannot check that no I/O works, we only check that the job starts
1050 " properly.
1051 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001052 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001053 call assert_equal("run", job_status(job))
1054 try
1055 call assert_equal('something', ch_read(job))
1056 finally
1057 call job_stop(job)
1058 endtry
1059
1060 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001061 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001062 call assert_equal("run", job_status(job))
1063 try
1064 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1065 finally
1066 call job_stop(job)
1067 endtry
1068
1069 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001070 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001071 call assert_equal("run", job_status(job))
1072 try
1073 call assert_equal('something', ch_read(job))
1074 finally
1075 call job_stop(job)
1076 endtry
1077
1078 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001079 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001080 call assert_equal("run", job_status(job))
1081 call job_stop(job)
1082
1083 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001084 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001085 call assert_equal("run", job_status(job))
1086 call assert_equal('channel fail', string(job_getchannel(job)))
1087 call assert_equal('fail', ch_status(job))
1088 call job_stop(job)
1089endfunc
1090
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001091func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001092 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1093 split testout
1094 let job = job_start([s:python, '-c',
1095 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001096 " the job may be done quickly, also accept "dead"
1097 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001098 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001099 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001100 let totlen = 0
1101 for line in getline(1, '$')
1102 call assert_equal('', substitute(line, '^\.*', '', ''))
1103 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001104 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001105 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001106 finally
1107 call job_stop(job)
1108 bwipe!
1109 endtry
1110endfunc
1111
Bram Moolenaarde279892016-03-11 22:19:44 +01001112func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001113 let job = job_start(s:python . " test_channel_pipe.py")
1114 call assert_equal("run", job_status(job))
1115 let handle = job_getchannel(job)
1116 try
1117 call ch_sendraw(handle, "echo something\n")
1118 call assert_equal("something", ch_readraw(handle))
1119 finally
1120 call job_stop(job)
1121 endtry
1122
1123 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1124 call assert_equal("run", job_status(job))
1125 let handle = job_getchannel(job)
1126 try
1127 call ch_sendraw(handle, "echo again\n")
1128 call assert_equal("again", ch_readraw(handle))
1129 finally
1130 call job_stop(job)
1131 endtry
1132endfunc
1133
Bram Moolenaar75f72652016-03-20 22:16:56 +01001134func Test_out_cb()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001135 let dict = {'thisis': 'dict: '}
1136 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001137 if type(a:msg) == v:t_string
1138 let g:Ch_outmsg = self.thisis . a:msg
1139 else
1140 let g:Ch_outobj = a:msg
1141 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001142 endfunc
1143 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001144 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001145 endfunc
1146 let job = job_start(s:python . " test_channel_pipe.py",
1147 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001148 \ 'out_mode': 'json',
1149 \ 'err_cb': dict.errHandler,
1150 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001151 call assert_equal("run", job_status(job))
1152 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001153 let g:Ch_outmsg = ''
1154 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001155 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1156 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001157 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1158 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001159
1160 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001161 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001162 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001163 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001164 finally
1165 call job_stop(job)
1166 endtry
1167endfunc
1168
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001169func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001170 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001171 let g:Ch_msg1 = ''
1172 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001173 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001174 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001175 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001176 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001177 let s:counter += 1
1178 endfunc
1179 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001180 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001181 let s:counter += 1
1182 endfunc
1183 let job = job_start(s:python . " test_channel_pipe.py quit now",
1184 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001185 \ 'close_cb': 'CloseHandler'})
1186 " the job may be done quickly, also accept "dead"
1187 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001188 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001189 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1190 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001191 finally
1192 call job_stop(job)
1193 delfunc OutHandler
1194 delfunc CloseHandler
1195 endtry
1196endfunc
1197
Bram Moolenaar437905c2016-04-26 19:01:05 +02001198func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001199 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001200 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001201 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001202 endfunc
1203 let job = job_start(s:python . " test_channel_pipe.py quit now",
1204 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001205 " the job may be done quickly, also accept "dead"
1206 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001207 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001208 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001209 finally
1210 call job_stop(job)
1211 delfunc CloseHandler
1212 endtry
1213endfunc
1214
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001215" Use channel in NL mode but received text does not end in NL.
1216func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001217 let g:Ch_received = ''
1218 func! CloseHandler(chan)
1219 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1220 let g:Ch_received .= ch_read(a:chan)
1221 endwhile
1222 endfunc
1223 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1224 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001225 " the job may be done quickly, also accept "dead"
1226 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001227 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001228 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001229 finally
1230 call job_stop(job)
1231 delfunc CloseHandler
1232 endtry
1233endfunc
1234
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001235func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001236 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001237 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1238 \ 'out_mode': 'json',
1239 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1240 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001241 call assert_equal("run", job_status(job))
1242 try
1243 let g:Ch_outmsg = ''
1244 let g:Ch_errmsg = ''
1245 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1246 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001247 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1248 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001249 finally
1250 call job_stop(job)
1251 endtry
1252endfunc
1253
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001254func Test_close_and_exit_cb()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001255 let g:retdict = {'ret': {}}
1256 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001257 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001258 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001259 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001260 let self.ret['exit_cb'] = job_status(a:job)
1261 endfunc
1262
Bram Moolenaare2956092019-01-25 21:01:17 +01001263 let job = job_start([&shell, &shellcmdflag, 'echo'],
1264 \ {'close_cb': g:retdict.close_cb,
1265 \ 'exit_cb': g:retdict.exit_cb})
1266 " the job may be done quickly, also accept "dead"
1267 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001268 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001269 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001270 call assert_equal('dead', g:retdict.ret['exit_cb'])
1271 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001272endfunc
1273
Bram Moolenaard46ae142016-02-16 13:33:52 +01001274""""""""""
1275
Bram Moolenaar0b146882018-09-06 16:27:24 +02001276function ExitCbWipe(job, status)
1277 exe g:wipe_buf 'bw!'
1278endfunction
1279
1280" This caused a crash, because messages were handled while peeking for a
1281" character.
1282func Test_exit_cb_wipes_buf()
1283 if !has('timers')
1284 return
1285 endif
1286 set cursorline lazyredraw
1287 call test_override('redraw_flag', 1)
1288 new
1289 let g:wipe_buf = bufnr('')
1290
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001291 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001292 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001293 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1294 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1295 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1296 call timer_stop(timer)
1297
1298 set nocursorline nolazyredraw
1299 unlet g:wipe_buf
1300 call test_override('ALL', 0)
1301endfunc
1302
1303""""""""""
1304
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001305let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001306func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001307 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001308 unlet s:channelfd
1309endfunc
1310
1311" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001312func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001313 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001314 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001315 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001316endfunc
1317
1318func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001319 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001320 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001321endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001322
Bram Moolenaard46ae142016-02-16 13:33:52 +01001323""""""""""
1324
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001325let g:Ch_unletResponse = ''
1326func Ch_CloseHandler(handle, msg)
1327 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001328 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001329endfunc
1330
1331" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001332func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001333 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001334 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001335 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001336endfunc
1337
1338func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001339 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001340endfunc
1341
1342""""""""""
1343
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001344func Test_open_fail()
1345 silent! let ch = ch_open("noserver")
1346 echo ch
1347 let d = ch
1348endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001349
1350""""""""""
1351
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001352func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001353 " Wait up to a second for the port to open.
1354 let s:chopt.waittime = 1000
1355 let channel = ch_open('localhost:' . a:port, s:chopt)
1356 unlet s:chopt.waittime
1357 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001358 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001359 return
1360 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001361 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001362 call ch_close(channel)
1363endfunc
1364
1365func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001366 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001367 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001368endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001369
1370"""""""""
1371
1372function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001373 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001374endfunc
1375
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001376function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001377 let handle = ch_open('localhost:' . a:port, s:chopt)
1378 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001379 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001380 return
1381 endif
1382
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001383 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001384 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001385 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarece61b02016-02-20 21:39:05 +01001386endfunc
1387
1388func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001389 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001390endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001391
1392"""""""""
1393
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001394let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001395function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001396 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001397endfunc
1398
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001399function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001400 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001401 let g:Ch_exit_job = g:currentJob
1402 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001403endfunc
1404
1405func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001406 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001407
Bram Moolenaarf386f082019-07-29 23:03:03 +02001408 " wait up to a second for the job to exit
1409 for i in range(100)
1410 if g:Ch_job_exit_ret == 'done'
1411 break
1412 endif
1413 sleep 10m
1414 " calling job_status() triggers the callback
1415 call job_status(g:Ch_exit_job)
1416 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001417
Bram Moolenaarf386f082019-07-29 23:03:03 +02001418 call assert_equal('done', g:Ch_job_exit_ret)
1419 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1420 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001421endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001422
Bram Moolenaar97792de2016-10-15 18:36:49 +02001423function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001424 if job_info(a:job).process == g:exit_cb_val.process
1425 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1426 endif
1427 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001428endfunction
1429
1430func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001431 CheckFunction reltimefloat
1432
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001433 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001434 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001435 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001436 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001437 let elapsed = reltimefloat(g:exit_cb_val.end)
1438 call assert_true(elapsed > 0.5)
1439 call assert_true(elapsed < 1.0)
1440
1441 " case: unreferenced job, using timer
1442 if !has('timers')
1443 return
1444 endif
1445
1446 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1447 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1448 let g:exit_cb_val.process = job_info(g:job).process
1449 unlet g:job
1450 call Standby(1000)
1451 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1452 let elapsed = reltimefloat(g:exit_cb_val.end)
1453 else
1454 let elapsed = 1.0
1455 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001456 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001457endfunc
1458
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001459"""""""""
1460
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001461let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001462function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001463 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001464endfunc
1465
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001466function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001467 let handle = ch_open('localhost:' . a:port, s:chopt)
1468 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001469 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001470 return
1471 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001472 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001473
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001474 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001475 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001476endfunc
1477
1478func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001479 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001480endfunc
1481
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001482function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001483 let handle = ch_open('localhost:' . a:port, s:chopt)
1484 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001485 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001486 return
1487 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001488 let g:Ch_d = {}
1489 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001490 let self.close_ret = 'closed'
1491 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001492 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001493
1494 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001495 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001496 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001497endfunc
1498
1499func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001500 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001501endfunc
1502
Bram Moolenaar80385682016-03-27 19:13:35 +02001503func Test_job_start_invalid()
1504 call assert_fails('call job_start($x)', 'E474:')
1505 call assert_fails('call job_start("")', 'E474:')
1506endfunc
1507
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001508func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001509 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001510 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001511 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001512 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001513 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001514 call job_stop(g:job, 'kill')
1515 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001516 endtry
1517endfunc
1518
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001519" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001520func Test_partial_in_channel_cycle()
1521 let d = {}
1522 let d.a = function('string', [d])
1523 try
1524 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1525 catch
1526 call assert_exception('E901:')
1527 endtry
1528 unlet d
1529endfunc
1530
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001531func Test_using_freed_memory()
1532 let g:a = job_start(['ls'])
1533 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001534 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001535endfunc
1536
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001537func Test_collapse_buffers()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001538 CheckExecutable cat
1539
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001540 sp test_channel.vim
1541 let g:linecount = line('$')
1542 close
1543 split testout
1544 1,$delete
1545 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001546 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001547 bwipe!
1548endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001549
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001550func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001551 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001552 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001553
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001554 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001555 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001556 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001557 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1558 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001559
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001560 bdel test_buffer
1561 call assert_equal([], getbufline(bufnr, 1, '$'))
1562
1563 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001564 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001565 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1566 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1567
1568 bwipe! test_buffer
1569endfunc
1570
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001571func Test_cmd_parsing()
1572 if !has('unix')
1573 return
1574 endif
1575 call assert_false(filereadable("file with space"))
1576 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001577 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001578 call delete("file with space")
1579
1580 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001581 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001582 call delete("file with space")
1583endfunc
1584
Bram Moolenaar82117982016-08-27 19:21:48 +02001585func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001586 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001587
1588 " Test lines from the job containing NUL are stored correctly in a buffer.
1589 new
1590 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1591 w! Xtestread
1592 bwipe!
1593 split testout
1594 1,$delete
1595 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1596 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001597 call assert_equal("asdf\nasdf", getline(1))
1598 call assert_equal("xxx\n", getline(2))
1599 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001600
1601 call delete('Xtestread')
1602 bwipe!
1603
1604 " Test lines from a buffer with NUL bytes are written correctly to the job.
1605 new mybuffer
1606 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1607 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 +02001608 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001609 bwipe!
1610 split Xtestwrite
1611 call assert_equal("asdf\nasdf", getline(1))
1612 call assert_equal("xxx\n", getline(2))
1613 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001614 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001615
1616 call delete('Xtestwrite')
1617 bwipe!
1618endfunc
1619
Bram Moolenaarec68a992016-10-03 21:37:41 +02001620func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001621 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001622 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001623 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001624 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001625 unlet g:linecount
1626endfunc
1627
1628func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001629 func s:close_cb(ch)
1630 while ch_status(a:ch) == 'buffered'
1631 let g:out .= ch_read(a:ch)
1632 endwhile
1633 endfunc
1634
1635 let g:out = ''
1636 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1637 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
1638 call WaitForAssert({-> assert_equal('123', g:out)})
1639 unlet g:out
1640 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001641endfunc
1642
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001643func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001644 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001645 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001646 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001647 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001648 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001649endfunc
1650
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001651func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001652 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001653
1654 " Check that backslash in $COMSPEC is handled properly.
1655 let g:echostr = ''
1656 let cmd = $COMSPEC . ' /c echo 123'
1657 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1658 let info = job_info(job)
1659 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1660
1661 call WaitForAssert({-> assert_equal("123", g:echostr)})
1662 unlet g:echostr
1663endfunc
1664
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001665func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001666 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001667 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001668 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001669 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001670 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001671 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001672 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1673 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001674 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001675 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001676endfunc
1677
1678func Test_cwd()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001679 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001680 if has('win32')
1681 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001682 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001683 else
1684 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001685 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001686 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001687 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001688 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001689 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001690 let expect = substitute(expect, '[/\\]$', '', '')
1691 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1692 if $CI != '' && stridx(g:envstr, '/private/') == 0
1693 let g:envstr = g:envstr[8:]
1694 endif
1695 call assert_equal(expect, g:envstr)
1696 finally
1697 call job_stop(job)
1698 unlet g:envstr
1699 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001700endfunc
1701
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001702function Ch_test_close_lambda(port)
1703 let handle = ch_open('localhost:' . a:port, s:chopt)
1704 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001705 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001706 return
1707 endif
1708 let g:Ch_close_ret = ''
1709 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1710
1711 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001712 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001713endfunc
1714
1715func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001716 call s:run_server('Ch_test_close_lambda')
1717endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001718
1719func s:test_list_args(cmd, out, remove_lf)
1720 try
1721 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001722 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 +02001723 call WaitFor('"" != g:out')
1724 if has('win32')
1725 let g:out = substitute(g:out, '\r', '', 'g')
1726 endif
1727 if a:remove_lf
1728 let g:out = substitute(g:out, '\n$', '', 'g')
1729 endif
1730 call assert_equal(a:out, g:out)
1731 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001732 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001733 unlet g:out
1734 endtry
1735endfunc
1736
1737func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001738 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1739 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1740 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1741 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1742 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1743 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1744 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1745 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1746 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1747 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1748 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1749 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1750 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1751
1752 " tests which not contain spaces in the argument
1753 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1754 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1755 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1756 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1757 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1758 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1759 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1760 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1761 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1762endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001763
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001764func Test_keep_pty_open()
1765 if !has('unix')
1766 return
1767 endif
1768
Bram Moolenaare2956092019-01-25 21:01:17 +01001769 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1770 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001771 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1772 call assert_inrange(200, 1000, elapsed)
1773 call job_stop(job)
1774endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001775
1776func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001777 CheckFeature timers
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001778 CheckFeature reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01001779
1780 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001781 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001782 endfunc
1783
1784 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001785 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001786 call Resume()
1787 endfunc
1788
1789 func TimerCb(timer)
1790 if has('win32')
1791 let cmd = ['cmd', '/c', 'echo.']
1792 else
1793 let cmd = ['echo']
1794 endif
1795 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1796 call substitute(repeat('a', 100000), '.', '', 'g')
1797 endfunc
1798
1799 " We should be interrupted before 'updatetime' elapsed.
1800 let g:val = 0
1801 call timer_start(1, 'TimerCb')
1802 let elapsed = Standby(&ut)
1803 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001804
1805 " Wait for both OutCb() and ExitCb() to have been called before deleting
1806 " them.
1807 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01001808 call job_stop(g:job)
1809
1810 delfunc OutCb
1811 delfunc ExitCb
1812 delfunc TimerCb
1813 unlet! g:val
1814 unlet! g:job
1815endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01001816
1817func Test_raw_large_data()
1818 try
1819 let g:out = ''
1820 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001821 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
1822 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01001823
Bram Moolenaare2956092019-01-25 21:01:17 +01001824 let outlen = 79999
1825 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02001826 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01001827 call WaitFor({-> len(g:out) >= outlen}, 10000)
1828 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01001829 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
1830 finally
1831 call job_stop(job)
1832 unlet g:out
1833 endtry
1834endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001835
Bram Moolenaar65240682019-02-10 22:23:26 +01001836func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001837 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01001838
1839 try
1840 let job = job_start(s:python . " test_channel_pipe.py busy",
1841 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
1842 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
1843 finally
1844 call job_stop(job)
1845 endtry
1846endfunc
1847
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001848func Test_job_exitval_and_termsig()
1849 if !has('unix')
1850 return
1851 endif
1852
1853 " Terminate job normally
1854 let cmd = ['echo']
1855 let job = job_start(cmd)
1856 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1857 let info = job_info(job)
1858 call assert_equal(0, info.exitval)
1859 call assert_equal("", info.termsig)
1860
1861 " Terminate job by signal
1862 let cmd = ['sleep', '10']
1863 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01001864 " 10m usually works but 50m is needed when running Valgrind
1865 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001866 call job_stop(job)
1867 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1868 let info = job_info(job)
1869 call assert_equal(-1, info.exitval)
1870 call assert_equal("term", info.termsig)
1871endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01001872
1873func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001874 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01001875
1876 call writefile(['test'], 'Xtestin')
1877 let in_opts = [{},
1878 \ {'in_io': 'null'},
1879 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
1880 let out_opts = [{},
1881 \ {'out_io': 'null'},
1882 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
1883 let err_opts = [{},
1884 \ {'err_io': 'null'},
1885 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
1886 \ {'err_io': 'out'}]
1887 let opts = []
1888
1889 for in_opt in in_opts
1890 let x = copy(in_opt)
1891 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001892 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01001893 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001894 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01001895 let opts += [extend({'pty': 1}, x)]
1896 endfor
1897 endfor
1898 endfor
1899
1900 for opt in opts
1901 let job = job_start('echo', opt)
1902 let info = job_info(job)
1903 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
1904 \ get(opt, 'in_io', 'tty'),
1905 \ get(opt, 'out_io', 'tty'),
1906 \ get(opt, 'err_io', 'tty'))
1907
1908 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
1909 call assert_notequal('', info.tty_in, msg)
1910 else
1911 call assert_equal('', info.tty_in, msg)
1912 endif
1913 call assert_equal(info.tty_in, info.tty_out, msg)
1914
1915 call WaitForAssert({-> assert_equal('dead', job_status(job))})
1916 endfor
1917
1918 call delete('Xtestin')
1919 call delete('Xtestout')
1920 call delete('Xtesterr')
1921endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02001922
1923" Do this last, it stops any channel log.
1924func Test_zz_nl_err_to_out_pipe()
Bram Moolenaar570497a2019-08-22 22:55:13 +02001925 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001926 call ch_log('Test_zz_nl_err_to_out_pipe()')
1927 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
1928 call assert_equal("run", job_status(job))
1929 try
1930 let handle = job_getchannel(job)
1931 call ch_sendraw(handle, "echo something\n")
1932 call assert_equal("something", ch_readraw(handle))
1933
1934 call ch_sendraw(handle, "echoerr wrong\n")
1935 call assert_equal("wrong", ch_readraw(handle))
1936 finally
1937 call job_stop(job)
1938 call ch_logfile('')
1939 let loglines = readfile('Xlog')
1940 call assert_true(len(loglines) > 10)
1941 let found_test = 0
1942 let found_send = 0
1943 let found_recv = 0
1944 let found_stop = 0
1945 for l in loglines
1946 if l =~ 'Test_zz_nl_err_to_out_pipe'
1947 let found_test = 1
1948 endif
1949 if l =~ 'SEND on.*echo something'
1950 let found_send = 1
1951 endif
1952 if l =~ 'RECV on.*something'
1953 let found_recv = 1
1954 endif
1955 if l =~ 'Stopping job with'
1956 let found_stop = 1
1957 endif
1958 endfor
1959 call assert_equal(1, found_test)
1960 call assert_equal(1, found_send)
1961 call assert_equal(1, found_recv)
1962 call assert_equal(1, found_stop)
1963 " On MS-Windows need to sleep for a moment to be able to delete the file.
1964 sleep 10m
1965 call delete('Xlog')
1966 endtry
1967endfunc
1968
Bram Moolenaara27655e2019-12-21 22:22:01 +01001969func Test_empty_job()
1970 " This was crashing on MS-Windows.
Bram Moolenaarba0a7472019-12-22 16:09:06 +01001971 call assert_fails('let job = job_start([""])', 'E474:')
1972 call assert_fails('let job = job_start([" "])', 'E474:')
Bram Moolenaar7c2a2f82019-12-22 18:28:51 +01001973 call assert_fails('let job = job_start("")', 'E474:')
1974 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaara27655e2019-12-21 22:22:01 +01001975endfunc
1976
Bram Moolenaarf386f082019-07-29 23:03:03 +02001977" Do this last, it stops any channel log.
1978func Test_zz_ch_log()
1979 call ch_logfile('Xlog', 'w')
1980 call ch_log('hello there')
1981 call ch_log('%s%s')
1982 call ch_logfile('')
1983 let text = readfile('Xlog')
1984 call assert_match("hello there", text[1])
1985 call assert_match("%s%s", text[2])
1986 call delete('Xlog')
1987endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01001988
1989func Test_job_start_fails()
1990 " this was leaking memory
1991 call assert_fails("call job_start([''])", "E474:")
1992endfunc
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01001993
1994func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01001995 if has('win32')
1996 let cmd = 'cmd /c pause'
1997 else
1998 let cmd = 'grep foo'
1999 endif
2000 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002001 call job_stop(g:job)
2002 sleep 10m
2003 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002004 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002005 call job_stop(g:job, 'term')
2006 sleep 10m
2007 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002008 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002009 call job_stop(g:job, 'kill')
2010 sleep 10m
2011 call assert_equal(-1, job_info(g:job).exitval)
2012endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002013
2014func Test_issue_5485()
2015 let $VAR1 = 'global'
2016 let g:Ch_reply = ""
2017 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2018 let g:Ch_job = l:job
2019 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2020 unlet $VAR1
2021endfunc